geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r411406 - in /geronimo/sandbox/gshell/trunk: gshell-api/src/main/java/org/apache/geronimo/gshell/command/ gshell-assemblies/gshell-assembly/ gshell-assemblies/gshell-retro-assembly/ gshell-commands/gshell-standard-commands/src/main/java/org...
Date Sat, 03 Jun 2006 08:41:23 GMT
Author: jdillon
Date: Sat Jun  3 01:41:23 2006
New Revision: 411406

URL: http://svn.apache.org/viewvc?rev=411406&view=rev
Log:
Fixed problem with VariablesImpl.names() not including the parents namespace + tests
Set command now support setting variables + system properties, though parser is a bit horked

Modified:
    geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
    geronimo/sandbox/gshell/trunk/gshell-assemblies/gshell-assembly/pom.xml
    geronimo/sandbox/gshell/trunk/gshell-assemblies/gshell-retro-assembly/pom.xml
    geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/SetCommand.java
    geronimo/sandbox/gshell/trunk/gshell-core/pom.xml
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/Shell.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/VariablesImpl.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/VariablesImplTest.java

Modified: geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java?rev=411406&r1=411405&r2=411406&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
Sat Jun  3 01:41:23 2006
@@ -22,6 +22,8 @@
 import org.apache.geronimo.gshell.util.Arguments;
 import org.apache.geronimo.gshell.ExitNotification;
 
