You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by cr...@apache.org on 2004/10/18 03:48:52 UTC

cvs commit: jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet ChainProcessor.java PathInfoMapper.java RequestParameterMapper.java ServletPathMapper.java

craigmcc    2004/10/17 18:48:52

  Modified:    chain/src/java/org/apache/commons/chain/web
                        ChainListener.java ChainServlet.java
               chain/src/java/org/apache/commons/chain/web/servlet
                        ChainProcessor.java PathInfoMapper.java
                        RequestParameterMapper.java ServletPathMapper.java
  Log:
  Update ChainListener and ChainServlet (and the subordinate servlet related
  processing commands) to make the presence of the attribute init parameter
  (org.apache.commons.chain.CONFIG_ATTR) select the previous behavior
  where a Catalog is registered as an application scope parameter.  Absence
  of this init parameter will trigger the new-style behavior where config
  files are assumed to contain <catalog> elements, which will cause the catalogs
  to be registered with the CatalogFactory for this application.
  
  Revision  Changes    Path
  1.6       +98 -26    jakarta-commons/chain/src/java/org/apache/commons/chain/web/ChainListener.java
  
  Index: ChainListener.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/web/ChainListener.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ChainListener.java	25 Feb 2004 00:01:06 -0000	1.5
  +++ ChainListener.java	18 Oct 2004 01:48:51 -0000	1.6
  @@ -25,6 +25,7 @@
   import javax.servlet.ServletContextEvent;
   import javax.servlet.ServletContextListener;
   import org.apache.commons.chain.Catalog;
  +import org.apache.commons.chain.CatalogFactory;
   import org.apache.commons.chain.config.ConfigParser;
   import org.apache.commons.chain.impl.CatalogBase;
   import org.apache.commons.digester.RuleSet;
  @@ -49,8 +50,13 @@
    *     will be loaded.</li>
    * <li><strong>org.apache.commons.chain.CONFIG_ATTR</strong> -
    *     Name of the servlet context attribute under which the
  - *     resulting {@link Catalog} will be created or updated.  If not specified,
  - *     defaults to <code>catalog</code>.</li>
  + *     resulting {@link Catalog} will be created or updated.
  + *     If not specified, it is expected that parsed resources will
  + *     contain <code>&lt;catalog&gt;</code> elements (which will
  + *     cause registration of the created {@link Catalog}s into
  + *     the {@link CatalogFactory} for this application, and no
  + *     servet context attribute will be created.
  + *     <strong>NOTE</strong> - This parameter is deprecated.</p>
    * <li><strong>org.apache.commons.chain.RULE_SET</strong> -
    *     Fully qualified class name of a Digester <code>RuleSet</code>
    *     implementation to use for parsing configuration resources (this
  @@ -72,6 +78,11 @@
    *     archive (via <code>ServetContext.getResource()</code>).</li>
    * </ul>
    *
  + * <p>If no attribute key is specified, on the other hand, parsed configuration
  + * resources are expected to contain <code>&lt;catalog&gt;</code> elements,
  + * and the catalogs will be registered with the {@link CatalogFactory}
  + * for this web application.</p>
  + *
    * <p>This class requires Servlet 2.3 or later.  If you are running on
    * Servlet 2.2 system, consider using {@link ChainServlet} instead.
    * Note that {@link ChainServlet} uses parameters of the
  @@ -100,12 +111,6 @@
   
   
       /**
  -     * <p>The default servlet context attribute key.</p>
  -     */
  -    private static final String CONFIG_ATTR_DEFAULT = "catalog";
  -
  -
  -    /**
        * <p>The name of the context init parameter containing a comma-delimited
        * list of class loader resources to be scanned.</p>
        */
  @@ -152,10 +157,10 @@
   
           ServletContext context = event.getServletContext();
           String attr = context.getInitParameter(CONFIG_ATTR);
  -        if (attr == null) {
  -            attr = CONFIG_ATTR_DEFAULT;
  +        if (attr != null) {
  +            context.removeAttribute(attr);
           }
  -        context.removeAttribute(attr);
  +        CatalogFactory.clear();
   
       }
   
  @@ -176,18 +181,18 @@
   
           // Retrieve context init parameters that we need
           String attr = context.getInitParameter(CONFIG_ATTR);
  -        if (attr == null) {
  -            attr = CONFIG_ATTR_DEFAULT;
  -        }
           String classResources =
               context.getInitParameter(CONFIG_CLASS_RESOURCE);
           String ruleSet = context.getInitParameter(RULE_SET);
           String webResources = context.getInitParameter(CONFIG_WEB_RESOURCE);
   
  -        // Retrieve or create the Catalog instance we will be updating
  -        Catalog catalog = (Catalog) context.getAttribute(attr);
  -        if (catalog == null) {
  -            catalog = new CatalogBase();
  +        // Retrieve or create the Catalog instance we may be updating
  +        Catalog catalog = null;
  +        if (attr != null) {
  +            catalog = (Catalog) context.getAttribute(attr);
  +            if (catalog == null) {
  +                catalog = new CatalogBase();
  +            }
           }
   
           // Construct the configuration resource parser we will use
  @@ -209,14 +214,24 @@
           }
   
           // Parse the resources specified in our init parameters (if any)
  -        parseJarResources(catalog, context, parser);
  -        ChainResources.parseClassResources
  -            (catalog, classResources, parser);
  -        ChainResources.parseWebResources
  -            (catalog, context, webResources, parser);
  +        if (attr == null) {
  +            parseJarResources(context, parser);
  +            ChainResources.parseClassResources
  +                (classResources, parser);
  +            ChainResources.parseWebResources
  +                (context, webResources, parser);
  +        } else {
  +            parseJarResources(catalog, context, parser);
  +            ChainResources.parseClassResources
  +                (catalog, classResources, parser);
  +            ChainResources.parseWebResources
  +                (catalog, context, webResources, parser);
  +        }
   
  -        // Expose the completed catalog
  -        context.setAttribute(attr, catalog);
  +        // Expose the completed catalog (if requested)
  +        if (attr != null) {
  +            context.setAttribute(attr, catalog);
  +        }
   
       }
   
  @@ -228,9 +243,66 @@
        * <p>Parse resources found in JAR files in the <code>/WEB-INF/lib</code>
        * subdirectory (if any).</p>
        *
  +     * @param context <code>ServletContext</code> for this web application
  +     * @param parser {@link ConfigParser} to use for parsing
  +     */
  +    private void parseJarResources(ServletContext context,
  +                                   ConfigParser parser) {
  +
  +        Set jars = context.getResourcePaths("/WEB-INF/lib");
  +        if (jars == null) {
  +            jars = new HashSet();
  +        }
  +        String path = null;
  +        Iterator paths = jars.iterator();
  +        while (paths.hasNext()) {
  +
  +            path = (String) paths.next();
  +            if (!path.endsWith(".jar")) {
  +                continue;
  +            }
  +            URL resourceURL = null;
  +            try {
  +                URL jarURL = context.getResource(path);
  +                resourceURL = new URL("jar:" +
  +                                      translate(jarURL.toExternalForm()) +
  +                                      "!/META-INF/chain-config.xml");
  +                if (resourceURL == null) {
  +                    continue;
  +                }
  +                InputStream is = null;
  +                try {
  +                    is = resourceURL.openStream();
  +                } catch (Exception e) {
  +                    ; // means there is no such resource
  +                }
  +                if (is == null) {
  +                    continue;
  +                } else {
  +                    is.close();
  +                }
  +                parser.parse(resourceURL);
  +            } catch (Exception e) {
  +                throw new RuntimeException
  +                    ("Exception parsing chain config resource '" +
  +                     resourceURL.toExternalForm() + "': " +
  +                     e.getMessage());
  +            }
  +        }
  +
  +    }
  +
  +
  +    /**
  +     * <p>Parse resources found in JAR files in the <code>/WEB-INF/lib</code>
  +     * subdirectory (if any).</p>
  +     *
        * @param catalog {@link Catalog} we are populating
        * @param context <code>ServletContext</code> for this web application
        * @param parser {@link ConfigParser} to use for parsing
  +     *
  +     * @deprecated Use the variant that does not take a catalog, on a
  +     *  configuration resource containing "catalog" element(s)
        */
       private void parseJarResources(Catalog catalog, ServletContext context,
                                      ConfigParser parser) {
  
  
  
  1.7       +51 -21    jakarta-commons/chain/src/java/org/apache/commons/chain/web/ChainServlet.java
  
  Index: ChainServlet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/web/ChainServlet.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ChainServlet.java	25 Feb 2004 00:01:05 -0000	1.6
  +++ ChainServlet.java	18 Oct 2004 01:48:51 -0000	1.7
  @@ -24,6 +24,7 @@
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
   import org.apache.commons.chain.Catalog;
  +import org.apache.commons.chain.CatalogFactory;
   import org.apache.commons.chain.config.ConfigParser;
   import org.apache.commons.chain.impl.CatalogBase;
   import org.apache.commons.digester.RuleSet;
  @@ -47,8 +48,13 @@
    *     will be loaded.</li>
    * <li><strong>org.apache.commons.chain.CONFIG_ATTR</strong> -
    *     Name of the servlet context attribute under which the
  - *     resulting {@link Catalog} will be created or updated.  If not specified,
  - *     defaults to <code>catalog</code>.</li>
  + *     resulting {@link Catalog} will be created or updated.
  + *     If not specified, it is expected that parsed resources will
  + *     contain <code>&lt;catalog&gt;</code> elements (which will
  + *     cause registration of the created {@link Catalog}s into
  + *     the {@link CatalogFactory} for this application, and no
  + *     servet context attribute will be created.
  + *     <strong>NOTE</strong> - This parameter is deprecated.</p>
    * <li><strong>org.apache.commons.chain.RULE_SET</strong> -
    *     Fully qualified class name of a Digester <code>RuleSet</code>
    *     implementation to use for parsing configuration resources (this
  @@ -68,6 +74,11 @@
    *     archive (via <code>ServetContext.getResource()</code>).</li>
    * </ul>
    *
  + * <p>If no attribute key is specified, on the other hand, parsed configuration
  + * resources are expected to contain <code>&lt;catalog&gt;</code> elements,
  + * and the catalogs will be registered with the {@link CatalogFactory}
  + * for this web application.</p>
  + *
    * <p>This class runs on Servlet 2.2 or later.  If you are running on a
    * Servlet 2.3 or later system, you should also consider using
    * {@link ChainListener} to initialize your {@link Catalog}.  Note that
  @@ -97,12 +108,6 @@
   
   
       /**
  -     * <p>The default servlet context attribute key.</p>
  -     */
  -    public static final String CONFIG_ATTR_DEFAULT = "catalog";
  -
  -
  -    /**
        * <p>The name of the context init parameter containing a comma-delimited
        * list of class loader resources to be scanned.</p>
        */
  @@ -140,6 +145,22 @@
   
   
       /**
  +     * <p>Clean up after ourselves as this application shuts down.</p>
  +     */
  +    public void destroy() {
  +
  +        ServletConfig config = getServletConfig();
  +        ServletContext context = getServletContext();
  +        String attr = config.getInitParameter(CONFIG_ATTR);
  +        if (attr != null) {
  +            context.removeAttribute(attr);
  +        }
  +        CatalogFactory.clear();
  +
  +    }
  +
  +
  +    /**
        * <p>Create (if necessary) and configure a {@link Catalog} from the
        * servlet init parameters that have been specified.</p>
        *
  @@ -156,18 +177,18 @@
   
           // Retrieve servlet init parameters that we need
           String attr = config.getInitParameter(CONFIG_ATTR);
  -        if (attr == null) {
  -            attr = CONFIG_ATTR_DEFAULT;
  -        }
           String classResources =
               context.getInitParameter(CONFIG_CLASS_RESOURCE);
           String ruleSet = context.getInitParameter(RULE_SET);
           String webResources = context.getInitParameter(CONFIG_WEB_RESOURCE);
   
  -        // Retrieve or create the Catalog instance we will be updating
  -        Catalog catalog = (Catalog) context.getAttribute(attr);
  -        if (catalog == null) {
  -            catalog = new CatalogBase();
  +        // Retrieve or create the Catalog instance we may be updating
  +        Catalog catalog = null;
  +        if (attr != null) {
  +            catalog = (Catalog) context.getAttribute(attr);
  +            if (catalog == null) {
  +                catalog = new CatalogBase();
  +            }
           }
   
           // Construct the configuration resource parser we will use
  @@ -188,13 +209,22 @@
           }
   
           // Parse the resources specified in our init parameters (if any)
  -        ChainResources.parseClassResources
  -            (catalog, classResources, parser);
  -        ChainResources.parseWebResources
  -            (catalog, context, webResources, parser);
  +        if (attr == null) {
  +            ChainResources.parseClassResources
  +                (classResources, parser);
  +            ChainResources.parseWebResources
  +                (context, webResources, parser);
  +        } else {
  +            ChainResources.parseClassResources
  +                (catalog, classResources, parser);
  +            ChainResources.parseWebResources
  +                (catalog, context, webResources, parser);
  +        }
   
  -        // Expose the completed catalog
  -        context.setAttribute(attr, catalog);
  +        // Expose the completed catalog (if requested)
  +        if (attr != null) {
  +            context.setAttribute(attr, catalog);
  +        }
           
       }
   
  
  
  
  1.6       +67 -9     jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ChainProcessor.java
  
  Index: ChainProcessor.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ChainProcessor.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ChainProcessor.java	25 Feb 2004 00:01:04 -0000	1.5
  +++ ChainProcessor.java	18 Oct 2004 01:48:52 -0000	1.6
  @@ -21,6 +21,7 @@
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
   import org.apache.commons.chain.Catalog;
  +import org.apache.commons.chain.CatalogFactory;
   import org.apache.commons.chain.Command;
   import org.apache.commons.chain.Context;
   import org.apache.commons.chain.web.ChainServlet;
  @@ -28,13 +29,16 @@
   
   /**
    * <p>Custom subclass of {@link ChainServlet} that also dispatches incoming
  - * requests to a configurable {@link Command} loaded from the configured
  + * requests to a configurable {@link Command} loaded from the specified
    * {@link Catalog}.</p>
    *
    * <p>In addition to the <em>servlet</em> init parameters supported by
    * {@link ChainServlet}, this class supports the following additional
    * parameters:</p>
    * <ul>
  + * <li><strong>org.apache.commons.chain.CATALOG</strong> - Name of the
  + *     catalog from which to acquire commands to be executed.  If not
  + *     specified, the default catalog for this application will be used.</li>
    * <li><strong>org.apache.commons.chain.COMMAND</strong> - Name of the
    *     {@link Command} (looked up in our configured {@link Catalog} used
    *     to process all incoming servlet requests.  If not specified,
  @@ -57,6 +61,22 @@
   
       /**
        * <p>The name of the servlet init parameter containing the name of the
  +     * {@link Catalog} to use for processing incoming requests.</p>
  +     */
  +    public static final String CATALOG =
  +        "org.apache.commons.chain.CATALOG";
  +
  +
  +    /**
  +     * <p>The default request attribute under which we expose the
  +     * {@link Catalog} being used to subordinate {@link Command}s.</p>
  +     */
  +    public static final String CATALOG_DEFAULT =
  +        "org.apache.commons.chain.CATALOG";
  +
  +
  +    /**
  +     * <p>The name of the servlet init parameter containing the name of the
        * {@link Command} (loaded from our configured {@link Catalog} to use
        * for processing each incoming request.</p>
        */
  @@ -76,22 +96,52 @@
       /**
        * <p>The name of the context attribute under which our {@link Catalog}
        * is stored.  This value is also used as the name of the
  -     * context attribute under which the catalog is exposed to commands.</p>
  +     * context attribute under which the catalog is exposed to commands.
  +     * If not specified, we will look up commands in the appropriate
  +     * {@link Catalog} retrieved from our {@link CatalogFactory}.</p>
        */
       private String attribute = null;
   
   
       /**
  +     * <p>The name of the {@link Catalog} to retrieve from the
  +     * {@link CatalogFactory} for this application, or <code>null</code>
  +     * to select the default {@link Catalog}.</p>
  +     */
  +    private String catalog = null;
  +
  +
  +    /**
        * <p>The name of the {@link Command} to be executed for each incoming
        * request.</p>
        */
       private String command = null;
   
   
  +    /**
  +     * <p>The {@link CatalogFactory} for this application.</p>
  +     */
  +    private CatalogFactory factory = null;
  +
  +
       // --------------------------------------------------------- Servlet Methods
   
   
       /**
  +     * <p>Clean up as this application is shut down.</p>
  +     */
  +    public void destroy() {
  +
  +        super.destroy();
  +        attribute = null;
  +        catalog = null;
  +        command = null;
  +        factory = null;
  +
  +    }
  +
  +
  +    /**
        * <p>Cache the name of the command we should execute for each request.</p>
        *
        * @exception ServletException if an initialization error occurs
  @@ -100,13 +150,12 @@
   
           super.init();
           attribute = getServletConfig().getInitParameter(CONFIG_ATTR);
  -        if (attribute == null) {
  -            attribute = CONFIG_ATTR_DEFAULT;
  -        }
  +        catalog = getServletConfig().getInitParameter(CATALOG);
           command = getServletConfig().getInitParameter(COMMAND);
           if (command == null) {
               command = COMMAND_DEFAULT;
           }
  +        factory = CatalogFactory.getInstance();
   
       }
   
  @@ -128,10 +177,19 @@
   
           ServletWebContext context =
               new ServletWebContext(getServletContext(), request, response);
  -        Catalog catalog =
  -            (Catalog) getServletContext().getAttribute(this.attribute);
  -        context.put(this.attribute, catalog);
  -        Command command = catalog.getCommand(this.command);
  +        Catalog theCatalog = null;
  +        if (attribute != null) {
  +            theCatalog = (Catalog) getServletContext().getAttribute
  +                (this.attribute);
  +        } else if (catalog != null) {
  +            theCatalog = factory.getCatalog(catalog);
  +        } else {
  +            theCatalog = factory.getCatalog();
  +        }
  +        if (attribute == null) {
  +            request.setAttribute(CATALOG_DEFAULT, theCatalog);
  +        }
  +        Command command = theCatalog.getCommand(this.command);
           try {
               command.execute(context);
           } catch (Exception e) {
  
  
  
  1.4       +1 -1      jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/PathInfoMapper.java
  
  Index: PathInfoMapper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/PathInfoMapper.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- PathInfoMapper.java	25 Feb 2004 00:01:04 -0000	1.3
  +++ PathInfoMapper.java	18 Oct 2004 01:48:52 -0000	1.4
  @@ -40,7 +40,7 @@
       // ------------------------------------------------------ Instance Variables
   
   
  -    private String catalogKey = ChainProcessor.CONFIG_ATTR_DEFAULT;
  +    private String catalogKey = ChainProcessor.CATALOG_DEFAULT;
   
   
       // -------------------------------------------------------------- Properties
  
  
  
  1.4       +1 -1      jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/RequestParameterMapper.java
  
  Index: RequestParameterMapper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/RequestParameterMapper.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- RequestParameterMapper.java	25 Feb 2004 00:01:04 -0000	1.3
  +++ RequestParameterMapper.java	18 Oct 2004 01:48:52 -0000	1.4
  @@ -41,7 +41,7 @@
       // ------------------------------------------------------ Instance Variables
   
   
  -    private String catalogKey = ChainProcessor.CONFIG_ATTR_DEFAULT;
  +    private String catalogKey = ChainProcessor.CATALOG_DEFAULT;
       private String parameter = "command";
   
   
  
  
  
  1.4       +1 -1      jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletPathMapper.java
  
  Index: ServletPathMapper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletPathMapper.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ServletPathMapper.java	25 Feb 2004 00:01:04 -0000	1.3
  +++ ServletPathMapper.java	18 Oct 2004 01:48:52 -0000	1.4
  @@ -40,7 +40,7 @@
       // ------------------------------------------------------ Instance Variables
   
   
  -    private String catalogKey = ChainProcessor.CONFIG_ATTR_DEFAULT;
  +    private String catalogKey = ChainProcessor.CATALOG_DEFAULT;
   
   
       // -------------------------------------------------------------- Properties
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org