KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > webapp > ServletContextImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.webapp;
31
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34 import com.caucho.vfs.Path;
35 import com.caucho.vfs.Vfs;
36
37 import javax.servlet.RequestDispatcher JavaDoc;
38 import javax.servlet.Servlet JavaDoc;
39 import javax.servlet.ServletContext JavaDoc;
40 import javax.servlet.ServletContextAttributeEvent JavaDoc;
41 import javax.servlet.ServletContextAttributeListener JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.InputStream JavaDoc;
44 import java.net.URL JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Collections JavaDoc;
47 import java.util.Enumeration JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.HashSet JavaDoc;
50 import java.util.Set JavaDoc;
51 import java.util.logging.Level JavaDoc;
52 import java.util.logging.Logger JavaDoc;
53
54 /**
55  * Bare-bones servlet context implementation.
56  */

57 public class ServletContextImpl implements ServletContext JavaDoc {
58   static final Logger JavaDoc log = Log.open(ServletContextImpl.class);
59   static final L10N L = new L10N(ServletContextImpl.class);
60
61   private String JavaDoc _name;
62   
63   private HashMap JavaDoc<String JavaDoc,Object JavaDoc> _attributes = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
64
65   private ArrayList JavaDoc<ServletContextAttributeListener JavaDoc>
66     _applicationAttributeListeners;
67
68   private HashMap JavaDoc<String JavaDoc,String JavaDoc> _initParams = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
69
70   public Path getAppDir()
71   {
72     return Vfs.lookup();
73   }
74
75   /**
76    * Sets the servlet context name
77    */

78   public void setDisplayName(String JavaDoc name)
79   {
80     _name = name;
81   }
82
83   /**
84    * Gets the servlet context name
85    */

86   public String JavaDoc getServletContextName()
87   {
88     return _name;
89   }
90
91   /**
92    * Gets the servlet context name
93    */

94   public String JavaDoc getContextPath()
95   {
96     return _name;
97   }
98
99   /**
100    * Adds the listener.
101    */

102   protected void addAttributeListener(ServletContextAttributeListener JavaDoc listener)
103   {
104     if (_applicationAttributeListeners == null)
105       _applicationAttributeListeners = new ArrayList JavaDoc<ServletContextAttributeListener JavaDoc>();
106     
107     _applicationAttributeListeners.add(listener);
108   }
109
110   /**
111    * Returns the server information
112    */

113   public String JavaDoc getServerInfo()
114   {
115     return "Resin/" + com.caucho.Version.VERSION;
116   }
117
118   /**
119    * Returns the servlet major version
120    */

121   public int getMajorVersion()
122   {
123     return 2;
124   }
125
126   /**
127    * Returns the servlet minor version
128    */

129   public int getMinorVersion()
130   {
131     return 5;
132   }
133
134   /**
135    * Sets an init param
136    */

137   protected void setInitParameter(String JavaDoc name, String JavaDoc value)
138   {
139     _initParams.put(name, value);
140   }
141
142   /**
143    * Gets the init params
144    */

145   public String JavaDoc getInitParameter(String JavaDoc name)
146   {
147     return (String JavaDoc) _initParams.get(name);
148   }
149
150   /**
151    * Gets the init params
152    */

153   public Enumeration JavaDoc getInitParameterNames()
154   {
155     return Collections.enumeration(_initParams.keySet());
156   }
157
158   /**
159    * Returns the named attribute.
160    */

161   public Object JavaDoc getAttribute(String JavaDoc name)
162   {
163     synchronized (_attributes) {
164       Object JavaDoc value = _attributes.get(name);
165
166       return value;
167     }
168   }
169
170   /**
171    * Returns an enumeration of the attribute names.
172    */

173   public Enumeration JavaDoc getAttributeNames()
174   {
175     synchronized (_attributes) {
176       return Collections.enumeration(_attributes.keySet());
177     }
178   }
179
180   /**
181    * Sets an application attribute.
182    *
183    * @param name the name of the attribute
184    * @param value the value of the attribute
185    */

186   public void setAttribute(String JavaDoc name, Object JavaDoc value)
187   {
188     Object JavaDoc oldValue;
189
190     synchronized (_attributes) {
191       if (value != null)
192         oldValue = _attributes.put(name, value);
193       else
194         oldValue = _attributes.remove(name);
195     }
196     
197     // Call any listeners
198
if (_applicationAttributeListeners != null) {
199       ServletContextAttributeEvent JavaDoc event;
200
201       if (oldValue != null)
202         event = new ServletContextAttributeEvent JavaDoc(this, name, oldValue);
203       else
204         event = new ServletContextAttributeEvent JavaDoc(this, name, value);
205         
206       for (int i = 0; i < _applicationAttributeListeners.size(); i++) {
207         ServletContextAttributeListener JavaDoc listener;
208
209         Object JavaDoc objListener = _applicationAttributeListeners.get(i);
210         listener = (ServletContextAttributeListener JavaDoc) objListener;
211
212         try {
213           if (oldValue != null)
214             listener.attributeReplaced(event);
215           else
216             listener.attributeAdded(event);
217         } catch (Throwable JavaDoc e) {
218           log.log(Level.FINE, e.toString(), e);
219         }
220       }
221     }
222   }
223
224   /**
225    * Removes an attribute from the servlet context.
226    *
227    * @param name the name of the attribute to remove.
228    */

229   public void removeAttribute(String JavaDoc name)
230   {
231     Object JavaDoc oldValue;
232     
233     synchronized (_attributes) {
234       oldValue = _attributes.remove(name);
235     }
236
237     // Call any listeners
238
if (_applicationAttributeListeners != null) {
239       ServletContextAttributeEvent JavaDoc event;
240
241       event = new ServletContextAttributeEvent JavaDoc(this, name, oldValue);
242         
243       for (int i = 0; i < _applicationAttributeListeners.size(); i++) {
244         ServletContextAttributeListener JavaDoc listener;
245
246         Object JavaDoc objListener = _applicationAttributeListeners.get(i);
247         listener = (ServletContextAttributeListener JavaDoc) objListener;
248
249         try {
250           listener.attributeRemoved(event);
251         } catch (Throwable JavaDoc e) {
252           log.log(Level.FINE, e.toString(), e);
253         }
254       }
255     }
256   }
257
258   /**
259    * Maps from a URI to a real path.
260    */

261   public String JavaDoc getRealPath(String JavaDoc uri)
262   {
263     return getAppDir().lookup("./" + uri).getNativePath();
264   }
265
266   /**
267    * Returns a resource for the given uri.
268    *
269    * <p>XXX: jdk 1.1.x doesn't appear to allow creation of private
270    * URL streams.
271    */

272   public URL JavaDoc getResource(String JavaDoc name)
273     throws java.net.MalformedURLException JavaDoc
274   {
275     if (! name.startsWith("/"))
276       throw new java.net.MalformedURLException JavaDoc(name);
277     
278     String JavaDoc realPath = getRealPath(name);
279
280     Path path = getAppDir().lookupNative(realPath);
281
282     if (path.exists())
283       return new URL JavaDoc(path.getURL());
284
285     return null;
286   }
287
288   /**
289    * Returns the resource for a uripath as an input stream.
290    */

291   public InputStream JavaDoc getResourceAsStream(String JavaDoc uripath)
292   {
293     Path path = getAppDir().lookupNative(getRealPath(uripath));
294
295     try {
296       if (path.canRead())
297         return path.openRead();
298       else
299         return null;
300     } catch (IOException JavaDoc e) {
301       log.log(Level.FINEST, e.toString(), e);
302
303       return null;
304     }
305   }
306
307   /**
308     * Returns an enumeration of all the resources.
309     */

310   public Set JavaDoc getResourcePaths(String JavaDoc prefix)
311   {
312     if (! prefix.endsWith("/"))
313       prefix = prefix + "/";
314
315     Path path = getAppDir().lookup(getRealPath(prefix));
316
317     HashSet JavaDoc<String JavaDoc> set = new HashSet JavaDoc<String JavaDoc>();
318
319     try {
320       String JavaDoc []list = path.list();
321
322       for (int i = 0; i < list.length; i++) {
323         if (path.lookup(list[i]).isDirectory())
324           set.add(prefix + list[i] + '/');
325         else
326           set.add(prefix + list[i]);
327       }
328     } catch (IOException JavaDoc e) {
329     }
330      
331     return set;
332   }
333
334   /**
335    * Returns the servlet context for the name.
336    */

337   public ServletContext JavaDoc getContext(String JavaDoc uri)
338   {
339     return this;
340   }
341
342   /**
343    * Returns the mime type for the name.
344    */

345   public String JavaDoc getMimeType(String JavaDoc uri)
346   {
347     return null;
348   }
349
350   /**
351    * Returns the dispatcher.
352    */

353   public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc uri)
354   {
355     return null;
356   }
357
358   /**
359    * Returns a dispatcher for the named servlet.
360    */

