geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r581649 - in /geronimo/sandbox/gshell/trunk: gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/ gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/ gshell-core/src/main/java/org/apac...
Date Wed, 03 Oct 2007 16:34:18 GMT
Author: gnodet
Date: Wed Oct  3 09:34:16 2007
New Revision: 581649

URL: http://svn.apache.org/viewvc?rev=581649&view=rev
Log:
GSHELL-35: Make CommandDescriptor optional and make Command stateless

Added:
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/plugin/PlexusCommandWrapper.java
Modified:
    geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/Command.java
    geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/CommandContext.java
    geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
    geronimo/sandbox/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/HelpCommand.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/DefaultLayoutManager.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/LayoutManager.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/plugin/CommandDiscoveryListener.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/registry/CommandRegistry.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/registry/DefaultCommandRegistry.java

Modified: geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/Command.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/Command.java?rev=581649&r1=581648&r2=581649&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/Command.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/Command.java
Wed Oct  3 09:34:16 2007
@@ -33,14 +33,22 @@
     Result FAILURE = Result.FAILURE;
 
     /**
-     * Initialize the command with its execution context.
+     * Command id
+     *
+     * @return
      */
-    void init(CommandContext context);
+    String getId();
+
+    /**
+     * Description of the command
+     * @return
+     */
+    String getDescription();
 
     /**
      * Execute the commands behavior.
      */
-    Object execute(Object... args) throws Exception;
+    Object execute(CommandContext context, Object... args) throws Exception;
 
     //
     // Result

Modified: geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/CommandContext.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/CommandContext.java?rev=581649&r1=581648&r2=581649&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/CommandContext.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/CommandContext.java
Wed Oct  3 09:34:16 2007
@@ -31,6 +31,4 @@
     IO getIO();
 
     Variables getVariables();
-
-    CommandDescriptor getCommandDescriptor();
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java?rev=581649&r1=581648&r2=581649&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
Wed Oct  3 09:34:16 2007
@@ -22,6 +22,7 @@
 import org.apache.geronimo.gshell.clp.CommandLineProcessor;
 import org.apache.geronimo.gshell.clp.Option;
 import org.apache.geronimo.gshell.clp.Printer;
+import org.apache.geronimo.gshell.command.annotation.CommandComponent;
 import org.apache.geronimo.gshell.common.Arguments;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -45,6 +46,22 @@
     @Option(name="-h", aliases={"--help"}, description="Display this help message")
     private boolean displayHelp;
 
+    public String getId() {
+        CommandComponent cmd = getClass().getAnnotation(CommandComponent.class);
+        if (cmd == null) {
+            throw new IllegalStateException("Command id not found");
+        }
+        return cmd.id();
+    }
+
+    public String getDescription() {
+        CommandComponent cmd = getClass().getAnnotation(CommandComponent.class);
+        if (cmd == null) {
+            throw new IllegalStateException("Command description not found");
+        }
+        return cmd.description();
+    }
+
     public void init(final CommandContext context) {
         assert context != null;
 
@@ -53,13 +70,16 @@
         this.variables = context.getVariables();
 
         // Re-setup logging using our id
-        String id = context.getCommandDescriptor().getId();
+        String id = getId();
         log = LoggerFactory.getLogger(getClass().getName() + "." + id);
     }
 
