1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.web.servlet;
20
21 import javax.servlet.ServletContext;
22
23
24
25
26
27
28 public class ServletContextSupport {
29
30
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
75
76
77
78
79 @Override
80 public String toString() {
81 return toStringBuilder().toString();
82 }
83
84
85
86
87
88
89
90 protected StringBuilder toStringBuilder() {
91 return new StringBuilder(super.toString());
92 }
93 }