[seam-commits] Seam SVN: r13386 - in sandbox/encore/shell/src/main/java/org/jboss/encore/shell: cli/builtin and 1 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Tue Jul 13 15:00:49 EDT 2010


Author: lincolnthree
Date: 2010-07-13 15:00:49 -0400 (Tue, 13 Jul 2010)
New Revision: 13386

Added:
   sandbox/encore/shell/src/main/java/org/jboss/encore/shell/plugins/Default.java
Removed:
   sandbox/encore/shell/src/main/java/org/jboss/encore/shell/plugins/BuiltIn.java
Modified:
   sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/CommandLibraryExtension.java
   sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/CommandMetadata.java
   sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/ExecutionParser.java
   sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/PluginCommandCompletionHandler.java
   sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/PluginMetadata.java
   sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/PluginRegistry.java
   sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/Echo.java
   sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/ExitShell.java
   sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/Help.java
Log:
Replaced @BuiltIn plugins/implicit default commands with declarative @Default commands.

Modified: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/CommandLibraryExtension.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/CommandLibraryExtension.java	2010-07-13 17:50:19 UTC (rev 13385)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/CommandLibraryExtension.java	2010-07-13 19:00:49 UTC (rev 13386)
@@ -34,8 +34,8 @@
 import javax.enterprise.inject.spi.ProcessBean;
 import javax.inject.Named;
 
-import org.jboss.encore.shell.plugins.BuiltIn;
 import org.jboss.encore.shell.plugins.Command;
+import org.jboss.encore.shell.plugins.Default;
 import org.jboss.encore.shell.plugins.Option;
 import org.jboss.encore.shell.plugins.Plugin;
 import org.jboss.encore.shell.util.Annotations;
