servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r602090 - in /servicemix/branches/servicemix-4.0/runtime/gshell: gshell-core/src/main/java/org/apache/geronimo/gshell/spring/ gshell-osgi/src/main/java/org/apache/geronimo/gshell/osgi/
Date Fri, 07 Dec 2007 13:06:27 GMT
Author: gnodet
Date: Fri Dec  7 05:06:27 2007
New Revision: 602090

URL: http://svn.apache.org/viewvc?rev=602090&view=rev
Log:
Improve subshells a bit

Modified:
    servicemix/branches/servicemix-4.0/runtime/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/HelpCommand.java
    servicemix/branches/servicemix-4.0/runtime/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/SpringCommandRegistry.java
    servicemix/branches/servicemix-4.0/runtime/gshell/gshell-osgi/src/main/java/org/apache/geronimo/gshell/osgi/OsgiShell.java

Modified: servicemix/branches/servicemix-4.0/runtime/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/HelpCommand.java
URL: http://svn.apache.org/viewvc/servicemix/branches/servicemix-4.0/runtime/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/HelpCommand.java?rev=602090&r1=602089&r2=602090&view=diff
==============================================================================
--- servicemix/branches/servicemix-4.0/runtime/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/HelpCommand.java
(original)
+++ servicemix/branches/servicemix-4.0/runtime/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/HelpCommand.java
Fri Dec  7 05:06:27 2007
@@ -1,6 +1,9 @@
 package org.apache.geronimo.gshell.spring;
 
+import java.util.Comparator;
 import java.util.List;
+import java.util.SortedSet;
+import java.util.TreeSet;
 
 import org.apache.geronimo.gshell.ansi.Code;
 import org.apache.geronimo.gshell.ansi.Renderer;
@@ -74,6 +77,8 @@
                     gn = (GroupNode) n;
                 } else if (n instanceof CommandNode) {
                     cn = (CommandNode) n;
+                } else if (n instanceof AliasNode) {
+                    cn = (CommandNode) layoutManager.findNode(gn, ((AliasNode) n).getCommand());
                 } else {
                     throw new IllegalStateException("Unsupported node type " + n.getParent().getName());
                 }
