001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.webapp;
031:
032: import com.caucho.log.Log;
033: import com.caucho.util.L10N;
034: import com.caucho.vfs.Path;
035: import com.caucho.vfs.Vfs;
036:
037: import javax.servlet.RequestDispatcher;
038: import javax.servlet.Servlet;
039: import javax.servlet.ServletContext;
040: import javax.servlet.ServletContextAttributeEvent;
041: import javax.servlet.ServletContextAttributeListener;
042: import java.io.IOException;
043: import java.io.InputStream;
044: import java.net.URL;
045: import java.util.ArrayList;
046: import java.util.Collections;
047: import java.util.Enumeration;
048: import java.util.HashMap;
049: import java.util.HashSet;
050: import java.util.Set;
051: import java.util.logging.Level;
052: import java.util.logging.Logger;
053:
054: /**
055: * Bare-bones servlet context implementation.
056: */
057: public class ServletContextImpl implements ServletContext {
058: static final Logger log = Log.open(ServletContextImpl.class);
059: static final L10N L = new L10N(ServletContextImpl.class);
060:
061: private String _name;
062:
063: private HashMap<String, Object> _attributes = new HashMap<String, Object>();
064:
065: private ArrayList<ServletContextAttributeListener> _applicationAttributeListeners;
066:
067: private HashMap<String, String> _initParams = new HashMap<String, String>();
068:
069: public Path getAppDir() {
070: return Vfs.lookup();
071: }
072:
073: /**
074: * Sets the servlet context name
075: */
076: public void setDisplayName(String name) {
077: _name = name;
078: }
079:
080: /**
081: * Gets the servlet context name
082: */
083: public String getServletContextName() {
084: return _name;
085: }
086:
087: /**
088: * Gets the servlet context name
089: */
090: public String getContextPath() {
091: return _name;
092: }
093:
094: /**
095: * Adds the listener.
096: */
097: protected void addAttributeListener(
098: ServletContextAttributeListener listener) {
099: if (_applicationAttributeListeners == null)
100: _applicationAttributeListeners = new ArrayList<ServletContextAttributeListener>();
101:
102: _applicationAttributeListeners.add(listener);
103: }
104:
105: /**
106: * Returns the server information
107: */
108: public String getServerInfo() {
109: return "Resin/" + com.caucho.Version.VERSION;
110: }
111:
112: /**
113: * Returns the servlet major version
114: */
115: public int getMajorVersion() {
116: return 2;
117: }
118:
119: /**
120: * Returns the servlet minor version
121: */
122: public int getMinorVersion() {
123: return 5;
124: }
125:
126: /**
127: * Sets an init param
128: */
129: protected void setInitParameter(String name, String value) {
130: _initParams.put(name, value);
131: }
132:
133: /**
134: * Gets the init params
135: */
136: public String getInitParameter(String name) {
137: return (String) _initParams.get(name);
138: }
139:
140: /**
141: * Gets the init params
142: */
143: public Enumeration getInitParameterNames() {
144: return Collections.enumeration(_initParams.keySet());
145: }
146:
147: /**
148: * Returns the named attribute.
149: */
150: public Object getAttribute(String name) {
151: synchronized (_attributes) {
152: Object value = _attributes.get(name);
153:
154: return value;
155: }
156: }
157:
158: /**
159: * Returns an enumeration of the attribute names.
160: */
161: public Enumeration getAttributeNames() {
162: synchronized (_attributes) {
163: return Collections.enumeration(_attributes.keySet());
164: }
165: }
166:
167: /**
168: * Sets an application attribute.
169: *
170: * @param name the name of the attribute
171: * @param value the value of the attribute
172: */
173: public void setAttribute(String name, Object value) {
174: Object oldValue;
175:
176: synchronized (_attributes) {
177: if (value != null)
178: oldValue = _attributes.put(name, value);
179: else
180: oldValue = _attributes.remove(name);
181: }
182:
183: // Call any listeners
184: if (_applicationAttributeListeners != null) {
185: ServletContextAttributeEvent event;
186:
187: if (oldValue != null)
188: event = new ServletContextAttributeEvent(this , name,
189: oldValue);
190: else
191: event = new ServletContextAttributeEvent(this , name,
192: value);
193:
194: for (int i = 0; i < _applicationAttributeListeners.size(); i++) {
195: ServletContextAttributeListener listener;
196:
197: Object objListener = _applicationAttributeListeners
198: .get(i);
199: listener = (ServletContextAttributeListener) objListener;
200:
201: try {
202: if (oldValue != null)
203: listener.attributeReplaced(event);
204: else
205: listener.attributeAdded(event);
206: } catch (Throwable e) {
207: log.log(Level.FINE, e.toString(), e);
208: }
209: }
210: }
211: }
212:
213: /**
214: * Removes an attribute from the servlet context.
215: *
216: * @param name the name of the attribute to remove.
217: */
218: public void removeAttribute(String name) {
219: Object oldValue;
220:
221: synchronized (_attributes) {
222: oldValue = _attributes.remove(name);
223: }
224:
225: // Call any listeners
226: if (_applicationAttributeListeners != null) {
227: ServletContextAttributeEvent event;
228:
229: event = new ServletContextAttributeEvent(this , name,
230: oldValue);
231:
232: for (int i = 0; i < _applicationAttributeListeners.size(); i++) {
233: ServletContextAttributeListener listener;
234:
235: Object objListener = _applicationAttributeListeners
236: .get(i);
237: listener = (ServletContextAttributeListener) objListener;
238:
239: try {
240: listener.attributeRemoved(event);
241: } catch (Throwable e) {
242: log.log(Level.FINE, e.toString(), e);
243: }
244: }
245: }
246: }
247:
248: /**
249: * Maps from a URI to a real path.
250: */
251: public String getRealPath(String uri) {
252: return getAppDir().lookup("./" + uri).getNativePath();
253: }
254:
255: /**
256: * Returns a resource for the given uri.
257: *
258: * <p>XXX: jdk 1.1.x doesn't appear to allow creation of private
259: * URL streams.
260: */
261: public URL getResource(String name)
262: throws java.net.MalformedURLException {
263: if (!name.startsWith("/"))
264: throw new java.net.MalformedURLException(name);
265:
266: String realPath = getRealPath(name);
267:
268: Path path = getAppDir().lookupNative(realPath);
269:
270: if (path.exists())
271: return new URL(path.getURL());
272:
273: return null;
274: }
275:
276: /**
277: * Returns the resource for a uripath as an input stream.
278: */
279: public InputStream getResourceAsStream(String uripath) {
280: Path path = getAppDir().lookupNative(getRealPath(uripath));
281:
282: try {
283: if (path.canRead())
284: return path.openRead();
285: else
286: return null;
287: } catch (IOException e) {
288: log.log(Level.FINEST, e.toString(), e);
289:
290: return null;
291: }
292: }
293:
294: /**
295: * Returns an enumeration of all the resources.
296: */
297: public Set getResourcePaths(String prefix) {
298: if (!prefix.endsWith("/"))
299: prefix = prefix + "/";
300:
301: Path path = getAppDir().lookup(getRealPath(prefix));
302:
303: HashSet<String> set = new HashSet<String>();
304:
305: try {
306: String[] list = path.list();
307:
308: for (int i = 0; i < list.length; i++) {
309: if (path.lookup(list[i]).isDirectory())
310: set.add(prefix + list[i] + '/');
311: else
312: set.add(prefix + list[i]);
313: }
314: } catch (IOException e) {
315: }
316:
317: return set;
318: }
319:
320: /**
321: * Returns the servlet context for the name.
322: */
323: public ServletContext getContext(String uri) {
324: return this ;
325: }
326:
327: /**
328: * Returns the mime type for the name.
329: */
330: public String getMimeType(String uri) {
331: return null;
332: }
333:
334: /**
335: * Returns the dispatcher.
336: */
337: public RequestDispatcher getRequestDispatcher(String uri) {
338: return null;
339: }
340:
341: /**
342: * Returns a dispatcher for the named servlet.
343: */
344: public RequestDispatcher getNamedDispatcher(String servletName) {
345: return null;
346: }
347:
348: /**
349: * Logging.
350: */
351:
352: /**
353: * Logs a message to the error file.
354: *
355: * @param msg the message to log
356: */
357: public final void log(String message) {
358: log(message, null);
359: }
360:
361: /**
362: * @deprecated
363: */
364: public final void log(Exception e, String msg) {
365: log(msg, e);
366: }
367:
368: /**
369: * Error logging
370: *
371: * @param message message to log
372: * @param e stack trace of the error
373: */
374: public void log(String message, Throwable e) {
375: if (e != null)
376: log.log(Level.WARNING, message, e);
377: else
378: log.info(message);
379: }
380:
381: //
382: // Deprecated methods
383: //
384:
385: public Servlet getServlet(String name) {
386: throw new UnsupportedOperationException(
387: "getServlet is deprecated");
388: }
389:
390: public Enumeration getServletNames() {
391: throw new UnsupportedOperationException(
392: "getServletNames is deprecated");
393: }
394:
395: public Enumeration getServlets() {
396: throw new UnsupportedOperationException(
397: "getServlets is deprecated");
398: }
399: }
|