KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > util > FakeServletContext


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.directwebremoting.util;
18
19 import java.io.File JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Properties JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import javax.servlet.RequestDispatcher JavaDoc;
31 import javax.servlet.Servlet JavaDoc;
32 import javax.servlet.ServletContext JavaDoc;
33
34 /**
35  * Fake implementation of the ServletContext interface.
36  * @author Rod Johnson
37  * @author Juergen Hoeller
38  * @author Joe Walker [joe at getahead dot ltd dot uk]
39  */

40 public class FakeServletContext implements ServletContext JavaDoc
41 {
42     /**
43      * Create a new FakeServletContext, using no base path and a
44      * DefaultResourceLoader (i.e. the classpath root as WAR root).
45      */

46     public FakeServletContext()
47     {
48         this("");
49     }
50
51     /**
52      * Create a new FakeServletContext, using a DefaultResourceLoader.
53      * @param resourceBasePath the WAR root directory (should not end with a slash)
54      */

55     public FakeServletContext(String JavaDoc resourceBasePath)
56     {
57         this.resourceBasePath = (resourceBasePath != null ? resourceBasePath : "");
58
59         // Use JVM temp dir as ServletContext temp dir.
60
String JavaDoc tempDir = System.getProperty("java.io.tmpdir");
61         if (tempDir != null)
62         {
63             attributes.put("javax.servlet.context.tempdir", new File JavaDoc(tempDir));
64         }
65     }
66
67     /**
68      * Build a full resource location for the given path,
69      * prepending the resource base path of this FakeServletContext.
70      * @param path the path as specified
71      * @return the full resource path
72      */

73     protected String JavaDoc getResourceLocation(String JavaDoc path)
74     {
75         String JavaDoc output = path;
76         if (!output.startsWith("/"))
77         {
78             output = "/" + output;
79         }
80         output = resourceBasePath + output;
81
82         return output;
83     }
84
85     public ServletContext JavaDoc getContext(String JavaDoc name)
86     {
87         throw new UnsupportedOperationException JavaDoc("getContext");
88     }
89
90     /* (non-Javadoc)
91      * @see javax.servlet.ServletContext#getMajorVersion()
92      */

93     public int getMajorVersion()
94     {
95         return 2;
96     }
97
98     /* (non-Javadoc)
99      * @see javax.servlet.ServletContext#getMinorVersion()
100      */

101     public int getMinorVersion()
102     {
103         return 4;
104     }
105
106     /* (non-Javadoc)
107      * @see javax.servlet.ServletContext#getMimeType(java.lang.String)
108      */

109     public String JavaDoc getMimeType(String JavaDoc filePath)
110     {
111         throw new UnsupportedOperationException JavaDoc("getMimeType");
112     }
113
114     /* (non-Javadoc)
115      * @see javax.servlet.ServletContext#getResourcePaths(java.lang.String)
116      */

117     public Set JavaDoc getResourcePaths(String JavaDoc path)
118     {
119         throw new UnsupportedOperationException JavaDoc();
120     }
121
122     /* (non-Javadoc)
123      * @see javax.servlet.ServletContext#getResource(java.lang.String)
124      */

125     public URL JavaDoc getResource(String JavaDoc path) throws MalformedURLException JavaDoc
126     {
127         throw new UnsupportedOperationException JavaDoc();
128     }
129
130     /* (non-Javadoc)
131      * @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String)
132      */

133     public InputStream JavaDoc getResourceAsStream(String JavaDoc path)
134     {
135         throw new UnsupportedOperationException JavaDoc();
136     }
137
138     /* (non-Javadoc)
139      * @see javax.servlet.ServletContext#getRequestDispatcher(java.lang.String)
140      */

141     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path)
142     {
143         if (!path.startsWith("/"))
144         {
145             throw new IllegalArgumentException JavaDoc("RequestDispatcher path at ServletContext level must start with '/'");
146         }
147         return new FakeRequestDispatcher(path);
148     }
149
150     /* (non-Javadoc)
151      * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
152      */

153     public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc path)
154     {
155         throw new UnsupportedOperationException JavaDoc("getNamedDispatcher");
156     }
157
158     /* (non-Javadoc)
159      * @see javax.servlet.ServletContext#getServlet(java.lang.String)
160      */

161     public Servlet JavaDoc getServlet(String JavaDoc name)
162     {
163         throw new UnsupportedOperationException JavaDoc("getServlet");
164     }
165
166     /* (non-Javadoc)
167      * @see javax.servlet.ServletContext#getServlets()
168      */