+import java.util.Iterator;
+
 /**
  * Provides support for {@link Command} implemenations.
  *
@@ -67,6 +69,20 @@
     // Life-cycle
     //
 
+    private void dump(final Variables vars) {
+        Iterator<String> iter = vars.names();
+
+        if (iter.hasNext()) {
+            log.debug("Variables:");
+        }
+
+        while (iter.hasNext()) {
+            String name = iter.next();
+
+            log.debug("    " + name + "=" + vars.get(name));
+        }
+    }
+
     public final void init(final CommandContext context) {
         if (this.context != null) {
             throw new IllegalStateException("Command already initalized");
@@ -79,7 +95,13 @@
 
         this.context = context;
 
+        if (log.isDebugEnabled()) {
+            dump(context.getVariables());
+        }
+
         doInit();
+
+        log.debug("Initialized");
     }
 
     protected void doInit() {
@@ -99,9 +121,15 @@
 
         log.debug("Destroying");
 
+        if (log.isDebugEnabled()) {
+            dump(context.getVariables());
+        }
+
         doDestroy();
 
         this.context = null;
+
+        log.debug("Destroyed");
     }
 
     protected void doDestroy() {
@@ -117,7 +145,9 @@
     //
 
     protected CommandContext getCommandContext() {
-        assert context != null;
+        if (context == null) {
+            throw new IllegalStateException("Not initialized; missing command context");
+        }
 
         return context;
     }

Modified: geronimo/sandbox/gshell/trunk/gshell-assemblies/gshell-assembly/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-assemblies/gshell-assembly/pom.xml?rev=411406&r1=411405&r2=411406&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-assemblies/gshell-assembly/pom.xml (original)
+++ geronimo/sandbox/gshell/trunk/gshell-assemblies/gshell-assembly/pom.xml Sat Jun  3 01:41:23
2006
@@ -17,7 +17,7 @@
     </parent>
     
     <artifactId>gshell-assembly</artifactId>
-    <name>GShell :: Assembly</name>
+    <name>GShell Assemblies :: Complete</name>
     <packaging>pom</packaging>
     
     <dependencies>

Modified: geronimo/sandbox/gshell/trunk/gshell-assemblies/gshell-retro-assembly/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-assemblies/gshell-retro-assembly/pom.xml?rev=411406&r1=411405&r2=411406&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-assemblies/gshell-retro-assembly/pom.xml (original)
+++ geronimo/sandbox/gshell/trunk/gshell-assemblies/gshell-retro-assembly/pom.xml Sat Jun
 3 01:41:23 2006
@@ -17,7 +17,7 @@
     </parent>
     
     <artifactId>gshell-retro-assembly</artifactId>
-    <name>GShell :: Retro Assembly</name>
+    <name>GShell Assemblies :: Retro</name>
     <packaging>pom</packaging>
     
     <dependencies>

Modified: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/SetCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/SetCommand.java?rev=411406&r1=411405&r2=411406&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/SetCommand.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/SetCommand.java
Sat Jun  3 01:41:23 2006
@@ -24,6 +24,7 @@
 import org.apache.commons.cli.PosixParser;
 import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.command.CommandSupport;
+import org.apache.geronimo.gshell.command.Variables;
 import org.apache.geronimo.gshell.console.IO;
 
 /**
@@ -38,7 +39,13 @@
         super("set");
     }
 
-    protected int doExecute(final String[] args) throws Exception {
+    enum Mode
+    {
+        VARIABLE,
+        PROPERTY
+    }
+
+    protected int doExecute(String[] args) throws Exception {
         assert args != null;
 
         //
@@ -53,10 +60,29 @@
             .withDescription("Display this help message")
             .create('h'));
 
+        options.addOption(OptionBuilder.withLongOpt("property")
+            .withDescription("Set a system property")
+            .create('p'));
+
+        //
+        // TODO: Add support to set immutable
+        //
+
+        //
+        // TODO: Add support to set in parent (parent) scope
+        //
+
         CommandLineParser parser = new PosixParser();
         CommandLine line = parser.parse(options, args);
 
-        if (line.hasOption('h')) {
+        boolean usage = false;
+        String[] _args = line.getArgs();
+
+        if (_args.length == 0) {
+            usage = true;
+        }
+
+        if (usage || line.hasOption('h')) {
             io.out.println(getName() + " -- set a variable or property");
             io.out.println();
 
@@ -64,7 +90,7 @@
             formatter.printHelp(
                 io.out,
                 80, // width (FIXME: Should pull from gshell.columns variable)
-                getName() + " [options] <name=value>",
+                getName() + " [options] (<name=value>)+",
                 "",
                 options,
                 4, // left pad
@@ -77,37 +103,72 @@
             return Command.SUCCESS;
         }
 
-        String[] _args = line.getArgs();
-        if (args.length != 1) {
-            log.error("Expected one and only one argument.");
+        Mode mode = Mode.VARIABLE;
+
+        if (line.hasOption('p')) {
+            mode = Mode.PROPERTY;
         }
-        else {
-            //
-            // TODO: Support setting context variables
-            //
-            
-            setPropertyFrom(_args[0]);
+
+        //
+        // FIXME: This does not jive well with the parser, and stuff like foo = "b a r"
+        //
+        
+        for (String arg : _args) {
+            switch (mode) {
+                case PROPERTY:
+                    setProperty(arg);
+                    break;
+
+                case VARIABLE:
+                    setVariable(arg);
+                    break;
+            }
         }
 
         return Command.SUCCESS;
     }
 
-    private void setPropertyFrom(final String namevalue) {
-        String name, value;
-        int j = namevalue.indexOf("=");
-
-        if (j == -1) {
-            name = namevalue;
-            value = "true";
+    class NameValue
+    {
+        String name;
+        String value;
+    }
+
+    private NameValue parse(final String input) {
+        NameValue nv = new NameValue();
+
+        int i = input.indexOf("=");
+
+        if (i == -1) {
+            nv.name = input;
+            nv.value = "true";
         }
         else {
-            name = namevalue.substring(0, j);
-            value = namevalue.substring(j + 1, namevalue.length());
+            nv.name = input.substring(0, i);
+            nv.value = input.substring(i + 1, input.length());
         }
-        name = name.trim();
 
-        log.info("Setting system property: " + name + "=" + value);
+        nv.name = nv.name.trim();
+
+        return nv;
+    }
+
+    private void setProperty(final String namevalue) {
+        NameValue nv = parse(namevalue);
+
+        log.info("Setting system property: " + nv.name + "=" + nv.value);
+
+        System.setProperty(nv.name, nv.value);
+    }
+
+    private void setVariable(final String namevalue) {
+        NameValue nv = parse(namevalue);
+
+        log.info("Setting variable: " + nv.name + "=" + nv.value);
+
+        // Command vars always has a parent, set only makes sence when setting in parent's
scope
+        Variables vars = this.getVariables().parent();
 
-        System.setProperty(name, value);
+        vars.set(nv.name, nv.value);
     }
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-core/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/pom.xml?rev=411406&r1=411405&r2=411406&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/pom.xml (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/pom.xml Sat Jun  3 01:41:23 2006
@@ -26,7 +26,7 @@
             <artifactId>gshell-api</artifactId>
             <version>${pom.version}</version>
         </dependency>
-        
+
         <dependency>
             <groupId>jline</groupId>
             <artifactId>jline</artifactId>

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/Shell.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/Shell.java?rev=411406&r1=411405&r2=411406&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/Shell.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/Shell.java
Sat Jun  3 01:41:23 2006
@@ -33,6 +33,8 @@
 import org.apache.geronimo.gshell.commandline.CommandLine;
 import org.apache.geronimo.gshell.util.Arguments;
 
+import java.util.Iterator;
+
 /**
  * ???
  *
@@ -51,7 +53,7 @@
 
     private final CommandLineBuilder commandLineBuilder;
 
-    private final Variables vars = new VariablesImpl();
+    private final Variables variables = new VariablesImpl();
 
     public Shell(final IO io) throws CommandException {
         if (io == null) {
@@ -77,7 +79,7 @@
     }
 
     public Variables getVariables() {
-        return vars;
+        return variables;
     }
 
     public IO getIO() {
@@ -103,6 +105,20 @@
     // CommandExecutor
     //
 
+    private void dump(final Variables vars) {
+        Iterator<String> iter = vars.names();
+
+        if (iter.hasNext()) {
+            log.debug("Variables:");
+        }
+
+        while (iter.hasNext()) {
+            String name = iter.next();
+
+            log.debug("    " + name + "=" + vars.get(name));
+        }
+    }
+
     public int execute(final String commandName, final String[] args) throws Exception {
         assert commandName != null;
         assert args != null;
@@ -126,11 +142,9 @@
         // TODO: DI all bits if we can, then free up "context" to replace "category" as a
term
         //
 
-        final Variables parent = getVariables();
+        final Variables vars = new VariablesImpl(getVariables());
 
         cmd.init(new CommandContext() {
-            final Variables vars = new VariablesImpl(parent);
-
             public IO getIO() {
                 return io;
             }
@@ -148,7 +162,6 @@
         }
 
         int status;
-
         try {
             status = cmd.execute(args);
 

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/VariablesImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/VariablesImpl.java?rev=411406&r1=411405&r2=411406&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/VariablesImpl.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/VariablesImpl.java
Sat Jun  3 01:41:23 2006
@@ -164,7 +164,30 @@
     }
 
     public Iterator<String> names() {
-        return Collections.unmodifiableSet(map.keySet()).iterator();
+        // Chain to parent iterator if we have a parent
+        return new Iterator<String>() {
+            Iterator<String> iter = map.keySet().iterator();
+            boolean more = parent() != null;
+
+            public boolean hasNext() {
+                boolean next = iter.hasNext();
+                if (!next && more) {
+                    iter = parent().names();
+                    more = false;
+                    next = hasNext();
+                }
+
+                return next;
+            }
+
+            public String next() {
+                return iter.next();
+            }
+
+            public void remove() {
+                throw new UnsupportedOperationException();
+            }
+        };
     }
 
     public Variables parent() {

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/VariablesImplTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/VariablesImplTest.java?rev=411406&r1=411405&r2=411406&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/VariablesImplTest.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/VariablesImplTest.java
Sat Jun  3 01:41:23 2006
@@ -124,6 +124,20 @@
         }
     }
 
+    public void testSetParentFromChild() throws Exception {
+        Variables parent = new VariablesImpl();
+        VariablesImpl vars = new VariablesImpl(parent);
+        String name = "a";
+        Object value = new Object();
+
+        // Make sure we can add to parent's scope from child
+        vars.parent().set(name, value);
+        assertEquals(value, parent.get(name));
+
+        // Make sure the iter sees it
+        assertTrue(vars.names().hasNext());
+    }
+
     public void testGet() throws Exception {
         VariablesImpl vars = new VariablesImpl();
         String name = "a";



Mime
View raw message