e.g. Calendar Search Help
You must enter a value before pressing Search
tomcat

Class: org.apache.jasper.servlet.JspCServletContext   ©

 OK to copy?
001 /*
002  * Copyright 1999,2004 The Apache Software Foundation.
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.apache.jasper.servlet;
018 
019 
020 import java.io.File;
021 import java.io.InputStream;
022 import java.io.PrintWriter;
023 import java.net.MalformedURLException;
024 import java.net.URL;
025 import java.util.Enumeration;
026 import java.util.HashSet;
027 import java.util.Hashtable;
028 import java.util.Set;
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 /**
038  * Simple <code>ServletContext</code> implementation without
039  * HTTP-specific methods.
040  *
041  * @author Peter Rossbach (pr@webapp.de)
042  */
043 
044 public class JspCServletContext implements ServletContext {
045 
046 
047     // ----------------------------------------------------- Instance Variables
048 
049 
050     /**
051      * Servlet context attributes.
052      */
053     protected Hashtable myAttributes;
054 
055 
056     /**
057      * The log writer we will write log messages to.
058      */
059     protected PrintWriter myLogWriter;
060 
061 
062     /**
063      * The base URL (document root) for this context.
064      */
065     protected URL myResourceBaseURL;
066 
067 
068     // ----------------------------------------------------------- Constructors
069 
070 
071     /**
072      * Create a new instance of this ServletContext implementation.
073      *
074      * @param aLogWriter PrintWriter which is used for <code>log()</code> calls
075      * @param aResourceBaseURL Resource base URL
076      */
077     public JspCServletContext(PrintWriter aLogWriter, URL aResourceBaseURL) {
078 
079         myAttributes = new Hashtable();
080         myLogWriter = aLogWriter;
081         myResourceBaseURL = aResourceBaseURL;
082 
083     }
084 
085 
086     // --------------------------------------------------------- Public Methods
087 
088 
089     /**
090      * Return the specified context attribute, if any.
091      *
092      * @param name Name of the requested attribute
093      */
094     public Object getAttribute(String name) {
095 
096         return (myAttributes.get(name));
097 
098     }
099 
100 
101     /**
102      * Return an enumeration of context attribute names.
103      */
104     public Enumeration getAttributeNames() {
105 
106         return (myAttributes.keys());
107 
108     }
109 
110 
111     /**
112      * Return the servlet context for the specified path.
113      *
114      * @param uripath Server-relative path starting with '/'
115      */
116     public ServletContext getContext(String uripath) {
117 
118         return (null);
119 
120     }
121 
122 
123     /**
124      * Return the specified context initialization parameter.
125      *
126      * @param name Name of the requested parameter
127      */
128     public String getInitParameter(String name) {
129 
130         return (null);
131 
132     }
133 
134 
135     /**
136      * Return an enumeration of the names of context initialization
137      * parameters.
138      */
139     public Enumeration getInitParameterNames() {
140 
141         return (new Vector().elements());
142 
143     }
144 
145 
146     /**
147      * Return the Servlet API major version number.
148      */
149     public int getMajorVersion() {
150 
151         return (2);
152 
153     }
154 
155 
156     /**
157      * Return the MIME type for the specified filename.
158      *
159      * @param file Filename whose MIME type is requested
160      */
161     public String getMimeType(String file) {
162 
163         return (null);
164 
165     }
166 
167 
168     /**
169      * Return the Servlet API minor version number.
170      */
171     public int getMinorVersion() {
172 
173         return (3);
174 
175     }
176 
177 
178     /**
179      * Return a request dispatcher for the specified servlet name.
180      *
181      * @param name Name of the requested servlet
182      */
183     public RequestDispatcher getNamedDispatcher(String name) {
184 
185         return (null);
186 
187     }
188 
189 
190     /**
191      * Return the real path for the specified context-relative
192      * virtual path.
193      *
194      * @param path The context-relative virtual path to resolve
195      */
196     public String getRealPath(String path) {
197 
198         if (!myResourceBaseURL.getProtocol().equals("file"))
199             return (null);
200         if (!path.startsWith("/"))
201             return (null);
202         try {
203             return
204                 (getResource(path).getFile().replace('/', File.separatorChar));
205         } catch (Throwable t) {
206             return (null);
207         }
208 
209     }
210             
211             
212     /**
213      * Return a request dispatcher for the specified context-relative path.
214      *
215      * @param path Context-relative path for which to acquire a dispatcher
216      */
217     public RequestDispatcher getRequestDispatcher(String path) {
218 
219         return (null);
220 
221     }
222 
223 
224     /**
225      * Return a URL object of a resource that is mapped to the
226      * specified context-relative path.
227      *
228      * @param path Context-relative path of the desired resource
229      *
230      * @exception MalformedURLException if the resource path is
231      *  not properly formed
232      */
233     public URL getResource(String path) throws MalformedURLException {
234 
235         if (!path.startsWith("/"))
236             throw new MalformedURLException("Path '" + path +
237                                             "' does not start with '/'");
238         return (new URL(myResourceBaseURL, path.substring(1)));
239 
240     }
241 
242 
243     /**
244      * Return an InputStream allowing access to the resource at the
245      * specified context-relative path.
246      *
247      * @param path Context-relative path of the desired resource
248      */
249     public InputStream getResourceAsStream(String path) {
250 
251         try {
252             return (getResource(path).openStream());
253         } catch (Throwable t) {
254             return (null);
255         }
256 
257     }
258 
259 
260     /**
261      * Return the set of resource paths for the "directory" at the
262      * specified context path.
263      *
264      * @param path Context-relative base path
265      */
266     public Set getResourcePaths(String path) {
267 
268         Set thePaths = new HashSet();
269         if (!path.endsWith("/"))
270             path += "/";
271         String basePath = getRealPath(path);
272         if (basePath == null)
273             return (thePaths);
274         File theBaseDir = new File(basePath);
275         if (!theBaseDir.exists() || !theBaseDir.isDirectory())
276             return (thePaths);
Rate277         String theFiles[] = theBaseDir.list();
278         for (int i = 0; i < theFiles.length; i++) {
279             File testFile = new File(basePath + File.separator + theFiles[i]);
280             if (testFile.isFile())
281                 thePaths.add(path + theFiles[i]);
282             else if (testFile.isDirectory())
283                 thePaths.add(path + theFiles[i] + "/");
284         }
285         return (thePaths);
286 
287     }
288 
289 
290     /**
291      * Return descriptive information about this server.
292      */
293     public String getServerInfo() {
294 
295         return ("JspCServletContext/1.0");
296 
297     }
298 
299 
300     /**
301      * Return a null reference for the specified servlet name.
302      *
303      * @param name Name of the requested servlet
304      *
305      * @deprecated This method has been deprecated with no replacement
306      */
307     public Servlet getServlet(String name) throws ServletException {
308 
309         return (null);
310 
311     }
312 
313 
314     /**
315      * Return the name of this servlet context.
316      */
317     public String getServletContextName() {
318 
319         return (getServerInfo());
320 
321     }
322 
323 
324     /**
325      * Return an empty enumeration of servlet names.
326      *
327      * @deprecated This method has been deprecated with no replacement
328      */
329     public Enumeration getServletNames() {
330 
331         return (new Vector().elements());
332 
333     }
334 
335 
336     /**
337      * Return an empty enumeration of servlets.
338      *
339      * @deprecated This method has been deprecated with no replacement
340      */
341     public Enumeration getServlets() {
342 
343         return (new Vector().elements());
344 
345     }
346 
347 
348     /**
349      * Log the specified message.
350      *
351      * @param message The message to be logged
352      */
353     public void log(String message) {
354 
355         myLogWriter.println(message);
356 
357     }
358 
359 
360     /**
361      * Log the specified message and exception.
362      *
363      * @param exception The exception to be logged
364      * @param message The message to be logged
365      *
366      * @deprecated Use log(String,Throwable) instead
367      */
368     public void log(Exception exception, String message) {
369 
370         log(message, exception);
371 
372     }
373 
374 
375     /**
376      * Log the specified message and exception.
377      *
378      * @param message The message to be logged
379      * @param exception The exception to be logged
380      */
381     public void log(String message, Throwable exception) {
382 
383         myLogWriter.println(message);
384         exception.printStackTrace(myLogWriter);
385 
386     }
387 
388 
389     /**
390      * Remove the specified context attribute.
391      *
392      * @param name Name of the attribute to remove
393      */
394     public void removeAttribute(String name) {
395 
396         myAttributes.remove(name);
397 
398     }
399 
400 
401     /**
402      * Set or replace the specified context attribute.
403      *
404      * @param name Name of the context attribute to set
405      * @param value Corresponding attribute value
406      */
407     public void setAttribute(String name, Object value) {
408 
409         myAttributes.put(name, value);
410 
411     }
412 
413 
414 
415 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
277 0% of 0