361   public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc servletName)
362   {
363     return null;
364   }
365
366   /**
367    * Logging.
368    */

369
370   /**
371    * Logs a message to the error file.
372    *
373    * @param msg the message to log
374    */

375   public final void log(String JavaDoc message)
376   {
377     log(message, null);
378   }
379
380   /**
381    * @deprecated
382    */

383   public final void log(Exception JavaDoc e, String JavaDoc msg)
384   {
385     log(msg, e);
386   }
387
388   /**
389    * Error logging
390    *
391    * @param message message to log
392    * @param e stack trace of the error
393    */

394   public void log(String JavaDoc message, Throwable JavaDoc e)
395   {
396     if (e != null)
397       log.log(Level.WARNING, message, e);
398     else
399       log.info(message);
400   }
401
402   //
403
// Deprecated methods
404
//
405

406   public Servlet JavaDoc getServlet(String JavaDoc name)
407   {
408     throw new UnsupportedOperationException JavaDoc("getServlet is deprecated");
409   }
410
411   public Enumeration JavaDoc getServletNames()
412   {
413     throw new UnsupportedOperationException JavaDoc("getServletNames is deprecated");
414   }
415
416   public Enumeration JavaDoc getServlets()
417   {
418     throw new UnsupportedOperationException JavaDoc("getServlets is deprecated");
419   }
420 }
421
Popular Tags