You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2008/09/26 10:16:50 UTC

svn commit: r699212 - in /geronimo/gshell/trunk: gshell-api/src/main/java/org/apache/geronimo/gshell/command/ gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/alias/ gshell-wisdom/gshell-wisdom-core/src/main/java/org/apa...

Author: jdillon
Date: Fri Sep 26 01:16:49 2008
New Revision: 699212

URL: http://svn.apache.org/viewvc?rev=699212&view=rev
Log:
Refactored CommandSupport to split up its main execute() into prepareAction() and executeAction()
Hooked up i18n muck for alias support
Implemented additional arguments for aliases

Added:
    geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/resources/org/apache/geronimo/gshell/wisdom/alias/
    geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/resources/org/apache/geronimo/gshell/wisdom/alias/AliasCommand.properties   (with props)
Modified:
    geronimo/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/Arguments.java
    geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/alias/AliasCommand.java
    geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/CommandMessageSource.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/MessageSourceCommandDocumenter.java
    geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatefulCommand.java

Modified: geronimo/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/Arguments.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/Arguments.java?rev=699212&r1=699211&r2=699212&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/Arguments.java (original)
+++ geronimo/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/Arguments.java Fri Sep 26 01:16:49 2008
@@ -86,22 +86,4 @@
 
         return strings;
     }
-
-    public static String asQuotedString(final Object[] args, final String quoteToken) {
-        assert args != null;
-        assert quoteToken != null;
-
-        StringBuilder buff = new StringBuilder();
-
-        for (int i=0; i<args.length; i++) {
-            buff.append(quoteToken);
-            buff.append(args[i]);
-            buff.append(quoteToken);
-            if (i+1 < args.length) {
-                buff.append(" ");
-            }
-        }
-
-        return buff.toString();
-    }
 }

Modified: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/alias/AliasCommand.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/alias/AliasCommand.java?rev=699212&r1=699211&r2=699212&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/alias/AliasCommand.java (original)
+++ geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/alias/AliasCommand.java Fri Sep 26 01:16:49 2008
@@ -19,18 +19,24 @@
 
 package org.apache.geronimo.gshell.wisdom.alias;
 
+import org.apache.geronimo.gshell.clp.Argument;
 import org.apache.geronimo.gshell.command.CommandAction;
 import org.apache.geronimo.gshell.command.CommandContext;
 import org.apache.geronimo.gshell.command.Variables;
+import org.apache.geronimo.gshell.commandline.CommandLineExecutor;
 import org.apache.geronimo.gshell.i18n.MessageSource;
-import org.apache.geronimo.gshell.wisdom.command.CommandDocumenterSupport;
+import org.apache.geronimo.gshell.i18n.ResourceBundleMessageSource;
+import org.apache.geronimo.gshell.io.IO;
+import org.apache.geronimo.gshell.shell.ShellContext;
 import org.apache.geronimo.gshell.wisdom.command.CommandSupport;
+import org.apache.geronimo.gshell.wisdom.command.HelpSupport;
+import org.apache.geronimo.gshell.wisdom.command.MessageSourceCommandDocumenter;
 import org.apache.geronimo.gshell.wisdom.command.NullCommandCompleter;
-import org.apache.geronimo.gshell.commandline.CommandLineExecutor;
-import org.apache.geronimo.gshell.shell.ShellContext;
-import org.apache.geronimo.gshell.io.IO;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import java.util.Iterator;
+import java.util.List;
+
 /**
  * Alias {@link org.apache.geronimo.gshell.command.Command} component.
  *
@@ -66,10 +72,23 @@
 
         this.executor = executor;
     }
-    
+
+    @Override
+    protected void prepareAction(final ShellContext context, final Object[] args) {
+        // HACK: Reset state for proper appendArgs muck
+        setAction(new AliasCommandAction());
+        super.prepareAction(context, args);
+    }
+
+    /**
+     * Action to handle invocation of the alias target + additional arguments.
+     */
     private class AliasCommandAction
         implements CommandAction
