001: /*
002: License $Id: JoServletContext.java,v 1.11 2005/03/15 20:08:14 hendriks73 Exp $
003:
004: Copyright (c) 2001-2005 tagtraum industries.
005:
006: LGPL
007: ====
008:
009: jo! is free software; you can redistribute it and/or
010: modify it under the terms of the GNU Lesser General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: jo! 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. See the GNU
017: Lesser General Public License for more details.
018:
019: You should have received a copy of the GNU Lesser General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022:
023: For LGPL see <http://www.fsf.org/copyleft/lesser.txt>
024:
025:
026: Sun license
027: ===========
028:
029: This release contains software by Sun Microsystems. Therefore
030: the following conditions have to be met, too. They apply to the
031: files
032:
033: - lib/mail.jar
034: - lib/activation.jar
035: - lib/jsse.jar
036: - lib/jcert.jar
037: - lib/jaxp.jar
038: - lib/crimson.jar
039: - lib/servlet.jar
040: - lib/jnet.jar
041: - lib/jaas.jar
042: - lib/jaasmod.jar
043:
044: contained in this release.
045:
046: a. Licensee may not modify the Java Platform
047: Interface (JPI, identified as classes contained within the javax
048: package or any subpackages of the javax package), by creating additional
049: classes within the JPI or otherwise causing the addition to or modification
050: of the classes in the JPI. In the event that Licensee creates any
051: Java-related API and distribute such API to others for applet or
052: application development, you must promptly publish broadly, an accurate
053: specification for such API for free use by all developers of Java-based
054: software.
055:
056: b. Software is confidential copyrighted information of Sun and
057: title to all copies is retained by Sun and/or its licensors. Licensee
058: shall not modify, decompile, disassemble, decrypt, extract, or otherwise
059: reverse engineer Software. Software may not be leased, assigned, or
060: sublicensed, in whole or in part. Software is not designed or intended
061: for use in on-line control of aircraft, air traffic, aircraft navigation
062: or aircraft communications; or in the design, construction, operation or
063: maintenance of any nuclear facility. Licensee warrants that it will not
064: use or redistribute the Software for such purposes.
065:
066: c. Software is provided "AS IS," without a warranty
067: of any kind. ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES,
068: INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
069: PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
070:
071: d. This License is effective until terminated. Licensee may
072: terminate this License at any time by destroying all copies of Software.
073: This License will terminate immediately without notice from Sun if Licensee
074: fails to comply with any provision of this License. Upon such termination,
075: Licensee must destroy all copies of Software.
076:
077: e. Software, including technical data, is subject to U.S.
078: export control laws, including the U.S. Export Administration Act and its
079: associated regulations, and may be subject to export or import regulations
080: in other countries. Licensee agrees to comply strictly with all such
081: regulations and acknowledges that it has the responsibility to obtain
082: licenses to export, re-export, or import Software. Software may not be
083: downloaded, or otherwise exported or re-exported (i) into, or to a national
084: or resident of, Cuba, Iraq, Iran, North Korea, Libya, Sudan, Syria or any
085: country to which the U.S. has embargoed goods; or (ii) to anyone on the
086: U.S. Treasury Department's list of Specially Designated Nations or the U.S.
087: Commerce Department's Table of Denial Orders.
088:
089:
090: Feedback
091: ========
092:
093: We encourage your feedback and suggestions and want to use your feedback to
094: improve the Software. Send all such feedback to:
095: <feedback@tagtraum.com>
096:
097: For more information on tagtraum industries and jo!
098: please see <http://www.tagtraum.com/>.
099:
100:
101: */
102: package com.tagtraum.jo;
103:
104: import com.tagtraum.framework.http.URI;
105: import com.tagtraum.framework.log.*;
106: import com.tagtraum.framework.recycler.Recycler;
107: import com.tagtraum.framework.util.FactoryException;
108: import com.tagtraum.jo.servlets.JoFileServlet;
109:
110: import javax.servlet.RequestDispatcher;
111: import javax.servlet.Servlet;
112: import javax.servlet.ServletContext;
113: import javax.servlet.ServletException;
114: import java.io.InputStream;
115: import java.io.File;
116: import java.net.MalformedURLException;
117: import java.net.URL;
118: import java.util.*;
119:
120: /**
121: * Context (webapp) of a group of servlets.
122: *
123: * @author <a href="mailto:hs@tagtraum.com">Hendrik Schreiber</a>
124: * @version 1.1beta1 $Id: JoServletContext.java,v 1.11 2005/03/15 20:08:14 hendriks73 Exp $
125: * @see I_JoServletContextPeer
126: */
127: public class JoServletContext implements I_JoServletContext, C_Jo {
128:
129: /**
130: * Source-Version
131: */
132: public static String vcid = "$Id: JoServletContext.java,v 1.11 2005/03/15 20:08:14 hendriks73 Exp $";
133: private static ResourceBundle localStrings = ResourceBundle
134: .getBundle("com.tagtraum.jo.localStrings");
135:
136: /**
137: * This context's service.
138: */
139: private I_JoServletService service;
140:
141: /**
142: * This context's host.
143: */
144: private I_JoHost host;
145:
146: /**
147: * This context's peer.
148: */
149: private I_JoServletContextPeer peer;
150:
151: /**
152: * Symbolic classname for the RequestDispatcher.
153: */
154: private String requestDispatcherClassname = C_FactoryKey_RequestDispatcher;
155:
156: /**
157: * This context's factory.
158: */
159: private Recycler factory;
160:
161: /**
162: * Log prefix.
163: */
164: private String logPrefix;
165:
166: /**
167: * Log.
168: */
169: private Log log;
170:
171: /**
172: * Initializes this Context. Sets the peer it is associated with.
173: *
174: * @param aPeer this context's peer.
175: * @see I_JoServletContextPeer
176: */
177: public void init(I_JoServletContextPeer aPeer) {
178: this .peer = aPeer;
179: this .host = peer.getHost();
180: this .factory = Recycler.getNamedRecycler(host.getName());
181: this .service = host.getService();
182: this .logPrefix = "[" + peer.getName() + "]: ";
183: this .log = Log.getLog(host.getName());
184: }
185:
186: /**
187: * Returns a <code>String</code> containing the value of the named
188: * context-wide initialization parameter, or <code>null</code> if the
189: * parameter does not exist.
190: *
191: * <p>This method can make available configuration information useful
192: * to an entire "web application". For example, it can provide a
193: * webmaster's email address or the name of a system that holds
194: * critical data.
195: *
196: * @param name a <code>String</code> containing the name of the
197: * parameter whose value is requested
198: *
199: * @return a <code>String</code> containing at least the
200: * servlet container name and version number
201: *
202: * @see javax.servlet.ServletConfig#getInitParameter
203: */
204: public String getInitParameter(String name) {
205: return peer.getInitParameter(name);
206: }
207:
208: /**
209: * Returns the names of the context's initialization parameters as an
210: * <code>Enumeration</code> of <code>String</code> objects, or an
211: * empty <code>Enumeration</code> if the context has no initialization
212: * parameters.
213: *
214: * @return an <code>Enumeration</code> of <code>String</code>
215: * objects containing the names of the context's
216: * initialization parameters
217: *
218: * @see javax.servlet.ServletConfig#getInitParameter
219: */
220: public Enumeration getInitParameterNames() {
221: return peer.getInitParameterNames();
222: }
223:
224: /**
225: * Returns the name of this context.
226: *
227: * @return Name
228: */
229: public String getName() {
230: return peer.getName();
231: }
232:
233: /**
234: * Returns a directory-like listing of all the paths to resources within the web
235: * application whose longest sub-path matches the supplied path argument.
236: * Paths indicating subdirectory paths end with a '/'. The returned paths are all
237: * relative to the root of the web application and have a leading '/'. For example,
238: * for a web application containing<br>
239: * <xmp>
240: * /welcome.html
241: * /catalog/index.html
242: * /catalog/products.html
243: * /catalog/offers/books.html
244: * /catalog/offers/music.html
245: * /customer/login.jsp
246: * /WEB-INF/web.xml
247: * /WEB-INF/classes/com.acme.OrderServlet.class,
248: * </xmp>
249: * getResourcePaths("/") returns {"/welcome.html", "/catalog/", "/customer/",
250: * "/WEB-INF/"}<br>
251: * getResourcePaths("/catalog/") returns {"/catalog/index.html", "/catalog/
252: * products.html", "/catalog/offers/"}.
253: */
254: public Set getResourcePaths(String s) {
255: File path = new File(getRealPath(s));
256: if (!path.isDirectory())
257: return null;
258: Set set = new HashSet();
259: File[] files = path.listFiles();
260: if (files == null || files.length == 0)
261: return null;
262: final int beginIndex = getRealPath("/").length() - 1;
263: for (int i = 0; i < files.length; i++) {
264: File file = files[i];
265: String shortenedFile = file.toString()
266: .substring(beginIndex);
267: shortenedFile = shortenedFile.replace(File.separatorChar,
268: '/');
269: if (file.isDirectory()) {
270: set.add(shortenedFile + "/");
271: } else if (file.isFile()) {
272: set.add(shortenedFile);
273: }
274: }
275: return Collections.unmodifiableSet(set);
276: }
277:
278: /**
279: * Returns the name of this web application correponding to this ServletContext
280: * as specified in the deployment descriptor for this web application by the display-
281: * name element.
282: */
283: public String getServletContextName() {
284: // having to cast is awkward (hs)
285: return ((I_JoWebAppElement) peer).getDisplayName();
286: }
287:
288: /**
289: * Returns a matching <code>RequestDispatcher</code> or
290: * <code>null</code> if none exists.
291: *
292: * @param aName Name
293: * @return a matching RequestDispatcher
294: * @see I_JoRequestDispatcher
295: * @see JoRequestDispatcher
296: */
297: public RequestDispatcher getNamedDispatcher(String aName) {
298: if (aName == null) {
299: throw new NullPointerException(localStrings
300: .getString("name_cannot_be_null"));
301: }
302: try {
303: I_JoServletModel theModel = peer
304: .getNamedServletModel(aName);
305: if (theModel == null) {
306: return null;
307: }
308: I_JoRequestDispatcher aDispatcher = (I_JoRequestDispatcher) factory
309: .get(requestDispatcherClassname);
310: aDispatcher.init(aName, peer);
311: return aDispatcher;
312: } catch (FactoryException fe) {
313: if (Log.isLog(service.getName())) {
314: Log.getLog(service.getName()).log(fe);
315: }
316: } catch (ServletException se) {
317: // ignore
318: }
319: return null;
320: }
321:
322: /**
323: * Logs a message.
324: *
325: * @param msg message
326: */
327: public void log(String msg) {
328: log.log(logPrefix + msg, C_Log.MODULE);
329: }
330:
331: /**
332: * Writes a <code>Throwable</code> and a mesage to the log.
333: *
334: * @param t Throwable
335: * @param msg message
336: */
337: public void log(String msg, Throwable t) {
338: log.log(new LogEvent(logPrefix + msg, t, this ), C_Log.MODULE);
339: }
340:
341: /**
342: * Logs an exception and a message.
343: *
344: * @param e Exception
345: * @param msg Message
346: * @deprecated Please use {@link #log(String, Throwable)}.
347: */
348: public void log(Exception e, String msg) {
349: log.log(new LogEvent(logPrefix + msg, e, this ), C_Log.MODULE);
350: }
351:
352: /**
353: * Returns a server info string.
354: *
355: * @return Server-Info-String
356: * @see I_JoHost#getServerInfo()
357: */
358: public String getServerInfo() {
359: return host.getServerInfo();
360: }
361:
362: /**
363: * Returns an attribute of this context.
364: *
365: * @param name name
366: * @return value
367: */
368: public Object getAttribute(String name) {
369: return peer.getAttribute(name);
370: }
371:
372: /**
373: * Sets an attribute.
374: *
375: * @param aName name
376: * @param aValue value
377: */
378: public void setAttribute(String aName, Object aValue) {
379: peer.setAttribute(aName, aValue);
380: }
381:
382: /**
383: * Removes an attribute.
384: *
385: * @param aName Name
386: */
387: public void removeAttribute(String aName) {
388: peer.removeAttribute(aName);
389: }
390:
391: /**
392: * Returns an enumeration of the attribute names.
393: *
394: * @return enumeration.
395: */
396: public Enumeration getAttributeNames() {
397: return peer.getAttributeNames();
398: }
399:
400: /**
401: * Returns the minor version if the implements servlet api.
402: * E.g. for 2.1, 1 is returned.
403: *
404: * return minor version
405: */
406: public int getMinorVersion() {
407: return 2;
408: }
409:
410: /**
411: * Returns the major version if the implements servlet api.
412: * E.g. for 2.1, 2 is returned.
413: *
414: * return major version
415: */
416: public int getMajorVersion() {
417: return 2;
418: }
419:
420: /**
421: * Conveniencemethod that returns an <code>Inputstream</code> for a resource.
422: *
423: * @param aURI URI relative to the root of this context
424: * @param aLocale localized version of this resource
425: * @return the resource's InputStream
426: */
427: public InputStream getResourceAsStream(String aURI, Locale aLocale) {
428: try {
429: URL aURL = getResource(aURI, aLocale);
430: if (aURL == null) {
431: return null;
432: }
433: return aURL.openStream();
434: } catch (Exception e) {
435: // ignore
436: }
437: return null;
438: }
439:
440: /**
441: * Returns a corresponding servletcontext.
442: *
443: * @return ServletContext for an URI.
444: */
445: public ServletContext getContext(String aURI) {
446: return host.getServletContextPeer(aURI).getServletContext();
447: }
448:
449: /**
450: * Conveniencemethod that returns an <code>Inputstream</code> to a
451: * URI.
452: *
453: * @param aURI URI relative to the root of this context
454: * @return the resource's InputStream
455: */
456: public InputStream getResourceAsStream(String aURI) {
457: try {
458: URL aURL = getResource(aURI);
459: if (aURL == null) {
460: return null;
461: }
462: // return the cached version (rik)
463: return service.getFileCache().getFileProxy(aURL)
464: .getInputStream();
465: } catch (Throwable t) {
466: // ignore
467: }
468: return null;
469: }
470:
471: /**
472: * Translates a virtual path into a real one.
473: *
474: * @param path virtual path
475: * @return real path
476: */
477: public String getRealPath(String path) {
478: return peer.getRealPath(path);
479: }
480:
481: /**
482: * Returns the mime type for a file.
483: *
484: * @param file file name.
485: */
486: public String getMimeType(String file) {
487: return peer.getMimeType(file);
488: }
489:
490: /**
491: * Returns the URL-object of a resource.
492: *
493: * @return a resource's URL
494: */
495: public URL getResource(String aURI) throws MalformedURLException {
496: return peer.getResource(aURI);
497: }
498:
499: /**
500: * Returns the URL of the server's resource.
501: *
502: * @param aLocale localized version of this resource
503: * @return URL of a resource
504: */
505: public URL getResource(String aURI, Locale aLocale)
506: throws MalformedURLException {
507: return peer.getResource(aURI, aLocale);
508: }
509:
510: /**
511: * Returns a RequestDispatcher or null.
512: *
513: * @param originalURI
514: * @return RequestDispatcher or null
515: */
516: public RequestDispatcher getRequestDispatcher(String originalURI) {
517: if (originalURI == null) {
518: throw new NullPointerException(localStrings
519: .getString("uri_cannot_be_null"));
520: }
521: URI theURI = new URI(originalURI);
522: String aURI = peer.getContextPath() + theURI.getPath();
523: try {
524: I_JoServletModel theModel = peer.getServletModel(aURI);
525: if (theModel == null) {
526: return null;
527: }
528: if (theModel.getServletClassName().equals(
529: JoFileServlet.class.getName())) {
530: try {
531: if (getResource(originalURI) == null) {
532: return null;
533: }
534: } catch (MalformedURLException mfue) {
535: return null;
536: }
537: }
538:
539: theURI.setPath(aURI);
540: I_JoRequestDispatcher aDispatcher = (I_JoRequestDispatcher) factory
541: .get(C_FactoryKey_RequestDispatcher);
542:
543: aDispatcher.init(theURI, peer);
544: return aDispatcher;
545: } catch (FactoryException fe) {
546: if (Log.isLog(service.getName())) {
547: Log.getLog(service.getName()).log(fe);
548: }
549: } catch (ServletException se) {
550: // ignore
551: }
552: return null;
553: }
554:
555: // Legacies
556:
557: /**
558: * Legacy code.
559: *
560: * @deprecated
561: * @param name the name of the desired servlet
562: * @return <code>null</code>
563: */
564: public Servlet getServlet(String name) throws ServletException {
565: return null;
566: }
567:
568: /**
569: * Legacy code.
570: *
571: * @deprecated
572: * @return empty <code>Enumeration</code>
573: */
574: public Enumeration getServlets() {
575: return new Vector().elements();
576: }
577:
578: /**
579: * Legacy code.
580: *
581: * @deprecated
582: * @return empty <code>Enumeration</code>
583: */
584: public Enumeration getServletNames() {
585: return new Vector().elements();
586: }
587: }
|