169     public Enumeration JavaDoc getServlets()
170     {
171         throw new UnsupportedOperationException JavaDoc("getServlets");
172     }
173
174     /* (non-Javadoc)
175      * @see javax.servlet.ServletContext#getServletNames()
176      */

177     public Enumeration JavaDoc getServletNames()
178     {
179         throw new UnsupportedOperationException JavaDoc("getServletNames");
180     }
181
182     /* (non-Javadoc)
183      * @see javax.servlet.ServletContext#log(java.lang.String)
184      */

185     public void log(String JavaDoc message)
186     {
187         log.info(message);
188     }
189
190     /* (non-Javadoc)
191      * @see javax.servlet.ServletContext#log(java.lang.Exception, java.lang.String)
192      */

193     public void log(Exception JavaDoc ex, String JavaDoc message)
194     {
195         log.warn(message, ex);
196     }
197
198     /* (non-Javadoc)
199      * @see javax.servlet.ServletContext#log(java.lang.String, java.lang.Throwable)
200      */

201     public void log(String JavaDoc message, Throwable JavaDoc ex)
202     {
203         log.warn(message, ex);
204     }
205
206     /* (non-Javadoc)
207      * @see javax.servlet.ServletContext#getRealPath(java.lang.String)
208      */

209     public String JavaDoc getRealPath(String JavaDoc path)
210     {
211         throw new UnsupportedOperationException JavaDoc();
212     }
213
214     /* (non-Javadoc)
215      * @see javax.servlet.ServletContext#getServerInfo()
216      */

217     public String JavaDoc getServerInfo()
218     {
219         return "FakeServletContext";
220     }
221
222     /* (non-Javadoc)
223      * @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
224      */

225     public String JavaDoc getInitParameter(String JavaDoc name)
226     {
227         return initParameters.getProperty(name);
228     }
229
230     /**
231      * Add an init parameter to the list that we hand out
232      * @param name The name of the new init parameter
233      * @param value The value of the new init parameter
234      */

235     public void addInitParameter(String JavaDoc name, String JavaDoc value)
236     {
237         initParameters.setProperty(name, value);
238     }
239
240     /* (non-Javadoc)
241      * @see javax.servlet.ServletContext#getInitParameterNames()
242      */

243     public Enumeration JavaDoc getInitParameterNames()
244     {
245         return initParameters.keys();
246     }
247
248     /* (non-Javadoc)
249      * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
250      */

251     public Object JavaDoc getAttribute(String JavaDoc name)
252     {
253         return attributes.get(name);
254     }
255
256     /* (non-Javadoc)
257      * @see javax.servlet.ServletContext#getAttributeNames()
258      */

259     public Enumeration JavaDoc getAttributeNames()
260     {
261         return Collections.enumeration(attributes.keySet());
262     }
263
264     /* (non-Javadoc)
265      * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object)
266      */

267     public void setAttribute(String JavaDoc name, Object JavaDoc value)
268     {
269         if (value != null)
270         {
271             attributes.put(name, value);
272         }
273         else
274         {
275             attributes.remove(name);
276         }
277     }
278
279     /* (non-Javadoc)
280      * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
281      */

282     public void removeAttribute(String JavaDoc name)
283     {
284         attributes.remove(name);
285     }
286
287     /**
288      * Accessor for the servlet context name. Normally read-only, but read
289      * write in this fake context
290      * @param servletContextName The new servlet context name
291      */

292     public void setServletContextName(String JavaDoc servletContextName)
293     {
294         this.servletContextName = servletContextName;
295     }
296
297     /* (non-Javadoc)
298      * @see javax.servlet.ServletContext#getServletContextName()
299      */

300     public String JavaDoc getServletContextName()
301     {
302         return servletContextName;
303     }
304
305     /**
306      * See Servlet API version 2.5
307      * @return The context path for this servlet
308      */

309     public String JavaDoc getContextPath()
310     {
311         throw new UnsupportedOperationException JavaDoc("getContextPath");
312     }
313
314     /**
315      * The log stream
316      */

317     private static final Logger log = Logger.getLogger(FakeServletContext.class);
318
319     /**
320      *
321      */

322     private final String JavaDoc resourceBasePath;
323
324     /**
325      * The init parameters to this servlet
326      */

327     private final Properties JavaDoc initParameters = new Properties JavaDoc();
328
329     /**
330      * The servlet level attributes
331      */

332     private final Map JavaDoc attributes = new HashMap JavaDoc();
333
334     /**
335      * The servlet context name
336      */

337     private String JavaDoc servletContextName = "FakeServletContext";
338 }
339
Popular Tags