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