1 package com.eyeq.pivot4j.pentaho.servlet;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.util.Enumeration;
8 import java.util.LinkedList;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Set;
12
13 import javax.faces.view.facelets.ResourceResolver;
14 import javax.servlet.RequestDispatcher;
15 import javax.servlet.Servlet;
16 import javax.servlet.ServletContext;
17 import javax.servlet.ServletContextAttributeEvent;
18 import javax.servlet.ServletContextEvent;
19 import javax.servlet.ServletException;
20
21 import org.apache.commons.lang.ObjectUtils;
22 import org.apache.commons.lang.UnhandledException;
23 import org.apache.myfaces.webapp.StartupServletContextListener;
24
25 import edu.emory.mathcs.backport.java.util.Collections;
26
27 public class PluginServletContext implements ServletContext {
28
29 private ServletContext wrappedContext;
30
31 private StartupServletContextListener listener;
32
33 private Map<String, String> initParameters;
34
35 private ResourceResolver resourceResolver;
36
37
38
39
40
41 public PluginServletContext(ServletContext context,
42 Map<String, String> initParameters) {
43 this.wrappedContext = context;
44 this.initParameters = initParameters;
45
46 this.listener = new StartupServletContextListener();
47 this.resourceResolver = new PluginResourceResolver();
48 }
49
50
51
52
53 protected ServletContext getWrappedContext() {
54 return wrappedContext;
55 }
56
57
58
59
60 protected ResourceResolver getResourceResolver() {
61 return resourceResolver;
62 }
63
64
65
66
67 public StartupServletContextListener getListener() {
68 return listener;
69 }
70
71 public void initialize() {
72 setAttribute("org.apache.myfaces.DYNAMICALLY_ADDED_FACES_SERVLET", true);
73
74 listener.contextInitialized(new ServletContextEvent(this));
75 }
76
77 public void destroy() {
78 listener.contextDestroyed(new ServletContextEvent(this));
79 }
80
81
82
83
84
85
86 public Object getAttribute(String name) {
87 return wrappedContext.getAttribute(name);
88 }
89
90
91
92
93
94 public Enumeration<?> getAttributeNames() {
95 return wrappedContext.getAttributeNames();
96 }
97
98
99
100
101
102
103 public ServletContext getContext(String uripath) {
104 return wrappedContext.getContext(uripath);
105 }
106
107
108
109
110 public String getContextPath() {
111 return wrappedContext.getContextPath();
112 }
113
114
115
116
117
118
119 public String getInitParameter(String name) {
120 if (initParameters != null && initParameters.containsKey(name)) {
121 return initParameters.get(name);
122 }
123
124 return wrappedContext.getInitParameter(name);
125 }
126
127
128
129
130
131 public Enumeration<?> getInitParameterNames() {
132 Enumeration<?> e = wrappedContext.getInitParameterNames();
133
134 if (initParameters == null) {
135 return e;
136 }
137
138 List<String> names = new LinkedList<String>();
139
140 for (String name : initParameters.keySet()) {
141 names.add(name);
142 }
143
144 while (e.hasMoreElements()) {
145 names.add(e.nextElement().toString());
146 }
147
148 return Collections.enumeration(names);
149 }
150
151
152
153
154
155 public int getMajorVersion() {
156 return wrappedContext.getMajorVersion();
157 }
158
159
160
161
162
163 public int getMinorVersion() {
164 return wrappedContext.getMinorVersion();
165 }
166
167
168
169
170
171
172 public String getMimeType(String file) {
173 return wrappedContext.getMimeType(file);
174 }
175
176
177
178
179
180
181 public RequestDispatcher getNamedDispatcher(String name) {
182 return wrappedContext.getNamedDispatcher(name);
183 }
184
185
186
187
188
189
190 public String getRealPath(String path) {
191 return wrappedContext.getRealPath(path);
192 }
193
194
195
196
197
198
199 public RequestDispatcher getRequestDispatcher(String path) {
200 return wrappedContext.getRequestDispatcher(path);
201 }
202
203
204
205
206
207
208
209 public URL getResource(String path) throws MalformedURLException {
210 return resourceResolver.resolveUrl(path);
211 }
212
213
214
215
216
217
218 public InputStream getResourceAsStream(String path) {
219 InputStream in = null;
220
221 try {
222 URL url = getResource(path);
223
224 if (url != null) {
225 in = url.openStream();
226 }
227 } catch (IOException e) {
228 throw new UnhandledException(e);
229 }
230
231 return in;
232 }
233
234
235
236
237
238
239 public Set<?> getResourcePaths(String path) {
240 return wrappedContext.getResourcePaths(path);
241 }
242
243
244
245
246
247 public String getServerInfo() {
248 return wrappedContext.getServerInfo();
249 }
250
251
252
253
254
255
256
257
258 public Servlet getServlet(String name) throws ServletException {
259 return wrappedContext.getServlet(name);
260 }
261
262
263
264
265
266 public String getServletContextName() {
267 return wrappedContext.getServletContextName();
268 }
269
270
271
272
273
274
275 public Enumeration<?> getServletNames() {
276 return wrappedContext.getServletNames();
277 }
278
279
280
281
282
283
284 public Enumeration<?> getServlets() {
285 return wrappedContext.getServlets();
286 }
287
288
289
290
291
292
293
294
295 public void log(Exception throwable, String msg) {
296 wrappedContext.log(throwable, msg);
297 }
298
299
300
301
302
303
304
305 public void log(String msg, Throwable throwable) {
306 wrappedContext.log(msg, throwable);
307 }
308
309
310
311
312
313 public void log(String msg) {
314 wrappedContext.log(msg);
315 }
316
317
318
319
320
321 public void removeAttribute(String name) {
322 Object value = getAttribute(name);
323
324 wrappedContext.removeAttribute(name);
325
326 listener.attributeRemoved(new ServletContextAttributeEvent(this, name,
327 value));
328 }
329
330
331
332
333
334
335
336 public void setAttribute(String name, Object value) {
337 Object oldValue = getAttribute(name);
338
339 wrappedContext.setAttribute(name, value);
340
341 if (oldValue == null) {
342 listener.attributeAdded(new ServletContextAttributeEvent(this,
343 name, value));
344 } else if (!ObjectUtils.equals(oldValue, value)) {
345 listener.attributeReplaced(new ServletContextAttributeEvent(this,
346 name, value));
347 }
348 }
349 }