View Javadoc

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  	 * @param context
39  	 * @param initParameters
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  	 * @return the wrappedContext
52  	 */
53  	protected ServletContext getWrappedContext() {
54  		return wrappedContext;
55  	}
56  
57  	/**
58  	 * @return the resourceResolver
59  	 */
60  	protected ResourceResolver getResourceResolver() {
61  		return resourceResolver;
62  	}
63  
64  	/**
65  	 * @return the listener
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  	 * @param name
83  	 * @return
84  	 * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
85  	 */
86  	public Object getAttribute(String name) {
87  		return wrappedContext.getAttribute(name);
88  	}
89  
90  	/**
91  	 * @return
92  	 * @see javax.servlet.ServletContext#getAttributeNames()
93  	 */
94  	public Enumeration<?> getAttributeNames() {
95  		return wrappedContext.getAttributeNames();
96  	}
97  
98  	/**
99  	 * @param uripath
100 	 * @return
101 	 * @see javax.servlet.ServletContext#getContext(java.lang.String)
102 	 */
103 	public ServletContext getContext(String uripath) {
104 		return wrappedContext.getContext(uripath);
105 	}
106 
107 	/**
108 	 * @return
109 	 */
110 	public String getContextPath() {
111 		return wrappedContext.getContextPath();
112 	}
113 
114 	/**
115 	 * @param name
116 	 * @return
117 	 * @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
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 	 * @return
129 	 * @see javax.servlet.ServletContext#getInitParameterNames()
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 	 * @return
153 	 * @see javax.servlet.ServletContext#getMajorVersion()
154 	 */
155 	public int getMajorVersion() {
156 		return wrappedContext.getMajorVersion();
157 	}
158 
159 	/**
160 	 * @return
161 	 * @see javax.servlet.ServletContext#getMinorVersion()
162 	 */
163 	public int getMinorVersion() {
164 		return wrappedContext.getMinorVersion();
165 	}
166 
167 	/**
168 	 * @param file
169 	 * @return
170 	 * @see javax.servlet.ServletContext#getMimeType(java.lang.String)
171 	 */
172 	public String getMimeType(String file) {
173 		return wrappedContext.getMimeType(file);
174 	}
175 
176 	/**
177 	 * @param name
178 	 * @return
179 	 * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
180 	 */
181 	public RequestDispatcher getNamedDispatcher(String name) {
182 		return wrappedContext.getNamedDispatcher(name);
183 	}
184 
185 	/**
186 	 * @param path
187 	 * @return
188 	 * @see javax.servlet.ServletContext#getRealPath(java.lang.String)
189 	 */
190 	public String getRealPath(String path) {
191 		return wrappedContext.getRealPath(path);
192 	}
193 
194 	/**
195 	 * @param path
196 	 * @return
197 	 * @see javax.servlet.ServletContext#getRequestDispatcher(java.lang.String)
198 	 */
199 	public RequestDispatcher getRequestDispatcher(String path) {
200 		return wrappedContext.getRequestDispatcher(path);
201 	}
202 
203 	/**
204 	 * @param path
205 	 * @return
206 	 * @throws MalformedURLException
207 	 * @see javax.servlet.ServletContext#getResource(java.lang.String)
208 	 */
209 	public URL getResource(String path) throws MalformedURLException {
210 		return resourceResolver.resolveUrl(path);
211 	}
212 
213 	/**
214 	 * @param path
215 	 * @return
216 	 * @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String)
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 	 * @param path
236 	 * @return
237 	 * @see javax.servlet.ServletContext#getResourcePaths(java.lang.String)
238 	 */
239 	public Set<?> getResourcePaths(String path) {
240 		return wrappedContext.getResourcePaths(path);
241 	}
242 
243 	/**
244 	 * @return
245 	 * @see javax.servlet.ServletContext#getServerInfo()
246 	 */
247 	public String getServerInfo() {
248 		return wrappedContext.getServerInfo();
249 	}
250 
251 	/**
252 	 * @param name
253 	 * @return
254 	 * @throws ServletException
255 	 * @deprecated
256 	 * @see javax.servlet.ServletContext#getServlet(java.lang.String)
257 	 */
258 	public Servlet getServlet(String name) throws ServletException {
259 		return wrappedContext.getServlet(name);
260 	}
261 
262 	/**
263 	 * @return
264 	 * @see javax.servlet.ServletContext#getServletContextName()
265 	 */
266 	public String getServletContextName() {
267 		return wrappedContext.getServletContextName();
268 	}
269 
270 	/**
271 	 * @return
272 	 * @deprecated
273 	 * @see javax.servlet.ServletContext#getServletNames()
274 	 */
275 	public Enumeration<?> getServletNames() {
276 		return wrappedContext.getServletNames();
277 	}
278 
279 	/**
280 	 * @return
281 	 * @deprecated
282 	 * @see javax.servlet.ServletContext#getServlets()
283 	 */
284 	public Enumeration<?> getServlets() {
285 		return wrappedContext.getServlets();
286 	}
287 
288 	/**
289 	 * @param throwable
290 	 * @param msg
291 	 * @deprecated
292 	 * @see javax.servlet.ServletContext#log(java.lang.Exception,
293 	 *      java.lang.String)
294 	 */
295 	public void log(Exception throwable, String msg) {
296 		wrappedContext.log(throwable, msg);
297 	}
298 
299 	/**
300 	 * @param msg
301 	 * @param throwable
302 	 * @see javax.servlet.ServletContext#log(java.lang.String,
303 	 *      java.lang.Throwable)
304 	 */
305 	public void log(String msg, Throwable throwable) {
306 		wrappedContext.log(msg, throwable);
307 	}
308 
309 	/**
310 	 * @param msg
311 	 * @see javax.servlet.ServletContext#log(java.lang.String)
312 	 */
313 	public void log(String msg) {
314 		wrappedContext.log(msg);
315 	}
316 
317 	/**
318 	 * @param name
319 	 * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
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 	 * @param name
332 	 * @param value
333 	 * @see javax.servlet.ServletContext#setAttribute(java.lang.String,
334 	 *      java.lang.Object)
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 }