geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r593731 - in /geronimo/gshell/trunk/gshell-core/src: main/java/org/apache/geronimo/gshell/ main/java/org/apache/geronimo/gshell/layout/ test/java/org/apache/geronimo/gshell/layout/
Date Sat, 10 Nov 2007 04:27:24 GMT
Author: jdillon
Date: Fri Nov  9 20:27:23 2007
New Revision: 593731

URL: http://svn.apache.org/viewvc?rev=593731&view=rev
Log:
(GSHELL-42) Update the layout manager to return nodes and leave command lookup as usage requires

Modified:
    geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java
    geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/DefaultLayoutManager.java
    geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/LayoutManager.java
    geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/DefaultLayoutManagerTest.java

Modified: geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java?rev=593731&r1=593730&r2=593731&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java
(original)
+++ geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java
Fri Nov  9 20:27:23 2007
@@ -27,6 +27,12 @@
 import org.apache.geronimo.gshell.common.Arguments;
 import org.apache.geronimo.gshell.common.StopWatch;
 import org.apache.geronimo.gshell.layout.LayoutManager;
+import org.apache.geronimo.gshell.layout.NotFoundException;
+import org.apache.geronimo.gshell.layout.model.AliasNode;
+import org.apache.geronimo.gshell.layout.model.CommandNode;
+import org.apache.geronimo.gshell.layout.model.Node;
+import org.apache.geronimo.gshell.registry.CommandRegistry;
+import org.apache.geronimo.gshell.registry.NotRegisteredException;
 import org.apache.geronimo.gshell.shell.Environment;
 import org.codehaus.plexus.component.annotations.Component;
 import org.codehaus.plexus.component.annotations.Requirement;
@@ -48,6 +54,9 @@
     private LayoutManager layoutManager;
 
     @Requirement
+    private CommandRegistry commandRegistry;
+
+    @Requirement
     private CommandLineBuilder commandLineBuilder;
 
     @Requirement
@@ -97,6 +106,38 @@
         return execute(String.valueOf(args[0]), Arguments.shift(args));
     }
 
