1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.context.servlet;
20
21 import java.io.InputStream;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.util.Map;
25 import java.util.Set;
26
27 import javax.faces.context.ExternalContext;
28 import javax.servlet.ServletContext;
29
30 import org.apache.myfaces.context.ReleaseableExternalContext;
31
32
33
34
35
36
37
38
39
40 public abstract class ServletExternalContextImplBase extends ExternalContext
41 implements ReleaseableExternalContext
42 {
43
44 private static final String INIT_PARAMETER_MAP_ATTRIBUTE = InitParameterMap.class.getName();
45
46 private ServletContext _servletContext;
47 private Map<String, Object> _applicationMap;
48 private Map<String, String> _initParameterMap;
49
50 public ServletExternalContextImplBase(ServletContext servletContext)
51 {
52 _servletContext = servletContext;
53 _applicationMap = null;
54 _initParameterMap = null;
55 }
56
57 public void release()
58 {
59 _servletContext = null;
60 _applicationMap = null;
61 _initParameterMap = null;
62 }
63
64
65
66 @Override
67 public Map<String, Object> getApplicationMap()
68 {
69 if (_applicationMap == null)
70 {
71 _applicationMap = new ApplicationMap(_servletContext);
72 }
73 return _applicationMap;
74 }
75
76 @Override
77 public String getMimeType(String file)
78 {
79 checkNull(file, "file");
80 return _servletContext.getMimeType(file);
81 }
82
83 @Override
84 public Object getContext()
85 {
86 return _servletContext;
87 }
88
89 @Override
90 public String getContextName()
91 {
92 return _servletContext.getServletContextName();
93 }
94
95 @Override
96 public String getInitParameter(final String s)
97 {
98 return _servletContext.getInitParameter(s);
99 }
100
101 @Override
102 @SuppressWarnings("unchecked")
103 public Map<String, String> getInitParameterMap()
104 {
105 if (_initParameterMap == null)
106 {
107
108 if ((_initParameterMap = (Map<String, String>) _servletContext.getAttribute(INIT_PARAMETER_MAP_ATTRIBUTE)) == null)
109 {
110 _initParameterMap = new InitParameterMap(_servletContext);
111 _servletContext.setAttribute(INIT_PARAMETER_MAP_ATTRIBUTE, _initParameterMap);
112 }
113 }
114 return _initParameterMap;
115 }
116
117 @Override
118 public URL getResource(final String path) throws MalformedURLException
119 {
120 checkNull(path, "path");
121 return _servletContext.getResource(path);
122 }
123
124 @Override
125 public InputStream getResourceAsStream(final String path)
126 {
127 checkNull(path, "path");
128 return _servletContext.getResourceAsStream(path);
129 }
130
131 @Override
132 @SuppressWarnings("unchecked")
133 public Set<String> getResourcePaths(final String path)
134 {
135 checkNull(path, "path");
136 return _servletContext.getResourcePaths(path);
137 }
138
139 @Override
140 public void log(final String message)
141 {
142 checkNull(message, "message");
143 _servletContext.log(message);
144 }
145
146 @Override
147 public void log(final String message, final Throwable exception)
148 {
149 checkNull(message, "message");
150 checkNull(exception, "exception");
151 _servletContext.log(message, exception);
152 }
153
154 @Override
155 public String getRealPath(String path)
156 {
157 checkNull(path, "path");
158 return _servletContext.getRealPath(path);
159 }
160
161
162
163 protected void checkNull(final Object o, final String param)
164 {
165 if (o == null)
166 {
167 throw new NullPointerException(param + " can not be null.");
168 }
169 }
170
171 }