geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r697851 - in /geronimo/gshell/trunk: gshell-commands/gshell-builtins/src/main/resources/META-INF/spring/ gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/ gshell-wisdom/gshell-wisdom-core/src/main/res...
Date Mon, 22 Sep 2008 14:34:38 GMT
Author: jdillon
Date: Mon Sep 22 07:34:37 2008
New Revision: 697851

URL: http://svn.apache.org/viewvc?rev=697851&view=rev
Log:
Added StatefulCommand
Moved the meat of CommandAction execution to CommandSupport
Changed gshell-builtins commands (except clear) to be stateful

Added:
    geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatefulCommand.java
  (contents, props changed)
      - copied, changed from r697823, geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatelessCommand.java
Modified:
    geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/resources/META-INF/spring/components.xml
    geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/AliasCommand.java
    geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/CommandSupport.java
    geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/GroupCommand.java
    geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatelessCommand.java
    geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/resources/META-INF/spring/components.xml

Modified: geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/resources/META-INF/spring/components.xml
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/resources/META-INF/spring/components.xml?rev=697851&r1=697850&r2=697851&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/resources/META-INF/spring/components.xml
(original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/resources/META-INF/spring/components.xml
Mon Sep 22 07:34:37 2008
@@ -41,65 +41,58 @@
         </property>
     </bean>
 
+    <bean name="gshell-builtins:help" class="org.apache.geronimo.gshell.commands.builtins.HelpAction"
scope="prototype"/>
+
+    <bean name="gshell-builtins:exit" class="org.apache.geronimo.gshell.commands.builtins.ExitAction"
scope="prototype"/>
+
+    <bean name="gshell-builtins:echo" class="org.apache.geronimo.gshell.commands.builtins.EchoAction"
scope="prototype"/>
+
+    <bean name="gshell-builtins:clear" class="org.apache.geronimo.gshell.commands.builtins.ClearAction"
scope="prototype"/>
+
+    <bean name="gshell-builtins:source" class="org.apache.geronimo.gshell.commands.builtins.SourceAction"
scope="prototype"/>
+
+    <bean name="gshell-builtins:set" class="org.apache.geronimo.gshell.commands.builtins.SetAction"
scope="prototype"/>
+
+    <bean name="gshell-builtins:unset" class="org.apache.geronimo.gshell.commands.builtins.UnsetAction"
scope="prototype"/>
+
     <bean class="org.apache.geronimo.gshell.wisdom.plugin.bundle.CommandBundle" lazy-init="true">
         <property name="id" value="default"/>
 
         <property name="commands">
             <list>
-                <bean parent="commandTemplate">
+                <bean parent="statelessCommandTemplate">
                     <property name="id" value="gshell-builtins:clear"/>
-
-                    <property name="action">
-                        <bean class="org.apache.geronimo.gshell.commands.builtins.ClearAction"/>
-                    </property>
+                    <property name="action" ref="gshell-builtins:clear"/>
                 </bean>
 
-                <bean parent="commandTemplate">
+                <bean parent="statefulCommandTemplate">
                     <property name="id" value="gshell-builtins:echo"/>
-
-                    <property name="action">
-                        <bean class="org.apache.geronimo.gshell.commands.builtins.EchoAction"/>
-                    </property>
+                    <property name="actionId" value="gshell-builtins:echo"/>
                 </bean>
 
-                <bean parent="commandTemplate">
+                <bean parent="statefulCommandTemplate">
                     <property name="id" value="gshell-builtins:exit"/>
-
-                    <property name="action">
-                        <bean class="org.apache.geronimo.gshell.commands.builtins.ExitAction"/>
-                    </property>
+                    <property name="actionId" value="gshell-builtins:exit"/>
                 </bean>
 
-                <bean parent="commandTemplate">
+                <bean parent="statefulCommandTemplate">
                     <property name="id" value="gshell-builtins:help"/>
-
-                    <property name="action">
-                        <bean class="org.apache.geronimo.gshell.commands.builtins.HelpAction"/>
-                    </property>
+                    <property name="actionId" value="gshell-builtins:help"/>
                 </bean>
 
-                <bean parent="commandTemplate">
+                <bean parent="statefulCommandTemplate">
                     <property name="id" value="gshell-builtins:source"/>
-
-                    <property name="action">
-                        <bean class="org.apache.geronimo.gshell.commands.builtins.SourceAction"/>
-                    </property>
+                    <property name="actionId" value="gshell-builtins:source"/>
                 </bean>
                 
-                <bean parent="commandTemplate">
+                <bean parent="statefulCommandTemplate">
                     <property name="id" value="gshell-builtins:set"/>
-
-                    <property name="action">
-                        <bean class="org.apache.geronimo.gshell.commands.builtins.SetAction"/>
-                    </property>
+                    <property name="actionId" value="gshell-builtins:set"/>
                 </bean>
 
-                <bean parent="commandTemplate">
+                <bean parent="statefulCommandTemplate">
                     <property name="id" value="gshell-builtins:unset"/>
-
-                    <property name="action">
-                        <bean class="org.apache.geronimo.gshell.commands.builtins.UnsetAction"/>
-                    </property>
+                    <property name="actionId" value="gshell-builtins:unset"/>
                 </bean>
             </list>
         </property>

Modified: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/AliasCommand.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/AliasCommand.java?rev=697851&r1=697850&r2=697851&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/AliasCommand.java
(original)
+++ geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/AliasCommand.java
Mon Sep 22 07:34:37 2008
@@ -32,8 +32,7 @@
 public class AliasCommand
     extends CommandSupport
 {
-    private final Logger log = LoggerFactory.getLogger(getClass());
-
+    @Override
     public CommandResult execute(final ShellContext context, final Object[] args) {
         assert context != null;
         assert args != null;

Modified: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/CommandSupport.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/CommandSupport.java?rev=697851&r1=697850&r2=697851&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/CommandSupport.java
(original)
+++ geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/CommandSupport.java
Mon Sep 22 07:34:37 2008
@@ -24,7 +24,20 @@
 import org.apache.geronimo.gshell.command.CommandAware;
 import org.apache.geronimo.gshell.command.CommandCompleter;
 import org.apache.geronimo.gshell.command.CommandDocumenter;
+import org.apache.geronimo.gshell.command.CommandResult;
+import org.apache.geronimo.gshell.command.CommandContext;
+import org.apache.geronimo.gshell.command.Variables;
+import org.apache.geronimo.gshell.command.Arguments;
 import org.apache.geronimo.gshell.i18n.MessageSource;
+import org.apache.geronimo.gshell.spring.BeanContainerAware;
+import org.apache.geronimo.gshell.spring.BeanContainer;
+import org.apache.geronimo.gshell.shell.ShellContext;
+import org.apache.geronimo.gshell.io.IO;
+import org.apache.geronimo.gshell.notification.Notification;
+import org.apache.geronimo.gshell.clp.CommandLineProcessor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.slf4j.MDC;
 
 /**
  * Provides support for {@link Command} implementations.
@@ -32,8 +45,10 @@
  * @version $Rev$ $Date$
  */
 public abstract class CommandSupport
-    implements Command
+    implements Command, BeanContainerAware
 {
+    protected final Logger log = LoggerFactory.getLogger(getClass());
+
     private String id;
 
     private CommandAction action;
@@ -71,6 +86,8 @@
 
         handleCommandAware(action);
 
+        log.trace("Action: {}", action);
+
         this.action = action;
     }
 
@@ -87,6 +104,8 @@
 
         handleCommandAware(documenter);
 
+        log.trace("Documenter: {}", documenter);
+
         this.documenter = documenter;
     }
 
@@ -103,6 +122,8 @@
 
         handleCommandAware(completer);
 
+        log.trace("Completer: {}", completer);
+
         this.completer = completer;
     }
 
@@ -119,6 +140,8 @@
 
         handleCommandAware(messages);
 
+        log.trace("Messages: {}", messages);
+
         this.messages = messages;
     }
 
