KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > server > AbstractServletContextWrapper


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.server;
21
22 import java.io.InputStream JavaDoc;
23
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26
27 import java.util.Enumeration JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import javax.servlet.RequestDispatcher JavaDoc;
32 import javax.servlet.Servlet JavaDoc;
33 import javax.servlet.ServletContext JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35
36 /**
37  * Abstract wrapper around <code>ServletContext</code>. This class provides
38  * a common implementation of the wrapper for the different servlet API. In
39  * addition to implementing the <code>ServletContext</code> interface it
40  * provides additional features helpful for writing unit tests. More
41  * specifically the <code>getRequestDispatcher()</code> method is overrided
42  * to return an request dispatcher wrapper. In addition logs generated by
43  * calls to the <code>log()</code> methods can be retrieved and asserted by
44  * calling the <code>getLogs()</code> method.
45  *
46  * @version $Id: AbstractServletContextWrapper.java,v 1.2 2004/10/24 01:30:23 felipeal Exp $
47  */

48 public abstract class AbstractServletContextWrapper implements ServletContext JavaDoc
49 {
50     /**
51      * The original servlet context object
52      */

53     protected ServletContext JavaDoc originalContext;
54
55     /**
56      * List of parameters set using the <code>setInitParameter()</code> method.
57      */

58     protected Hashtable JavaDoc initParameters;
59     
60     /**
61      * The logs resulting from calling the <code>log()</code> methods
62      */

63     private Vector JavaDoc logs = new Vector JavaDoc();
64
65     // Constructors -------------------------------------------------------
66

67     /**
68      * @param theOriginalContext the original servlet context object
69      */

70     public AbstractServletContextWrapper(ServletContext JavaDoc theOriginalContext)
71     {
72         this.originalContext = theOriginalContext;
73         this.initParameters = new Hashtable JavaDoc();
74     }
75
76     // New methods ---------------------------------------------------------
77

78     /**
79      * @return the original unmodified config object
80      * @since 1.6
81      */

82     public ServletContext JavaDoc getOriginalContext()
83     {
84         return this.originalContext;
85     }
86     
87     /**
88      * Sets a parameter as if it were set in the <code>web.xml</code> file
89      * (using the &lt;context-param&gt; element).
90      *
91      * @param theName the parameter's name
92      * @param theValue the parameter's value
93      */

94     public void setInitParameter(String JavaDoc theName, String JavaDoc theValue)
95     {
96         this.initParameters.put(theName, theValue);
97     }
98     
99     /**
100      * Returns all the text logs that have been generated using the
101      * <code>log()</code> methods so that it is possible to easily assert the
102      * content of the logs. This method does not return the exceptions or
103      * throwable sent for logging; it only returns the messages.
104      *
105      * @return the logs as a vector of strings (each string contains the
106      * message that was sent for logging).
107      */

108     public Vector JavaDoc getLogs()
109     {
110         return this.logs;
111     }
112     
113     // Overridden methods --------------------------------------------------
114

115     /**
116      * @see ServletContext#setAttribute(String, Object)
117      */

118     public void setAttribute(String JavaDoc theName, Object JavaDoc theAttribute)
119     {
120         this.originalContext.setAttribute(theName, theAttribute);
121     }
122
123     /**
124      * @see ServletContext#removeAttribute(String)
125      */

126     public void removeAttribute(String JavaDoc theName)
127     {
128         this.originalContext.removeAttribute(theName);
129     }
130
131     /**
132      * Intercept the log call and add the message to an internal vector of
133      * log messages that can then later be retrieved and asserted by the
134      * test case writer. Note that the throwable is not saved.
135      *
136      * @param theMessage a <code>String</code> that describes the error or
137      * exception
138      * @param theCause the <code>Throwable</code> error or exception
139      *
140      * @see #getLogs()
141      * @see ServletContext#log(String, Throwable)
142      */

143     public void log(String JavaDoc theMessage, Throwable JavaDoc theCause)
144     {
145         if (theMessage != null)
146         {
147             this.logs.addElement(theMessage);
148         }
149
150         this.originalContext.log(theMessage, theCause);
151     }
152
153     /**
154      * Intercept the log call and add the message to an internal vector of
155      * log messages that can then later be retrieved and asserted by the
156      * test case writer. Note that the throwable is not saved.
157      *
158      * @param theMessage a <code>String</code> that describes the error or
159      * exception
160      *
161      * @see #getLogs()
162      * @see ServletContext#log(String)
163      */

164     public void log(String JavaDoc theMessage)
165     {
166         if (theMessage != null)
167         {
168             this.logs.addElement(theMessage);
169         }
170
171         this.originalContext.log(theMessage);
172     }
173
174     /**
175      * Intercept the log call and add the message to an internal vector of
176      * log messages that can then later be retrieved and asserted by the
177      * test case writer. Note that the throwable is not saved.
178      *
179      * @param theException the exception to log
180      * @param theMessage a <code>String</code> that describes the error or
181      * exception
182      *
183      * @see #getLogs()
184      * @see ServletContext#log(Exception, String)
185      *
186      * @deprecated As of Java Servlet API 2.1, use
187      * {@link #log(String message, Throwable throwable)} instead.
188      * This method was originally defined to write an exception's
189      * stack trace and an explanatory error message to the servlet
190      * log file.
191      */

192     public void log(Exception JavaDoc theException, String JavaDoc theMessage)
193     {
194         if (theMessage != null)
195         {
196             this.logs.addElement(theMessage);
197         }
198
199         this.originalContext.log(theException, theMessage);
200     }
201
202     /**
203      * @see ServletContext#getServlets()
204      */

205     public Enumeration JavaDoc getServlets()
206     {
207         return this.originalContext.getServlets();
208     }
209
210     /**
211      * @see ServletContext#getServletNames()
212      */

213     public Enumeration JavaDoc getServletNames()
214     {
215         return this.originalContext.getServletNames();
216     }
217
218     /**
219      * @see ServletContext#getServlet(String)
220      */

221     public Servlet JavaDoc getServlet(String JavaDoc theName) throws ServletException JavaDoc
222     {
223         return this.originalContext.getServlet(theName);
224     }
225
226     /**
227      * @see ServletContext#getServerInfo()
228      */

229     public String JavaDoc getServerInfo()
230     {
231         return this.originalContext.getServerInfo();
232     }
233
234     /**
235      * @see ServletContext#getResourceAsStream(String)
236      */

237     public InputStream JavaDoc getResourceAsStream(String JavaDoc thePath)
238     {
239         return this.originalContext.getResourceAsStream(thePath);
240     }
241
242     /**
243      * @see ServletContext#getResource(String)
244      */

245     public URL JavaDoc getResource(String JavaDoc thePath) throws MalformedURLException JavaDoc
246     {
247         return this.originalContext.getResource(thePath);
248     }
249
250     /**
251      * @param thePath a string specifying the pathname to the resource
252      * @return our request dispatcher wrapper
253      * @see ServletContext#getRequestDispatcher(String)
254      */

255     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc thePath)
256     {
257         RequestDispatcher JavaDoc wrappedDispatcher = null;
258
259         RequestDispatcher JavaDoc originalDispatcher =
260             this.originalContext.getRequestDispatcher(thePath);
261
262         if (originalDispatcher != null)
263         {
264             wrappedDispatcher =
265                 new RequestDispatcherWrapper(originalDispatcher);
266         }
267
268         return wrappedDispatcher;
269     }
270
271     /**
272      * @param theName a string specifying the name of a servlet to wrap
273      * @return our request dispatcher wrapper or null if the servlet cannot
274      * be found.
275      * @see ServletContext#getNamedDispatcher(String)
276      */