+    private String findCommandId(final Node node) throws NotFoundException {
+        assert node != null;
+
+        if (node instanceof AliasNode) {
+            AliasNode aliasNode = (AliasNode) node;
+            String targetPath = aliasNode.getCommand();
+            Node target = layoutManager.findNode(layoutManager.getLayout(), targetPath);
+
+            return findCommandId(target);
+        }
+        else if (node instanceof CommandNode) {
+            CommandNode commandNode = (CommandNode) node;
+
+            return commandNode.getId();
+        }
+
+        throw new NotFoundException("Unable to get command id for: " + node);
+    }
+
+    private Command findCommand(final String path) throws NotFoundException {
+        Node node = layoutManager.findNode(path);
+
+        String id = findCommandId(node);
+
+        try {
+            return commandRegistry.lookup(id);
+        }
+        catch (NotRegisteredException e) {
+            throw new NotFoundException(e.getMessage());
+        }
+    }
+
     public Object execute(final String path, final Object[] args) throws Exception {
         assert path != null;
         assert args != null;
@@ -104,7 +145,7 @@
         log.info("Executing ({}): [{}]", path, Arguments.asString(args));
 
         // Look up the command for the given path
-        Command command = layoutManager.find(path);
+        Command command = findCommand(path);
 
         // Setup the command context and pass it to the command instance
         CommandContext context = new CommandContext() {

Modified: geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/DefaultLayoutManager.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/DefaultLayoutManager.java?rev=593731&r1=593730&r2=593731&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/DefaultLayoutManager.java
(original)
+++ geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/DefaultLayoutManager.java
Fri Nov  9 20:27:23 2007
@@ -21,15 +21,10 @@
 
 import java.io.IOException;
 
-import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.layout.loader.LayoutLoader;
-import org.apache.geronimo.gshell.layout.model.AliasNode;
-import org.apache.geronimo.gshell.layout.model.CommandNode;
 import org.apache.geronimo.gshell.layout.model.GroupNode;
 import org.apache.geronimo.gshell.layout.model.Layout;
 import org.apache.geronimo.gshell.layout.model.Node;
-import org.apache.geronimo.gshell.registry.CommandRegistry;
-import org.apache.geronimo.gshell.registry.NotRegisteredException;
 import org.apache.geronimo.gshell.shell.Environment;
 import org.codehaus.plexus.component.annotations.Component;
 import org.codehaus.plexus.component.annotations.Requirement;
@@ -50,9 +45,6 @@
     private final Logger log = LoggerFactory.getLogger(getClass());
 
     @Requirement
-    private CommandRegistry commandRegistry;
-
-    @Requirement
     private LayoutLoader loader;
 
     @Requirement
@@ -62,14 +54,12 @@
 
     public DefaultLayoutManager() {}
     
-    public DefaultLayoutManager(final CommandRegistry commandRegistry, final LayoutLoader
loader, final Environment env) {
-        this.commandRegistry = commandRegistry;
+    public DefaultLayoutManager(final LayoutLoader loader, final Environment env) {
         this.loader = loader;
         this.env = env;
     }
 
-    public DefaultLayoutManager(final CommandRegistry commandRegistry, final Layout layout,
final Environment env) {
-        this.commandRegistry = commandRegistry;
+    public DefaultLayoutManager(final Layout layout, final Environment env) {
         this.layout = layout;
         this.env = env;
     }
@@ -89,63 +79,32 @@
         return layout;
     }
 
-    public Command find(final String path) throws NotFoundException {
+    public Node findNode(final String path) throws NotFoundException {
         assert path != null;
 
-        log.debug("Searching for command: {}", path);
-
         Node start;
 
-        if (path.startsWith("/")) {
+        if (path.startsWith(PATH_SEPARATOR)) {
             start = layout;
         }
         else {
-            //
-            // FIXME: Use a FQN for this and expose as static final
-            //
-
-            start = (Node) env.getVariables().get("CURRENT_NODE");
+            start = (Node) env.getVariables().get(CURRENT_NODE);
 
             if (start == null) {
                 start = layout;
             }
         }
 
-        String id = findCommandId(start, path);
-
-        try {
-            return commandRegistry.lookup(id);
-        }
-        catch (NotRegisteredException e) {
-            throw new NotFoundException(e.getMessage());
-        }
-    }
-
-    private String findCommandId(final Node start, final String path) throws NotFoundException
{
-        assert start != null;
-        assert path != null;
-
-        Node node = findNode(start, path);
-
-        if (node instanceof CommandNode) {
-            return ((CommandNode)node).getId();
-        }
-        else if (node instanceof AliasNode) {
-            String cmd = ((AliasNode)node).getCommand();
-
-            return findCommandId(layout, cmd);
-        }
-
-        throw new NotFoundException(path);
+        return findNode(start, path);
     }
 
-    private Node findNode(final Node start, final String path) throws NotFoundException {
+    public Node findNode(final Node start, final String path) throws NotFoundException {
         assert start != null;
         assert path != null;
 
         Node current = start;
 
-        String[] elements = path.split("/");
+        String[] elements = path.split(PATH_SEPARATOR);
         
         for (String element : elements) {
             if (current instanceof GroupNode) {

Modified: geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/LayoutManager.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/LayoutManager.java?rev=593731&r1=593730&r2=593731&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/LayoutManager.java
(original)
+++ geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/LayoutManager.java
Fri Nov  9 20:27:23 2007
@@ -21,6 +21,7 @@
 
 import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.layout.model.Layout;
+import org.apache.geronimo.gshell.layout.model.Node;
 
 /**
  * Provies the shell with a simple mechanism to organize commands.
@@ -29,13 +30,15 @@
  */
 public interface LayoutManager
 {
-    /**
-     * Returns the currently loaded layout; never null.
-     */
+    String CURRENT_NODE = LayoutManager.class.getName() + ".currentNode";
+
+    String ROOT = "/";
+
+    String PATH_SEPARATOR = "/";
+
     Layout getLayout();
 
-    /**
-     * Find the command for the given path, or null if not found.
-     */
-    Command find(String path) throws NotFoundException;
+    Node findNode(String path) throws NotFoundException;
+
+    Node findNode(Node start, String path) throws NotFoundException;
 }

Modified: geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/DefaultLayoutManagerTest.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/DefaultLayoutManagerTest.java?rev=593731&r1=593730&r2=593731&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/DefaultLayoutManagerTest.java
(original)
+++ geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/DefaultLayoutManagerTest.java
Fri Nov  9 20:27:23 2007
@@ -21,16 +21,11 @@
 
 import junit.framework.TestCase;
 import org.apache.geronimo.gshell.DefaultEnvironment;
-import org.apache.geronimo.gshell.command.Command;
-import org.apache.geronimo.gshell.command.CommandContext;
 import org.apache.geronimo.gshell.command.IO;
-import org.apache.geronimo.gshell.layout.loader.XMLLayoutLoader;
-import org.apache.geronimo.gshell.layout.model.Layout;
 import org.apache.geronimo.gshell.layout.model.CommandNode;
-import org.apache.geronimo.gshell.layout.model.AliasNode;
 import org.apache.geronimo.gshell.layout.model.GroupNode;
-import org.apache.geronimo.gshell.registry.CommandRegistry;
-import org.apache.geronimo.gshell.registry.DefaultCommandRegistry;
+import org.apache.geronimo.gshell.layout.model.Layout;
+import org.apache.geronimo.gshell.layout.model.Node;
 import org.apache.geronimo.gshell.shell.Environment;
 
 /**
@@ -43,65 +38,32 @@
 {
     private DefaultLayoutManager layoutManager;
 
-    private CommandRegistry registry;
-
-    private Layout layout;
-
-    private Environment env;
-
     protected void setUp() throws Exception {
-        registry = new DefaultCommandRegistry();
-        env = new DefaultEnvironment(new IO());
+        Environment env = new DefaultEnvironment(new IO());
 
-        layout = new Layout();
+        Layout layout = new Layout();
         layout.add(new CommandNode("help", "help"));
         GroupNode g = new GroupNode("test");
         g.add(new CommandNode("foo", "foo"));
         layout.add(g);
 
-        layoutManager = new DefaultLayoutManager(registry, layout, env);
+        layoutManager = new DefaultLayoutManager(layout, env);
         // layoutManager.initialize();
     }
 
     public void testFind() throws Exception {
-        registry.register(new Command() {
-            public String getId() {
-                return "help";
-            }
-
-            public String getDescription() {
-                return null;
-            }
-
-            public Object execute(CommandContext context, Object... args) throws Exception
{
-                return null;
-            }
-        });
-
-        layoutManager.find("help");
+        Node node = layoutManager.findNode("help");
+        assertNotNull(node);
     }
 
     public void testFindInGroup() throws Exception {
-        registry.register(new Command() {
-            public String getId() {
-                return "foo";
-            }
-
-            public String getDescription() {
-                return null;
-            }
-
-            public Object execute(CommandContext context, Object... args) throws Exception
{
-                return null;
-            }
-        });
-
-        layoutManager.find("test/foo");
+        Node node = layoutManager.findNode("test/foo");
+        assertNotNull(node);
     }
 
     public void testFindNotFound() throws Exception {
         try {
-            layoutManager.find("no-such-command");
+            layoutManager.findNode("no-such-command");
             fail();
         }
         catch(NotFoundException expected) {}



Mime
View raw message