@@ -129,4 +152,141 @@
             ((CommandAware)target).setCommand(this);
         }
     }
+
+    // BeanContainerAware
+
+    private BeanContainer container;
+
+    public void setBeanContainer(final BeanContainer container) {
+        assert container != null;
+
+        this.container = container;
+    }
+
+    protected BeanContainer getContainer() {
+        if (container == null) {
+            throw new IllegalStateException("Bean container not configured");
+        }
+
+        return container;
+    }
+
+    // CommandAction execution
+    
+    public CommandResult execute(final ShellContext context, final Object[] args) {
+        assert context != null;
+        assert args != null;
+
+        log.trace("Executing");
+
+        // Provide logging context for the command execution
+        MDC.put("command-id", getId());
+
+        // Set the TCL to the command bean containers realm
+        final ClassLoader prevCL = Thread.currentThread().getContextClassLoader();
+        Thread.currentThread().setContextClassLoader(getContainer().getClassRealm());
+
+        CommandResult result;
+
+        try {
+            final IO io = context.getIo();
+            CommandAction action = getAction();
+
+            // Setup the command action
+            try {
+                // Process command line options/arguments
+                if (processArguments(io, action, args)) {
+                    // return if we have been asked to display --help
+                    return new CommandResult.ValueResult(CommandAction.Result.SUCCESS);
+                }
+
+                // TODO: Add preferences processor
+            }
+            catch (Exception e) {
+                return new CommandResult.FailureResult(e);
+            }
+
+            // Setup the command context
+            CommandContext ctx = new CommandContext() {
+                private final Variables variables = new Variables(context.getVariables());
+
+                public Object[] getArguments() {
+                    return args;
+                }
+
+                public IO getIo() {
+                    return io;
+                }
+
+                public Variables getVariables() {
+                    return variables;
+                }
+
+                public Command getCommand() {
+                    return CommandSupport.this;
+                }
+            };
+
+            // Execute the action
+            try {
+                log.trace("Executing action: {}", action);
+
+                Object value = action.execute(ctx);
+
+                log.trace("Result: {}", value);
+
+                result = new CommandResult.ValueResult(value);
+            }
+            catch (final Notification n) {
+                log.trace("Notified: {}, n");
+
+                result = new CommandResult.NotificationResult(n);
+            }
+            catch (final Throwable t) {
+                log.trace("Caught: {}", t);
+
+                result = new CommandResult.FailureResult(t);
+            }
+        }
+        finally {
+            Thread.currentThread().setContextClassLoader(prevCL);
+
+            MDC.remove("command-id");
+        }
+
+        return result;
+    }
+
+    private boolean processArguments(final IO io, final CommandAction action, final Object[]
args) throws Exception {
+        assert io != null;
+        assert action != null;
+        assert args != null;
+
+        if (log.isTraceEnabled()) {
+            log.trace("Processing arguments: {}", Arguments.toStringArray(args));
+        }
+
+        CommandLineProcessor clp = new CommandLineProcessor();
+        clp.addBean(action);
+
+        // Attach some help context
+        CommandDocumenter documenter = getDocumenter();
+        clp.addBean(documenter);
+
+        HelpSupport help = new HelpSupport();
+        clp.addBean(help);
+
+        // Process the arguments
+        clp.process(Arguments.toStringArray(args));
+
+        // Render command-line usage
+        if (help.displayHelp) {
+            log.trace("Render command-line usage");
+
+            documenter.renderUsage(io.out);
+            return true;
+        }
+
+        return false;
+    }
 }
