geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r411417 - in /geronimo/sandbox/gshell/trunk: gshell-api/src/main/java/org/apache/geronimo/gshell/command/ gshell-core/src/main/java/org/apache/geronimo/gshell/ gshell-core/src/main/java/org/apache/geronimo/gshell/builtins/ gshell-core/src/m...
Date Sat, 03 Jun 2006 10:13:53 GMT
Author: jdillon
Date: Sat Jun  3 03:13:53 2006
New Revision: 411417

URL: http://svn.apache.org/viewvc?rev=411417&view=rev
Log:
Added definition-based command alias support
Help command now displays alias details

Modified:
    geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandDefinition.java
    geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandManager.java
    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/builtins/HelpCommand.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandManagerImpl.java

Modified: geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandDefinition.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandDefinition.java?rev=411417&r1=411416&r2=411417&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandDefinition.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandDefinition.java
Sat Jun  3 03:13:53 2006
@@ -16,6 +16,8 @@
 
 package org.apache.geronimo.gshell.command;
 
+import org.apache.geronimo.gshell.util.Arguments;
+
 import java.util.Properties;
 
 /**
@@ -29,6 +31,8 @@
 
     private final String classname;
 
+    private final String[] aliases;
+
     public CommandDefinition(final Properties props) throws InvalidDefinitionException {
         if (props == null) {
             throw new IllegalArgumentException("Properties is null");
@@ -43,6 +47,28 @@
         if (classname == null) {
             throw new MissingPropertyException("class", props);
         }
+
+        this.aliases = loadAliasesFrom(props);
+    }
+
+    //
+    // TODO: Add 'alias' and 'unalias' command support
+    //
+
+    private String[] loadAliasesFrom(final Properties props) {
+        String input = props.getProperty("aliases");
+
+        if (input != null) {
+            String[] list = input.split(",");
+            for (int i=0; i < list.length; i++) {
+                list[i] = list[i].trim();
+            }
+
+            return list;
+        }
+        else {
+            return new String[0];
+        }
     }
 
     public String getName() {
@@ -53,8 +79,14 @@
         return classname;
     }
 
+    public String[] getAliases() {
+        return aliases;
+    }
+
     public String toString() {
-        return getName() + "=" + getClassName();
+        return getName() + "=" + getClassName() + "{ aliases=" +
+                Arguments.asString(getAliases()) +
+                " }";
     }
 
     public Class loadClass() throws ClassNotFoundException {

Modified: geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandManager.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandManager.java?rev=411417&r1=411416&r2=411417&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandManager.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandManager.java
Sat Jun  3 03:13:53 2006
@@ -17,17 +17,24 @@
 package org.apache.geronimo.gshell.command;
 
 import java.util.Set;
+import java.util.Collection;
 
 /**
- * ???
+ * Interface for manager of command definitions and provides access to command instances.
  *
  * @version $Id$
  */
 public interface CommandManager
 {
-    void addCommandDefinition(CommandDefinition def);
+    //
+    // TODO: Rename, this has become a registry
+    //
+
+    boolean addCommandDefinition(CommandDefinition def);
 
     CommandDefinition getCommandDefinition(String name) throws CommandNotFoundException;
+
+    Collection<CommandDefinition> commandDefinitions();
 
     Set<String> commandNames();
 }

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=411417&r1=411416&r2=411417&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 03:13:53 2006
@@ -131,6 +131,11 @@
         ShellContainer container = new ShellContainer(shellContainer);
 
         CommandDefinition def = commandManager.getCommandDefinition(commandName);
+
+        //
+        // TODO: Pass the command instance the name it was registered with?, could be an
alias
+        //
+
         container.registerComponentInstance(def);
         container.registerComponentImplementation(def.loadClass());
 

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/builtins/HelpCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/builtins/HelpCommand.java?rev=411417&r1=411416&r2=411417&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/builtins/HelpCommand.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/builtins/HelpCommand.java
Sat Jun  3 03:13:53 2006
@@ -26,7 +26,9 @@
 import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.command.CommandSupport;
 import org.apache.geronimo.gshell.command.CommandManager;
