Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ServletContextSupport |
|
| 1.3333333333333333;1.333 |
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 | 553 | public class ServletContextSupport { |
29 | ||
30 | //TODO - complete JavaDoc | |
31 | 553 | private ServletContext servletContext = null; |
32 | ||
33 | public ServletContext getServletContext() { | |
34 | 7 | return servletContext; |
35 | } | |
36 | ||
37 | public void setServletContext(ServletContext servletContext) { | |
38 | 162 | this.servletContext = servletContext; |
39 | 162 | } |
40 | ||
41 | @SuppressWarnings({"UnusedDeclaration"}) | |
42 | protected String getContextInitParam(String paramName) { | |
43 | 0 | return getServletContext().getInitParameter(paramName); |
44 | } | |
45 | ||
46 | private ServletContext getRequiredServletContext() { | |
47 | 0 | ServletContext servletContext = getServletContext(); |
48 | 0 | if (servletContext == null) { |
49 | 0 | String msg = "ServletContext property must be set via the setServletContext method."; |
50 | 0 | throw new IllegalStateException(msg); |
51 | } | |
52 | 0 | return servletContext; |
53 | } | |
54 | ||
55 | @SuppressWarnings({"UnusedDeclaration"}) | |
56 | protected void setContextAttribute(String key, Object value) { | |
57 | 0 | if (value == null) { |
58 | 0 | removeContextAttribute(key); |
59 | } else { | |
60 | 0 | getRequiredServletContext().setAttribute(key, value); |
61 | } | |
62 | 0 | } |
63 | ||
64 | @SuppressWarnings({"UnusedDeclaration"}) | |
65 | protected Object getContextAttribute(String key) { | |
66 | 0 | return getRequiredServletContext().getAttribute(key); |
67 | } | |
68 | ||
69 | protected void removeContextAttribute(String key) { | |
70 | 0 | getRequiredServletContext().removeAttribute(key); |
71 | 0 | } |
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 | 16 | 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 | 0 | return new StringBuilder(super.toString()); |
92 | } | |
93 | } |