-    {
+    {    
+        @Argument
+        private List<String> appendArgs = null;
+
         public Object execute(final CommandContext context) throws Exception {
             assert context != null;
 
@@ -86,26 +105,24 @@
             StringBuilder buff = new StringBuilder();
             buff.append(target);
 
-            //
-            // FIXME: For this to work correctly, need to follow stateful command pattern
-            //        and construct this action, or override more muck in CommandSupport
-            //
-
-            // Append arguments from the context to the line to execute quoted
-            Object[] args = context.getArguments();
-            for (int i=0; i<args.length; i++) {
-                buff.append("'");
-                buff.append(args[i]);
-                buff.append("'");
-                if (i+1 < args.length) {
-                    buff.append(" ");
+            // If we have args to append, then do it
+            if (appendArgs != null && !appendArgs.isEmpty()) {
+                buff.append(" ");
+                
+                Iterator iter = appendArgs.iterator();
+                while (iter.hasNext()) {
+                    // Append args quoted as they have already been processed by the parser
+                    // HACK: Using double quote instead of single quote for now as the parser's handling of single quote is broken
+                    buff.append('"').append(iter.next()).append('"');
+                    if (iter.hasNext()) {
+                        buff.append(" ");
+                    }
                 }
             }
-            
-            String line = buff.toString();
-            log.debug("Executing alias target: {}", line);
 
-            Object result = executor.execute(shellContext, line);
+            log.debug("Executing alias target: {}", buff);
+
+            Object result = executor.execute(shellContext, buff.toString());
 
             log.debug("Alias result: {}", result);
 
@@ -113,39 +130,35 @@
         }
     }
 
-    //
-    // TODO: May be able to stuff all of these into messages, since they are mostly the same for everything
-    //
-    
+    /**
+     * Alias command documenter.
+     */
     private class AliasCommandDocumenter
-        extends CommandDocumenterSupport
+        extends MessageSourceCommandDocumenter
     {
         public String getName() {
             return name;
         }
 
         public String getDescription() {
-            return "Alias to: " + target;
-        }
-
-        protected String getManual() {
-            return "TODO: general alias manual";
+            return getMessages().format(COMMAND_DESCRIPTION, target);
         }
     }
 
-    //
-    // NOTE: This is still needed for syntax rendering and such
-    //
-
+    /**
+     * Alias message source.
+     */
     private class AliasCommandMessageSource
         implements MessageSource
     {
+        private final MessageSource messages = new ResourceBundleMessageSource(new Class[] { AliasCommand.class, HelpSupport.class });
+
         public String getMessage(final String code) {
-            return null;
+            return messages.getMessage(code);
         }
 
         public String format(final String code, final Object... args) {
-            return null;
+            return messages.format(code, args);
         }
     }
 }

Modified: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/CommandMessageSource.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/CommandMessageSource.java?rev=699212&r1=699211&r2=699212&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/CommandMessageSource.java (original)
+++ geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/CommandMessageSource.java Fri Sep 26 01:16:49 2008
@@ -46,8 +46,6 @@
             assert command != null;
             messages = new ResourceBundleMessageSource(new Class[] {
                 command.getAction().getClass(),
-                
-                // HACK: Include messages for --help support
                 HelpSupport.class
             });
         }

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=699212&r1=699211&r2=699212&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 Fri Sep 26 01:16:49 2008
@@ -162,84 +162,67 @@
 
         log.trace("Executing");
 
-        //
-        // FIXME: This TCL stuff is not needed for alias execution, so consider splitting thigns up a wee bit more
-        //
-        
         // 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);
-            }
+            // First prepare the action
+            prepareAction(context, args);
 
-            // Setup the command context
-            CommandContext ctx = new CommandContext() {
-                private final Variables variables = new Variables(context.getVariables());
-
-                public Object[] getArguments() {
-                    return args;
-                }
+            // Then execute it
+            result = executeAction(context, args);
+        }
+        catch (AbortExecutionNotification n) {
+            result = n.result;
+        }
+        finally {
+            Thread.currentThread().setContextClassLoader(prevCL);
+        }
 
-                public IO getIo() {
-                    return io;
-                }
+        return result;
+    }
 
-                public Variables getVariables() {
-                    return variables;
-                }
+    protected class AbortExecutionNotification
+        extends Notification
+    {
+        public final CommandResult result;
 
-                public Command getCommand() {
-                    return CommandSupport.this;
-                }
-            };
+        public AbortExecutionNotification(final CommandResult result) {
+            assert result != null;
 
-            // Execute the action
-            try {
-                log.trace("Executing action: {}", action);
+            this.result = result;
+        }
+    }
 
