001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.server;
021:
022: import java.io.InputStream;
023:
024: import java.net.MalformedURLException;
025: import java.net.URL;
026:
027: import java.util.Enumeration;
028: import java.util.Hashtable;
029: import java.util.Vector;
030:
031: import javax.servlet.RequestDispatcher;
032: import javax.servlet.Servlet;
033: import javax.servlet.ServletContext;
034: import javax.servlet.ServletException;
035:
036: /**
037: * Abstract wrapper around <code>ServletContext</code>. This class provides
038: * a common implementation of the wrapper for the different servlet API. In
039: * addition to implementing the <code>ServletContext</code> interface it
040: * provides additional features helpful for writing unit tests. More
041: * specifically the <code>getRequestDispatcher()</code> method is overrided
042: * to return an request dispatcher wrapper. In addition logs generated by
043: * calls to the <code>log()</code> methods can be retrieved and asserted by
044: * calling the <code>getLogs()</code> method.
045: *
046: * @version $Id: AbstractServletContextWrapper.java 239054 2004-10-24 01:30:23Z felipeal $
047: */
048: public abstract class AbstractServletContextWrapper implements
049: ServletContext {
050: /**
051: * The original servlet context object
052: */
053: protected ServletContext originalContext;
054:
055: /**
056: * List of parameters set using the <code>setInitParameter()</code> method.
057: */
058: protected Hashtable initParameters;
059:
060: /**
061: * The logs resulting from calling the <code>log()</code> methods
062: */
063: private Vector logs = new Vector();
064:
065: // Constructors -------------------------------------------------------
066:
067: /**
068: * @param theOriginalContext the original servlet context object
069: */
070: public AbstractServletContextWrapper(
071: ServletContext theOriginalContext) {
072: this .originalContext = theOriginalContext;
073: this .initParameters = new Hashtable();
074: }
075:
076: // New methods ---------------------------------------------------------
077:
078: /**
079: * @return the original unmodified config object
080: * @since 1.6
081: */
082: public ServletContext getOriginalContext() {
083: return this .originalContext;
084: }
085:
086: /**
087: * Sets a parameter as if it were set in the <code>web.xml</code> file
088: * (using the <context-param> element).
089: *
090: * @param theName the parameter's name
091: * @param theValue the parameter's value
092: */
093: public void setInitParameter(String theName, String theValue) {
094: this .initParameters.put(theName, theValue);
095: }
096:
097: /**
098: * Returns all the text logs that have been generated using the
099: * <code>log()</code> methods so that it is possible to easily assert the
100: * content of the logs. This method does not return the exceptions or
101: * throwable sent for logging; it only returns the messages.
102: *
103: * @return the logs as a vector of strings (each string contains the
104: * message that was sent for logging).
105: */
106: public Vector getLogs() {
107: return this .logs;
108: }
109:
110: // Overridden methods --------------------------------------------------
111:
112: /**
113: * @see ServletContext#setAttribute(String, Object)
114: */
115: public void setAttribute(String theName, Object theAttribute) {
116: this .originalContext.setAttribute(theName, theAttribute);
117: }
118:
119: /**
120: * @see ServletContext#removeAttribute(String)
121: */
122: public void removeAttribute(String theName) {
123: this .originalContext.removeAttribute(theName);
124: }
125:
126: /**
127: * Intercept the log call and add the message to an internal vector of
128: * log messages that can then later be retrieved and asserted by the
129: * test case writer. Note that the throwable is not saved.
130: *
131: * @param theMessage a <code>String</code> that describes the error or
132: * exception
133: * @param theCause the <code>Throwable</code> error or exception
134: *
135: * @see #getLogs()
136: * @see ServletContext#log(String, Throwable)
137: */
138: public void log(String theMessage, Throwable theCause) {
139: if (theMessage != null) {
140: this .logs.addElement(theMessage);
141: }
142:
143: this .originalContext.log(theMessage, theCause);
144: }
145:
146: /**
147: * Intercept the log call and add the message to an internal vector of
148: * log messages that can then later be retrieved and asserted by the
149: * test case writer. Note that the throwable is not saved.
150: *
151: * @param theMessage a <code>String</code> that describes the error or
152: * exception
153: *
154: * @see #getLogs()
155: * @see ServletContext#log(String)
156: */
157: public void log(String theMessage) {
158: if (theMessage != null) {
159: this .logs.addElement(theMessage);
160: }
161:
162: this .originalContext.log(theMessage);
163: }
164:
165: /**
166: * Intercept the log call and add the message to an internal vector of
167: * log messages that can then later be retrieved and asserted by the
168: * test case writer. Note that the throwable is not saved.
169: *
170: * @param theException the exception to log
171: * @param theMessage a <code>String</code> that describes the error or
172: * exception
173: *
174: * @see #getLogs()
175: * @see ServletContext#log(Exception, String)
176: *
177: * @deprecated As of Java Servlet API 2.1, use
178: * {@link #log(String message, Throwable throwable)} instead.
179: * This method was originally defined to write an exception's
180: * stack trace and an explanatory error message to the servlet
181: * log file.
182: */
183: public void log(Exception theException, String theMessage) {
184: if (theMessage != null) {
185: this .logs.addElement(theMessage);
186: }
187:
188: this .originalContext.log(theException, theMessage);
189: }
190:
191: /**
192: * @see ServletContext#getServlets()
193: */
194: public Enumeration getServlets() {
195: return this .originalContext.getServlets();
196: }
197:
198: /**
199: * @see ServletContext#getServletNames()
200: */
201: public Enumeration getServletNames() {
202: return this .originalContext.getServletNames();
203: }
204:
205: /**
206: * @see ServletContext#getServlet(String)
207: */
208: public Servlet getServlet(String theName) throws ServletException {
209: return this .originalContext.getServlet(theName);
210: }
211:
212: /**
213: * @see ServletContext#getServerInfo()
214: */
215: public String getServerInfo() {
216: return this .originalContext.getServerInfo();
217: }
218:
219: /**
220: * @see ServletContext#getResourceAsStream(String)
221: */
222: public InputStream getResourceAsStream(String thePath) {
223: return this .originalContext.getResourceAsStream(thePath);
224: }
225:
226: /**
227: * @see ServletContext#getResource(String)
228: */
229: public URL getResource(String thePath) throws MalformedURLException {
230: return this .originalContext.getResource(thePath);
231: }
232:
233: /**
234: * @param thePath a string specifying the pathname to the resource
235: * @return our request dispatcher wrapper
236: * @see ServletContext#getRequestDispatcher(String)
237: */
238: public RequestDispatcher getRequestDispatcher(String thePath) {
239: RequestDispatcher wrappedDispatcher = null;
240:
241: RequestDispatcher originalDispatcher = this .originalContext
242: .getRequestDispatcher(thePath);
243:
244: if (originalDispatcher != null) {
245: wrappedDispatcher = new RequestDispatcherWrapper(
246: originalDispatcher);
247: }
248:
249: return wrappedDispatcher;
250: }
251:
252: /**
253: * @param theName a string specifying the name of a servlet to wrap
254: * @return our request dispatcher wrapper or null if the servlet cannot
255: * be found.
256: * @see ServletContext#getNamedDispatcher(String)
257: */
258: public RequestDispatcher getNamedDispatcher(String theName) {
259: RequestDispatcher wrappedDispatcher = null;
260:
261: RequestDispatcher originalDispatcher = this .originalContext
262: .getNamedDispatcher(theName);
263:
264: if (originalDispatcher != null) {
265: wrappedDispatcher = new RequestDispatcherWrapper(
266: originalDispatcher);
267: }
268:
269: return wrappedDispatcher;
270: }
271:
272: /**
273: * @see ServletContext#getRealPath(String)
274: */
275: public String getRealPath(String thePath) {
276: return this .originalContext.getRealPath(thePath);
277: }
278:
279: /**
280: * @see ServletContext#getMinorVersion()
281: */
282: public int getMinorVersion() {
283: return this .originalContext.getMinorVersion();
284: }
285:
286: /**
287: * @see ServletContext#getMimeType(String)
288: */
289: public String getMimeType(String theFilename) {
290: return this .originalContext.getMimeType(theFilename);
291: }
292:
293: /**
294: * @see ServletContext#getMajorVersion()
295: */
296: public int getMajorVersion() {
297: return this .originalContext.getMajorVersion();
298: }
299:
300: /**
301: * @return the union of the parameters defined in the Redirector
302: * <code>web.xml</code> file and the one set using the
303: * <code>setInitParameter()</code> method.
304: */
305: public Enumeration getInitParameterNames() {
306: Vector names = new Vector();
307:
308: // Add parameters that were added using setInitParameter()
309: Enumeration en = this .initParameters.keys();
310:
311: while (en.hasMoreElements()) {
312: String value = (String) en.nextElement();
313:
314: names.add(value);
315: }
316:
317: // Add parameters from web.xml
318: en = this .originalContext.getInitParameterNames();
319:
320: while (en.hasMoreElements()) {
321: String value = (String) en.nextElement();
322:
323: // Do not add parameters that have been overriden by calling
324: // the setInitParameter() method.
325: if (!names.contains(value)) {
326: names.add(value);
327: }
328: }
329:
330: return names.elements();
331: }
332:
333: /**
334: * @param theName the name of the parameter's value to return
335: * @return the value of the parameter, looking for it first in the list of
336: * parameters set using the <code>setInitParameter()</code> method
337: * and then in those set in <code>web.xml</code>.
338: */
339: public String getInitParameter(String theName) {
340: // Look first in the list of parameters set using the
341: // setInitParameter() method.
342: String value = (String) this .initParameters.get(theName);
343:
344: if (value == null) {
345: value = this .originalContext.getInitParameter(theName);
346: }
347:
348: return value;
349: }
350:
351: /**
352: * @param theUripath a String specifying the context path of another web
353: * application in the container
354: * @return our servlet context wrapper
355: * @see ServletContext#getContext(String)
356: */
357: public ServletContext getContext(String theUripath) {
358: ServletContext context = new ServletContextWrapper(
359: this .originalContext.getContext(theUripath));
360:
361: return context;
362: }
363:
364: /**
365: * @see ServletContext#getAttributeNames()
366: */
367: public Enumeration getAttributeNames() {
368: return this .originalContext.getAttributeNames();
369: }
370:
371: /**
372: * @see ServletContext#getAttribute(String)
373: */
374: public Object getAttribute(String theName) {
375: return this.originalContext.getAttribute(theName);
376: }
377: }
|