277     public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc theName)
278     {
279         RequestDispatcher JavaDoc wrappedDispatcher = null;
280
281         RequestDispatcher JavaDoc originalDispatcher =
282             this.originalContext.getNamedDispatcher(theName);
283
284         if (originalDispatcher != null)
285         {
286             wrappedDispatcher =
287                 new RequestDispatcherWrapper(originalDispatcher);
288         }
289
290         return wrappedDispatcher;
291     }
292
293     /**
294      * @see ServletContext#getRealPath(String)
295      */

296     public String JavaDoc getRealPath(String JavaDoc thePath)
297     {
298         return this.originalContext.getRealPath(thePath);
299     }
300
301     /**
302      * @see ServletContext#getMinorVersion()
303      */

304     public int getMinorVersion()
305     {
306         return this.originalContext.getMinorVersion();
307     }
308
309     /**
310      * @see ServletContext#getMimeType(String)
311      */

312     public String JavaDoc getMimeType(String JavaDoc theFilename)
313     {
314         return this.originalContext.getMimeType(theFilename);
315     }
316
317     /**
318      * @see ServletContext#getMajorVersion()
319      */

320     public int getMajorVersion()
321     {
322         return this.originalContext.getMajorVersion();
323     }
324
325     /**
326      * @return the union of the parameters defined in the Redirector
327      * <code>web.xml</code> file and the one set using the
328      * <code>setInitParameter()</code> method.
329      */

330     public Enumeration JavaDoc getInitParameterNames()
331     {
332         Vector JavaDoc names = new Vector JavaDoc();
333
334         // Add parameters that were added using setInitParameter()
335
Enumeration JavaDoc en = this.initParameters.keys();
336
337         while (en.hasMoreElements())
338         {
339             String JavaDoc value = (String JavaDoc) en.nextElement();
340
341             names.add(value);
342         }
343
344         // Add parameters from web.xml
345
en = this.originalContext.getInitParameterNames();
346
347         while (en.hasMoreElements())
348         {
349             String JavaDoc value = (String JavaDoc) en.nextElement();
350
351             // Do not add parameters that have been overriden by calling
352
// the setInitParameter() method.
353
if (!names.contains(value))
354             {
355                 names.add(value);
356             }
357         }
358
359         return names.elements();
360     }
361
362     /**
363      * @param theName the name of the parameter's value to return
364      * @return the value of the parameter, looking for it first in the list of
365      * parameters set using the <code>setInitParameter()</code> method
366      * and then in those set in <code>web.xml</code>.
367      */

368     public String JavaDoc getInitParameter(String JavaDoc theName)
369     {
370         // Look first in the list of parameters set using the
371
// setInitParameter() method.
372
String JavaDoc value = (String JavaDoc) this.initParameters.get(theName);
373
374         if (value == null)
375         {
376             value = this.originalContext.getInitParameter(theName);
377         }
378
379         return value;
380     }
381     
382     /**
383      * @param theUripath a String specifying the context path of another web
384      * application in the container
385      * @return our servlet context wrapper
386      * @see ServletContext#getContext(String)
387      */

388     public ServletContext JavaDoc getContext(String JavaDoc theUripath)
389     {
390         ServletContext JavaDoc context = new ServletContextWrapper(
391             this.originalContext.getContext(theUripath));
392
393         return context;
394     }
395
396     /**
397      * @see ServletContext#getAttributeNames()
398      */

399     public Enumeration JavaDoc getAttributeNames()
400     {
401         return this.originalContext.getAttributeNames();
402     }
403
404     /**
405      * @see ServletContext#getAttribute(String)
406      */

407     public Object JavaDoc getAttribute(String JavaDoc theName)
408     {
409         return this.originalContext.getAttribute(theName);
410     }
411 }
412
Popular Tags