001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.jasper.servlet;
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: * Simple <code>ServletContext</code> implementation without
038: * HTTP-specific methods.
039: *
040: * @author Peter Rossbach (pr@webapp.de)
041: */
042:
043: public class JspCServletContext implements ServletContext {
044:
045: // ----------------------------------------------------- Instance Variables
046:
047: /**
048: * Servlet context attributes.
049: */
050: protected Hashtable myAttributes;
051:
052: /**
053: * The log writer we will write log messages to.
054: */
055: protected PrintWriter myLogWriter;
056:
057: /**
058: * The base URL (document root) for this context.
059: */
060: protected URL myResourceBaseURL;
061:
062: // ----------------------------------------------------------- Constructors
063:
064: /**
065: * Create a new instance of this ServletContext implementation.
066: *
067: * @param aLogWriter PrintWriter which is used for <code>log()</code> calls
068: * @param aResourceBaseURL Resource base URL
069: */
070: public JspCServletContext(PrintWriter aLogWriter,
071: URL aResourceBaseURL) {
072:
073: myAttributes = new Hashtable();
074: myLogWriter = aLogWriter;
075: myResourceBaseURL = aResourceBaseURL;
076:
077: }
078:
079: // --------------------------------------------------------- Public Methods
080:
081: /**
082: * Return the specified context attribute, if any.
083: *
084: * @param name Name of the requested attribute
085: */
086: public Object getAttribute(String name) {
087:
088: return (myAttributes.get(name));
089:
090: }
091:
092: /**
093: * Return an enumeration of context attribute names.
094: */
095: public Enumeration getAttributeNames() {
096:
097: return (myAttributes.keys());
098:
099: }
100:
101: /**
102: * Return the servlet context for the specified path.
103: *
104: * @param uripath Server-relative path starting with '/'
105: */
106: public ServletContext getContext(String uripath) {
107:
108: return (null);
109:
110: }
111:
112: /**
113: * Return the context path.
114: */
115: public String getContextPath() {
116:
117: return (null);
118:
119: }
120:
121: /**
122: * Return the specified context initialization parameter.
123: *
124: * @param name Name of the requested parameter
125: */
126: public String getInitParameter(String name) {
127:
128: return (null);
129:
130: }
131:
132: /**
133: * Return an enumeration of the names of context initialization
134: * parameters.
135: */
136: public Enumeration getInitParameterNames() {
137:
138: return (new Vector().elements());
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: * Return the MIME type for the specified filename.
153: *
154: * @param file Filename whose MIME type is requested
155: */
156: public String getMimeType(String file) {
157:
158: return (null);
159:
160: }
161:
162: /**
163: * Return the Servlet API minor version number.
164: */
165: public int getMinorVersion() {
166:
167: return (3);
168:
169: }
170:
171: /**
172: * Return a request dispatcher for the specified servlet name.
173: *
174: * @param name Name of the requested servlet
175: */
176: public RequestDispatcher getNamedDispatcher(String name) {
177:
178: return (null);
179:
180: }
181:
182: /**
183: * Return the real path for the specified context-relative
184: * virtual path.
185: *
186: * @param path The context-relative virtual path to resolve
187: */
188: public String getRealPath(String path) {
189:
190: if (!myResourceBaseURL.getProtocol().equals("file"))
191: return (null);
192: if (!path.startsWith("/"))
193: return (null);
194: try {
195: return (getResource(path).getFile().replace('/',
196: File.separatorChar));
197: } catch (Throwable t) {
198: return (null);
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: * Return a URL object of a resource that is mapped to the
216: * specified context-relative path.
217: *
218: * @param path Context-relative path of the desired resource
219: *
220: * @exception MalformedURLException if the resource path is
221: * not properly formed
222: */
223: public URL getResource(String path) throws MalformedURLException {
224:
225: if (!path.startsWith("/"))
226: throw new MalformedURLException("Path '" + path
227: + "' does not start with '/'");
228: URL url = new URL(myResourceBaseURL, path.substring(1));
229: InputStream is = null;
230: try {
231: is = url.openStream();
232: } catch (Throwable t) {
233: url = null;
234: } finally {
235: if (is != null) {
236: try {
237: is.close();
238: } catch (Throwable t2) {
239: // Ignore
240: }
241: }
242: }
243: return url;
244:
245: }
246:
247: /**
248: * Return an InputStream allowing access to the resource at the
249: * specified context-relative path.
250: *
251: * @param path Context-relative path of the desired resource
252: */
253: public InputStream getResourceAsStream(String path) {
254:
255: try {
256: return (getResource(path).openStream());
257: } catch (Throwable t) {
258: return (null);
259: }
260:
261: }
262:
263: /**
264: * Return the set of resource paths for the "directory" at the
265: * specified context path.
266: *
267: * @param path Context-relative base path
268: */
269: public Set getResourcePaths(String path) {
270:
271: Set thePaths = new HashSet();
272: if (!path.endsWith("/"))
273: path += "/";
274: String basePath = getRealPath(path);
275: if (basePath == null)
276: return (thePaths);
277: File theBaseDir = new File(basePath);
278: if (!theBaseDir.exists() || !theBaseDir.isDirectory())
279: return (thePaths);
280: String theFiles[] = theBaseDir.list();
281: for (int i = 0; i < theFiles.length; i++) {
282: File testFile = new File(basePath + File.separator
283: + theFiles[i]);
284: if (testFile.isFile())
285: thePaths.add(path + theFiles[i]);
286: else if (testFile.isDirectory())
287: thePaths.add(path + theFiles[i] + "/");
288: }
289: return (thePaths);
290:
291: }
292:
293: /**
294: * Return descriptive information about this server.
295: */
296: public String getServerInfo() {
297:
298: return ("JspCServletContext/1.0");
299:
300: }
301:
302: /**
303: * Return a null reference for the specified servlet name.
304: *
305: * @param name Name of the requested servlet
306: *
307: * @deprecated This method has been deprecated with no replacement
308: */
309: public Servlet getServlet(String name) throws ServletException {
310:
311: return (null);
312:
313: }
314:
315: /**
316: * Return the name of this servlet context.
317: */
318: public String getServletContextName() {
319:
320: return (getServerInfo());
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: * Return an empty enumeration of servlets.
337: *
338: * @deprecated This method has been deprecated with no replacement
339: */
340: public Enumeration getServlets() {
341:
342: return (new Vector().elements());
343:
344: }
345:
346: /**
347: * Log the specified message.
348: *
349: * @param message The message to be logged
350: */
351: public void log(String message) {
352:
353: myLogWriter.println(message);
354:
355: }
356:
357: /**
358: * Log the specified message and exception.
359: *
360: * @param exception The exception to be logged
361: * @param message The message to be logged
362: *
363: * @deprecated Use log(String,Throwable) instead
364: */
365: public void log(Exception exception, String message) {
366:
367: log(message, exception);
368:
369: }
370:
371: /**
372: * Log the specified message and exception.
373: *
374: * @param message The message to be logged
375: * @param exception The exception to be logged
376: */
377: public void log(String message, Throwable exception) {
378:
379: myLogWriter.println(message);
380: exception.printStackTrace(myLogWriter);
381:
382: }
383:
384: /**
385: * Remove the specified context attribute.
386: *
387: * @param name Name of the attribute to remove
388: */
389: public void removeAttribute(String name) {
390:
391: myAttributes.remove(name);
392:
393: }
394:
395: /**
396: * Set or replace the specified context attribute.
397: *
398: * @param name Name of the context attribute to set
399: * @param value Corresponding attribute value
400: */
401: public void setAttribute(String name, Object value) {
402:
403: myAttributes.put(name, value);
404:
405: }
406:
407: }
|