-    public Object execute(final Object... args) throws Exception {
+    public Object execute(final CommandContext context, final Object... args) throws Exception
{
+        assert context != null;
         assert args != null;
 
+        init(context);
+
         log.info("Executing w/args: [{}]", Arguments.asString(args));
 
         CommandLineProcessor clp = new CommandLineProcessor(this);
@@ -92,7 +112,7 @@
         // TODO: Need to ask the LayoutManager what the real name is for our command's ID
         //
 
-        io.out.println(context.getCommandDescriptor().getId());
+        io.out.println(getId());
         io.out.println(" -- ");
         io.out.println();
 

Modified: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/HelpCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/HelpCommand.java?rev=581649&r1=581648&r2=581649&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/HelpCommand.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/HelpCommand.java
Wed Oct  3 09:34:16 2007
@@ -19,11 +19,11 @@
 
 package org.apache.geronimo.gshell.commands.builtins;
 
-import java.util.Collection;
-
 import org.apache.geronimo.gshell.ansi.Code;
 import org.apache.geronimo.gshell.ansi.Renderer;
 import org.apache.geronimo.gshell.branding.Branding;
+import org.apache.geronimo.gshell.clp.Argument;
+import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.command.CommandSupport;
 import org.apache.geronimo.gshell.command.annotation.CommandComponent;
 import org.apache.geronimo.gshell.command.descriptor.CommandDescriptor;
@@ -32,6 +32,8 @@
 import org.codehaus.plexus.component.annotations.Requirement;
 import org.codehaus.plexus.util.StringUtils;
 
+import java.util.Collection;
+
 /**
  * Display help
  *
@@ -50,48 +52,63 @@
     @Requirement
     private Branding branding;
 
+    @Argument(description = "Command name")
+    private String command;
+
     private Renderer renderer = new Renderer();
 
     protected Object doExecute() throws Exception {
         assert commandRegistry != null;
         assert layoutManager != null;
 
-        io.out.println();
-        io.out.print(branding.getAbout());
-        io.out.println();
-
-        io.out.println("Available commands:");
-
-        Collection<CommandDescriptor> commands = commandRegistry.descriptors();
-
-        // Figure out the maximum length of a command name
-        int maxNameLen = 0;
-        for (CommandDescriptor desc : commands) {
-            if (desc.getId().length() > maxNameLen) {
-                maxNameLen = desc.getId().length();
+        if (command == null) {
+            io.out.println();
+            io.out.print(branding.getAbout());
+            io.out.println();
+
+            io.out.println("Available commands:");
+
+            Collection<Command> commands = commandRegistry.commands();
+
+            // Figure out the maximum length of a command name
+            int maxNameLen = 0;
+            for (Command desc : commands) {
+                if (desc.getId().length() > maxNameLen) {
+                    maxNameLen = desc.getId().length();
+                }
             }
-        }
 
-        //
-        // TODO: Need to ask the LayoutManager...
-        //
+            //
+            // TODO: Need to ask the LayoutManager...
+            //
+
+            for (Command d : commands) {
+                // Hide commands if they don't have descriptions
+                String name = d.getId();
+                name = StringUtils.rightPad(name, maxNameLen);
 
-        for (CommandDescriptor d : commands) {
-            // Hide commands if they don't have descriptions
-            String name = d.getId();
-            name = StringUtils.rightPad(name, maxNameLen);
-
-            io.out.print("  ");
-            io.out.print(renderer.render(Renderer.encode(name, Code.BOLD)));
+                io.out.print("  ");
+                io.out.print(renderer.render(Renderer.encode(name, Code.BOLD)));
 
-            String desc = d.getDescription();
+                String desc = d.getDescription();
 
-            if (desc != null) {
-                io.out.print("  ");
-                io.out.println(desc);
+                if (desc != null) {
+                    io.out.print("  ");
+                    io.out.println(desc);
+                }
+                else {
+                    io.out.println();
+                }
+            }
+        } else {
+            Command cmd = commandRegistry.lookup(command);
+            if (cmd == null) {
+                io.out.println("Command " + Renderer.encode(command, Code.BOLD) + " not found.");
+                io.out.println("Try " + Renderer.encode("help", Code.BOLD) + " for a list
of available commands.");
             }
             else {
-                io.out.println();
+                io.out.println("Command " + Renderer.encode(command, Code.BOLD));
+                io.out.println("   " + cmd.getDescription());
             }
         }
 

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java?rev=581649&r1=581648&r2=581649&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java
Wed Oct  3 09:34:16 2007
@@ -104,8 +104,8 @@
         log.info("Executing ({}): [{}]", path, Arguments.asString(args));
 
         // Look up the command descriptor for the given path
-        final CommandDescriptor desc = layoutManager.find(path);
-        if (desc == null) {
+        final Command command = layoutManager.find(path);
+        if (command == null) {
             throw new CommandNotFoundException(path);
         }
 
@@ -114,9 +114,6 @@
 
         log.debug("Child container realm: {}", realmId);
 
-        final PlexusContainer childContainer = container.createChildContainer(realmId, container.getContainerRealm());
-        final Command command = (Command)childContainer.lookup(desc.getRole(), desc.getRoleHint());
-
         // Setup the command context and pass it to the command instance
         CommandContext context = new CommandContext() {
             // Command instances get their own namespace with defaults from the current
@@ -130,18 +127,14 @@
                 return vars;
             }
 
-            public CommandDescriptor getCommandDescriptor() {
-                return desc;
-            }
         };
-        command.init(context);
 
         // Setup command timings
         StopWatch watch = new StopWatch(true);
 
         Object result;
         try {
-            result = command.execute(args);
+            result = command.execute(context, args);
 
             log.debug("Command completed with result: {}, after: {}", result, watch);
         }
@@ -151,9 +144,6 @@
                 env.getIO().flush();
             }
             catch (Exception ignore) {}
-
-            // Nuke the child container
-            container.removeChildContainer(realmId);
         }
 
         return result;

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/DefaultLayoutManager.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/DefaultLayoutManager.java?rev=581649&r1=581648&r2=581649&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/DefaultLayoutManager.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/DefaultLayoutManager.java
Wed Oct  3 09:34:16 2007
@@ -19,7 +19,7 @@
 
 package org.apache.geronimo.gshell.layout;
 
-import org.apache.geronimo.gshell.command.descriptor.CommandDescriptor;
+import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.layout.loader.LayoutLoader;
 import org.apache.geronimo.gshell.layout.model.Layout;
 import org.apache.geronimo.gshell.registry.CommandRegistry;
@@ -78,10 +78,10 @@
         return layout;
     }
 
-    public CommandDescriptor find(final String path) {
+    public Command find(final String path) {
         assert path != null;
 
-        log.debug("Searching for command descriptor for path: {}", path);
+        log.debug("Searching for command for path: {}", path);
 
         //
         // HACK: For now, assume the path is just the id... should eventually change this

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/LayoutManager.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/LayoutManager.java?rev=581649&r1=581648&r2=581649&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/LayoutManager.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/LayoutManager.java
Wed Oct  3 09:34:16 2007
@@ -19,7 +19,7 @@
 
 package org.apache.geronimo.gshell.layout;
 
-import org.apache.geronimo.gshell.command.descriptor.CommandDescriptor;
+import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.layout.model.Layout;
 
 /**
@@ -35,7 +35,7 @@
     Layout getLayout();
 
     /**
-     * Find the command descriptor for the given path, or null if not found.
+     * Find the command for the given path, or null if not found.
      */
-    CommandDescriptor find(String path);
+    Command find(String path);
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/plugin/CommandDiscoveryListener.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/plugin/CommandDiscoveryListener.java?rev=581649&r1=581648&r2=581649&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/plugin/CommandDiscoveryListener.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/plugin/CommandDiscoveryListener.java
Wed Oct  3 09:34:16 2007
@@ -21,12 +21,14 @@
 
 import org.apache.geronimo.gshell.command.descriptor.CommandDescriptor;
 import org.apache.geronimo.gshell.command.descriptor.CommandSetDescriptor;
+import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.registry.CommandRegistry;
 import org.codehaus.plexus.component.annotations.Component;
 import org.codehaus.plexus.component.annotations.Requirement;
 import org.codehaus.plexus.component.discovery.ComponentDiscoveryEvent;
 import org.codehaus.plexus.component.discovery.ComponentDiscoveryListener;
 import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
+import org.codehaus.plexus.PlexusContainer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -42,6 +44,9 @@
     private final Logger log = LoggerFactory.getLogger(getClass());
 
     @Requirement
+    private PlexusContainer container;
+
+    @Requirement
     private CommandRegistry registry;
 
     public String getId() {
@@ -59,7 +64,7 @@
             CommandSetDescriptor commands = (CommandSetDescriptor) set;
 
             for (CommandDescriptor descriptor : commands.getCommandDescriptors()) {
-                registry.register(descriptor);
+                registry.register(new PlexusCommandWrapper(container, descriptor));
             }
         }
     }

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/plugin/PlexusCommandWrapper.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/plugin/PlexusCommandWrapper.java?rev=581649&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/plugin/PlexusCommandWrapper.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/plugin/PlexusCommandWrapper.java
Wed Oct  3 09:34:16 2007
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.plugin;
+
+import org.apache.geronimo.gshell.command.Command;
+import org.apache.geronimo.gshell.command.CommandContext;
+import org.apache.geronimo.gshell.command.descriptor.CommandDescriptor;
+import org.codehaus.plexus.PlexusContainer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.UUID;
+
+/**
+ * A command wrapper using the CommandDescriptor
+ *
+ * @version $Rev: 581061 $ $Date: 2007-10-01 22:18:31 +0200 (Mon, 01 Oct 2007) $
+ */
+public class PlexusCommandWrapper implements Command {
+
+    private Logger log = LoggerFactory.getLogger(getClass());
+    private CommandDescriptor descriptor;
+    private PlexusContainer container;
+
+    public PlexusCommandWrapper(PlexusContainer container,
+                                CommandDescriptor descriptor) {
+        this.container = container;
+        this.descriptor = descriptor;
+    }
+
+    public String getId() {
+        return descriptor.getId();
+    }
+
+    public String getDescription() {
+        return descriptor.getDescription();
+    }
+
+    public Object execute(CommandContext context, Object... args) throws Exception {
+        // Create a new child container for the invocation and lookup the command instance
+        String realmId = "gshell:" + UUID.randomUUID();
+
+        log.debug("Child container realm: {}", realmId);
+
+        final PlexusContainer childContainer = container.createChildContainer(realmId, container.getContainerRealm());
+        final Command command = (Command) childContainer.lookup(descriptor.getRole(), descriptor.getRoleHint());
+
+        try {
+            return command.execute(context, args);
+        }
+        finally {
+            // Nuke the child container
+            container.removeChildContainer(realmId);
+        }
+    }
+}

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/registry/CommandRegistry.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/registry/CommandRegistry.java?rev=581649&r1=581648&r2=581649&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/registry/CommandRegistry.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/registry/CommandRegistry.java
Wed Oct  3 09:34:16 2007
@@ -19,10 +19,11 @@
 
 package org.apache.geronimo.gshell.registry;
 
-import java.util.Collection;
-
+import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.command.descriptor.CommandDescriptor;
 
+import java.util.Collection;
+
 /**
  * ???
  *
@@ -30,9 +31,11 @@
  */
 public interface CommandRegistry
 {
-    void register(CommandDescriptor descriptor);
+    void register(Command command);
+
+    void unregister(Command command);
 
-    Collection<CommandDescriptor> descriptors();
+    Collection<Command> commands();
     
-    CommandDescriptor lookup(String id);
+    Command lookup(String id);
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/registry/DefaultCommandRegistry.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/registry/DefaultCommandRegistry.java?rev=581649&r1=581648&r2=581649&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/registry/DefaultCommandRegistry.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/registry/DefaultCommandRegistry.java
Wed Oct  3 09:34:16 2007
@@ -19,16 +19,17 @@
 
 package org.apache.geronimo.gshell.registry;
 
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
+import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.command.descriptor.CommandDescriptor;
 import org.codehaus.plexus.component.annotations.Component;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * Registers command components as they are discovered by the container.
  *
@@ -40,29 +41,43 @@
 {
     private final Logger log = LoggerFactory.getLogger(getClass());
 
-    private Map<String, CommandDescriptor> descriptors = new HashMap<String,CommandDescriptor>();
+    private Map<String, Command> commands = new HashMap<String, Command>();
 
-    public void register(final CommandDescriptor descriptor) {
-        assert descriptor != null;
+    public void register(final Command command) {
+        assert command != null;
 
-        String id = descriptor.getId();
+        String id = command.getId();
 
-        if (descriptors.containsKey(id)) {
+        if (commands.containsKey(id)) {
             log.error("Ignoring duplicate: {}", id);
         }
         else {
-            descriptors.put(id, descriptor);
+            commands.put(id, command);
             log.debug("Registered: {}", id);
         }
     }
 
-    public CommandDescriptor lookup(final String id) {
+    public void unregister(final Command command) {
+        assert command != null;
+
+        String id = command.getId();
+
+        if (!commands.containsKey(id)) {
+            log.error("Ignoring uregistered: {}", id);
+        }
+        else {
+            commands.remove(id);
+            log.debug("Unregistered: {}", id);
+        }
+    }
+
+    public Command lookup(final String id) {
         assert id != null;
 
-        return descriptors.get(id);
+        return commands.get(id);
     }
 
-    public Collection<CommandDescriptor> descriptors() {
-        return Collections.unmodifiableCollection(descriptors.values());
+    public Collection<Command> commands() {
+        return Collections.unmodifiableCollection(commands.values());
     }
 }



Mime
View raw message