You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2003/08/13 12:55:51 UTC

cvs commit: incubator-geronimo/modules/twiddle/src/test/org/apache/geronimo/twiddle/config ConfigurationTest.java

jdillon     2003/08/13 03:55:51

  Modified:    modules/twiddle project.xml
               modules/twiddle/src/etc twiddle-configuration-binding.xml
               modules/twiddle/src/java/org/apache/geronimo/twiddle
                        Twiddle.java
               modules/twiddle/src/test/org/apache/geronimo/twiddle/config
                        ConfigurationTest.java
  Added:       modules/twiddle/src/java/org/apache/geronimo/twiddle/config
                        ConfigurationException.java Configurator.java
  Log:
   o Using Config suffix on some more configuration elements
   o Moved the burden of Twiddle config to Configurator
  
  Revision  Changes    Path
  1.2       +3 -1      incubator-geronimo/modules/twiddle/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/project.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- project.xml	12 Aug 2003 18:18:45 -0000	1.1
  +++ project.xml	13 Aug 2003 10:55:51 -0000	1.2
  @@ -72,11 +72,13 @@
         <url>http://jakarta.apache.org/commons/logging.html</url>
       </dependency>
       
  +    <!-- Will need this soon, but not right now
       <dependency>
         <id>commons-jexl</id>
         <version>SNAPSHOT</version>
         <url>http://jakarta.apache.org/commons/jexl.html</url>
       </dependency>
  +    -->
       
       <dependency>
         <id>commons-cli</id>
  
  
  
  1.2       +17 -1     incubator-geronimo/modules/twiddle/src/etc/twiddle-configuration-binding.xml
  
  Index: twiddle-configuration-binding.xml
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/etc/twiddle-configuration-binding.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- twiddle-configuration-binding.xml	12 Aug 2003 18:18:45 -0000	1.1
  +++ twiddle-configuration-binding.xml	13 Aug 2003 10:55:51 -0000	1.2
  @@ -32,8 +32,24 @@
     
     <!-- Custom element binding -->
     
  +  <elementBinding name="properties">
  +    <java-class name="PropertiesConfig"/>
  +  </elementBinding>
  +  
  +  <elementBinding name="property">
  +    <java-class name="PropertyConfig"/>
  +  </elementBinding>
  +  
  +  <elementBinding name="includes">
  +    <java-class name="IncludesConfig"/>
  +  </elementBinding>
  +  
     <elementBinding name="environment">
       <java-class name="EnvironmentConfig"/>
  +  </elementBinding>
  +  
  +  <elementBinding name="commands">
  +    <java-class name="CommandsConfig"/>
     </elementBinding>
     
     <elementBinding name="command">
  
  
  
  1.3       +57 -54    incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/Twiddle.java
  
  Index: Twiddle.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/Twiddle.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Twiddle.java	13 Aug 2003 09:12:14 -0000	1.2
  +++ Twiddle.java	13 Aug 2003 10:55:51 -0000	1.3
  @@ -64,83 +64,76 @@
   import org.apache.geronimo.common.NullArgumentException;
   
   import org.apache.geronimo.twiddle.config.Configuration;
  -import org.apache.geronimo.twiddle.config.ConfigurationReader;
  -import org.apache.geronimo.twiddle.config.Properties;
  -import org.apache.geronimo.twiddle.config.Property;
  -import org.apache.geronimo.twiddle.config.Commands;
  -import org.apache.geronimo.twiddle.config.CommandConfig;
  +import org.apache.geronimo.twiddle.config.Configurator;
   
   import org.apache.geronimo.twiddle.command.Command;
  -import org.apache.geronimo.twiddle.command.CommandInfo;
   import org.apache.geronimo.twiddle.command.CommandContainer;
   import org.apache.geronimo.twiddle.command.CommandExecutor;
  -import org.apache.geronimo.twiddle.command.CommandContext;
   import org.apache.geronimo.twiddle.command.CommandException;
  -import org.apache.geronimo.twiddle.command.CommandNotFoundException;
   
   /**
  - * Twiddle is a command processor.
  + * <em>Twiddle</em> is a command processor.
  + *
  + * <p><em>Twiddle</em> is a facade over the various components of the 
  + *    command processor, it serves only to facilitate their operation and to
  + *    provide a simple API to execute commands (hence facade).
    *
    * @version <tt>$Revision$ $Date$</tt>
    * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
    */
   public class Twiddle
   {
  +    /** The command container. */
       protected CommandContainer container;
  +    
  +    /** The command executor. */
       protected CommandExecutor executor;
       
  +    /**
  +     * Construct a <code>Twiddle</code> command processor.
  +     */
       public Twiddle()
       {
           container = new CommandContainer();
           executor = new CommandExecutor(container);
       }
       
  +    /**
  +     * Get the command container.
  +     *
  +     * @return The command container.
  +     */
  +    public CommandContainer getCommandContainer()
  +    {
  +        return container;
  +    }
  +    
  +    /**
  +     * Get the command executor.
  +     *
  +     * @return The command executor.
  +     */
  +    public CommandExecutor getCommandExecutor()
  +    {
  +        return executor;
  +    }
  +    
  +    
  +    /////////////////////////////////////////////////////////////////////////
  +    //                             Configuration                           //
  +    /////////////////////////////////////////////////////////////////////////
  +    
  +    /**
  +     * Configure <em>Twiddle</em> from the given configuration metadata.
  +     *
  +     * @param config    Configuration metadata.
  +     *
  +     * @throws CommandException     Failed to configure.
  +     */
       public void configure(final Configuration config) throws CommandException
       {
  -        if (config == null) {
  -            throw new NullArgumentException("config");
  -        }
  -        
  -        // Process properties
  -        if (config.getProperties() != null) {
  -            Property[] props = config.getProperties().getProperty();
  -            for (int i=0; i<props.length; i++) {
  -                String name = props[i].getName().trim();
  -                String value = props[i].getContent();
  -                //
  -                // TODO: Handle property value evaluation
  -                //
  -                System.setProperty(name, value);
  -            }
  -        }
  -        
  -        // Process includes
  -        try {
  -            if (config.getIncludes() != null) {
  -                String[] includes = config.getIncludes().getInclude();
  -                ConfigurationReader reader = new ConfigurationReader();
  -                for (int i=0; i<includes.length; i++) {
  -                    URL configURL = new URL(includes[i]);
  -                    //
  -                    // TODO: Need to properly resolve this URL
  -                    //
  -                    Configuration iconfig = reader.read(configURL);
  -                    this.configure(iconfig);
  -                }
  -            }
  -        }
  -        catch (Exception e) {
  -            throw new CommandException("Failed to process includes", e);
  -        }
  -        
  -        // Process commands
  -        CommandConfig[] commands = config.getCommands().getCommandConfig();
  -        if (commands != null) {
  -            for (int i=0; i<commands.length; i++) {
  -                CommandInfo info = new CommandInfo(commands[i]);
  -                container.addCommandInfo(info);
  -            }
  -        }
  +        Configurator c = new Configurator(this);
  +        c.configure(config);
       }
       
       
  @@ -148,6 +141,16 @@
       //                          Command Execution                          //
       /////////////////////////////////////////////////////////////////////////
       
  +    /**
  +     * Execute a command line.
  +     *
  +     * <p>The first argument is assumed to be the command name.
  +     *
  +     * @param args  The command line.
  +     * @return      The command status code.
  +     *
  +     * @throws Exception    An unhandled command failure has occured.
  +     */
       public int execute(final String[] args) throws Exception
       {
           return executor.execute(args);
  
  
  
  1.1                  incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/config/ConfigurationException.java
  
  Index: ConfigurationException.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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 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 Geronimo" 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",
   *    "Apache Geronimo", 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/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.twiddle.config;
  
  import org.apache.geronimo.twiddle.command.CommandException;
  
  /**
   * A configuration exception.
   *
   * @version <code>$Id: ConfigurationException.java,v 1.1 2003/08/13 10:55:51 jdillon Exp $</code>
   * @author  <a href="mailto:jason@planet57.com">Jason Dillon</a>
   */
  public class ConfigurationException
      extends CommandException
  {
      /**
       * Construct a <code>ConfigurationException</code> with the specified detail 
       * message.
       *
       * @param msg  Detail message.
       */
      public ConfigurationException(String msg) {
          super(msg);
      }
      
      /**
       * Construct a <code>ConfigurationException</code> with the specified detail 
       * message and nested <code>Throwable</code>.
       *
       * @param msg     Detail message.
       * @param nested  Nested <code>Throwable</code>.
       */
      public ConfigurationException(String msg, Throwable nested) {
          super(msg, nested);
      }
      
      /**
       * Construct a <code>ConfigurationException</code> with the specified
       * nested <code>Throwable</code>.
       *
       * @param nested  Nested <code>Throwable</code>.
       */
      public ConfigurationException(Throwable nested) {
          super(nested);
      }
      
      /**
       * Construct a <code>ConfigurationException</code> with no detail.
       */
      public ConfigurationException() {
          super();
      }
  }
  
  
  
  1.1                  incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/config/Configurator.java
  
  Index: Configurator.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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 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 Geronimo" 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",
   *    "Apache Geronimo", 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/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.twiddle.config;
  
  import java.net.URL;
  
  import org.apache.geronimo.common.NullArgumentException;
  
  import org.apache.geronimo.twiddle.Twiddle;
  
  import org.apache.geronimo.twiddle.command.CommandContainer;
  import org.apache.geronimo.twiddle.command.CommandInfo;
  import org.apache.geronimo.twiddle.command.CommandException;
  
  /**
   * Handles the details of Twiddle configuration.
   *
   * @version <code>$Id: Configurator.java,v 1.1 2003/08/13 10:55:51 jdillon Exp $</code>
   * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
   */
  public class Configurator
  {
      protected Twiddle twiddle;
      
      public Configurator(final Twiddle twiddle)
      {
          if (twiddle == null) {
              throw new NullArgumentException("twiddle");
          }
          
          this.twiddle = twiddle;
      }
      
      public Twiddle getTwiddle()
      {
          return twiddle;
      }
      
      public void configure(final Configuration config) throws CommandException
      {
          if (config == null) {
              throw new NullArgumentException("config");
          }
          
          configureProperties(config.getPropertiesConfig());
          configureIncludes(config.getIncludesConfig());
          configureCommands(config.getCommandsConfig());
      }
      
      protected void configureProperties(final PropertiesConfig config)
      {
          if (config == null) return;
          
          PropertyConfig[] props = config.getPropertyConfig();
          for (int i=0; i<props.length; i++) {
              if (props[i] == null) {
                  throw new NullArgumentException("PropertyConfig", i);
              }
              
              String name = props[i].getName().trim();
              String value = props[i].getContent();
              //
              // TODO: Handle property value evaluation
              //
              System.setProperty(name, value);
          }
      }
      
      protected void configureIncludes(final IncludesConfig config) throws CommandException
      {
          if (config == null) return;
          
          String[] includes = config.getInclude();
          if (includes != null && includes.length != 0) {
              ConfigurationReader reader = new ConfigurationReader();
              
              for (int i=0; i<includes.length; i++) {
                  if (includes[i] == null) {
                      throw new NullArgumentException("Includes", i);
                  }
                  
                  try {
                      URL configURL = new URL(includes[i]);
                      //
                      // TODO: Need to properly resolve this URL
                      //
                      Configuration iconfig = reader.read(configURL);
                      this.configure(iconfig);
                  }
                  catch (Exception e) {
                      throw new ConfigurationException("Failed to process include: " + includes[i], e);
                  }
              }
          }
      }
      
      protected void configureCommands(final CommandsConfig config) throws CommandException
      {
          if (config == null) return;
          
          CommandConfig[] commands = config.getCommandConfig();
          if (commands != null) {
              CommandContainer container = twiddle.getCommandContainer();
              
              for (int i=0; i<commands.length; i++) {
                  if (commands[i] == null) {
                      throw new NullArgumentException("CommandConfig", i);
                  }
                  
                  CommandInfo info = new CommandInfo(commands[i]);
                  container.addCommandInfo(info);
              }
          }
      }
  }
  
  
  
  1.2       +4 -4      incubator-geronimo/modules/twiddle/src/test/org/apache/geronimo/twiddle/config/ConfigurationTest.java
  
  Index: ConfigurationTest.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/test/org/apache/geronimo/twiddle/config/ConfigurationTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ConfigurationTest.java	13 Aug 2003 08:35:33 -0000	1.1
  +++ ConfigurationTest.java	13 Aug 2003 10:55:51 -0000	1.2
  @@ -153,10 +153,10 @@
       {
           Configuration config = read("test1.xml");
           
  -        Properties p = config.getProperties();
  +        PropertiesConfig p = config.getPropertiesConfig();
           Assert.assertNotNull(p);
           
  -        Property[] props = p.getProperty();
  +        PropertyConfig[] props = p.getPropertyConfig();
           Assert.assertEquals(2, props.length);
           
           Assert.assertEquals("property1", props[0].getName());
  @@ -183,7 +183,7 @@
       {
           Configuration config = read("test1.xml");
           
  -        Includes i = config.getIncludes();
  +        IncludesConfig i = config.getIncludesConfig();
           Assert.assertNotNull(i);
           
           String[] incs = i.getInclude();