+import org.apache.geronimo.gshell.command.CommandDefinition;
 import org.apache.geronimo.gshell.console.IO;
+import org.apache.geronimo.gshell.util.Arguments;
 
 /**
  * Display help
@@ -53,6 +55,7 @@
     public void setCommandManager(final CommandManager commandManager) {
         this.commandManager = commandManager;
     }
+    
     */
 
     private CommandManager getCommandManager() {
@@ -131,15 +134,25 @@
             io.out.println();
         }
         else if (topic.equals("commands")) {
-            io.out.println("Available commands:");
+            io.out.println("Available commands (and aliases):");
 
             //
             // HACK: For now just list all know commands
             //
 
-            for (String name : manager.commandNames()) {
+            for (CommandDefinition def : manager.commandDefinitions()) {
                 io.out.print("  ");
-                io.out.println(name);
+                io.out.print(def.getName());
+
+                // Include a list of aliases
+                String[] aliases = def.getAliases();
+                if (aliases.length != 0) {
+                    io.out.print(" ( ");
+                    io.out.print(Arguments.asString(aliases));
+                    io.out.print(" )");
+                }
+
+                io.out.println();
             }
 
             io.out.println();

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandManagerImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandManagerImpl.java?rev=411417&r1=411416&r2=411417&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandManagerImpl.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandManagerImpl.java
Sat Jun  3 03:13:53 2006
@@ -26,6 +26,7 @@
 import java.util.HashMap;
 import java.util.Set;
 import java.util.Collections;
+import java.util.Collection;
 
 /**
  * Manager of command definitions and provides access to command instances.
@@ -35,14 +36,12 @@
 public class CommandManagerImpl
     implements CommandManager
 {
-    //
-    // TODO: Rename, this has become a registry
-    //
-
     private static final Log log = LogFactory.getLog(CommandManager.class);
 
     private Map<String,CommandDefinition> commandDefMap = new HashMap<String,CommandDefinition>();
 
+    private Map<String,CommandDefinition> commandAliasMap = new HashMap<String,CommandDefinition>();
+
     public CommandManagerImpl() throws CommandException {
         try {
             discoverCommands();
@@ -65,16 +64,37 @@
         }
     }
 
-    public void addCommandDefinition(final CommandDefinition def) {
+    public boolean addCommandDefinition(final CommandDefinition def) {
         if (def == null) {
             throw new IllegalArgumentException("Def is null");
         }
 
-        commandDefMap.put(def.getName(), def);
+        boolean debug = log.isDebugEnabled();
 
-        if (log.isDebugEnabled()) {
+        CommandDefinition prev = commandDefMap.put(def.getName(), def);
+        if (debug) {
             log.debug("Added definition: " + def);
         }
+
+        addCommandAliases(def);
+
+        return prev != null;
+    }
+
+    private void addCommandAliases(final CommandDefinition def) {
+        assert def != null;
+
+        boolean debug = log.isDebugEnabled();
+
+        for (String alias : def.getAliases()) {
+            CommandDefinition prev = commandAliasMap.put(alias, def);
+            if (debug) {
+                log.debug("Added alias (to " + def.getName() + "): " + def);
+                if (prev != null) {
+                    log.debug("    Replaces previous alias to: " + prev.getName());
+                }
+            }
+        }
     }
 
     public CommandDefinition getCommandDefinition(String name) throws CommandNotFoundException
{
@@ -96,7 +116,11 @@
 
         CommandDefinition def = commandDefMap.get(name);
         if (def == null) {
-            throw new CommandNotFoundException(name);
+            def = commandAliasMap.get(name);
+
+            if (def == null) {
+                throw new CommandNotFoundException(name);
+            }
         }
 
         return def;
@@ -104,5 +128,9 @@
 
     public Set<String> commandNames() {
         return Collections.unmodifiableSet(commandDefMap.keySet());
+    }
+
+    public Collection<CommandDefinition> commandDefinitions() {
+        return Collections.unmodifiableCollection(commandDefMap.values());
     }
 }



Mime
View raw message