Save This Page
Home » apache-tomcat-6.0.16-src » org.apache » jasper » servlet » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    * 
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   
   18   package org.apache.jasper.servlet;
   19   
   20   
   21   import java.io.File;
   22   import java.io.InputStream;
   23   import java.io.PrintWriter;
   24   import java.net.MalformedURLException;
   25   import java.net.URL;
   26   import java.util.Enumeration;
   27   import java.util.HashSet;
   28   import java.util.Hashtable;
   29   import java.util.Set;
   30   import java.util.Vector;
   31   
   32   import javax.servlet.RequestDispatcher;
   33   import javax.servlet.Servlet;
   34   import javax.servlet.ServletContext;
   35   import javax.servlet.ServletException;
   36   
   37   
   38   /**
   39    * Simple <code>ServletContext</code> implementation without
   40    * HTTP-specific methods.
   41    *
   42    * @author Peter Rossbach (pr@webapp.de)
   43    */
   44   
   45   public class JspCServletContext implements ServletContext {
   46   
   47   
   48       // ----------------------------------------------------- Instance Variables
   49   
   50   
   51       /**
   52        * Servlet context attributes.
   53        */
   54       protected Hashtable myAttributes;
   55   
   56   
   57       /**
   58        * The log writer we will write log messages to.
   59        */
   60       protected PrintWriter myLogWriter;
   61   
   62   
   63       /**
   64        * The base URL (document root) for this context.
   65        */
   66       protected URL myResourceBaseURL;
   67   
   68   
   69       // ----------------------------------------------------------- Constructors
   70   
   71   
   72       /**
   73        * Create a new instance of this ServletContext implementation.
   74        *
   75        * @param aLogWriter PrintWriter which is used for <code>log()</code> calls
   76        * @param aResourceBaseURL Resource base URL
   77        */
   78       public JspCServletContext(PrintWriter aLogWriter, URL aResourceBaseURL) {
   79   
   80           myAttributes = new Hashtable();
   81           myLogWriter = aLogWriter;
   82           myResourceBaseURL = aResourceBaseURL;
   83   
   84       }
   85   
   86   
   87       // --------------------------------------------------------- Public Methods
   88   
   89   
   90       /**
   91        * Return the specified context attribute, if any.
   92        *
   93        * @param name Name of the requested attribute
   94        */
   95       public Object getAttribute(String name) {
   96   
   97           return (myAttributes.get(name));
   98   
   99       }
  100   
  101   
  102       /**
  103        * Return an enumeration of context attribute names.
  104        */
  105       public Enumeration getAttributeNames() {
  106   
  107           return (myAttributes.keys());
  108   
  109       }
  110   
  111   
  112       /**
  113        * Return the servlet context for the specified path.
  114        *
  115        * @param uripath Server-relative path starting with '/'
  116        */
  117       public ServletContext getContext(String uripath) {
  118   
  119           return (null);
  120   
  121       }
  122   
  123   
  124       /**
  125        * Return the context path.
  126        */
  127       public String getContextPath() {
  128   
  129           return (null);
  130   
  131       }
  132   
  133   
  134       /**
  135        * Return the specified context initialization parameter.
  136        *
  137        * @param name Name of the requested parameter
  138        */
  139       public String getInitParameter(String name) {
  140   
  141           return (null);
  142   
  143       }
  144   
  145   
  146       /**
  147        * Return an enumeration of the names of context initialization
  148        * parameters.
  149        */
  150       public Enumeration getInitParameterNames() {
  151   
  152           return (new Vector().elements());
  153   
  154       }
  155   
  156   
  157       /**
  158        * Return the Servlet API major version number.
  159        */
  160       public int getMajorVersion() {
  161   
  162           return (2);
  163   
  164       }
  165   
  166   
  167       /**
  168        * Return the MIME type for the specified filename.
  169        *
  170        * @param file Filename whose MIME type is requested
  171        */
  172       public String getMimeType(String file) {
  173   
  174           return (null);
  175   
  176       }
  177   
  178   
  179       /**
  180        * Return the Servlet API minor version number.
  181        */
  182       public int getMinorVersion() {
  183   
  184           return (3);
  185   
  186       }
  187   
  188   
  189       /**
  190        * Return a request dispatcher for the specified servlet name.
  191        *
  192        * @param name Name of the requested servlet
  193        */
  194       public RequestDispatcher getNamedDispatcher(String name) {
  195   
  196           return (null);
  197   
  198       }
  199   
  200   
  201       /**
  202        * Return the real path for the specified context-relative
  203        * virtual path.
  204        *
  205        * @param path The context-relative virtual path to resolve
  206        */
  207       public String getRealPath(String path) {
  208   
  209           if (!myResourceBaseURL.getProtocol().equals("file"))
  210               return (null);
  211           if (!path.startsWith("/"))
  212               return (null);
  213           try {
  214               return
  215                   (getResource(path).getFile().replace('/', File.separatorChar));
  216           } catch (Throwable t) {
  217               return (null);
  218           }
  219   
  220       }
  221               
  222               
  223       /**
  224        * Return a request dispatcher for the specified context-relative path.
  225        *
  226        * @param path Context-relative path for which to acquire a dispatcher
  227        */
  228       public RequestDispatcher getRequestDispatcher(String path) {
  229   
  230           return (null);
  231   
  232       }
  233   
  234   
  235       /**
  236        * Return a URL object of a resource that is mapped to the
  237        * specified context-relative path.
  238        *
  239        * @param path Context-relative path of the desired resource
  240        *
  241        * @exception MalformedURLException if the resource path is
  242        *  not properly formed
  243        */
  244       public URL getResource(String path) throws MalformedURLException {
  245   
  246           if (!path.startsWith("/"))
  247               throw new MalformedURLException("Path '" + path +
  248                                               "' does not start with '/'");
  249           URL url = new URL(myResourceBaseURL, path.substring(1));
  250           InputStream is = null;
  251           try {
  252               is = url.openStream();
  253           } catch (Throwable t) {
  254               url = null;
  255           } finally {
  256               if (is != null) {
  257                   try {
  258                       is.close();
  259                   } catch (Throwable t2) {
  260                       // Ignore
  261                   }
  262               }
  263           }
  264           return url;
  265   
  266       }
  267   
  268   
  269       /**
  270        * Return an InputStream allowing access to the resource at the
  271        * specified context-relative path.
  272        *
  273        * @param path Context-relative path of the desired resource
  274        */
  275       public InputStream getResourceAsStream(String path) {
  276   
  277           try {
  278               return (getResource(path).openStream());
  279           } catch (Throwable t) {
  280               return (null);
  281           }
  282   
  283       }
  284   
  285   
  286       /**
  287        * Return the set of resource paths for the "directory" at the
  288        * specified context path.
  289        *
  290        * @param path Context-relative base path
  291        */
  292       public Set getResourcePaths(String path) {
  293   
  294           Set thePaths = new HashSet();
  295           if (!path.endsWith("/"))
  296               path += "/";
  297           String basePath = getRealPath(path);
  298           if (basePath == null)
  299               return (thePaths);
  300           File theBaseDir = new File(basePath);
  301           if (!theBaseDir.exists() || !theBaseDir.isDirectory())
  302               return (thePaths);
  303           String theFiles[] = theBaseDir.list();
  304           for (int i = 0; i < theFiles.length; i++) {
  305               File testFile = new File(basePath + File.separator + theFiles[i]);
  306               if (testFile.isFile())
  307                   thePaths.add(path + theFiles[i]);
  308               else if (testFile.isDirectory())
  309                   thePaths.add(path + theFiles[i] + "/");
  310           }
  311           return (thePaths);
  312   
  313       }
  314   
  315   
  316       /**
  317        * Return descriptive information about this server.
  318        */
  319       public String getServerInfo() {
  320   
  321           return ("JspCServletContext/1.0");
  322   
  323       }
  324   
  325   
  326       /**
  327        * Return a null reference for the specified servlet name.
  328        *
  329        * @param name Name of the requested servlet
  330        *
  331        * @deprecated This method has been deprecated with no replacement
  332        */
  333       public Servlet getServlet(String name) throws ServletException {
  334   
  335           return (null);
  336   
  337       }
  338   
  339   
  340       /**
  341        * Return the name of this servlet context.
  342        */
  343       public String getServletContextName() {
  344   
  345           return (getServerInfo());
  346   
  347       }
  348   
  349   
  350       /**
  351        * Return an empty enumeration of servlet names.
  352        *
  353        * @deprecated This method has been deprecated with no replacement
  354        */
  355       public Enumeration getServletNames() {
  356   
  357           return (new Vector().elements());
  358   
  359       }
  360   
  361   
  362       /**
  363        * Return an empty enumeration of servlets.
  364        *
  365        * @deprecated This method has been deprecated with no replacement
  366        */
  367       public Enumeration getServlets() {
  368   
  369           return (new Vector().elements());
  370   
  371       }
  372   
  373   
  374       /**
  375        * Log the specified message.
  376        *
  377        * @param message The message to be logged
  378        */
  379       public void log(String message) {
  380   
  381           myLogWriter.println(message);
  382   
  383       }
  384   
  385   
  386       /**
  387        * Log the specified message and exception.
  388        *
  389        * @param exception The exception to be logged
  390        * @param message The message to be logged
  391        *
  392        * @deprecated Use log(String,Throwable) instead
  393        */
  394       public void log(Exception exception, String message) {
  395   
  396           log(message, exception);
  397   
  398       }
  399   
  400   
  401       /**
  402        * Log the specified message and exception.
  403        *
  404        * @param message The message to be logged
  405        * @param exception The exception to be logged
  406        */
  407       public void log(String message, Throwable exception) {
  408   
  409           myLogWriter.println(message);
  410           exception.printStackTrace(myLogWriter);
  411   
  412       }
  413   
  414   
  415       /**
  416        * Remove the specified context attribute.
  417        *
  418        * @param name Name of the attribute to remove
  419        */
  420       public void removeAttribute(String name) {
  421   
  422           myAttributes.remove(name);
  423   
  424       }
  425   
  426   
  427       /**
  428        * Set or replace the specified context attribute.
  429        *
  430        * @param name Name of the context attribute to set
  431        * @param value Corresponding attribute value
  432        */
  433       public void setAttribute(String name, Object value) {
  434   
  435           myAttributes.put(name, value);
  436   
  437       }
  438   
  439   
  440   
  441   }

Save This Page
Home » apache-tomcat-6.0.16-src » org.apache » jasper » servlet » [javadoc | source]