001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.directwebremoting.util;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileNotFoundException;
022: import java.io.InputStream;
023: import java.net.MalformedURLException;
024: import java.net.URL;
025: import java.util.Collections;
026: import java.util.Enumeration;
027: import java.util.HashMap;
028: import java.util.Map;
029: import java.util.Set;
030:
031: import javax.servlet.RequestDispatcher;
032: import javax.servlet.Servlet;
033: import javax.servlet.ServletContext;
034: import javax.servlet.http.HttpServlet;
035:
036: import org.apache.commons.logging.LogFactory;
037: import org.apache.commons.logging.Log;
038:
039: /**
040: * Fake implementation of the ServletContext interface.
041: * @author Rod Johnson
042: * @author Juergen Hoeller
043: * @author Joe Walker [joe at getahead dot ltd dot uk]
044: */
045: public class FakeServletContext implements ServletContext {
046: /**
047: * Create a new FakeServletContext, using no base path and a
048: * DefaultResourceLoader (i.e. the classpath root as WAR root).
049: */
050: public FakeServletContext() {
051: this ("");
052: }
053:
054: /**
055: * Create a new FakeServletContext, using a DefaultResourceLoader.
056: * @param resourceBasePath the WAR root directory (should not end with a slash)
057: */
058: public FakeServletContext(String resourceBasePath) {
059: this .resourceBasePath = (resourceBasePath != null ? resourceBasePath
060: : "");
061:
062: // Use JVM temp dir as ServletContext temp dir.
063: String tempDir = System.getProperty("java.io.tmpdir");
064: if (tempDir != null) {
065: attributes.put("javax.servlet.context.tempdir", new File(
066: tempDir));
067: }
068: }
069:
070: /**
071: * Build a full resource location for the given path,
072: * prepending the resource base path of this FakeServletContext.
073: * @param path the path as specified
074: * @return the full resource path
075: */
076: protected String getResourceLocation(String path) {
077: String output = path;
078: if (!output.startsWith("/")) {
079: output = "/" + output;
080: }
081: output = resourceBasePath + output;
082:
083: return output;
084: }
085:
086: public ServletContext getContext(String name) {
087: throw new UnsupportedOperationException("getContext");
088: }
089:
090: /* (non-Javadoc)
091: * @see javax.servlet.ServletContext#getMajorVersion()
092: */
093: public int getMajorVersion() {
094: return 2;
095: }
096:
097: /* (non-Javadoc)
098: * @see javax.servlet.ServletContext#getMinorVersion()
099: */
100: public int getMinorVersion() {
101: return 4;
102: }
103:
104: /* (non-Javadoc)
105: * @see javax.servlet.ServletContext#getMimeType(java.lang.String)
106: */
107: public String getMimeType(String filePath) {
108: throw new UnsupportedOperationException("getMimeType");
109: }
110:
111: /* (non-Javadoc)
112: * @see javax.servlet.ServletContext#getResourcePaths(java.lang.String)
113: */
114: public Set<String> getResourcePaths(String path) {
115: throw new UnsupportedOperationException();
116: }
117:
118: /* (non-Javadoc)
119: * @see javax.servlet.ServletContext#getResource(java.lang.String)
120: */
121: public URL getResource(String path) throws MalformedURLException {
122: throw new UnsupportedOperationException();
123: }
124:
125: /* (non-Javadoc)
126: * @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String)
127: */
128: public InputStream getResourceAsStream(String path) {
129: try {
130: return new FileInputStream(resourceBasePath + path);
131: } catch (FileNotFoundException ex) {
132: return null;
133: }
134: }
135:
136: /* (non-Javadoc)
137: * @see javax.servlet.ServletContext#getRequestDispatcher(java.lang.String)
138: */
139: public RequestDispatcher getRequestDispatcher(String path) {
140: if (!path.startsWith("/")) {
141: throw new IllegalArgumentException(
142: "RequestDispatcher path at ServletContext level must start with '/'");
143: }
144: return new FakeRequestDispatcher(path);
145: }
146:
147: /* (non-Javadoc)
148: * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
149: */
150: public RequestDispatcher getNamedDispatcher(String path) {
151: throw new UnsupportedOperationException("getNamedDispatcher");
152: }
153:
154: /* (non-Javadoc)
155: * @see javax.servlet.ServletContext#getServlet(java.lang.String)
156: */
157: @Deprecated
158: public Servlet getServlet(String name) {
159: throw new UnsupportedOperationException("getServlet");
160: }
161:
162: /* (non-Javadoc)
163: * @see javax.servlet.ServletContext#getServlets()
164: */
165: @Deprecated
166: public Enumeration<HttpServlet> getServlets() {
167: throw new UnsupportedOperationException("getServlets");
168: }
169:
170: /* (non-Javadoc)
171: * @see javax.servlet.ServletContext#getServletNames()
172: */
173: @Deprecated
174: public Enumeration<String> getServletNames() {
175: throw new UnsupportedOperationException("getServletNames");
176: }
177:
178: /* (non-Javadoc)
179: * @see javax.servlet.ServletContext#log(java.lang.String)
180: */
181: public void log(String message) {
182: log.info(message);
183: }
184:
185: /* (non-Javadoc)
186: * @see javax.servlet.ServletContext#log(java.lang.Exception, java.lang.String)
187: */
188: @Deprecated
189: public void log(Exception ex, String message) {
190: log.warn(message, ex);
191: }
192:
193: /* (non-Javadoc)
194: * @see javax.servlet.ServletContext#log(java.lang.String, java.lang.Throwable)
195: */
196: public void log(String message, Throwable ex) {
197: log.warn(message, ex);
198: }
199:
200: /* (non-Javadoc)
201: * @see javax.servlet.ServletContext#getRealPath(java.lang.String)
202: */
203: public String getRealPath(String path) {
204: throw new UnsupportedOperationException();
205: }
206:
207: /* (non-Javadoc)
208: * @see javax.servlet.ServletContext#getServerInfo()
209: */
210: public String getServerInfo() {
211: return "FakeServletContext";
212: }
213:
214: /* (non-Javadoc)
215: * @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
216: */
217: public String getInitParameter(String name) {
218: return initParameters.get(name);
219: }
220:
221: /**
222: * Add an init parameter to the list that we hand out
223: * @param name The name of the new init parameter
224: * @param value The value of the new init parameter
225: */
226: public void addInitParameter(String name, String value) {
227: initParameters.put(name, value);
228: }
229:
230: /* (non-Javadoc)
231: * @see javax.servlet.ServletContext#getInitParameterNames()
232: */
233: public Enumeration<String> getInitParameterNames() {
234: return Collections.enumeration(initParameters.keySet());
235: }
236:
237: /* (non-Javadoc)
238: * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
239: */
240: public Object getAttribute(String name) {
241: return attributes.get(name);
242: }
243:
244: /* (non-Javadoc)
245: * @see javax.servlet.ServletContext#getAttributeNames()
246: */
247: public Enumeration<String> getAttributeNames() {
248: return Collections.enumeration(attributes.keySet());
249: }
250:
251: /* (non-Javadoc)
252: * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object)
253: */
254: public void setAttribute(String name, Object value) {
255: if (value != null) {
256: attributes.put(name, value);
257: } else {
258: attributes.remove(name);
259: }
260: }
261:
262: /* (non-Javadoc)
263: * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
264: */
265: public void removeAttribute(String name) {
266: attributes.remove(name);
267: }
268:
269: /**
270: * Accessor for the servlet context name. Normally read-only, but read
271: * write in this fake context
272: * @param servletContextName The new servlet context name
273: */
274: public void setServletContextName(String servletContextName) {
275: this .servletContextName = servletContextName;
276: }
277:
278: /* (non-Javadoc)
279: * @see javax.servlet.ServletContext#getServletContextName()
280: */
281: public String getServletContextName() {
282: return servletContextName;
283: }
284:
285: /**
286: * See Servlet API version 2.5
287: * @return The context path for this servlet
288: */
289: public String getContextPath() {
290: throw new UnsupportedOperationException("getContextPath");
291: }
292:
293: /**
294: * The log stream
295: */
296: private static final Log log = LogFactory
297: .getLog(FakeServletContext.class);
298:
299: /**
300: * The resource path to allow us to fetch resources from the disk
301: */
302: private final String resourceBasePath;
303:
304: /**
305: * The init parameters to this servlet
306: */
307: private final Map<String, String> initParameters = new HashMap<String, String>();
308:
309: /**
310: * The servlet level attributes
311: */
312: private final Map<String, Object> attributes = new HashMap<String, Object>();
313:
314: /**
315: * The servlet context name
316: */
317: private String servletContextName = "FakeServletContext";
318: }
|