geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject cvs commit: incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/config ConfigurationReader.java
Date Wed, 13 Aug 2003 08:32:09 GMT
jdillon     2003/08/13 01:32:09

  Modified:    modules/twiddle/src/java/org/apache/geronimo/twiddle/config
                        ConfigurationReader.java
  Added:       modules/twiddle/src/java/org/apache/geronimo/twiddle
                        Twiddle.java
               modules/twiddle/src/java/org/apache/geronimo/twiddle/cli
                        Main.java
               modules/twiddle/src/java/org/apache/geronimo/twiddle/command
                        AbstractCommand.java Command.java
                        CommandContainer.java CommandContext.java
                        CommandException.java CommandExecutor.java
                        CommandFactory.java CommandInfo.java
                        CommandNotFoundException.java
               modules/twiddle/src/java/org/apache/geronimo/twiddle/commands
                        ExitCommand.java
  Log:
   o Initial commit of Twiddle, can only support a root commands at the moment
     and does not have the interactive console hooked up, but will get to those
     soon.
  
  Revision  Changes    Path
  1.1                  incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/Twiddle.java
  
  Index: Twiddle.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;
  
  import java.net.URL;
  
  import java.io.StringWriter;
  import java.io.PrintWriter;
  
  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.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.
   *
   * @version <code>$Id: Twiddle.java,v 1.1 2003/08/13 08:32:09 jdillon Exp $</code>
   * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
   */
  public class Twiddle
  {
      protected CommandContainer container;
      protected CommandExecutor executor;
      
      public Twiddle()
      {
          container = new CommandContainer();
          executor = new CommandExecutor(container);
      }
      
      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);
              }
          }
      }
      
      
      /////////////////////////////////////////////////////////////////////////
      //                          Command Execution                          //
      /////////////////////////////////////////////////////////////////////////
      
      public int execute(final String[] args) throws Exception
      {
          return executor.execute(args);
      }
      
      
      /////////////////////////////////////////////////////////////////////////
      //                                 Misc                                //
      /////////////////////////////////////////////////////////////////////////
      
      /**
       * Get the fancy <em>Twiddle</em> banner text.
       *
       * @return The fancy <em>Twiddle</em> banner text.
       */
      public static String getBanner()
      {
          StringWriter writer = new StringWriter();
          PrintWriter out = new PrintWriter(writer);
          
          out.println(" _____          _     _     _ _");
          out.println("|_   _|_      _(_) __| | __| | | ___");
          out.println("  | | \\ \\ /\\ / / |/ _` |/ _` | |/ _ \\");
          out.println("  | |  \\ V  V /| | (_| | (_| | |  __/");
          out.println("  |_|   \\_/\\_/ |_|\\__,_|\\__,_|_|\\___|");
          out.flush();
          
          return writer.toString();
      }
  }
  
  
  
  1.1                  incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/cli/Main.java
  
  Index: Main.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.cli;
  
  import java.io.File;
  import java.net.URL;
  import java.net.URLDecoder;
  
  import org.apache.commons.cli.Options;
  import org.apache.commons.cli.Option;
  import org.apache.commons.cli.CommandLine;
  import org.apache.commons.cli.CommandLineParser;
  import org.apache.commons.cli.PosixParser;
  import org.apache.commons.cli.OptionBuilder;
  import org.apache.commons.cli.ParseException;
  import org.apache.commons.cli.HelpFormatter;
  
  import org.apache.geronimo.twiddle.Twiddle;
  
  import org.apache.geronimo.twiddle.config.Configuration;
  import org.apache.geronimo.twiddle.config.ConfigurationReader;
  
  /**
   * Command-line interface to <code>Twiddle</code>.
   *
   * @version <code>$Id: Main.java,v 1.1 2003/08/13 08:32:09 jdillon Exp $</code>
   * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
   */
  public class Main
  {
      /** Platform dependent line separator. */
      private static final String LINE_SEPARATOR = System.getProperty("line.separator");
      
      private String filename = "etc/twiddle.conf";
      
      public String[] processCommandLine(final String[] args) throws Exception
      {
          // create the Options
          Options options = new Options();
          options.addOption(OptionBuilder.withLongOpt("help")
                                         .withDescription("Display this help message")
                                         .create('h'));
                                         
          options.addOption(OptionBuilder.withLongOpt("define")
                                         .withDescription("Define a system property")
                                         .hasArg()
                                         .create('D'));
                                         
          options.addOption(OptionBuilder.withLongOpt("file")
                                         .withDescription("Read a specific configuration file")
                                         .hasArg()
                                         .create('f'));
          
          // create the command line parser
          CommandLineParser parser = new PosixParser();
          
          // parse the command line arguments
          CommandLine line = parser.parse(options, args, true);
          
          // Dump parsed options
          System.out.println("Twiddle options:");
          Option[] opts = line.getOptions();
          for (int i=0; i<opts.length; i++) {
              System.out.println(opts[i]);
          }
          
          // Display command-line help and exit
          if (line.hasOption('h')) {
              System.out.println(Twiddle.getBanner());
              HelpFormatter formatter = new HelpFormatter();
              formatter.printHelp(
                  "twiddle [options] (<command> [options] [arguments])*",
                  LINE_SEPARATOR + "Options:",
                  options,
                  LINE_SEPARATOR
              );
              System.out.println();
              System.exit(0);
          }
          
          // Set system properties
          if (line.hasOption('D')) {
              String[] values = line.getOptionValues('D');
              
              for (int i=0; i<values.length; i++) {
                  String name, value;
                  int j = values[i].indexOf("=");
                  
                  if (j == -1) {
                      name = values[i];
                      value = "true";
                  }
                  else {
                      name = values[i].substring(0, j);
                      value = values[i].substring(j + 1, values[i].length());
                  }
                  
                  System.setProperty(name.trim(), value);
              }
          }
          
          if (line.hasOption('f')) {
              filename = line.getOptionValue('f');
          }
          
          return line.getArgs();
      }
      
      public void boot(String[] args) throws Exception
      {
          // Process command-line options
          args = processCommandLine(args);
          
          // Determine what our home directory is
          String temp = System.getProperty("twiddle.home");
          if (temp == null) {
              String path = Main.class.getProtectionDomain().getCodeSource().getLocation().getFile();
              path = URLDecoder.decode(path);
              temp = new File(path).getParentFile().getParentFile().getCanonicalPath();
          }
          File homeDir = new File(temp);
          URL homeURL = homeDir.toURL();
          URL configURL = new URL(homeURL, filename);
          
          System.out.println("home directory: " + homeDir);
          System.out.println("home url: " + homeURL);
          System.out.println("configuration url: " + configURL);
          
          ConfigurationReader reader = new ConfigurationReader();
          Configuration config = reader.read(configURL);
          
          Twiddle twiddle = new Twiddle();
          twiddle.configure(config);
          
          if (args.length != 0) {
              int result = twiddle.execute(args);
              System.exit(result);
          }
          else {
              //
              // TODO: Start the interactive console
              //
              // twiddle.run();
              //
          }
      }
      
      public static void main(final String[] args) throws Exception
      {
          Main main = new Main();
          main.boot(args);
      }
  }
  
  
  
  1.1                  incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/AbstractCommand.java
  
  Index: AbstractCommand.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.command;
  
  import org.apache.geronimo.common.CloneableObject;
  import org.apache.geronimo.common.NullArgumentException;
  
  /**
   * An abstract implementation of a <code>Command</code>.
   *
   * <p>Sub-classes only need to implement {@link Command#execute} and
   *    can access the command context from {@link #getCommandContext}.
   *
   * @version <code>$Id: AbstractCommand.java,v 1.1 2003/08/13 08:32:09 jdillon Exp $</code>
   * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
   */
  public abstract class AbstractCommand
      extends CloneableObject
      implements Command
  {
      /** Platform dependent line separator. */
      protected static final String LINE_SEPARATOR = System.getProperty("line.separator");
      
      /** The command information. */
      private CommandInfo info;
      
      /** The command context. */
      private CommandContext context;
      
      public void setCommandInfo(final CommandInfo info)
      {
          if (info == null) {
              throw new NullArgumentException("info");
          }
          
          this.info = info;
      }
      
      public CommandInfo getCommandInfo()
      {
          if (info == null) {
              throw new IllegalStateException("Command information was not set");
          }
          
          return info;
      }
      
      public void setCommandContext(final CommandContext ctx)
      {
          if (ctx == null) {
              throw new NullArgumentException("ctx");
          }
          
          this.context = ctx;
      }
      
      public void unsetCommandContext()
      {
          context = null;
      }
      
      /**
       * Provides sub-classes with access to the command context.
       *
       * @return The command context.
       *
       * @throws IllegalStateException    Command context has not been set or was unset.
       */
      protected CommandContext getCommandContext()
      {
          if (context == null) {
              throw new IllegalStateException("Command context has not been set or was unset");
          }
          
          return context;
      }
  }
  
  
  
  1.1                  incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/Command.java
  
  Index: Command.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.command;
  
  /**
   * An abstraction of a command.
   *
   * <p>Commands follow the prototype pattern using the {@link #clone} method
   *    to create new instances from the prototype.
   *
   * @version <code>$Id: Command.java,v 1.1 2003/08/13 08:32:09 jdillon Exp $</code>
   * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
   */
  public interface Command
      extends Cloneable
  {
      /** Standard command success status code. */
      int SUCCESS = 0;
      
      /** Standard command failure status code. */
      int FAILURE = -1;
      
      /**
       * Set the information about the command.
       *
       * @param info  Information about the command.
       */
      void setCommandInfo(CommandInfo info);
      
      /**
       * Get information about the command.
       *
       * @return Information about the command.
       *
       * @throws IllegalStateException    Command information was not set.
       */
      CommandInfo getCommandInfo();
      
      /**
       * Set the context for the command.
       *
       * @param ctx   The context for the command.
       */
      void setCommandContext(CommandContext ctx);
      
      /**
       * Unset the context for the command.
       */
      void unsetCommandContext();
      
      /**
       * Execute the command.
       *
       * @param args  The arguments for the command.
       * @return      The status code for the command.
       *
       * @throws Exception    An unexpected failure has occured.
       */
      int execute(String[] args) throws Exception;
      
      /**
       * Clone this command prototype.
       *
       * @return A cloned instance of the command prototype.
       */
      Object clone();
  }
  
  
  
  1.1                  incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/CommandContainer.java
  
  Index: CommandContainer.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.command;
  
  import java.util.Map;
  import java.util.HashMap;
  import java.util.Iterator;
  
  import org.apache.geronimo.common.NullArgumentException;
  
  /**
   * ???
   *
   * @version <code>$Id: CommandContainer.java,v 1.1 2003/08/13 08:32:09 jdillon Exp $</code>
   * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
   */
  public class CommandContainer
  {
      protected Map descriptors;
      
      public CommandContainer()
      {
          descriptors = new HashMap();
      }
      
      public CommandInfo addCommandInfo(final CommandInfo info)
      {
          if (info == null) {
              throw new NullArgumentException("info");
          }
          
          return (CommandInfo)descriptors.put(info.getName(), info);
      }
      
      public CommandInfo getCommandInfo(final String name)
      {
          if (name == null) {
              throw new NullArgumentException("name");
          }
          
          return (CommandInfo)descriptors.get(name);
      }
      
      public boolean containsCommandInfo(final String name)
      {
          if (name == null) {
              throw new NullArgumentException("name");
          }
          
          return descriptors.containsKey(name);
      }
      
      public CommandInfo removeCommandInfo(final String name)
      {
          if (name == null) {
              throw new NullArgumentException("name");
          }
          
          return (CommandInfo)descriptors.remove(name);
      }
      
      public Iterator descriptors()
      {
          return descriptors.values().iterator();
      }
      
      public int size()
      {
          return descriptors.size();
      }
      
      public Command getCommand(final String name) throws CommandException
      {
          CommandInfo info = getCommandInfo(name);
          if (info == null) {
              return null;
          }
          
          Command prototype = info.getPrototype();
          
          return (Command)prototype.clone();
      }
      
      public Command findCommand(final String name) throws CommandException
      {
          Command command = getCommand(name);
          
          if (command == null) {
              throw new CommandNotFoundException(name);
          }
          
          return command;
      }
  }
  
  
  
  1.1                  incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/CommandContext.java
  
  Index: CommandContext.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.command;
  
  import java.io.PrintWriter;
  import java.io.Reader;
  
  /**
   * Provides a command with details on its environment.
   *
   * @version <code>$Id: CommandContext.java,v 1.1 2003/08/13 08:32:09 jdillon Exp $</code>
   * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
   */
  public interface CommandContext
  {
      /////////////////////////////////////////////////////////////////////////
      //                          Environemnt Access                         //
      /////////////////////////////////////////////////////////////////////////
      
      /**
       * Get an enviornment attribute.
       *
       * @param name  The name of the attribute.
       * @return      The attribute value, or null if the attribute was not set.
       */
      Object get(String name);
      
      /**
       * Set an environment attribute.
       *
       * @param name      The name of the attribute.
       * @param value     The value of the attribute.
       */
      Object set(String name, Object value);
      
      /**
       * Unset an enviornment attribute.
       *
       * @param name  The name of the attribute.
       * @return      The previous value of the attribute, or null if there was none.
       */
      Object unset(String name);
      
      /**
       * Check if an attribute is set.
       *
       * @param name  The name of the attribute.
       * @return      True if the attribute is set, else false.
       */
      boolean isSet(String name);
      
      
      /////////////////////////////////////////////////////////////////////////
      //                          Container Access                           //
      /////////////////////////////////////////////////////////////////////////
      
      CommandContainer getContainer();
      
      
      /////////////////////////////////////////////////////////////////////////
      //                         Input/Output Access                         //
      /////////////////////////////////////////////////////////////////////////
      
      /**
       * Get the output writer for the command.
       *
       * @return  The output writer.
       */
      PrintWriter getWriter();
      
      /**
       * Get the error output writer for the command.
       *
       * @return  The error output writer.
       */
      PrintWriter getErrorWriter();
      
      /**
       * Get the input reader for the command.
       *
       * @return  The input reader.
       */
      Reader getReader();
  }
  
  
  
  1.1                  incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/CommandException.java
  
  Index: CommandException.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.command;
  
  import org.apache.geronimo.common.NestedException;
  
  /**
   * A command exception.
   *
   * @version <code>$Id: CommandException.java,v 1.1 2003/08/13 08:32:09 jdillon Exp $</code>
   * @author  <a href="mailto:jason@planet57.com">Jason Dillon</a>
   */
  public class CommandException
      extends NestedException
  {
      /**
       * Construct a <code>CommandException</code> with the specified detail 
       * message.
       *
       * @param msg  Detail message.
       */
      public CommandException(String msg) {
          super(msg);
      }
      
      /**
       * Construct a <code>CommandException</code> with the specified detail 
       * message and nested <code>Throwable</code>.
       *
       * @param msg     Detail message.
       * @param nested  Nested <code>Throwable</code>.
       */
      public CommandException(String msg, Throwable nested) {
          super(msg, nested);
      }
      
      /**
       * Construct a <code>CommandException</code> with the specified
       * nested <code>Throwable</code>.
       *
       * @param nested  Nested <code>Throwable</code>.
       */
      public CommandException(Throwable nested) {
          super(nested);
      }
      
      /**
       * Construct a <code>CommandException</code> with no detail.
       */
      public CommandException() {
          super();
      }
  }
  
  
  
  1.1                  incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/CommandExecutor.java
  
  Index: CommandExecutor.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.command;
  
  import org.apache.geronimo.common.NullArgumentException;
  import org.apache.geronimo.common.Strings;
  
  import org.apache.geronimo.twiddle.command.Command;
  import org.apache.geronimo.twiddle.command.CommandContainer;
  import org.apache.geronimo.twiddle.command.CommandContext;
  import org.apache.geronimo.twiddle.command.CommandException;
  import org.apache.geronimo.twiddle.command.CommandNotFoundException;
  
  /**
   * ???
   *
   * @version <code>$Id: CommandExecutor.java,v 1.1 2003/08/13 08:32:09 jdillon Exp $</code>
   * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
   */
  public class CommandExecutor
  {
      protected CommandContainer container;
      
      public CommandExecutor(final CommandContainer container)
      {
          if (container == null) {
              throw new NullArgumentException("container");
          }
          
          this.container = container;
      }
      
      public CommandContainer getCommandContainer()
      {
          return container;
      }
      
      public Command findCommand(final String name) throws CommandException
      {
          // name is checked by the container
          return container.findCommand(name);
      }
      
      
      /////////////////////////////////////////////////////////////////////////
      //                          Command Execution                          //
      /////////////////////////////////////////////////////////////////////////
      
      /**
       * Execute a command.
       *
       * @param command
       * @param args
       * @return
       *
       * @throws Exception
       */
      protected int execute(final Command command, final String[] args) throws Exception
      {
          // Set the context
          //
          // TODO
          //
          // command.setCommandContext(ctx);
          
          // Execute the command
          int result = Command.FAILURE;
          try {
              result = command.execute(args);
          }
          finally {
              command.unsetCommandContext();
          }
          
          return result;
      }
      
      /**
       * Execute a command from the given arguments.
       *
       * @param args
       * @return
       *
       * @throws Exception
       */
      public int execute(final String[] args) throws Exception
      {
          if (args == null) {
              throw new NullArgumentException("args");
          }
          if (args.length == 0) {
              throw new RuntimeException("Arguments are empty");
          }
          
          // Locate the command
          Command command = findCommand(args[0]);
          
          // Setup the arguments for the command (strip off command name/path)
          String[] cargs = new String[args.length - 1];
          System.arraycopy(args, 1, cargs, 0, args.length - 1);
          
          // Execute the command
          return execute(command, cargs);
      }
      
      /**
       * Execute a command from the given raw input.
       *
       * @param input
       * @return
       *
       * @throws Exception
       */
      public int execute(final String input) throws Exception
      {
          if (input == null) {
              throw new NullArgumentException("input");
          }
          
          String[] args = Strings.split(input, " ");
          return execute(args);
      }
      
      /**
       * Execute a command at the given path with the given arguments.
       *
       * @param path
       * @param args
       * @return
       *
       * @throws Exception
       */
      public int execute(final String path, final String[] args) throws Exception
      {
          // path is checked by findCommand
          if (args == null) {
              throw new NullArgumentException("args");
          }
          if (args.length == 0) {
              throw new RuntimeException("Arguments are empty");
          }
          
          // Locate the command
          Command command = findCommand(path);
          
          // Execute the command
          return execute(command, args);
      }
      
      /**
       * Execute a command at the given path with the given raw input as arguments
       *
       * @param path
       * @param input
       * @return
       *
       * @throws Exception
       */
      public int execute(final String path, final String input) throws Exception
      {
          if (input == null) {
              throw new NullArgumentException("input");
          }
          
          String[] args = Strings.split(input, " ");
          return execute(path, args);
      }
  }
  
  
  
  1.1                  incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/CommandFactory.java
  
  Index: CommandFactory.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.command;
  
  import java.io.IOException;
  
  import java.beans.Beans;
  import java.beans.BeanInfo;
  import java.beans.Introspector;
  import java.beans.PropertyDescriptor;
  
  import java.lang.reflect.Method;
  
  import org.apache.geronimo.common.NullArgumentException;
  
  import org.apache.geronimo.twiddle.config.CommandConfig;
  import org.apache.geronimo.twiddle.config.Attribute;
  
  /**
   * A factory for creating <code>Command</code> instances from
   * a <code>CommandConfig</code>.
   *
   * @version <code>$Id: CommandFactory.java,v 1.1 2003/08/13 08:32:09 jdillon Exp $</code>
   * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
   */
  public class CommandFactory
  {
      protected CommandConfig config;
      
      public CommandFactory(final CommandConfig config)
      {
          if (config == null) {
              throw new NullArgumentException("config");
          }
          
          this.config = config;
      }
      
      public CommandConfig getConfig()
      {
          return config;
      }
      
      protected Command doCreate() throws Exception
      {
          // Load the command instance
          ClassLoader cl = Thread.currentThread().getContextClassLoader();
          Command command = (Command)Beans.instantiate(cl, config.getClazz());
          
          // Apply attributes
          BeanInfo info = Introspector.getBeanInfo(command.getClass());
          Attribute[] attrs = config.getAttribute();
          if (attrs != null) {
              PropertyDescriptor[] descs = info.getPropertyDescriptors();
              
              for (int i=0; i<attrs.length; i++) {
                  String name = attrs[i].getName();
                  
                  for (int j=0; j<descs.length; j++) {
                      //
                      // TODO: Handle attribute types, for now they are all strings
                      //
                      
                      if (name.equals(descs[j].getName())) {
                          Object value = attrs[i].getContent();
                          Method method = descs[j].getWriteMethod();
                          method.invoke(command, new Object[] { value });
                          break;
                      }
                  }
              }
          }
          
          return command;
      }
      
      public Command create() throws CommandException
      {
          try {
              return doCreate();
          }
          catch (Exception e) {
              throw new CommandException("Failed to create command: " + config.getName(), e);
          }
      }
  }
  
  
  
  1.1                  incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/CommandInfo.java
  
  Index: CommandInfo.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.command;
  
  import org.apache.geronimo.common.NullArgumentException;
  
  import org.apache.geronimo.twiddle.config.CommandConfig;
  
  /**
   * Information about a command.
   *
   * @version <code>$Id: CommandInfo.java,v 1.1 2003/08/13 08:32:09 jdillon Exp $</code>
   * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
   */
  public class CommandInfo
  {
      /** The command configuration. */
      protected CommandConfig config;
      
      /** The command prototype. */
      protected Command prototype;
      
      /**
       * Construct a <code>CommandInfo</code> from the given config.
       *
       * @param config    The command configuration.
       */
      public CommandInfo(final CommandConfig config)
      {
          if (config == null) {
              throw new NullArgumentException("config");
          }
          
          this.config = config;
      }
      
      /**
       * Get the command configuration.
       *
       * @return The command configuration.
       */
      public CommandConfig getConfig()
      {
          return config;
      }
      
      /**
       * Create the command prototype.
       *
       * @return The command prototype.
       *
       * @throws CommandException     Failed to create prototype.
       */
      protected Command createPrototype() throws CommandException
      {
          CommandFactory factory = new CommandFactory(config);
          Command command = factory.create();
          command.setCommandInfo(this);
          
          return command;
      }
      
      /**
       * Get the command prototype.
       *
       * @return The command prototype.
       *
       * @throws CommandException     Failed to create prototype.
       */
      public Command getPrototype() throws CommandException
      {
          if (prototype == null) {
              prototype = createPrototype();
          }
          
          return prototype;
      }
      
      
      /////////////////////////////////////////////////////////////////////////
      //                               Helpers                               //
      /////////////////////////////////////////////////////////////////////////
      
      /**
       * Get the name of the command.
       *
       * @return The name of the command.
       */
      public String getName()
      {
          return config.getName();
      }
      
      /**
       * Get the description of the command.
       *
       * @return The description of the command.
       */
      public String getDescription()
      {
          return config.getDescription();
      }
      
      /**
       * Check if this command has a configured description.
       *
       * @return True if the command has a configured description.
       */
      public boolean hasDescription()
      {
          return getDescription() != null;
      }
  }
  
  
  
  1.1                  incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/CommandNotFoundException.java
  
  Index: CommandNotFoundException.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.command;
  
  /**
   * Thrown to indicate that an operation which requires a named
   * command has failed because the command was not found.
   *
   * @version <code>$Id: CommandNotFoundException.java,v 1.1 2003/08/13 08:32:09 jdillon Exp $</code>
   * @author  <a href="mailto:jason@planet57.com">Jason Dillon</a>
   */
  public class CommandNotFoundException
     extends CommandException
  {
      /**
       * Construct a <code>CommandNotFoundException</code>.
       */
      public CommandNotFoundException(String path) {
          this(path, "Command or path was not found");
      }
      
      /**
       * Construct a <code>CommandNotFoundException</code>.
       */
      public CommandNotFoundException(String path, String msg) {
          super(msg + ": " + path);
      }
  }
  
  
  
  1.1                  incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/commands/ExitCommand.java
  
  Index: ExitCommand.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.commands;
  
  import org.apache.commons.cli.Options;
  import org.apache.commons.cli.Option;
  import org.apache.commons.cli.CommandLine;
  import org.apache.commons.cli.CommandLineParser;
  import org.apache.commons.cli.PosixParser;
  import org.apache.commons.cli.OptionBuilder;
  import org.apache.commons.cli.ParseException;
  import org.apache.commons.cli.HelpFormatter;
  
  import org.apache.geronimo.common.NullArgumentException;
  import org.apache.geronimo.common.UnreachableStatementException;
  
  import org.apache.geronimo.twiddle.command.Command;
  import org.apache.geronimo.twiddle.command.CommandInfo;
  import org.apache.geronimo.twiddle.command.CommandContext;
  import org.apache.geronimo.twiddle.command.AbstractCommand;
  
  /**
   * Exit command... terminates the virtual machine.
   *
   * @version <code>$Id: ExitCommand.java,v 1.1 2003/08/13 08:32:09 jdillon Exp $</code>
   * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
   */
  public class ExitCommand
      extends AbstractCommand
  {
      public int execute(String[] args) throws Exception
      {
          if (args == null) {
              throw new NullArgumentException("args");
          }
          
          // Dump input arguments
          System.out.println("Exit arguments:");
          for (int i=0; i<args.length; i++) {
              System.out.println(args[i]);
          }
          
          // Create the Options
          Options options = new Options();
          options.addOption(OptionBuilder.withLongOpt("help")
                                         .withDescription("Display this help message")
                                         .create('h'));
          options.addOption(OptionBuilder.withLongOpt("code")
                                         .withDescription("Exit with the given status code")
                                         .hasArg()
                                         .create('c'));
          
          // Create the command line parser
          CommandLineParser parser = new PosixParser();
          
          // Carse the command line arguments
          CommandLine line = parser.parse(options, args);
          
          // Dump parsed options
          System.out.println("Exit options:");
          Option[] opts = line.getOptions();
          for (int i=0; i<opts.length; i++) {
              System.out.println(opts[i]);
          }
          
          // Dump input arguments
          System.out.println("Unused arguments:");
          args = line.getArgs();
          for (int i=0; i<args.length; i++) {
              System.out.println(args[i]);
          }
          
          // Display help
          if (line.hasOption('h')) {
              CommandInfo info = getCommandInfo();
              if (info.hasDescription()) {
                  System.out.println(info.getDescription());
                  System.out.println();
              }
              
              HelpFormatter formatter = new HelpFormatter();
              formatter.printHelp(
                  info.getName() + " [options]",
                  LINE_SEPARATOR + "Options:",
                  options,
                  LINE_SEPARATOR);
              System.out.println();
              
              return Command.SUCCESS;
          }
          
          // Get the status code to exit with
          int statusCode = 0;
          if (line.hasOption('c') || line.hasOption("code")) {
              String value = line.getOptionValue('c');
              statusCode = Integer.parseInt(value);
          }
          
          //
          // TODO: Parse out unused arguments too... may want to just use args here
          //
          
          System.out.println("Exiting with status code: " + statusCode);
          System.exit(statusCode);
          
          throw new UnreachableStatementException();
      }
  }
  
  
  
  1.2       +3 -3      incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/config/ConfigurationReader.java
  
  Index: ConfigurationReader.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/config/ConfigurationReader.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ConfigurationReader.java	12 Aug 2003 18:25:21 -0000	1.1
  +++ ConfigurationReader.java	13 Aug 2003 08:32:09 -0000	1.2
  @@ -24,12 +24,12 @@
    *    if and wherever such third-party acknowledgments normally appear.
    *
    * 4. The names "Apache" and "Apache Software Foundation" and
  - *    "Apache Maven" must not be used to endorse or promote products
  + *    "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 Maven", nor may "Apache" appear in their name, without
  + *    "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
  @@ -125,7 +125,7 @@
        */
       public Configuration read(final File file) throws Exception
       {
  -        return doRead(new FileReader(file));
  +        return doRead(new BufferedReader(new FileReader(file)));
       }
       
       /**
  
  
  

Mime
View raw message