dobbs       02/03/12 12:20:00

  Modified:    src/java/org/apache/stratum/component ComponentLoader.java
               src/test/org/apache/stratum/component
                        TestComponentLoader.java
  Added:       src/java/org/apache/stratum/component
                        ComponentDescriptor.java
  Log:
  o refactoring to Introduce Parameter Object in
    ComponentLoader.loadComponenet().  This should enable the use of a
    better mock object for testing.
  
  o Added Martin as an author of ComponentLoader, since its his code I'm
    refactoring to create this thing.
  
  Revision  Changes    Path
  1.2       +58 -45    
jakarta-turbine-stratum/src/java/org/apache/stratum/component/ComponentLoader.java
  
  Index: ComponentLoader.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-stratum/src/java/org/apache/stratum/component/ComponentLoader.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ComponentLoader.java      12 Mar 2002 15:40:04 -0000      1.1
  +++ ComponentLoader.java      12 Mar 2002 20:19:59 -0000      1.2
  @@ -61,10 +61,12 @@
   import org.apache.stratum.configuration.PropertiesConfiguration;
   import org.apache.stratum.lifecycle.Configurable;
   import org.apache.stratum.lifecycle.Initializable;
  +import org.apache.stratum.component.ComponentDescriptor;
   
   /**
    * @author <a href="mailto:eric NOSPAM dobbse.net">Eric Dobbs</a>
  - * @version $Id: ComponentLoader.java,v 1.1 2002/03/12 15:40:04 dobbs Exp $
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Martin Poeschl</a>
  + * @version $Id: ComponentLoader.java,v 1.2 2002/03/12 20:19:59 dobbs Exp $
    */
   public class ComponentLoader
   {
  @@ -89,8 +91,16 @@
       }
   
       /**
  -     * Load all the components listed in the ComponentLoader's configuration.
  -     * Log any errors, but throw no exceptions if components cannot be loaded.
  +     * <p>Load all the components listed in the ComponentLoader's configuration.
  +     * Log any errors, but throw no exceptions if components cannot be loaded.</p>
  +     *
  +     * Configuration notes:<br/>
  +     * Components are identified in the properties file as follows:<br/>
  +     * <code>
  +     * component.name=NAME
  +     * component.NAME.classname = com.mycompany.components.SomeComponent
  +     * component.NAME.config    = path/to/SomeComponent.properties
  +     * </code>
        */
       public void load()
       {
  @@ -104,79 +114,82 @@
               componentClassName = getComponentClassname(componentName);
               componentConfig    = getComponentConfigFile(componentName);
               
  -            log.info("loading component " + componentName + " - class: "
  -                    + componentClassName + " - config: " + componentConfig);
  -        }
  -    }
  +            log.info("loading component: name=" + componentName + " class="
  +                    + componentClassName + " config=" + componentConfig);
   
  -    /**
  -     * <p>Get the component's classname as defined in the ComponentLoader
  -     * configuration.</p>
  -     *
  -     * <p>Example property:<br/>
  -     * component.NAME.classname=com.mycompany.components.MyComponent</p>
  -     *
  -     * @param name the String NAME of the component in the classfile
  -     */
  -    private String getComponentClassname(String name)
  -    {
  -        return configuration.getString(COMPONENT + '.' + name + '.' + CLASSNAME);
  -    }
  -
  -    /**
  -     * <p>Get the component's config file as defined in the ComponentLoader
  -     * configuration.</p>
  -     *
  -     * <p>Example property:<br/>
  -     * component.NAME.config=/path/to/your/config</p>
  -     *
  -     * @param name the String NAME of the component in the classfile
  -     */
  -    private String getComponentConfigFile(String name)
  -    {
  -        return configuration.getString(COMPONENT + '.' + name + '.' + CONFIG);
  +            ComponentDescriptor cd = new ComponentDescriptor(componentClassName,
  +                                                             componentConfig);
  +            loadComponent(cd);
  +        }
       }
   
       /**
        * load the given component, configure it with the given config
        * file, and initialize it.
        *
  -     * @param className the String class name of the component to load
  -     * @param configFile the String path name of the component's
  -     * config file */
  -    public void loadComponent(String className, String configFile)
  +     * @param cd the ComponentDescriptor of the component to load
  +     */
  +    public void loadComponent(ComponentDescriptor cd)
       {
           if (log.isDebugEnabled())
           {
  -            log.debug("attempting to load '" + className
  -                      + "' with the config file '" + configFile);
  +            log.debug("attempting to load '" + cd.getClassname()
  +                      + "' with the config file '" + cd.getConfigfile());
           }
           
           try
           {
  -            Object component = Class.forName(className).newInstance();
  +            Object component = cd.newInstance();
   
               // configure component using the given config file
               ((Configurable) component)
  -                .configure(new PropertiesConfiguration(configFile));
  +                .configure(new PropertiesConfiguration(cd.getConfigfile()));
   
               // initialize component
               ((Initializable) component).initialize();
   
               if (log.isDebugEnabled())
               {
  -                log.debug("good news! " + className
  +                log.debug("good news! " + cd.getClassname()
                             + " successfully configured and initialized");
               }
           }
           catch (IOException ioe)
           {
  -            log.error(className + " could not be configured with file '"
  -                      + configFile + "'.", ioe);
  +            log.error(cd.getClassname() + " could not be configured with file '"
  +                      + cd.getConfigfile() + "'.", ioe);
           }
           catch (Exception e)
           {
  -            log.error(className + " could not be initialized!", e);
  +            log.error(cd.getClassname() + " could not be initialized!", e);
           }
  +    }
  +
  +    /**
  +     * <p>Get the component's classname as defined in the ComponentLoader
  +     * configuration.</p>
  +     *
  +     * <p>Example property:<br/>
  +     * component.NAME.classname=com.mycompany.components.MyComponent</p>
  +     *
  +     * @param name the String NAME of the component in the classfile
  +     */
  +    private String getComponentClassname(String name)
  +    {
  +        return configuration.getString(COMPONENT + '.' + name + '.' + CLASSNAME);
  +    }
  +
  +    /**
  +     * <p>Get the component's config file as defined in the ComponentLoader
  +     * configuration.</p>
  +     *
  +     * <p>Example property:<br/>
  +     * component.NAME.config=path/to/your/config</p>
  +     *
  +     * @param name the String NAME of the component in the classfile
  +     */
  +    private String getComponentConfigFile(String name)
  +    {
  +        return configuration.getString(COMPONENT + '.' + name + '.' + CONFIG);
       }
   }
  
  
  
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/component/ComponentDescriptor.java
  
  Index: ComponentDescriptor.java
  ===================================================================
  package org.apache.stratum.component;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2002 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 acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * 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/>.
   */
  
  /**
   * @author <a href="mailto:eric NOSPAM dobbse.net">Eric Dobbs</a>
   * @version $Id: ComponentDescriptor.java,v 1.1 2002/03/12 20:19:59 dobbs Exp $
   */
  public class ComponentDescriptor
  {
      private String classname, configfile;
      
      //FIXME: throw exception if classname is not an instance of
      //Configurable and Initializable
      public ComponentDescriptor(String classname, String configfile)
      {
          this.classname  = classname;
          this.configfile = configfile;
      }
  
      public String getClassname()
      {
          return classname;
      }
  
      public String getConfigfile()
      {
          return configfile;
      }
  
      public Object newInstance()
          throws ClassNotFoundException,InstantiationException,IllegalAccessException
      {
          return Class.forName(classname).newInstance();
      }
  }
  
  
  
  1.2       +12 -3     
