View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.web.servlet;
20  
21  import javax.servlet.ServletContext;
22  
23  /**
24   * Base implementation for any components that need to access the web application's {@link ServletContext ServletContext}.
25   *
26   * @since 0.2
27   */
28  public class ServletContextSupport {
29  
30      //TODO - complete JavaDoc
31      private ServletContext servletContext = null;
32  
33      public ServletContext getServletContext() {
34          return servletContext;
35      }
36  
37      public void setServletContext(ServletContext servletContext) {
38          this.servletContext = servletContext;
39      }
40  
41      @SuppressWarnings({"UnusedDeclaration"})
42      protected String getContextInitParam(String paramName) {
43          return getServletContext().getInitParameter(paramName);
44      }
45  
46      private ServletContext getRequiredServletContext() {
47          ServletContext servletContext = getServletContext();
48          if (servletContext == null) {
49              String msg = "ServletContext property must be set via the setServletContext method.";
50              throw new IllegalStateException(msg);
51          }
52          return servletContext;
53      }
54  
55      @SuppressWarnings({"UnusedDeclaration"})
56      protected void setContextAttribute(String key, Object value) {
57          if (value == null) {
58              removeContextAttribute(key);
59          } else {
60              getRequiredServletContext().setAttribute(key, value);
61          }
62      }
63  
64      @SuppressWarnings({"UnusedDeclaration"})
65      protected Object getContextAttribute(String key) {
66          return getRequiredServletContext().getAttribute(key);
67      }
68  
69      protected void removeContextAttribute(String key) {
70          getRequiredServletContext().removeAttribute(key);
71      }
72  
73      /**
74       * It is highly recommended not to override this method directly, and instead override the
75       * {@link #toStringBuilder() toStringBuilder()} method, a better-performing alternative.
76       *
77       * @return the String representation of this instance.
78       */
79      @Override
80      public String toString() {
81          return toStringBuilder().toString();
82      }
83  
84      /**
85       * Same concept as {@link #toString() toString()}, but returns a {@link StringBuilder} instance instead.
86       *
87       * @return a StringBuilder instance to use for appending String data that will eventually be returned from a
88       *         {@code toString()} invocation.
89       */
90      protected StringBuilder toStringBuilder() {
91          return new StringBuilder(super.toString());
92      }
93  }