@@ -61,12 +61,11 @@
       Class<? extends Plugin> plugin = (Class<? extends Plugin>) bean.getBeanClass();
       if (Plugin.class.isAssignableFrom(plugin))
       {
-         System.out.println("\\t" + plugin);
+         System.out.println("\t> " + plugin);
 
          String name = getPluginName(plugin);
 
          PluginMetadata pluginMeta = new PluginMetadata();
-         pluginMeta.setBuiltIn(Annotations.isAnnotationPresent(plugin, BuiltIn.class));
          pluginMeta.setName(name);
          pluginMeta.setType(plugin);
 
@@ -91,6 +90,11 @@
             commandMeta.setHelp(command.help());
             commandMeta.setParent(pluginMeta);
 
+            if (Annotations.isAnnotationPresent(m, Default.class))
+            {
+               commandMeta.setDefault(true);
+            }
+
             if (command.value().length == 0)
             {
                commandMeta.setNames(m.getName().toLowerCase());

Modified: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/CommandMetadata.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/CommandMetadata.java	2010-07-13 17:50:19 UTC (rev 13385)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/CommandMetadata.java	2010-07-13 19:00:49 UTC (rev 13386)
@@ -34,6 +34,8 @@
 {
    private PluginMetadata parent;
    private Method method;
+   private boolean isDefault;
+
    private String[] names = {};
    private String help;
    private List<OptionMetadata> options = new ArrayList<OptionMetadata>();
@@ -77,6 +79,16 @@
       this.method = method;
    }
 
+   public boolean isDefault()
+   {
+      return isDefault;
+   }
+
+   public void setDefault(boolean isDefault)
+   {
+      this.isDefault = isDefault;
+   }
+
    public List<String> getNames()
    {
       return Arrays.asList(names);
@@ -115,8 +127,7 @@
    @Override
    public String toString()
    {
-      return "CommandMetadata [method=" + method + ", names=" + Arrays.toString(names) + ", help=" + help
-               + ", options=" + options + "]";
+      return "CommandMetadata [method=" + method + ", names=" + Arrays.toString(names) + ", help=" + help + ", options=" + options + "]";
    }
 
    public PluginMetadata getParent()

Modified: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/ExecutionParser.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/ExecutionParser.java	2010-07-13 17:50:19 UTC (rev 13385)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/ExecutionParser.java	2010-07-13 19:00:49 UTC (rev 13386)
@@ -24,7 +24,6 @@
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Queue;
 
 import javax.enterprise.inject.Instance;
@@ -54,12 +53,8 @@
    public Execution parse(final String line)
    {
       Queue<String> tokens = tokenize(line);
-
       Map<String, PluginMetadata> plugins = registry.getPlugins();
-      Map<String, PluginMetadata> builtInPlugins = registry.getBuiltInPlugins();
-
       Execution execution = executionInstance.get();
-
       execution.setOriginalStatement(line);
       CommandMetadata command = null;
 
@@ -68,48 +63,41 @@
          String first = tokens.remove();
 
          PluginMetadata plugin = plugins.get(first);
-         if (plugin == null)
+
+         if (plugin != null)
          {
-            for (Entry<String, PluginMetadata> entry : builtInPlugins.entrySet())
+            if (tokens.size() > 0)
             {
-               command = entry.getValue().getCommand(first);
-               if (command != null)
+               String second = tokens.peek();
+               if (command == null)
                {
-                  plugin = command.getParent();
+                  command = plugin.getCommand(second);
+                  if (command != null)
+                  {
+                     tokens.remove();
+                  }
                }
             }
-         }
 
-         if ((plugin != null) && plugin.isImplicitCommand())
-         {
-            command = plugin.getCommands().get(0);
-         }
-         else if (tokens.size() > 0)
-         {
-            String second = tokens.peek();
-            if ((plugin != null) && (command == null))
+            if (plugin.hasDefaultCommand())
             {
-               command = plugin.getCommand(second);
-               if (command != null)
-               {
-                  tokens.remove();
-               }
+               command = plugin.getDefaultCommand();
             }
-         }
 
-         if (command != null)
-         {
-            execution.setCommand(command);
+            if (command != null)
+            {
+               execution.setCommand(command);
 
-            // parse parameters and set order / nulls for command invocation
+               // parse parameters and set order / nulls for command invocation
 
-            Object[] parameters = parseParameters(command, tokens);
-            execution.setParameterArray(parameters);
+               Object[] parameters = parseParameters(command, tokens);
+               execution.setParameterArray(parameters);
+            }
+            else
+            {
+               throw new IllegalStateException("Missing command for plugin: " + plugin.getName());
+            }
          }
-         else
-         {
-            throw new IllegalStateException("Missing command for plugin: " + plugin.getName());
-         }
       }
 
       return execution;
@@ -130,10 +118,7 @@
    {
       Map<OptionMetadata, Object> valueMap = new HashMap<OptionMetadata, Object>();
 
-      CommandParser commandParser = new CompositeCommandParser(new NamedBooleanOptionParser(),
-               new NamedValueOptionParser(),
-               new NamedValueVarargsOptionParser(), new OrderedValueOptionParser(),
-               new OrderedValueVarargsOptionParser(), new ParseErrorParser());
+      CommandParser commandParser = new CompositeCommandParser(new NamedBooleanOptionParser(), new NamedValueOptionParser(), new NamedValueVarargsOptionParser(), new OrderedValueOptionParser(), new OrderedValueVarargsOptionParser(), new ParseErrorParser());
 
       commandParser.parse(command, valueMap, tokens);
 

Modified: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/PluginCommandCompletionHandler.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/PluginCommandCompletionHandler.java	2010-07-13 17:50:19 UTC (rev 13385)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/PluginCommandCompletionHandler.java	2010-07-13 19:00:49 UTC (rev 13386)
@@ -52,22 +52,7 @@
             String pluginName = p.getValue().getName();
             if (isPotentialMatch(pluginName, token))
             {
-               PluginMetadata pluginMetadata = plugins.get(pluginName);
-               if (pluginMetadata.isBuiltIn() && pluginMetadata.isImplicitCommand())
-               {
-                  List<String> names = pluginMetadata.getCommands().get(0).getNames();
-                  for (String name : names)
-                  {
-                     if (isPotentialMatch(name, token))
-                     {
-                        candidates.add(name.toLowerCase() + " ");
-                     }
-                  }
-               }
-               else
-               {
-                  candidates.add(pluginName.toLowerCase() + " ");
-               }
+               candidates.add(pluginName + " ");
             }
          }
       }

Modified: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/PluginMetadata.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/PluginMetadata.java	2010-07-13 17:50:19 UTC (rev 13385)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/PluginMetadata.java	2010-07-13 19:00:49 UTC (rev 13386)
@@ -32,7 +32,6 @@
  */
 public class PluginMetadata
 {
-   private boolean builtIn = false;
    private String name;
    private Class<? extends Plugin> type;
    private List<CommandMetadata> commands = new ArrayList<CommandMetadata>();
@@ -56,14 +55,25 @@
       return result;
    }
 
-   /**
-    * Return true if this plugin defines a single command to be executed by default.
-    */
-   public boolean isImplicitCommand()
+   public boolean hasDefaultCommand()
    {
-      return (commands.size() == 1) && isBuiltIn();
+      return getDefaultCommand() != null;
    }
 
+   public CommandMetadata getDefaultCommand()
+   {
+      CommandMetadata result = null;
+      for (CommandMetadata command : commands)
+      {
+         if (command.isDefault())
+         {
+            result = command;
+            break;
+         }
+      }
+      return result;
+   }
+
    public String getName()
    {
       return name;
@@ -97,17 +107,6 @@
    @Override
    public String toString()
    {
-      return "PluginMetadata [builtIn=" + builtIn + ", name=" + name + ", type=" + type + ", commands=" + commands
-               + "]";
+      return "PluginMetadata [name=" + name + ", type=" + type + ", commands=" + commands + "]";
    }
-
-   public boolean isBuiltIn()
-   {
-      return builtIn;
-   }
-
-   public void setBuiltIn(final boolean builtIn)
-   {
-      this.builtIn = builtIn;
-   }
 }

Modified: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/PluginRegistry.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/PluginRegistry.java	2010-07-13 17:50:19 UTC (rev 13385)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/PluginRegistry.java	2010-07-13 19:00:49 UTC (rev 13386)
@@ -21,9 +21,7 @@
  */
 package org.jboss.encore.shell.cli;
 
-import java.util.HashMap;
 import java.util.Map;
-import java.util.Map.Entry;
 
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
@@ -54,19 +52,6 @@
       return plugins;
    }
 
-   public Map<String, PluginMetadata> getBuiltInPlugins()
-   {
-      Map<String, PluginMetadata> result = new HashMap<String, PluginMetadata>();
-      for (Entry<String, PluginMetadata> plugin : plugins.entrySet())
-      {
-         if (plugin.getValue().isBuiltIn())
-         {
-            result.put(plugin.getKey(), plugin.getValue());
-         }
-      }
-      return result;
-   }
-
    public void addPlugin(final PluginMetadata plugin)
    {
       plugins.put(plugin.getName(), plugin);

Modified: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/Echo.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/Echo.java	2010-07-13 17:50:19 UTC (rev 13385)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/Echo.java	2010-07-13 19:00:49 UTC (rev 13386)
@@ -25,6 +25,7 @@
 
 import org.jboss.encore.shell.Shell;
 import org.jboss.encore.shell.plugins.Command;
+import org.jboss.encore.shell.plugins.Default;
 import org.jboss.encore.shell.plugins.Option;
 import org.jboss.encore.shell.plugins.Plugin;
 
@@ -39,6 +40,7 @@
    @Inject
    Shell shell;
 
+   @Default
    @Command(help = "This is a demo command")
    public void run(@Option(help = "The text to be echoed") final String text)
    {

Modified: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/ExitShell.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/ExitShell.java	2010-07-13 17:50:19 UTC (rev 13385)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/ExitShell.java	2010-07-13 19:00:49 UTC (rev 13386)
@@ -23,25 +23,28 @@
 
 import javax.enterprise.event.Event;
 import javax.inject.Inject;
+import javax.inject.Named;
 
 import org.jboss.encore.shell.events.Shutdown;
-import org.jboss.encore.shell.plugins.BuiltIn;
 import org.jboss.encore.shell.plugins.Command;
+import org.jboss.encore.shell.plugins.Default;
 import org.jboss.encore.shell.plugins.Plugin;
 
 /**
- * Implements a {@link Plugin} that fires the shell {@link Shutdown#NORMAL} event.
+ * Implements a {@link Plugin} that fires the shell {@link Shutdown#NORMAL}
+ * event.
  * 
  * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
  * 
  */
- at BuiltIn
+ at Named("exit")
 public class ExitShell implements Plugin
 {
    @Inject
    private Event<Shutdown> shutdown;
 
-   @Command(value = { "exit", "quit" }, help = "Exit the shell")
+   @Default
+   @Command(help = "Exit the shell")
    public void exit()
    {
       shutdown.fire(Shutdown.NORMAL);

Modified: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/Help.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/Help.java	2010-07-13 17:50:19 UTC (rev 13385)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/Help.java	2010-07-13 19:00:49 UTC (rev 13386)
@@ -24,20 +24,21 @@
 import java.util.Arrays;
 
 import org.jboss.encore.shell.events.Shutdown;
-import org.jboss.encore.shell.plugins.BuiltIn;
 import org.jboss.encore.shell.plugins.Command;
+import org.jboss.encore.shell.plugins.Default;
 import org.jboss.encore.shell.plugins.Option;
 import org.jboss.encore.shell.plugins.Plugin;
 
 /**
- * Implements a {@link Plugin} that fires the shell {@link Shutdown#NORMAL} event.
+ * Implements a {@link Plugin} that fires the shell {@link Shutdown#NORMAL}
+ * event.
  * 
  * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
  * 
  */
- at BuiltIn
 public class Help implements Plugin
 {
+   @Default
    @Command(help = "Get help about specific commands")
    public void help(@Option("test") final String test, @Option final String... commands)
    {

Deleted: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/plugins/BuiltIn.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/plugins/BuiltIn.java	2010-07-13 17:50:19 UTC (rev 13385)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/plugins/BuiltIn.java	2010-07-13 19:00:49 UTC (rev 13386)
@@ -1,47 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc., and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.encore.shell.plugins;
-
-import static java.lang.annotation.ElementType.FIELD;
-import static java.lang.annotation.ElementType.METHOD;
-import static java.lang.annotation.ElementType.PARAMETER;
-import static java.lang.annotation.ElementType.TYPE;
-import static java.lang.annotation.RetentionPolicy.RUNTIME;
-
-import java.lang.annotation.Documented;
-import java.lang.annotation.Retention;
-import java.lang.annotation.Target;
-
-
-/**
- * Defines a #{@link Plugin} as built in, thus, commands will be accessible without providing a plugin name;
- * 
- * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
- * 
- */
- at Target({ TYPE, METHOD, PARAMETER, FIELD })
- at Retention(RUNTIME)
- at Documented
-public @interface BuiltIn
-{
-
-}

Added: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/plugins/Default.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/plugins/Default.java	                        (rev 0)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/plugins/Default.java	2010-07-13 19:00:49 UTC (rev 13386)
@@ -0,0 +1,47 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.encore.shell.plugins;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Defines a @{@link Command} as the plugin default. It will be run if no other
+ * command matches the input line.
+ * 
+ * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ * 
+ */
+ at Target({ TYPE, METHOD, PARAMETER, FIELD })
+ at Retention(RUNTIME)
+ at Documented
+public @interface Default
+{
+
+}



More information about the seam-commits mailing list