1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.jasper.servlet;
19
20
21 import com.opensymphony.xwork2.util.finder.ClassLoaderInterface;
22 import com.opensymphony.xwork2.ActionContext;
23
24 import javax.servlet.RequestDispatcher;
25 import javax.servlet.Servlet;
26 import javax.servlet.ServletContext;
27 import javax.servlet.ServletException;
28 import java.io.File;
29 import java.io.InputStream;
30 import java.io.PrintWriter;
31 import java.net.MalformedURLException;
32 import java.net.URL;
33 import java.util.*;
34
35 import org.apache.struts2.ServletActionContext;
36
37
38 /***
39 * Simple <code>ServletContext</code> implementation without
40 * HTTP-specific methods.
41 *
42 * @author Peter Rossbach (pr@webapp.de)
43 */
44
45 public class JspCServletContext implements ServletContext {
46
47
48
49
50
51 /***
52 * Servlet context attributes.
53 */
54 protected Hashtable myAttributes;
55
56
57 /***
58 * The log writer we will write log messages to.
59 */
60 protected PrintWriter myLogWriter;
61
62 private ClassLoaderInterface classLoaderInterface;
63 private static final String WEB_XML = "/WEB-INF/web.xml";
64
65
66
67
68
69 /***
70 * Create a new instance of this ServletContext implementation.
71 *
72 * @param aLogWriter PrintWriter which is used for <code>log()</code> calls
73 */
74 public JspCServletContext(PrintWriter aLogWriter, ClassLoaderInterface classLoaderInterface) {
75
76 myAttributes = new Hashtable();
77 myLogWriter = aLogWriter;
78 this.classLoaderInterface = classLoaderInterface;
79 }
80
81
82
83
84
85 /***
86 * Return the specified context attribute, if any.
87 *
88 * @param name Name of the requested attribute
89 */
90 public Object getAttribute(String name) {
91
92 return (myAttributes.get(name));
93
94 }
95
96
97 /***
98 * Return an enumeration of context attribute names.
99 */
100 public Enumeration getAttributeNames() {
101
102 return (myAttributes.keys());
103
104 }
105
106
107 /***
108 * Return the servlet context for the specified path.
109 *
110 * @param uripath Server-relative path starting with '/'
111 */
112 public ServletContext getContext(String uripath) {
113
114 return (null);
115
116 }
117
118
119 /***
120 * Return the specified context initialization parameter.
121 *
122 * @param name Name of the requested parameter
123 */
124 public String getInitParameter(String name) {
125
126 return (null);
127
128 }
129
130
131 /***
132 * Return an enumeration of the names of context initialization
133 * parameters.
134 */
135 public Enumeration getInitParameterNames() {
136
137 return (new Vector().elements());
138
139 }
140
141
142 /***
143 * Return the Servlet API major version number.
144 */
145 public int getMajorVersion() {
146
147 return (2);
148
149 }
150
151
152 /***
153 * Return the MIME type for the specified filename.
154 *
155 * @param file Filename whose MIME type is requested
156 */
157 public String getMimeType(String file) {
158
159 return (null);
160
161 }
162
163
164 /***
165 * Return the Servlet API minor version number.
166 */
167 public int getMinorVersion() {
168
169 return (3);
170
171 }
172
173
174 /***
175 * Return a request dispatcher for the specified servlet name.
176 *
177 * @param name Name of the requested servlet
178 */
179 public RequestDispatcher getNamedDispatcher(String name) {
180
181 return (null);
182
183 }
184
185
186 /***
187 * Return the real path for the specified context-relative
188 * virtual path.
189 *
190 * @param path The context-relative virtual path to resolve
191 */
192 public String getRealPath(String path) {
193 try {
194 return
195 (getResource(path).getFile().replace('/', File.separatorChar));
196 } catch (Throwable t) {
197 return (null);
198 }
199
200 }
201
202
203 /***
204 * Return a request dispatcher for the specified context-relative path.
205 *
206 * @param path Context-relative path for which to acquire a dispatcher
207 */
208 public RequestDispatcher getRequestDispatcher(String path) {
209
210 return (null);
211
212 }
213
214
215 /***
216 * Return a URL object of a resource that is mapped to the
217 * specified context-relative path.
218 *
219 * @param path Context-relative path of the desired resource
220 * @throws MalformedURLException if the resource path is
221 * not properly formed
222 */
223 public URL getResource(String path) throws MalformedURLException {
224 if (WEB_XML.equals(path)) {
225 if (ActionContext.getContext() != null) {
226 ServletContext context = ServletActionContext.getServletContext();
227 return context.getResource(path);
228 }
229
230 return null;
231 }
232 return classLoaderInterface.getResource(path);
233 }
234
235
236 /***
237 * Return an InputStream allowing access to the resource at the
238 * specified context-relative path.
239 *
240 * @param path Context-relative path of the desired resource
241 */
242 public InputStream getResourceAsStream(String path) {
243
244 try {
245 return classLoaderInterface.getResourceAsStream(path);
246 } catch (Throwable t) {
247 return (null);
248 }
249
250 }
251
252
253 /***
254 * Return the set of resource paths for the "directory" at the
255 * specified context path.
256 *
257 * @param path Context-relative base path
258 */
259 public Set getResourcePaths(String path) {
260
261 Set thePaths = new HashSet();
262 if (!path.endsWith("/"))
263 path += "/";
264 String basePath = getRealPath(path);
265 if (basePath == null)
266 return (thePaths);
267 File theBaseDir = new File(basePath);
268 if (!theBaseDir.exists() || !theBaseDir.isDirectory())
269 return (thePaths);
270 String theFiles[] = theBaseDir.list();
271 for (int i = 0; i < theFiles.length; i++) {
272 File testFile = new File(basePath + File.separator + theFiles[i]);
273 if (testFile.isFile())
274 thePaths.add(path + theFiles[i]);
275 else if (testFile.isDirectory())
276 thePaths.add(path + theFiles[i] + "/");
277 }
278 return (thePaths);
279
280 }
281
282
283 /***
284 * Return descriptive information about this server.
285 */
286 public String getServerInfo() {
287
288 return ("JspCServletContext/1.0");
289
290 }
291
292
293 /***
294 * Return a null reference for the specified servlet name.
295 *
296 * @param name Name of the requested servlet
297 * @deprecated This method has been deprecated with no replacement
298 */
299 public Servlet getServlet(String name) throws ServletException {
300
301 return (null);
302
303 }
304
305
306 /***
307 * Return the name of this servlet context.
308 */
309 public String getServletContextName() {
310
311 return (getServerInfo());
312
313 }
314
315
316 /***
317 * Return an empty enumeration of servlet names.
318 *
319 * @deprecated This method has been deprecated with no replacement
320 */
321 public Enumeration getServletNames() {
322
323 return (new Vector().elements());
324
325 }
326
327
328 /***
329 * Return an empty enumeration of servlets.
330 *
331 * @deprecated This method has been deprecated with no replacement
332 */
333 public Enumeration getServlets() {
334
335 return (new Vector().elements());
336
337 }
338
339
340 /***
341 * Log the specified message.
342 *
343 * @param message The message to be logged
344 */
345 public void log(String message) {
346
347 myLogWriter.println(message);
348
349 }
350
351
352 /***
353 * Log the specified message and exception.
354 *
355 * @param exception The exception to be logged
356 * @param message The message to be logged
357 * @deprecated Use log(String,Throwable) instead
358 */
359 public void log(Exception exception, String message) {
360
361 log(message, exception);
362
363 }
364
365
366 /***
367 * Log the specified message and exception.
368 *
369 * @param message The message to be logged
370 * @param exception The exception to be logged
371 */
372 public void log(String message, Throwable exception) {
373
374 myLogWriter.println(message);
375 exception.printStackTrace(myLogWriter);
376
377 }
378
379
380 /***
381 * Remove the specified context attribute.
382 *
383 * @param name Name of the attribute to remove
384 */
385 public void removeAttribute(String name) {
386
387 myAttributes.remove(name);
388
389 }
390
391
392 /***
393 * Set or replace the specified context attribute.
394 *
395 * @param name Name of the context attribute to set
396 * @param value Corresponding attribute value
397 */
398 public void setAttribute(String name, Object value) {
399
400 myAttributes.put(name, value);
401
402 }
403
404
405 }