@@ -81,13 +86,9 @@
         }
 
         if (cn == null) {
-            // TODO: take into account the sub shell
-            if (path == null || path.isEmpty()) {
+            if (gn == layoutManager.getLayout()) {
                 io.out.print(branding.getAbout());
                 io.out.println();
-                io.out.println("Available commands:");
-            } else {
-                io.out.println("Available commands in " + path);
             }
             displayGroupCommands(gn);
         }
@@ -101,8 +102,23 @@
     private void displayGroupCommands(final GroupNode group) throws Exception {
         int maxNameLen = 20; // FIXME: Figure this out dynamically
 
+        boolean hasShells = false;
+
+        if (group == layoutManager.getLayout()) {
+            io.out.println("Available commands:");
+        } else {
+            io.out.println("Available commands in " + Renderer.encode(group.getName(), Code.BOLD)
+ ":");
+        }
+
+        SortedSet<Node> nodes = new TreeSet<Node>(new Comparator<Node>()
{
+            public int compare(Node o1, Node o2) {
+                return o1.getName().compareTo(o2.getName());
+            }
+        });
+        nodes.addAll(group.nodes());
+
         // First display command/aliases nodes
-        for (Node child : group.nodes()) {
+        for (Node child : nodes) {
             if (child instanceof CommandNode) {
                 try {
                     CommandNode node = (CommandNode) child;
@@ -128,33 +144,40 @@
             else if (child instanceof AliasNode) {
                 AliasNode node = (AliasNode) child;
                 String name = StringUtils.rightPad(node.getName(), maxNameLen);
+                String cmd = layoutManager.findNode(group, node.getCommand()).getName();
 
                 io.out.print("  ");
                 io.out.print(renderer.render(Renderer.encode(name, Code.BOLD)));
                 io.out.print("  ");
 
                 io.out.print("Alias to: ");
-                io.out.println(renderer.render(Renderer.encode(node.getCommand(), Code.BOLD)));
+
+                io.out.println(renderer.render(Renderer.encode(cmd, Code.BOLD)));
+            } else if (child instanceof GroupNode) {
+                hasShells = true;
             }
         }
 
         io.out.println();
 
-        // Then groups
-        for (Node child : group.nodes()) {
-            if (child instanceof GroupNode) {
-                GroupNode node = (GroupNode) child;
-
-                io.out.print("  ");
-                io.out.println(renderer.render(Renderer.encode(node.getPath(), Code.BOLD)));
-
-                io.out.println();
-                //displayGroupCommands(node);
-                //io.out.println();
+        if (hasShells) {
+            io.out.println("Available shells:");
+            // Then groups
+            for (Node child : nodes) {
+                if (child instanceof GroupNode) {
+                    GroupNode node = (GroupNode) child;
+                    io.out.print("  ");
+                    io.out.println(renderer.render(Renderer.encode(node.getName(), Code.BOLD)));
+                }
             }
+            io.out.println();
         }
     }
 
+    private String extractCommandName(String command) {
+        return command.substring(command.lastIndexOf(':') + 1);
+    }
+
     private void displayCommandHelp(final String path) throws Exception {
         assert path != null;
 
@@ -165,8 +188,10 @@
             io.out.println("Try " + Renderer.encode("help", Code.BOLD) + " for a list of
available commands.");
         }
         else {
-            io.out.println("Command " + Renderer.encode(path, Code.BOLD));
+            io.out.println("Command " + Renderer.encode(extractCommandName(path), Code.BOLD));
             io.out.println("   " + cmd.getDescription());
+
+            cmd.execute(context, "--help");
         }
 
         io.out.println();

Modified: servicemix/branches/servicemix-4.0/runtime/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/SpringCommandRegistry.java
URL: http://svn.apache.org/viewvc/servicemix/branches/servicemix-4.0/runtime/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/SpringCommandRegistry.java?rev=602090&r1=602089&r2=602090&view=diff
==============================================================================
--- servicemix/branches/servicemix-4.0/runtime/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/SpringCommandRegistry.java
(original)
+++ servicemix/branches/servicemix-4.0/runtime/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/SpringCommandRegistry.java
Fri Dec  7 05:06:27 2007
@@ -23,6 +23,7 @@
 import org.apache.geronimo.gshell.common.Arguments;
 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.GroupNode;
 import org.apache.geronimo.gshell.layout.model.Layout;
@@ -43,48 +44,54 @@
 
     public static final String SEPARATOR = ":";
 
+    public static final String ALIAS_PREFIX = "alias:";
+
     private Environment env;
 
-    private Map groupAliases;
     private Layout layout = new Layout();
 
     public SpringCommandRegistry(Environment env) {
         this.env = env;
     }
 
-    public Map getGroupAliases() {
-        return groupAliases;
-    }
-
-    public void setGroupAliases(Map groupAliases) {
-        this.groupAliases = groupAliases;
-    }
-
     public void register(final Command command, Map<String, ?> properties) throws DuplicateRegistrationException
{
-        String id = command.getId();
-        String[] s = id.split(SEPARATOR);
+        // Find command name
+        String name = command.getId();
+        if (name.lastIndexOf(':') >= 0) {
+            name = name.substring(name.lastIndexOf(':') + 1);
+        }
+        if (properties.containsKey("name")) {
+            name = (String) properties.get("name");
+        }
+
+        // Find or create the subshell group
         GroupNode gn = layout;
-        for (int i = 0; i < s.length - 1; i++) {
-            if (groupAliases != null && groupAliases.containsKey(s[i])) {
-                s[i] = (String) groupAliases.get(s[i]);
-                if (s[i].length() == 0) {
-                    continue;
-                }
-            }
-            Node n = gn.find(s[i]);
+        String shell = (String) properties.get("shell");
+        String[] aliases = properties.get("alias") != null ? properties.get("alias").toString().split(",")
: new String[0];
+        if (shell != null && shell.length() > 0) {
+            Node n = gn.find(shell);
             if (n == null) {
-                GroupNode g = new GroupNode(s[i]);
+                GroupNode g = new GroupNode(shell);
                 gn.add(g);
-                register(new GroupCommand(s[i], g));
+                register(new GroupCommand(shell, g));
                 gn = g;
             } else if (n instanceof GroupNode) {
                 gn = (GroupNode) n;
             } else {
-                throw new IllegalStateException("A command conflicts has been detected when
registering " + id);
+                throw new IllegalStateException("A command conflicts has been detected when
registering " + command.getId());
             }
         }
-        CommandNode cn = new CommandNode(s[s.length - 1], id);
+
+        CommandNode cn = new CommandNode(name, command.getId());
         gn.add(cn);
+
+        for (int i = 0; i < aliases.length; i++) {
+            if (!name.equals(aliases[i])) {
+                AliasNode an = new AliasNode(aliases[i], ALIAS_PREFIX + command.getId());
+                gn.add(an);
+            }
+        }
+
         register(command);
     }
 
@@ -109,6 +116,15 @@
 
     public Node findNode(Node node, String s) throws NotFoundException {
         if (node instanceof GroupNode) {
+            if (s.startsWith(ALIAS_PREFIX)) {
+                s = s.substring(ALIAS_PREFIX.length());
+                for (Node n : ((GroupNode) node).nodes()) {
+                    if (n instanceof CommandNode && ((CommandNode) n).getId().equals(s))
{
+                        return n;
+                    }
+                }
+                throw new NotFoundException(s);
+            }
             Node n = ((GroupNode) node).find(s);
             if (n instanceof GroupNode) {
                 return new CommandNode(n.getName(), n.getName());
@@ -140,23 +156,31 @@
         }
 
         public Object execute(CommandContext commandContext, Object... objects) throws Exception
{
+            env.getVariables().set(CURRENT_NODE, gn);
             if (objects.length > 0) {
-                String cmdId = String.valueOf(objects[0]);
-                Node n = gn.find(cmdId);
-                CommandContext ctx = commandContext;
-                Command cmd;
-                if (n instanceof CommandNode) {
-                    cmd = lookup(((CommandNode) n).getId());
-                } else if (n instanceof GroupNode) {
-                    cmd = new GroupCommand(cmdId, (GroupNode) n);
-                } else {
-                    throw new IllegalStateException("Unrecognized node type: " + n.getClass().getName());
+                try {
+                    String cmdId = String.valueOf(objects[0]);
+                    Node n = gn.find(cmdId);
+                    if (n == null) {
+                        n = layout.find(cmdId);
+                    }
+                    CommandContext ctx = commandContext;
+                    Command cmd;
+                    if (n instanceof CommandNode) {
+                        cmd = lookup(((CommandNode) n).getId());
+                    } else if (n instanceof GroupNode) {
+                        cmd = new GroupCommand(cmdId, (GroupNode) n);
+                    } else if (n instanceof AliasNode) {
+                        cmd = lookup(((AliasNode) n).getCommand().substring(ALIAS_PREFIX.length()));
+                    } else {
+                        throw new IllegalStateException("Unrecognized node type: " + n.getClass().getName());
+                    }
+                    return cmd.execute(ctx, Arguments.shift(objects));
+                } finally {
+                    env.getVariables().unset(CURRENT_NODE);
                 }
-                return cmd.execute(ctx, Arguments.shift(objects));
-            } else {
-                env.getVariables().set(CURRENT_NODE, gn);
-                return SUCCESS;
             }
+            return SUCCESS;
         }
     }
 }

Modified: servicemix/branches/servicemix-4.0/runtime/gshell/gshell-osgi/src/main/java/org/apache/geronimo/gshell/osgi/OsgiShell.java
URL: http://svn.apache.org/viewvc/servicemix/branches/servicemix-4.0/runtime/gshell/gshell-osgi/src/main/java/org/apache/geronimo/gshell/osgi/OsgiShell.java?rev=602090&r1=602089&r2=602090&view=diff
==============================================================================
--- servicemix/branches/servicemix-4.0/runtime/gshell/gshell-osgi/src/main/java/org/apache/geronimo/gshell/osgi/OsgiShell.java
(original)
+++ servicemix/branches/servicemix-4.0/runtime/gshell/gshell-osgi/src/main/java/org/apache/geronimo/gshell/osgi/OsgiShell.java
Fri Dec  7 05:06:27 2007
@@ -23,6 +23,7 @@
 import org.apache.geronimo.gshell.branding.BrandingSupport;
 import org.apache.geronimo.gshell.command.CommandContext;
 import org.apache.geronimo.gshell.command.annotation.CommandComponent;
+import org.apache.geronimo.gshell.support.OsgiCommandSupport;
 
 /**
  * Created by IntelliJ IDEA.



Mime
View raw message