1   
2   
3   /*
4    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5    * 
6    * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
7    * 
8    * Portions Copyright Apache Software Foundation.
9    * 
10   * The contents of this file are subject to the terms of either the GNU
11   * General Public License Version 2 only ("GPL") or the Common Development
12   * and Distribution License("CDDL") (collectively, the "License").  You
13   * may not use this file except in compliance with the License. You can obtain
14   * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
15   * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
16   * language governing permissions and limitations under the License.
17   * 
18   * When distributing the software, include this License Header Notice in each
19   * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
20   * Sun designates this particular file as subject to the "Classpath" exception
21   * as provided by Sun in the GPL Version 2 section of the License file that
22   * accompanied this code.  If applicable, add the following below the License
23   * Header, with the fields enclosed by brackets [] replaced by your own
24   * identifying information: "Portions Copyrighted [year]
25   * [name of copyright owner]"
26   * 
27   * Contributor(s):
28   * 
29   * If you wish your version of this file to be governed by only the CDDL or
30   * only the GPL Version 2, indicate your decision by adding "[Contributor]
31   * elects to include this software in this distribution under the [CDDL or GPL
32   * Version 2] license."  If you don't indicate a single choice of license, a
33   * recipient has the option to distribute your version of this file under
34   * either the CDDL, the GPL Version 2 or to extend the choice of license to
35   * its licensees as provided above.  However, if you add GPL Version 2 code
36   * and therefore, elected the GPL Version 2 license, then the option applies
37   * only if the new code is made subject to such option by the copyright
38   * holder.
39   */
40  
41  package org.apache.jasper.servlet;
42  
43  
44  import java.io.File;
45  import java.io.InputStream;
46  import java.io.IOException;
47  import java.io.PrintWriter;
48  import java.net.MalformedURLException;
49  import java.net.URL;
50  import java.util.EnumSet;
51  import java.util.Enumeration;
52  import java.util.HashSet;
53  import java.util.Hashtable;
54  import java.util.Map;
55  import java.util.Set;
56  import java.util.Vector;
57  import java.net.URL;
58  
59  import javax.servlet.DispatcherType;
60  import javax.servlet.RequestDispatcher;
61  import javax.servlet.Servlet;
62  import javax.servlet.ServletContext;
63  import javax.servlet.ServletException;
64  
65  
66  /**
67   * Simple <code>ServletContext</code> implementation without
68   * HTTP-specific methods.
69   *
70   * @author Peter Rossbach (pr@webapp.de)
71   */
72  
73  public class JspCServletContext implements ServletContext {
74  
75  
76      // ----------------------------------------------------- Instance Variables
77  
78  
79      /**
80       * Servlet context attributes.
81       */
82      protected Hashtable myAttributes;
83  
84  
85      /**
86       * The log writer we will write log messages to.
87       */
88      protected PrintWriter myLogWriter;
89  
90  
91      /**
92       * The base URL (document root) for this context.
93       */
94      protected URL myResourceBaseURL;
95  
96  
97      // ----------------------------------------------------------- Constructors
98  
99  
100     /**
101      * Create a new instance of this ServletContext implementation.
102      *
103      * @param aLogWriter PrintWriter which is used for <code>log()</code> calls
104      * @param aResourceBaseURL Resource base URL
105      */
106     public JspCServletContext(PrintWriter aLogWriter, URL aResourceBaseURL) {
107 
108         myAttributes = new Hashtable();
109         myLogWriter = aLogWriter;
110         myResourceBaseURL = aResourceBaseURL;
111 
112     }
113 
114 
115     // --------------------------------------------------------- Public Methods
116 
117 
118     /**
119      * Return the specified context attribute, if any.
120      *
121      * @param name Name of the requested attribute
122      */
123     public Object getAttribute(String name) {
124 
125         return (myAttributes.get(name));
126 
127     }
128 
129 
130     /**
131      * Return an enumeration of context attribute names.
132      */
133     public Enumeration getAttributeNames() {
134 
135         return (myAttributes.keys());
136 
137     }
138 
139 
140     /**
141      * Returns the context path of the web application.
142      */
143     public String getContextPath() {
144         return null;
145     }
146 
147 
148     /**
149      * Return the servlet context for the specified path.
150      *
151      * @param uripath Server-relative path starting with '/'
152      */
153     public ServletContext getContext(String uripath) {
154 
155         return (null);
156 
157     }
158 
159 
160     /**
161      * Return the specified context initialization parameter.
162      *
163      * @param name Name of the requested parameter
164      */
165     public String getInitParameter(String name) {
166 
167         return (null);
168 
169     }
170 
171 
172     /**
173      * Return an enumeration of the names of context initialization
174      * parameters.
175      */
176     public Enumeration getInitParameterNames() {
177 
178         return (new Vector().elements());
179 
180     }
181 
182 
183     /**
184      * Return the Servlet API major version number.
185      */
186     public int getMajorVersion() {
187 
188         return (2);
189 
190     }
191 
192 
193     /**
194      * Return the MIME type for the specified filename.
195      *
196      * @param file Filename whose MIME type is requested
197      */
198     public String getMimeType(String file) {
199 
200         return (null);
201 
202     }
203 
204 
205     /**
206      * Return the Servlet API minor version number.
207      */
208     public int getMinorVersion() {
209 
210         return (3);
211 
212     }
213 
214 
215     /**
216      * Return a request dispatcher for the specified servlet name.
217      *
218      * @param name Name of the requested servlet
219      */
220     public RequestDispatcher getNamedDispatcher(String name) {
221 
222         return (null);
223 
224     }
225 
226 
227     /**
228      * Return the real path for the specified context-relative
229      * virtual path.
230      *
231      * @param path The context-relative virtual path to resolve
232      */
233     public String getRealPath(String path) {
234 
235         if (!myResourceBaseURL.getProtocol().equals("file"))
236             return (null);
237         if (!path.startsWith("/"))
238             return (null);
239         try {
240             return
241                 (getResource(path).getFile().replace('/', File.separatorChar));
242         } catch (Throwable t) {
243             return (null);
244         }
245 
246     }
247             
248             
249     /**
250      * Return a request dispatcher for the specified context-relative path.
251      *
252      * @param path Context-relative path for which to acquire a dispatcher
253      */
254     public RequestDispatcher getRequestDispatcher(String path) {
255 
256         return (null);
257 
258     }
259 
260 
261     /**
262      * Return a URL object of a resource that is mapped to the
263      * specified context-relative path.
264      *
265      * @param path Context-relative path of the desired resource
266      *
267      * @exception MalformedURLException if the resource path is
268      *  not properly formed
269      */
270     public URL getResource(String path) throws MalformedURLException {
271 
272         if (!path.startsWith("/"))
273             throw new MalformedURLException("Path '" + path +
274                                             "' does not start with '/'");
275         URL url = new URL(myResourceBaseURL, path.substring(1));
276         InputStream is = null;
277         try {
278             is = url.openStream();
279         } catch (Throwable t) {
280             url = null;
281         } finally {
282             if (is != null) {
283                 try {
284                     is.close();
285                 } catch (Throwable t2) {
286                     // Ignore
287                 }
288             }
289         }
290         return url;
291     }
292 
293 
294     /**
295      * Return an InputStream allowing access to the resource at the
296      * specified context-relative path.
297      *
298      * @param path Context-relative path of the desired resource
299      */
300     public InputStream getResourceAsStream(String path) {
301 
302         try {
303             return (getResource(path).openStream());
304         } catch (Throwable t) {
305             return (null);
306         }
307 
308     }
309 
310 
311     /**
312      * Return the set of resource paths for the "directory" at the
313      * specified context path.
314      *
315      * @param path Context-relative base path
316      */
317     public Set getResourcePaths(String path) {
318 
319         Set thePaths = new HashSet();
320         if (!path.endsWith("/"))
321             path += "/";
322         String basePath = getRealPath(path);
323         if (basePath == null)
324             return (thePaths);
325         File theBaseDir = new File(basePath);
326         if (!theBaseDir.exists() || !theBaseDir.isDirectory())
327             return (thePaths);
328         String theFiles[] = theBaseDir.list();
329         for (int i = 0; i < theFiles.length; i++) {
330             File testFile = new File(basePath + File.separator + theFiles[i]);
331             if (testFile.isFile())
332                 thePaths.add(path + theFiles[i]);
333             else if (testFile.isDirectory())
334                 thePaths.add(path + theFiles[i] + "/");
335         }
336         return (thePaths);
337 
338     }
339 
340 
341     /**
342      * Return descriptive information about this server.
343      */
344     public String getServerInfo() {
345 
346         return ("JspCServletContext/1.0");
347 
348     }
349 
350 
351     /**
352      * Return a null reference for the specified servlet name.
353      *
354      * @param name Name of the requested servlet
355      *
356      * @deprecated This method has been deprecated with no replacement
357      */
358     public Servlet getServlet(String name) throws ServletException {
359 
360         return (null);
361 
362     }
363 
364 
365     /**
366      * Return the name of this servlet context.
367      */
368     public String getServletContextName() {
369 
370         return (getServerInfo());
371 
372     }
373 
374 
375     /**
376      * Return an empty enumeration of servlet names.
377      *
378      * @deprecated This method has been deprecated with no replacement
379      */
380     public Enumeration getServletNames() {
381 
382         return (new Vector().elements());
383 
384     }
385 
386 
387     /**
388      * Return an empty enumeration of servlets.
389      *
390      * @deprecated This method has been deprecated with no replacement
391      */
392     public Enumeration getServlets() {
393 
394         return (new Vector().elements());
395 
396     }
397 
398 
399     /**
400      * Log the specified message.
401      *
402      * @param message The message to be logged
403      */
404     public void log(String message) {
405 
406         myLogWriter.println(message);
407 
408     }
409 
410 
411     /**
412      * Log the specified message and exception.
413      *
414      * @param exception The exception to be logged
415      * @param message The message to be logged
416      *
417      * @deprecated Use log(String,Throwable) instead
418      */
419     public void log(Exception exception, String message) {
420 
421         log(message, exception);
422 
423     }
424 
425 
426     /**
427      * Log the specified message and exception.
428      *
429      * @param message The message to be logged
430      * @param exception The exception to be logged
431      */
432     public void log(String message, Throwable exception) {
433 
434         myLogWriter.println(message);
435         exception.printStackTrace(myLogWriter);
436 
437     }
438 
439 
440     /**
441      * Remove the specified context attribute.
442      *
443      * @param name Name of the attribute to remove
444      */
445     public void removeAttribute(String name) {
446 
447         myAttributes.remove(name);
448 
449     }
450 
451 
452     /**
453      * Set or replace the specified context attribute.
454      *
455      * @param name Name of the context attribute to set
456      * @param value Corresponding attribute value
457      */
458     public void setAttribute(String name, Object value) {
459 
460         myAttributes.put(name, value);
461 
462     }
463 
464 
465     /* ------------------------------------------------------------ */
466     /* (non-Javadoc)
467      * @see javax.servlet.ServletContext#addFilter(java.lang.String, java.lang.String, java.lang.String, java.util.Map)
468      */
469     public void addFilter(String filterName, String description, String className, Map<String, String> initParameters)
470     {
471         // TODO Auto-generated method stub
472         
473     }
474 
475 
476     /* ------------------------------------------------------------ */
477     /* (non-Javadoc)
478      * @see javax.servlet.ServletContext#addFilterMapping(java.lang.String, java.lang.String[], java.lang.String[], java.util.EnumSet, boolean)
479      */
480     public void addFilterMapping(String filterName, String[] urlPatterns, String[] servletNames, EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter)
481     {
482         // TODO Auto-generated method stub
483         
484     }
485 
486 
487     /* ------------------------------------------------------------ */
488     /* (non-Javadoc)
489      * @see javax.servlet.ServletContext#addServlet(java.lang.String, java.lang.String, java.lang.String, java.util.Map, int)
490      */
491     public void addServlet(String servletName, String description, String className, Map<String, String> initParameters, int loadOnStartup)
492     {
493         // TODO Auto-generated method stub
494         
495     }
496 
497 
498     /* ------------------------------------------------------------ */
499     /* (non-Javadoc)
500      * @see javax.servlet.ServletContext#addServletMapping(java.lang.String, java.lang.String[])
501      */
502     public void addServletMapping(String servletName, String[] urlPatterns)
503     {
504         // TODO Auto-generated method stub
505         
506     }
507 
508 
509 
510 }