-                Object value = action.execute(ctx);
+    protected void prepareAction(final ShellContext context, final Object[] args) {
+        assert context != null;
+        assert args != null;
 
-                log.trace("Result: {}", value);
+        log.trace("Preparing action");
 
-                result = new CommandResult.ValueResult(value);
-            }
-            catch (final Notification n) {
-                log.trace("Notified: {}, n");
+        IO io = context.getIo();
+        CommandAction action = getAction();
 
-                result = new CommandResult.NotificationResult(n);
+        // Setup the command action
+        try {
+            // Process command line options/arguments
+            if (processArguments(io, action, args)) {
+                // Abort if we have been asked to display --help
+                throw new AbortExecutionNotification(new CommandResult.ValueResult(CommandAction.Result.SUCCESS));
             }
-            catch (final Throwable t) {
-                log.trace("Caught: {}", t);
 
-                result = new CommandResult.FailureResult(t);
-            }
+            // TODO: Add preferences processor
         }
-        finally {
-            Thread.currentThread().setContextClassLoader(prevCL);
+        catch (Exception e) {
+            // Abort if preparation caused a failure
+            throw new AbortExecutionNotification(new CommandResult.FailureResult(e));
         }
-
-        return result;
     }
 
-    private boolean processArguments(final IO io, final CommandAction action, final Object[] args) throws Exception {
+    protected boolean processArguments(final IO io, final CommandAction action, final Object[] args) throws Exception {
         assert io != null;
         assert action != null;
         assert args != null;
@@ -271,4 +254,56 @@
 
         return false;
     }
+
+    protected CommandResult executeAction(final ShellContext context, final Object[] args) {
+        assert context != null;
+        assert args != null;
+
+        log.trace("Executing action");
+
+        // 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 context.getIo();
+            }
+
+            public Variables getVariables() {
+                return variables;
+            }
+
+            public Command getCommand() {
+                return CommandSupport.this;
+            }
+        };
+
+        CommandResult result;
+
+        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 result;
+    }
 }
\ No newline at end of file

Modified: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/MessageSourceCommandDocumenter.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/MessageSourceCommandDocumenter.java?rev=699212&r1=699211&r2=699212&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/MessageSourceCommandDocumenter.java (original)
+++ geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/MessageSourceCommandDocumenter.java Fri Sep 26 01:16:49 2008
@@ -30,11 +30,11 @@
 public class MessageSourceCommandDocumenter
     extends CommandDocumenterSupport
 {
-    private static final String COMMAND_NAME = "command.name";
+    public static final String COMMAND_NAME = "command.name";
 
-    private static final String COMMAND_DESCRIPTION = "command.description";
+    public static final String COMMAND_DESCRIPTION = "command.description";
 
-    private static final String COMMAND_MANUAL = "command.manual";
+    public static final String COMMAND_MANUAL = "command.manual";
 
     protected MessageSource getMessages() {
         MessageSource messages = getCommand().getMessages();

Modified: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatefulCommand.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?rev=699212&r1=699211&r2=699212&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatefulCommand.java (original)
+++ geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/command/StatefulCommand.java Fri Sep 26 01:16:49 2008
@@ -86,9 +86,9 @@
     }
 
     @Override
-    public CommandResult execute(final ShellContext context, final Object[] args) {
+    protected CommandResult executeAction(final ShellContext context, final Object[] args) {
         try {
-            return super.execute(context, args);
+            return super.executeAction(context, args);
         }
         finally {
             clearAction();

Added: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/resources/org/apache/geronimo/gshell/wisdom/alias/AliasCommand.properties
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/resources/org/apache/geronimo/gshell/wisdom/alias/AliasCommand.properties?rev=699212&view=auto
==============================================================================
--- geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/resources/org/apache/geronimo/gshell/wisdom/alias/AliasCommand.properties (added)
+++ geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/resources/org/apache/geronimo/gshell/wisdom/alias/AliasCommand.properties Fri Sep 26 01:16:49 2008
@@ -0,0 +1,30 @@
+##
+## 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.
+##
+
+##
+## $Rev$ $Date$
+##
+
+command.description=Alias to: {0}
+
+command.argument.appendArgs=Supply additional ARGS to the alias execution.
+command.argument.appendArgs.token=ARGS
+
+command.manual=\
+  TODO: general alias manual

Propchange: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/resources/org/apache/geronimo/gshell/wisdom/alias/AliasCommand.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/resources/org/apache/geronimo/gshell/wisdom/alias/AliasCommand.properties
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/resources/org/apache/geronimo/gshell/wisdom/alias/AliasCommand.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain