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 2006/05/29 23:40:06 UTC

svn commit: r410154 - in /geronimo/sandbox/gshell/trunk/gshell-core/src: main/java/org/apache/geronimo/gshell/ main/java/org/apache/geronimo/gshell/command/ main/java/org/apache/geronimo/gshell/commandline/ test/java/org/apache/geronimo/gshell/command/...

Author: jdillon
Date: Mon May 29 14:40:05 2006
New Revision: 410154

URL: http://svn.apache.org/viewvc?rev=410154&view=rev
Log:
Hooked up the parser via CommandLineBuilder.  Parser is now used to perform command execution :-)

Added:
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandExecutor.java   (with props)
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/MockCommandExecutor.java   (with props)
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/CommandLineExecutingVisitorTest.java   (with props)
Modified:
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/GShell.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/GShellImpl.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineBuilder.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineExecutingVisitor.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/CommandLineBuilderTest.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserVisitorTest.java

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/GShell.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/GShell.java?rev=410154&r1=410153&r2=410154&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/GShell.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/GShell.java Mon May 29 14:40:05 2006
@@ -19,6 +19,7 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.geronimo.gshell.console.IO;
+import org.apache.geronimo.gshell.command.CommandExecutor;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
@@ -28,10 +29,12 @@
  * @version $Id$
  */
 public class GShell
+    implements CommandExecutor
 {
     private static final Log log = LogFactory.getLog(GShell.class);
     
     private ApplicationContext ctx;
+
     private GShellImpl impl;
     
     public GShell(final IO io) {

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/GShellImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/GShellImpl.java?rev=410154&r1=410153&r2=410154&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/GShellImpl.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/GShellImpl.java Mon May 29 14:40:05 2006
@@ -23,8 +23,11 @@
 import org.apache.geronimo.gshell.command.Variables;
 import org.apache.geronimo.gshell.command.VariablesMap;
 import org.apache.geronimo.gshell.command.CommandManager;
+import org.apache.geronimo.gshell.command.CommandExecutor;
 import org.apache.geronimo.gshell.console.IO;
 import org.apache.geronimo.gshell.util.Arguments;
+import org.apache.geronimo.gshell.commandline.CommandLineBuilder;
+import org.apache.geronimo.gshell.commandline.CommandLine;
 import org.springframework.beans.BeansException;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
@@ -35,11 +38,12 @@
  * @version $Id$
  */
 public class GShellImpl
-    implements ApplicationContextAware
+    implements CommandExecutor, ApplicationContextAware
 {
     private static final Log log = LogFactory.getLog(GShell.class);
 
     private IO io;
+    
     private ApplicationContext ctx;
 
     public void setIO(final IO io) {
@@ -48,20 +52,25 @@
         this.io = io;
     }
     
-    public int execute(final String commandline) throws Exception {
-        assert commandline != null;
+    public int execute(final String commandLine) throws Exception {
+        assert commandLine != null;
 
-        log.info("Executing (String): " + commandline);
+        log.info("Executing (String): " + commandLine);
 
         //
         // HACK: Just to get something to work...
         //
-        
-        String[] args = commandline.split("\\s");
-        String name = args[0];
-        args = Arguments.shift(args);
 
-        return execute(name, args);
+        CommandLineBuilder builder = new CommandLineBuilder(this);
+        CommandLine cl = builder.create(commandLine);
+        cl.execute();
+
+        //
+        // HACK: Current API needs to be revised to pass data back,
+        //       will be fixed latger, ignore for now
+        //
+
+        return 0;
     }
     
     public int execute(final String commandName, String[] args) throws Exception {
@@ -72,6 +81,10 @@
         
         //
         // HACK: Just get something working right now
+        //
+
+        //
+        // HACK: DI CommandManager...
         //
 
         Command cmd = new CommandManager().getCommand(commandName);

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandExecutor.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandExecutor.java?rev=410154&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandExecutor.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandExecutor.java Mon May 29 14:40:05 2006
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.geronimo.gshell.command;
+
+/**
+ * ???
+ *
+ * @version $Id$
+ */
+public interface CommandExecutor
+{
+    int execute(String commandline) throws Exception;
+
+    int execute(String[] args) throws Exception;
+
+    int execute(String commandName, String[] args) throws Exception;
+}

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandExecutor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandExecutor.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandExecutor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineBuilder.java?rev=410154&r1=410153&r2=410154&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineBuilder.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineBuilder.java Mon May 29 14:40:05 2006
@@ -17,6 +17,14 @@
 package org.apache.geronimo.gshell.commandline;
 
 import org.apache.geronimo.gshell.commandline.parser.CommandLineParser;
+import org.apache.geronimo.gshell.commandline.parser.ASTCommandLine;
+import org.apache.geronimo.gshell.commandline.parser.ParseException;
+import org.apache.geronimo.gshell.command.CommandExecutor;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.Reader;
+import java.io.StringReader;
 
 /**
  * ???
@@ -25,17 +33,52 @@
  */
 public class CommandLineBuilder
 {
+    private static final Log log = LogFactory.getLog(CommandLineBuilder.class);
+
+    private CommandExecutor executor;
+
     private CommandLineParser parser;
 
-    public CommandLineBuilder() {
+    public CommandLineBuilder(final CommandExecutor executor) {
+        if (executor == null) {
+            throw new IllegalArgumentException("Executor is null");
+        }
+
+        this.executor = executor;
         this.parser = new CommandLineParser();
     }
 
-    public CommandLine create() {
+    private ASTCommandLine parse(final String input) throws ParseException {
+        assert input != null;
+
+        Reader reader = new StringReader(input);
+        CommandLineParser parser = new CommandLineParser();
+        ASTCommandLine cl = parser.parse(reader);
+
         //
-        // TODO:
+        // TODO: Log results with log visitor
         //
 
-        return null;
+        cl.dump("# ");
+
+        return cl;
+    }
+
+    public CommandLine create(final String commandLine) throws ParseException {
+        if (commandLine == null) {
+            throw new IllegalArgumentException("Command line is null");
+        }
+        if (commandLine.trim().length() == 0) {
+            throw new IllegalArgumentException("Command line is empty");
+        }
+
+        final ASTCommandLine root = parse(commandLine);
+        final CommandLineExecutingVisitor visitor = new CommandLineExecutingVisitor(this.executor);
+
+        return new CommandLine() {
+            public void execute() throws Exception {
+                root.jjtAccept(visitor, null);
+            }
+        };
     }
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineExecutingVisitor.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineExecutingVisitor.java?rev=410154&r1=410153&r2=410154&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineExecutingVisitor.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineExecutingVisitor.java Mon May 29 14:40:05 2006
@@ -24,6 +24,8 @@
 import org.apache.geronimo.gshell.commandline.parser.ASTOpaqueString;
 import org.apache.geronimo.gshell.commandline.parser.ASTPlainString;
 import org.apache.geronimo.gshell.commandline.parser.StringSupport;
+import org.apache.geronimo.gshell.util.Arguments;
+import org.apache.geronimo.gshell.command.CommandExecutor;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -41,18 +43,32 @@
 {
     private static final Log log = LogFactory.getLog(CommandLineExecutingVisitor.class);
 
+    private CommandExecutor executor;
+
+    public CommandLineExecutingVisitor(final CommandExecutor executor) {
+        if (executor == null) {
+            throw new IllegalArgumentException("Executor is null");
+        }
+
+        this.executor = executor;
+    }
+
     public Object visit(final SimpleNode node, final Object data) {
         assert node != null;
-        assert data != null;
+        // assert data != null;
 
         log.error("Unhandled node type: " + node.getClass().getName());
 
+        //
+        // TODO: Exception?  Means impl node does not accept
+        //
+
         return null;
     }
 
     public Object visit(final ASTCommandLine node, final Object data) {
         assert node != null;
-        assert data != null;
+        // assert data != null;
 
         //
         // NOTE: Visiting children will execute seperate commands in serial
@@ -65,17 +81,32 @@
 
     public Object visit(final ASTExpression node, final Object data) {
         assert node != null;
-        assert data != null;
+        // assert data != null;
 
-        List args = new ArrayList(node.jjtGetNumChildren());
+        // Create the argument list (cmd name + args)
+        List list = new ArrayList(node.jjtGetNumChildren());
+        node.childrenAccept(this, list);
+
+        String[] args = (String[])list.toArray(new String[list.size()]);
+        assert list.size() >= 1;
+
+        String commandName = args[0];
+        args = Arguments.shift(args);
+
+        int result;
+
+        try {
+            result = executor.execute(commandName, args);
+        }
+        catch (Exception e) {
+            //
+            // FIXME: !!!
+            //
 
-        node.childrenAccept(this, args);
+            throw new RuntimeException(e);
+        }
 
-        //
-        // TODO: Execute?  Return result?
-        //
-
-        return args;
+        return result;
     }
 
     private Object appendString(final StringSupport node, final Object data) {

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/MockCommandExecutor.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/MockCommandExecutor.java?rev=410154&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/MockCommandExecutor.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/MockCommandExecutor.java Mon May 29 14:40:05 2006
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.geronimo.gshell.command;
+
+/**
+ * Unit tests for the {@link CommandSupport} class.
+ *
+ * @version $Id$
+ */
+public class MockCommandExecutor
+    implements CommandExecutor
+{
+    public Object commandLine;
+
+    public String[] args;
+
+    public String commandName;
+
+    public int execute(String commandline) throws Exception {
+        this.commandLine = commandLine;
+
+        return 0;
+    }
+
+    public int execute(String[] args) throws Exception {
+        this.args = args;
+
+        return 0;
+    }
+
+    public int execute(String commandName, String[] args) throws Exception {
+        this.commandName = commandName;
+        this.args = args;
+
+        return 0;
+    }
+}

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/MockCommandExecutor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/MockCommandExecutor.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/MockCommandExecutor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/CommandLineBuilderTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/CommandLineBuilderTest.java?rev=410154&r1=410153&r2=410154&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/CommandLineBuilderTest.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/CommandLineBuilderTest.java Mon May 29 14:40:05 2006
@@ -17,6 +17,7 @@
 package org.apache.geronimo.gshell.commandline;
 
 import junit.framework.TestCase;
+import org.apache.geronimo.gshell.command.MockCommandExecutor;
 
 /**
  * Unit tests for the {@link CommandLineBuilder} class.
@@ -27,10 +28,26 @@
     extends TestCase
 {
     public void testConstructor() throws Exception {
-        //
-        // NOTE: This is a lame duck test, remove once there is functionality to test
-        //
-        
-        new CommandLineBuilder();
+        try {
+            new CommandLineBuilder(null);
+            fail("Accepted null argument");
+        }
+        catch (IllegalArgumentException expected) {
+            // ignore
+        }
+    }
+
+    public void testSimple() throws Exception {
+        MockCommandExecutor executor = new MockCommandExecutor();
+        CommandLineBuilder builder = new CommandLineBuilder(executor);
+
+        CommandLine cl = builder.create("echo hi");
+        assertNotNull(cl);
+
+        cl.execute();
+
+        assertEquals("echo", executor.commandName);
+        assertEquals(1, executor.args.length);
+        assertEquals("hi", executor.args[0]);
     }
 }

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/CommandLineExecutingVisitorTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/CommandLineExecutingVisitorTest.java?rev=410154&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/CommandLineExecutingVisitorTest.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/CommandLineExecutingVisitorTest.java Mon May 29 14:40:05 2006
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.geronimo.gshell.commandline;
+
+import junit.framework.TestCase;
+
+import java.io.Reader;
+import java.io.StringReader;
+
+import org.apache.geronimo.gshell.commandline.parser.ASTCommandLine;
+import org.apache.geronimo.gshell.commandline.parser.ParseException;
+import org.apache.geronimo.gshell.commandline.parser.CommandLineParser;
+import org.apache.geronimo.gshell.command.CommandExecutor;
+import org.apache.geronimo.gshell.command.MockCommandExecutor;
+
+/**
+ * Unit tests for the {@link CommandLineExecutingVisitor} usage.
+ *
+ * @version $Id$
+ */
+public class CommandLineExecutingVisitorTest
+    extends TestCase
+{
+    private ASTCommandLine parse(final String input) throws ParseException {
+        assert input != null;
+
+        Reader reader = new StringReader(input);
+        CommandLineParser parser = new CommandLineParser();
+        ASTCommandLine cl = parser.parse(reader);
+
+        //
+        // TODO: Remove eventually, may want to make nodes use logging to dump too
+        //
+
+        cl.dump("> ");
+
+        assertNotNull(cl);
+
+        return cl;
+    }
+
+    public void testConstructor() throws Exception {
+        try {
+            new CommandLineExecutingVisitor(null);
+            fail("Accepted null value");
+        }
+        catch (IllegalArgumentException expected) {
+            // ignore
+        }
+
+        // Happy day
+        new CommandLineExecutingVisitor(new MockCommandExecutor());
+    }
+}

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/CommandLineExecutingVisitorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/CommandLineExecutingVisitorTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/CommandLineExecutingVisitorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserVisitorTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserVisitorTest.java?rev=410154&r1=410153&r2=410154&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserVisitorTest.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/parser/CommandLineParserVisitorTest.java Mon May 29 14:40:05 2006
@@ -46,7 +46,7 @@
 
         return cl;
     }
-
+    
     public void testVisitor1() throws Exception {
         String input = "a \"b\" 'c' d";