commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@apache.org
Subject cvs commit: jakarta-commons-sandbox/chain/src/java/org/apache/commons/chain/web ChainResources.java ChainListener.java ChainServlet.java ChainListener2.java
Date Sat, 04 Oct 2003 22:54:10 GMT
craigmcc    2003/10/04 15:54:10

  Modified:    chain    build.xml
               chain/src/java/org/apache/commons/chain/web
                        ChainListener.java ChainServlet.java
  Added:       chain/src/java/org/apache/commons/chain/web
                        ChainResources.java
  Removed:     chain/src/java/org/apache/commons/chain/web
                        ChainListener2.java
  Log:
  Merge configuration from class loader resources (as well as webapp
  resources) from ChainListener2 into ChainListener.
  
  Correct ChainListener so that it doesn't complain about JAR files in
  /WEB-INF/lib that do not have a META-INF/chain-config.xml resource.
  
  Unify the behavior and configuration parameter names for ChainListener
  and ChainServlet so that they behave similarly (except that ChainListener
  will also scan JAR files).
  
  ChainServlet uses *servlet* init parameters, while ChainListener uses
  *context* init parameters, so they can both be used in the same webapp
  if desired (for example, multiple instances of ChainServlet could be
  used to initialize different catalogs.
  
  TODO:  Unit tests to verify correct behavior.
  
  TODO:  Subclass of ChainServlet that dispatches requests to a named command
  in the default catalog, for stand alone testing and/or a "poor man's
  MVC controller" environments.
  
  Revision  Changes    Path
  1.7       +11 -1     jakarta-commons-sandbox/chain/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/chain/build.xml,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- build.xml	1 Oct 2003 12:28:01 -0000	1.6
  +++ build.xml	4 Oct 2003 22:54:09 -0000	1.7
  @@ -205,6 +205,16 @@
                   unless="logging.present"/>
         <exclude    name="org/apache/commons/chain/web/ChainListener.java"
                   unless="servlet23.present"/>
  +      <exclude    name="org/apache/commons/chain/web/ChainResources.java"
  +                unless="beanutils.present"/>
  +      <exclude    name="org/apache/commons/chain/web/ChainResources.java"
  +                unless="collections.present"/>
  +      <exclude    name="org/apache/commons/chain/web/ChainResources.java"
  +                unless="digester.present"/>
  +      <exclude    name="org/apache/commons/chain/web/ChainResources.java"
  +                unless="logging.present"/>
  +      <exclude    name="org/apache/commons/chain/web/ChainResources.java"
  +                unless="servlet23.present"/>
         <exclude    name="org/apache/commons/chain/web/ChainServlet.java"
                   unless="beanutils.present"/>
         <exclude    name="org/apache/commons/chain/web/ChainServlet.java"
  
  
  
  1.2       +122 -78   jakarta-commons-sandbox/chain/src/java/org/apache/commons/chain/web/ChainListener.java
  
  Index: ChainListener.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/chain/src/java/org/apache/commons/chain/web/ChainListener.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ChainListener.java	1 Oct 2003 04:23:00 -0000	1.1
  +++ ChainListener.java	4 Oct 2003 22:54:09 -0000	1.2
  @@ -76,44 +76,60 @@
   import org.apache.commons.chain.Catalog;
   import org.apache.commons.chain.config.ConfigParser;
   import org.apache.commons.chain.impl.CatalogBase;
  +import org.apache.commons.digester.RuleSet;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
   
   /**
  - * <p>Optional <code>ServletContextListener</code> that automatically
  + * <p><code>ServletContextListener</code> that automatically
    * scans chain configuration files in the current web application at
    * startup time, and exposes the result in a {@link Catalog} under a
  - * specified servlet context attribute.  The following context init
  + * specified servlet context attribute.  The following <em>context</em> init
    * parameters are utilized:</p>
    * <ul>
  - * <li><strong>org.apache.commons.chain.CONFIG_RESOURCE</strong> -
  - *     comma-deimited list of chain configuration webapp resources
  - *     to be loaded.  If not specified, defaults to
  - *     <code>/WEB-INF/chain-config.xml</code>.</li>
  - * <li><strong>org.apache.commons.chain.CONFIG_CATALOG</strong> -
  + * <li><strong>org.apache.commons.chain.CONFIG_CLASS_RESOURCE</strong>
-
  + *     comma-delimited list of chain configuration resources to be loaded
  + *     via <code>ClassLoader.getResource()</code> calls.  If not specified,
  + *     no class loader resources will be loaded.</li>
  + * <li><strong>org.apache.commons.chain.CONFIG_WEB_RESOURCE</strong>
-
  + *     comma-delimited list of chain configuration webapp resources
  + *     to be loaded.  If not specified, no web application resources
  + *     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 exposed.  If not specified,
  + *     resulting {@link Catalog} will be created or updated.  If not specified,
    *     defaults to <code>catalog</code>.</li>
  + * <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
  + *     class must have a public zero-args constructor).  If not defined,
  + *     the standard <code>RuleSet</code> implementation will be used.</li>
    * </ul>
    *
    * <p>When a web application that has configured this listener is
  - * started, it will create a new {@link Catalog} (if needed) and populate it by
  - * scanning configuration resources from the init parameter described
  - * above.  If a {@link Catalog} instance already exists at the specified
  - * attribute, any new parsed definitions will be merged into the existing
  - * catalog (the last definition for a particular name wins).  In addition,
  - * it will scan all of the JAR files in the <code>/WEB-INF/lib</code>
  - * directory, and automatically scan any <code>META-INF/chain-config.xml</code>
  - * files embedded in those JAR files.  In this way, it is easy to provide a
  - * library JAR that includes command chain definitions, and have it
  - * automatically get configured simply by dropping the JAR file into a
  - * web application.</p>
  + * started, it will acquire the {@link Catalog} under the specified servlet
  + * context attribute key, creating a new one if there is none already there.
  + * This {@link Catalog} will then be populated by scanning configuration
  + * resources from the following sources (loaded in this order):</p>
  + * <ul>
  + * <li>Resources loaded from any <code>META-INF/chain-config.xml</code>
  + *     resource found in a JAR file in <code>/WEB-INF/lib</code>.</li>
  + * <li>Resources loaded from specified resource paths from the
  + *     webapp's class loader (via <code>ClassLoader.getResource()</code>).</li>
  + * <li>Resources loaded from specified resource paths in the web application
  + *     archive (via <code>ServetContext.getResource()</code>).</li>
  + * </ul>
    *
    * <p>This class requires Servlet 2.3 or later.  If you are running on
  - * a Servlet 2.2 system, consider using {@link ChainServlet} instead.</p>
  + * Servlet 2.2 system, consider using {@link ChainServlet} instead.
  + * Note that {@link ChainServlet} uses parameters of the
  + * same names, but they are <em>servlet</em> init parameters instead
  + * of <em>context</em> init parameters.  Because of this, you can use
  + * both facilities in the same application, if desired.</p>
    *
    * @author Craig R. McClanahan
  + * @author Ted Husted
    * @version $Revision$ $Date$
    */
   
  @@ -133,24 +149,34 @@
   
   
       /**
  +     * <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 web applicaton resources to be scanned.</p>
  +     * list of class loader resources to be scanned.</p>
        */
  -    public static final String CONFIG_RESOURCE =
  -        "org.apache.commons.chain.CONFIG_RESOURCE";
  +    public static final String CONFIG_CLASS_RESOURCE =
  +        "org.apache.commons.chain.CONFIG_CLASS_RESOURCE";
   
   
       /**
  -     * <p>The default servlet context attribute key.</p>
  +     * <p>The name of the context init parameter containing a comma-delimited
  +     * list of web applicaton resources to be scanned.</p>
        */
  -    private static final String CONFIG_ATTR_DEFAULT = "catalog";
  +    public static final String CONFIG_WEB_RESOURCE =
  +        "org.apache.commons.chain.CONFIG_WEB_RESOURCE";
   
   
       /**
  -     * <p>The default resource list.</p>
  +     * <p>The name of the context init parameter containing the fully
  +     * qualified class name of the <code>RuleSet</code> implementation
  +     * for configuring our {@link ConfigParser}.</p>
        */
  -    private static final String CONFIG_RESOURCE_DEFAULT =
  -        "/WEB-INF/chain-config.xml";
  +    public static final String RULE_SET =
  +        "org.apache.commons.chain.RULE_SET";
   
   
       // -------------------------------------------------------- Static Variables
  @@ -192,17 +218,20 @@
        */
       public void contextInitialized(ServletContextEvent event) {
   
  +        if (log.isInfoEnabled()) {
  +            log.info("Initializing chain listener");
  +        }
           ServletContext context = event.getServletContext();
  +
  +        // Retrieve context init parameters that we need
           String attr = context.getInitParameter(CONFIG_ATTR);
           if (attr == null) {
               attr = CONFIG_ATTR_DEFAULT;
           }
  -        boolean defaulting = false;
  -        String resource = context.getInitParameter(CONFIG_RESOURCE);
  -        if (resource == null) {
  -            resource = CONFIG_RESOURCE_DEFAULT;
  -            defaulting = true;
  -        }
  +        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);
  @@ -211,51 +240,58 @@
           }
   
           // Construct the configuration resource parser we will use
  -        // FIXME - Do we need to make this more configurable?
           ConfigParser parser = new ConfigParser();
  -
  -        // Parse the resources specified in our init parameter (if any)
  -        String path = null;
  -        try {
  -            while (true) {
  -                int comma = resource.indexOf(",");
  -                if (comma < 0) {
  -                    path = resource.trim();
  -                    resource = "";
  -                } else {
  -                    path = resource.substring(0, comma);
  -                    resource = resource.substring(comma + 1);
  -                }
  -                if (path.length() < 1) {
  -                    break;
  -                }
  -                URL url = context.getResource(path);
  -                if (url == null) {
  -                    if (defaulting) {
  -                        continue; // It is OK not to have a default config file
  -                    } else {
  -                        throw new IllegalStateException
  -                            ("Missing chain config resource '" + path + "'");
  -                    }
  -                }
  -                if (log.isDebugEnabled()) {
  -                    log.debug("Loading chain config resource '" + path + "'");
  +        if (ruleSet != null) {
  +            try {
  +                ClassLoader loader =
  +                    Thread.currentThread().getContextClassLoader();
  +                if (loader == null) {
  +                    loader = this.getClass().getClassLoader();
                   }
  -                parser.parse(catalog, url);
  +                Class clazz = loader.loadClass(ruleSet);
  +                parser.setRuleSet((RuleSet) clazz.newInstance());
  +            } catch (Exception e) {
  +                throw new RuntimeException("Exception initalizing RuleSet '" +
  +                                           ruleSet + "' instance: " +
  +                                           e.getMessage());
               }
  -        } catch (Exception e) {
  -            throw new RuntimeException
  -                ("Exception parsing chain config resource '" + path + "': " +
  -                 e.getMessage());
           }
   
  -        // Parse the resources in any embedded JAR files
  +        // 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);
  +
  +        // Expose the completed catalog
  +        context.setAttribute(attr, catalog);
  +
  +    }
  +
  +
  +    // --------------------------------------------------------- Private Methods
  +
  +
  +    /**
  +     * <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
  +     */
  +    private void parseJarResources(Catalog catalog, 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;
  @@ -266,6 +302,20 @@
                   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(catalog, resourceURL);
               } catch (Exception e) {
                   throw new RuntimeException
  @@ -275,13 +325,7 @@
               }
           }
   
  -        // Expose the completed catalog
  -        context.setAttribute(attr, catalog);
  -
       }
  -
  -
  -    // --------------------------------------------------------- Private Methods
   
   
       /**
  
  
  
  1.3       +123 -134  jakarta-commons-sandbox/chain/src/java/org/apache/commons/chain/web/ChainServlet.java
  
  Index: ChainServlet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/chain/src/java/org/apache/commons/chain/web/ChainServlet.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ChainServlet.java	1 Oct 2003 12:41:07 -0000	1.2
  +++ ChainServlet.java	4 Oct 2003 22:54:10 -0000	1.3
  @@ -62,202 +62,188 @@
   package org.apache.commons.chain.web;
   
   
  -import java.io.IOException;
   import java.io.InputStream;
  +import java.io.IOException;
   import java.net.URL;
  -
  -import javax.servlet.*;
  -
  +import javax.servlet.GenericServlet;
  +import javax.servlet.ServletConfig;
  +import javax.servlet.ServletContext;
  +import javax.servlet.ServletException;
  +import javax.servlet.ServletRequest;
  +import javax.servlet.ServletResponse;
   import org.apache.commons.chain.Catalog;
  -import org.apache.commons.chain.config.ConfigRuleSet;
  +import org.apache.commons.chain.config.ConfigParser;
   import org.apache.commons.chain.impl.CatalogBase;
   import org.apache.commons.digester.Digester;
   import org.apache.commons.digester.RuleSet;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  -import org.xml.sax.InputSource;
   
   
   /**
  - * <p>Servlet that configures a Catalog based on an XML configuration file.  The
  - * catalog is stored in a servlet context attribute so that it is accessible to
  - * other servlets.</p>
  - * 
  - * <p>The servlet has three different initialization parameters, as described
  - * below:</p>
  + * <p><code>Servlet</code> that automatically scans chain configuration
files
  + * in the current web application at startup time, and exposes the result in a
  + * {@link Catalog} under a specified servlet context attribute.  The following
  + * <em>servlet</em> init parameters are utilized:</p>
  + * <ul>
  + * <li><strong>org.apache.commons.chain.CONFIG_CLASS_RESOURCE</strong>
-
  + *     comma-delimited list of chain configuration resources to be loaded
  + *     via <code>ClassLoader.getResource()</code> calls.  If not specified,
  + *     no class loader resources will be loaded.</li>
  + * <li><strong>org.apache.commons.chain.CONFIG_WEB_RESOURCE</strong>
-
  + *     comma-delimited list of chain configuration webapp resources
  + *     to be loaded.  If not specified, no web application resources
  + *     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>
  + * <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
  + *     class must have a public zero-args constructor).  If not defined,
  + *     the standard <code>RuleSet</code> implementation will be used.</li>
  + * </ul>
  + *
  + * <p>When a web application that has configured this servlet is
  + * started, it will acquire the {@link Catalog} under the specified servlet
  + * context attribute key, creating a new one if there is none already there.
  + * This {@link Catalog} will then be populated by scanning configuration
  + * resources from the following sources (loaded in this order):</p>
    * <ul>
  - *     <li><code>config</code> specifies the location of the XML file
used by
  - *     the Digester to create the Catalog.  The locations of files are relative
  - *     to the webapp's root context directory, so a
  - *     <code>/WEB-INF/chain-config.xml</code> is an example location for the
  - *     XML file.</li>
  - *     <li><code>ruleset</code> (optional) specifies the fully qualified
class
  - *     name of the rule set the Digester should use to create the Catalog.  If
  - *     this value is not specified the {@link
  - *     org.apache.commons.chain.config.ConfigRuleSet} will be used.</li>
  - *     <li><code>attribute</code> (optional) specifies the servlet context
  - *     attribute in which the Catalog should be stored.  If this value is not
  - *     specified the attribute used will be
  - *     <code>Catalog.CATALOG_KEY</code>.</li>
  + * <li>Resources loaded from specified resource paths from the
  + *     webapp's class loader (via <code>ClassLoader.getResource()</code>).</li>
  + * <li>Resources loaded from specified resource paths in the web application
  + *     archive (via <code>ServetContext.getResource()</code>).</li>
    * </ul>
  - * 
  - * <p>One final note is that if a Catalog is found to already exist in the
  - * context attribute to which the Catalog should be saved, the two catalogs will
  - * be <em>merged</em> as described here. (TODO create this documentation and
  - * link it here)</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}.</p>
  + * {@link ChainListener} to initialize your {@link Catalog}.  Note that
  + * {@link ChainListener} uses parameters of the same names, but they are
  + * <em>context</em> init parameters instead of <em>servlet</em>
init
  + * parameters. Because of this, you can use both facilities in the
  + * same application, if desired.</p>
    *
    * @author Matthew J. Sgarlata
    * @author Craig R. McClanahan
  + * @author Ted Husted
    */
  +
   public class ChainServlet extends GenericServlet {
       
   
  -    /**
  -     * <p>The <code>Log</code> instance to use with this class.</p>
  -     */
  -    protected static final Log log = LogFactory.getLog(ChainServlet.class);
  +    // ------------------------------------------------------ Manifest Constants
   
   
       /**
  -     * <p>The servlet initialization parameter which specifies the location of the
  -     * XML file that describes the servlet chain we wish to load.</p>
  +     * <p>The name of the context init parameter containing the name of the
  +     * servlet context attribute under which our resulting {@link Catalog}
  +     * will be stored.</p>
        */
  -    public static final String INIT_PARAM_CONFIG = "config";
  +    public static final String CONFIG_ATTR =
  +        "org.apache.commons.chain.CONFIG_ATTR";
   
   
       /**
  -     * <p>The optional servlet initialization parameter that specifies the fully
  -     * qualified class name of the rule set that will be used to configure the
  -     * Digester.</p>
  +     * <p>The default servlet context attribute key.</p>
        */
  -    public static final String INIT_PARAM_RULE_SET = "ruleset";
  +    private static final String CONFIG_ATTR_DEFAULT = "catalog";
   
   
       /**
  -     * <p>The optional servlet initialization parameter that specifies the
  -     * servlet context attribute in which the catalog will be stored.  If this
  -     * parameter is not specified, the catalog will be stored under the
  -     * attribute <code>org.apache.commons.chain.CATALOG</code>.</p>
  +     * <p>The name of the context init parameter containing a comma-delimited
  +     * list of class loader resources to be scanned.</p>
        */
  -    public static final String INIT_PARAM_ATTRIBUTE = "attribute";
  +    public static final String CONFIG_CLASS_RESOURCE =
  +        "org.apache.commons.chain.CONFIG_CLASS_RESOURCE";
   
   
       /**
  -     * <p>The servlet context attribute in which the catalog will be stored. </p>
  +     * <p>The name of the context init parameter containing a comma-delimited
  +     * list of web applicaton resources to be scanned.</p>
        */
  -    protected String catalogAttr = Catalog.CATALOG_KEY; ;
  +    public static final String CONFIG_WEB_RESOURCE =
  +        "org.apache.commons.chain.CONFIG_WEB_RESOURCE";
   
   
       /**
  -     * <p>The <code>RuleSet</code> to be used for configuring our Digester
  -     * parsing rules.</p>
  +     * <p>The name of the context init parameter containing the fully
  +     * qualified class name of the <code>RuleSet</code> implementation
  +     * for configuring our {@link ConfigParser}.</p>
        */
  -    protected RuleSet ruleSet = new ConfigRuleSet();
  +    public static final String RULE_SET =
  +        "org.apache.commons.chain.RULE_SET";
   
   
  -    /**
  -     * <p>The <code>Digester</code> to be used for parsing.</p>
  -     */
  -    protected Digester digester = null;
  +    // -------------------------------------------------------- Static Variables
   
   
       /**
  -     * <p>Return the <code>Digester</code> instance to be used for
  -     * parsing, creating one if necessary.</p>
  +     * <p>The <code>Log</code> instance to use with this class.</p>
        */
  -    public Digester getDigester() {
  +    protected static final Log log = LogFactory.getLog(ChainServlet.class);
   
  -        if (digester == null) {
  -            digester = new Digester();
  -            digester.setNamespaceAware(ruleSet.getNamespaceURI() != null);
  -            digester.setValidating(false);
  -            digester.addRuleSet(ruleSet);
  -        }
  -        return (digester);
   
  -    }
  +    // --------------------------------------------------------- Servlet Methods
   
   
       /**
  -     * <p>Use the Digester to create a {@link org.apache.commons.chain.Catalog}
  -     * based on the information in an XML file and store the catalog in the
  -     * servlet context. </p>
  +     * <p>Create (if necessary) and configure a {@link Catalog} from the
  +     * servlet init parameters that have been specified.</p>
        *
  -     * @param servletConfig the configuration information supplied with this
  -     * servlet
  -     *
  -     * @throws javax.servlet.ServletException if the servlet could not be initialized
  +     * @throws ServletException if the servlet could not be initialized
        */
  -    public void init(ServletConfig servletConfig) throws ServletException {
  -        
  -        String configParam;
  -        String rulesetParam;
  -        String attributeParam;
  +    public void init() throws ServletException {
           
  -        super.init(servletConfig);
  +        ServletConfig config = getServletConfig();
  +        ServletContext context = getServletContext();
           if (log.isInfoEnabled()) {
               log.info("Initializing chain servlet '" +
  -                servletConfig.getServletName() + "'");
  +                     config.getServletName() + "'");
           }
  -        
  -        configParam = servletConfig.getInitParameter(INIT_PARAM_CONFIG); 
  -        if (configParam == null) {
  -            log.error("The " + INIT_PARAM_CONFIG +
  -                " init-param must be specified");
  -            throw new ServletException("The " + INIT_PARAM_CONFIG +
  -                " init-param must be specified");
  -        }
  -        
  -        rulesetParam = servletConfig.getInitParameter(INIT_PARAM_RULE_SET);
  -        if (rulesetParam != null) {
  -            try {
  -                ruleSet = (RuleSet) Class.forName(rulesetParam).newInstance();
  -            }
  -            catch (Exception e) {
  -                log.error("Error creating RuleSet", e);
  -                throw new ServletException("Error creating RuleSet", e);
  -            }
  +
  +        // Retrieve servlet init parameters that we need
  +        String attr = config.getInitParameter(CONFIG_ATTR);
  +        if (attr == null) {
  +            attr = CONFIG_ATTR_DEFAULT;
           }
  -        
  -        attributeParam = servletConfig.getInitParameter(INIT_PARAM_ATTRIBUTE);
  -        if (attributeParam != null) {
  -            catalogAttr = attributeParam;
  +        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();
           }
  -        
  -        try {
  -            Catalog catalog;
  -            if (getServletContext().getAttribute(catalogAttr) != null) {
  -                catalog =
  -                    (Catalog) getServletContext().getAttribute(catalogAttr);
  -                if (log.isInfoEnabled()) {
  -                    log.info("Merging catalog with existing catalog " + catalog);
  -                }
  -            }
  -            else {
  -                catalog = new CatalogBase();
  -                if (log.isInfoEnabled()) {
  -                    log.info("Creating new catalog");
  +
  +        // Construct the configuration resource parser we will use
  +        ConfigParser parser = new ConfigParser();
  +        if (ruleSet != null) {
  +            try {
  +                ClassLoader loader =
  +                    Thread.currentThread().getContextClassLoader();
  +                if (loader == null) {
  +                    loader = this.getClass().getClassLoader();
                   }
  +                Class clazz = loader.loadClass(ruleSet);
  +                parser.setRuleSet((RuleSet) clazz.newInstance());
  +            } catch (Exception e) {
  +                throw new ServletException("Exception initalizing RuleSet '" +
  +                                           ruleSet + "' instance", e);
               }
  -            Digester digester = getDigester();
  -            InputStream input = null;
  -            URL url = getServletContext().getResource(configParam);
  -            InputSource is = new InputSource(url.toExternalForm());
  -            input = getServletContext().getResourceAsStream(configParam);
  -            is.setByteStream(input);
  -            digester.push(catalog);
  -            digester.parse(is);
  -            getServletContext().setAttribute(catalogAttr, catalog);
  -        }
  -        catch (Exception e) {
  -            log.error("Error initializing the ChainServlet", e);
  -            throw new ServletException("Error initializing the ChainServlet",
  -                e);
           }
  +
  +        // Parse the resources specified in our init parameters (if any)
  +        ChainResources.parseClassResources
  +            (catalog, classResources, parser);
  +        ChainResources.parseWebResources
  +            (catalog, context, webResources, parser);
  +
  +        // Expose the completed catalog
  +        context.setAttribute(attr, catalog);
           
       }
   
  @@ -278,5 +264,8 @@
           throws ServletException, IOException {
   
           ; // do nothing
  +
       }
  +
  +
   }
  
  
  
  1.1                  jakarta-commons-sandbox/chain/src/java/org/apache/commons/chain/web/ChainResources.java
  
  Index: ChainResources.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/chain/src/java/org/apache/commons/chain/web/ChainResources.java,v
1.1 2003/10/04 22:54:09 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2003/10/04 22:54:09 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.commons.chain.web;
  
  
  import java.net.URL;
  import javax.servlet.ServletContext;
  import org.apache.commons.chain.Catalog;
  import org.apache.commons.chain.config.ConfigParser;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  
  /**
   * <p>Utility methods for loading class loader and web application resources
   * to configure a {@link Catalog}.  These methods are shared between
   * <code>ChainListener</code> and <code>ChainServlet</code>.</p>
   *
   * @author Craig R. McClanahan
   * @author Ted Husted
   */
  
  final class ChainResources {
  
  
      // -------------------------------------------------------- Static Variables
  
  
      /**
       * <p>The <code>Log</code> instance for this class.</p>
       */
      private static final Log log = LogFactory.getLog(ChainListener.class);
  
  
      // ---------------------------------------------------------- Static Methods
  
  
      /**
       * <p>Parse the specified class loader resources.</p>
       *
       * @param catalog {@link Catalog} we are populating
       * @param resources Comma-delimited list of resources (or <code>null</code>)
       * @param parser {@link ConfigParser} to use for parsing
       */
      static void parseClassResources(Catalog catalog, String resources,
                                      ConfigParser parser) {
  
          ClassLoader loader =
              Thread.currentThread().getContextClassLoader();
          if (loader == null) {
              loader = ChainResources.class.getClassLoader();
          }
          String path = null;
          try {
              while (true) {
                  int comma = resources.indexOf(",");
                  if (comma < 0) {
                      path = resources.trim();
                      resources = "";
                  } else {
                      path = resources.substring(0, comma);
                      resources = resources.substring(comma + 1);
                  }
                  if (path.length() < 1) {
                      break;
                  }
                  URL url = loader.getResource(path);
                  if (url == null) {
                      throw new IllegalStateException
                          ("Missing chain config resource '" + path + "'");
                  }
                  if (log.isDebugEnabled()) {
                      log.debug("Loading chain config resource '" + path + "'");
                  }
                  parser.parse(catalog, url);
              }
          } catch (Exception e) {
              throw new RuntimeException
                  ("Exception parsing chain config resource '" + path + "': " +
                   e.getMessage());
          }
  
      }
  
  
      /**
       * <p>Parse the specified web application resources.</p>
       *
       * @param catalog {@link Catalog} we are populating
       * @param context <code>ServletContext</code> for this web application
       * @param resources Comma-delimited list of resources (or <code>null</code>)
       * @param parser {@link ConfigParser} to use for parsing
       */
      static void parseWebResources(Catalog catalog, ServletContext context,
                                    String resources,
                                    ConfigParser parser) {
  
          String path = null;
          try {
              while (true) {
                  int comma = resources.indexOf(",");
                  if (comma < 0) {
                      path = resources.trim();
                      resources = "";
                  } else {
                      path = resources.substring(0, comma);
                      resources = resources.substring(comma + 1);
                  }
                  if (path.length() < 1) {
                      break;
                  }
                  URL url = context.getResource(path);
                  if (url == null) {
                      throw new IllegalStateException
                          ("Missing chain config resource '" + path + "'");
                  }
                  if (log.isDebugEnabled()) {
                      log.debug("Loading chain config resource '" + path + "'");
                  }
                  parser.parse(catalog, url);
              }
          } catch (Exception e) {
              throw new RuntimeException
                  ("Exception parsing chain config resource '" + path + "': " +
                   e.getMessage());
          }
  
      }
  
  
  }
  
  
  

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


Mime
View raw message