jakarta-turbine-stratum/src/test/org/apache/stratum/component/TestComponentLoader.java
  
  Index: TestComponentLoader.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-stratum/src/test/org/apache/stratum/component/TestComponentLoader.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestComponentLoader.java  12 Mar 2002 15:40:04 -0000      1.1
  +++ TestComponentLoader.java  12 Mar 2002 20:20:00 -0000      1.2
  @@ -62,6 +62,7 @@
   import org.apache.stratum.configuration.Configuration;
   import org.apache.stratum.configuration.PropertiesConfiguration;
   import org.apache.stratum.component.ComponentLoader;
  +import org.apache.stratum.component.ComponentDescriptor;
   
   public class TestComponentLoader extends TestCase
   {
  @@ -105,14 +106,22 @@
           String badClassName   = "org.apache.stratum.component.MissingMockComponent";
           String badConfigFile  = "bin/classes/MissingTestComponentLoader.properties";
   
  +        ComponentDescriptor goodCd = new ComponentDescriptor(goodClassName,
  +                                                             goodConfigFile);
  +        ComponentDescriptor badCd1 = new ComponentDescriptor(badClassName,
  +                                                             goodConfigFile);
  +        ComponentDescriptor badCd2 = new ComponentDescriptor(goodClassName,
  +                                                             badConfigFile);
  +        
  +
           // test loading with a mock component
  -        cl.loadComponent(goodClassName,goodConfigFile);
  +        cl.loadComponent(goodCd);
   
           // test that loadComponent() catches the IOException
  -        cl.loadComponent(badClassName,goodConfigFile);
  +        cl.loadComponent(badCd1);
   
           // test that loadComponent() catches the ClassNotFoundException
  -        cl.loadComponent(goodClassName,badConfigFile);
  +        cl.loadComponent(badCd2);
       }
   
       public void testLoad()
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to