\ No newline at end of file

Modified: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/GroupCommand.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/GroupCommand.java?rev=697851&r1=697850&r2=697851&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/GroupCommand.java
(original)
+++ geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/GroupCommand.java
Mon Sep 22 07:34:37 2008
@@ -32,8 +32,7 @@
 public class GroupCommand
     extends CommandSupport
 {
-    private final Logger log = LoggerFactory.getLogger(getClass());
-
+    @Override
     public CommandResult execute(final ShellContext context, final Object[] args) {
         assert context != null;
         assert args != null;

Copied: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatefulCommand.java
(from r697823, geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatelessCommand.java)
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatefulCommand.java?p2=geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatefulCommand.java&p1=geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatelessCommand.java&r1=697823&r2=697851&rev=697851&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatelessCommand.java
(original)
+++ geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatefulCommand.java
Mon Sep 22 07:34:37 2008
@@ -19,181 +19,96 @@
 
 package org.apache.geronimo.gshell.wisdom.command;
 
-import org.apache.geronimo.gshell.clp.CommandLineProcessor;
-import org.apache.geronimo.gshell.clp.Option;
-import org.apache.geronimo.gshell.command.Arguments;
-import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.command.CommandAction;
-import org.apache.geronimo.gshell.command.CommandContext;
+import org.apache.geronimo.gshell.command.CommandCompleter;
 import org.apache.geronimo.gshell.command.CommandDocumenter;
 import org.apache.geronimo.gshell.command.CommandResult;
-import org.apache.geronimo.gshell.command.Variables;
-import org.apache.geronimo.gshell.command.CommandCompleter;
-import org.apache.geronimo.gshell.io.IO;
-import org.apache.geronimo.gshell.notification.Notification;
-import org.apache.geronimo.gshell.spring.BeanContainer;
-import org.apache.geronimo.gshell.spring.BeanContainerAware;
-import org.apache.geronimo.gshell.shell.ShellContext;
 import org.apache.geronimo.gshell.i18n.MessageSource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.slf4j.MDC;
+import org.apache.geronimo.gshell.shell.ShellContext;
 
 /**
- * Stateless {@link org.apache.geronimo.gshell.command.Command} component.
+ * Stateful {@link org.apache.geronimo.gshell.command.Command} component.
  *
  * @version $Rev$ $Date$
  */
-public class StatelessCommand
+public class StatefulCommand
     extends CommandSupport
-    implements BeanContainerAware
 {
-    private final Logger log = LoggerFactory.getLogger(getClass());
+    private InheritableThreadLocal<CommandAction> actionHolder = new InheritableThreadLocal<CommandAction>();
 
-    private BeanContainer container;
+    private String actionId;
 
-    public void setBeanContainer(final BeanContainer container) {
-        assert container != null;
+    public String getActionId() {
+        if (actionId == null) {
+            throw new IllegalStateException("Missing required property: actionId");
+        }
 
-        this.container = container;
+        return actionId;
     }
 
-    // Expose some of our super-classes properties for spring configuration
+    public void setActionId(final String actionId) {
+        assert actionId != null;
 
-    @Override
-    public void setAction(final CommandAction action) {
-        super.setAction(action);
+        this.actionId = actionId;
     }
 
     @Override
-    public void setDocumenter(final CommandDocumenter documenter) {
-        super.setDocumenter(documenter);
-    }
+    public synchronized CommandAction getAction() {
+        CommandAction action = actionHolder.get();
 
-    @Override
-    public void setCompleter(final CommandCompleter completer) {
-        super.setCompleter(completer);
+        if (action == null) {
+            action = createAction();
+            setAction(action);
+        }
+
+        return action;
     }
 
     @Override
-    public void setMessages(final MessageSource messages) {
-        super.setMessages(messages);
+    protected synchronized void setAction(final CommandAction action) {
+        super.setAction(action);
+
+        actionHolder.set(action);
     }
 
-    public CommandResult execute(final ShellContext context, final Object[] args) {
-        assert context != null;
-        assert args != null;
+    private synchronized CommandAction createAction() {
+        String id = getActionId();
 
-        log.trace("Executing");
+        log.debug("Creating action for ID: {}", id);
 
-        // Provide logging context for the command execution
-        MDC.put("command-id", getId());
+        return getContainer().getBean(id, CommandAction.class);
+    }
 
-        // Set the TCL to the command bean containers realm
-        final ClassLoader prevCL = Thread.currentThread().getContextClassLoader();
-        Thread.currentThread().setContextClassLoader(container.getClassRealm());
+    private synchronized void clearAction() {
+        actionHolder.remove();
 
-        CommandResult result;
+        log.debug("Action cleared");
+    }
 
+    @Override
+    public CommandResult execute(final ShellContext context, final Object[] args) {
         try {
-            final IO io = context.getIo();
-            CommandAction action = getAction();
-
-            // Setup the command action
-            try {
-                // Process command line options/arguments
-                if (processArguments(io, action, args)) {
-                    // return if we have been asked to display --help
-                    return new CommandResult.ValueResult(CommandAction.Result.SUCCESS);
-                }
-
-                // TODO: Add preferences processor
-            }
-            catch (Exception e) {
-                return new CommandResult.FailureResult(e);
-            }
-
-            // Setup the command context
-            CommandContext ctx = new CommandContext() {
-                private final Variables variables = new Variables(context.getVariables());
-
-                public Object[] getArguments() {
-                    return args;
-                }
-
-                public IO getIo() {
-                    return io;
-                }
-
-                public Variables getVariables() {
-                    return variables;
-                }
-
-                public Command getCommand() {
-                    return StatelessCommand.this;
-                }
-            };
-
-            // Execute the action
-            try {
-                log.trace("Executing action: {}", action);
-
-                Object value = action.execute(ctx);
-
-                log.trace("Result: {}", value);
-
-                result = new CommandResult.ValueResult(value);
-            }
-            catch (final Notification n) {
-                log.trace("Notified: {}, n");
-
-                result = new CommandResult.NotificationResult(n);
-            }
-            catch (final Throwable t) {
-                log.trace("Caught: {}", t);
-
-                result = new CommandResult.FailureResult(t);
-            }
+            return super.execute(context, args);
         }
         finally {
-            Thread.currentThread().setContextClassLoader(prevCL);
-
-            MDC.remove("command-id");
+            clearAction();
         }
-
-        return result;
     }
 
-    private boolean processArguments(final IO io, final CommandAction action, final Object[]
args) throws Exception {
-        assert io != null;
-        assert action != null;
-        assert args != null;
-
-        if (log.isTraceEnabled()) {
-            log.trace("Processing arguments: {}", Arguments.toStringArray(args));
-        }
+    // Expose some of our super-classes properties for spring configuration
 
-        CommandLineProcessor clp = new CommandLineProcessor();
-        clp.addBean(action);
+    @Override
+    public void setDocumenter(final CommandDocumenter documenter) {
+        super.setDocumenter(documenter);
+    }
 
-        // Attach some help context
-        CommandDocumenter documenter = getDocumenter();
-        clp.addBean(documenter);
-
-        HelpSupport help = new HelpSupport();
-        clp.addBean(help);
-
-        // Process the arguments
-        clp.process(Arguments.toStringArray(args));
-
-        // Render command-line usage
-        if (help.displayHelp) {
-            log.trace("Render command-line usage");
-            
-            documenter.renderUsage(io.out);
-            return true;
-        }
+    @Override
+    public void setCompleter(final CommandCompleter completer) {
+        super.setCompleter(completer);
+    }
 
-        return false;
+    @Override
+    public void setMessages(final MessageSource messages) {
+        super.setMessages(messages);
     }
 }
\ No newline at end of file

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

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

Propchange: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatefulCommand.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

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

Modified: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatelessCommand.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatelessCommand.java?rev=697851&r1=697850&r2=697851&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatelessCommand.java
(original)
+++ geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatelessCommand.java
Mon Sep 22 07:34:37 2008
@@ -19,25 +19,11 @@
 
 package org.apache.geronimo.gshell.wisdom.command;
 
-import org.apache.geronimo.gshell.clp.CommandLineProcessor;
-import org.apache.geronimo.gshell.clp.Option;
-import org.apache.geronimo.gshell.command.Arguments;
-import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.command.CommandAction;
-import org.apache.geronimo.gshell.command.CommandContext;
-import org.apache.geronimo.gshell.command.CommandDocumenter;
-import org.apache.geronimo.gshell.command.CommandResult;
-import org.apache.geronimo.gshell.command.Variables;
 import org.apache.geronimo.gshell.command.CommandCompleter;
-import org.apache.geronimo.gshell.io.IO;
-import org.apache.geronimo.gshell.notification.Notification;
-import org.apache.geronimo.gshell.spring.BeanContainer;
-import org.apache.geronimo.gshell.spring.BeanContainerAware;
-import org.apache.geronimo.gshell.shell.ShellContext;
+import org.apache.geronimo.gshell.command.CommandDocumenter;
 import org.apache.geronimo.gshell.i18n.MessageSource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.slf4j.MDC;
+import org.apache.geronimo.gshell.spring.BeanContainerAware;
 
 /**
  * Stateless {@link org.apache.geronimo.gshell.command.Command} component.
@@ -48,16 +34,6 @@
     extends CommandSupport
     implements BeanContainerAware
 {
-    private final Logger log = LoggerFactory.getLogger(getClass());
-
-    private BeanContainer container;
-
-    public void setBeanContainer(final BeanContainer container) {
-        assert container != null;
-
-        this.container = container;
-    }
-
     // Expose some of our super-classes properties for spring configuration
 
     @Override
@@ -79,121 +55,4 @@
     public void setMessages(final MessageSource messages) {
         super.setMessages(messages);
     }
-
-    public CommandResult execute(final ShellContext context, final Object[] args) {
-        assert context != null;
-        assert args != null;
-
-        log.trace("Executing");
-
-        // Provide logging context for the command execution
-        MDC.put("command-id", getId());
-
-        // Set the TCL to the command bean containers realm
-        final ClassLoader prevCL = Thread.currentThread().getContextClassLoader();
-        Thread.currentThread().setContextClassLoader(container.getClassRealm());
-
-        CommandResult result;
-
-        try {
-            final IO io = context.getIo();
-            CommandAction action = getAction();
-
-            // Setup the command action
-            try {
-                // Process command line options/arguments
-                if (processArguments(io, action, args)) {
-                    // return if we have been asked to display --help
-                    return new CommandResult.ValueResult(CommandAction.Result.SUCCESS);
-                }
-
-                // TODO: Add preferences processor
-            }
-            catch (Exception e) {
-                return new CommandResult.FailureResult(e);
-            }
-
-            // Setup the command context
-            CommandContext ctx = new CommandContext() {
-                private final Variables variables = new Variables(context.getVariables());
-
-                public Object[] getArguments() {
-                    return args;
-                }
-
-                public IO getIo() {
-                    return io;
-                }
-
-                public Variables getVariables() {
-                    return variables;
-                }
-
-                public Command getCommand() {
-                    return StatelessCommand.this;
-                }
-            };
-
-            // Execute the action
-            try {
-                log.trace("Executing action: {}", action);
-
-                Object value = action.execute(ctx);
-
-                log.trace("Result: {}", value);
-
-                result = new CommandResult.ValueResult(value);
-            }
-            catch (final Notification n) {
-                log.trace("Notified: {}, n");
-
-                result = new CommandResult.NotificationResult(n);
-            }
-            catch (final Throwable t) {
-                log.trace("Caught: {}", t);
-
-                result = new CommandResult.FailureResult(t);
-            }
-        }
-        finally {
-            Thread.currentThread().setContextClassLoader(prevCL);
-
-            MDC.remove("command-id");
-        }
-
-        return result;
-    }
-
-    private boolean processArguments(final IO io, final CommandAction action, final Object[]
args) throws Exception {
-        assert io != null;
-        assert action != null;
-        assert args != null;
-
-        if (log.isTraceEnabled()) {
-            log.trace("Processing arguments: {}", Arguments.toStringArray(args));
-        }
-
-        CommandLineProcessor clp = new CommandLineProcessor();
-        clp.addBean(action);
-
-        // Attach some help context
-        CommandDocumenter documenter = getDocumenter();
-        clp.addBean(documenter);
-
-        HelpSupport help = new HelpSupport();
-        clp.addBean(help);
-
-        // Process the arguments
-        clp.process(Arguments.toStringArray(args));
-
-        // Render command-line usage
-        if (help.displayHelp) {
-            log.trace("Render command-line usage");
-            
-            documenter.renderUsage(io.out);
-            return true;
-        }
-
-        return false;
-    }
 }
\ No newline at end of file

Modified: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/resources/META-INF/spring/components.xml
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/resources/META-INF/spring/components.xml?rev=697851&r1=697850&r2=697851&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/resources/META-INF/spring/components.xml
(original)
+++ geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/resources/META-INF/spring/components.xml
Mon Sep 22 07:34:37 2008
@@ -57,4 +57,32 @@
         </property>
     </bean>
 
+    <bean id="statelessCommandTemplate" class="org.apache.geronimo.gshell.wisdom.command.StatelessCommand"
abstract="true">
+        <property name="documenter">
+            <bean class="org.apache.geronimo.gshell.wisdom.command.CommandDocumenterImpl"/>
+        </property>
+
+        <property name="completer">
+            <bean class="org.apache.geronimo.gshell.wisdom.command.CommandCompleterImpl"/>
+        </property>
+
+        <property name="messages">
+            <bean class="org.apache.geronimo.gshell.wisdom.command.CommandMessageSource"/>
+        </property>
+    </bean>
+
+    <bean id="statefulCommandTemplate" class="org.apache.geronimo.gshell.wisdom.command.StatefulCommand"
abstract="true">
+        <property name="documenter">
+            <bean class="org.apache.geronimo.gshell.wisdom.command.CommandDocumenterImpl"/>
+        </property>
+
+        <property name="completer">
+            <bean class="org.apache.geronimo.gshell.wisdom.command.CommandCompleterImpl"/>
+        </property>
+
+        <property name="messages">
+            <bean class="org.apache.geronimo.gshell.wisdom.command.CommandMessageSource"/>
+        </property>
+    </bean>
+
 </beans>
\ No newline at end of file



Mime
View raw message