You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by js...@apache.org on 2010/09/16 20:59:11 UTC

svn commit: r997880 - in /karaf/trunk/shell/console/src: main/java/org/apache/felix/gogo/commands/ main/java/org/apache/karaf/shell/console/completer/ test/java/org/apache/karaf/shell/console/completer/

Author: jstrachan
Date: Thu Sep 16 18:59:10 2010
New Revision: 997880

URL: http://svn.apache.org/viewvc?rev=997880&view=rev
Log:
fixes https://issues.apache.org/jira/browse/KARAF-212 so that we can annotate a method with @CompleterValues and associate it with an argument index and it will be used by the completer to complete values for that argument

Added:
    karaf/trunk/shell/console/src/main/java/org/apache/felix/gogo/commands/CompleterValues.java
    karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/CompleterTestSupport.java
    karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/CompleterValuesTest.java
    karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/DummyCommandSession.java
Modified:
    karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/ArgumentCompleter.java
    karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/ArgumentCompleterTest.java

Added: karaf/trunk/shell/console/src/main/java/org/apache/felix/gogo/commands/CompleterValues.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/felix/gogo/commands/CompleterValues.java?rev=997880&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/felix/gogo/commands/CompleterValues.java (added)
+++ karaf/trunk/shell/console/src/main/java/org/apache/felix/gogo/commands/CompleterValues.java Thu Sep 16 18:59:10 2010
@@ -0,0 +1,37 @@
+/*
+ * 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.felix.gogo.commands;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Represents a method which can return a List or Array of values used for a
+ * {@link org.apache.karaf.shell.console.Completer}
+ * which is associated with the index of an
+ * {@link org.apache.felix.gogo.commands.Argument}
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.METHOD, ElementType.FIELD})
+public @interface CompleterValues
+{
+    int index() default 0;
+}

Modified: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/ArgumentCompleter.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/ArgumentCompleter.java?rev=997880&r1=997879&r2=997880&view=diff
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/ArgumentCompleter.java (original)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/ArgumentCompleter.java Thu Sep 16 18:59:10 2010
@@ -25,11 +25,17 @@
 package org.apache.karaf.shell.console.completer;
 
 import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.felix.gogo.commands.Action;
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.CompleterValues;
 import org.apache.felix.gogo.commands.Option;
 import org.apache.felix.gogo.commands.basic.AbstractCommand;
 import org.apache.felix.gogo.commands.basic.DefaultActionPreparator;
@@ -37,14 +43,19 @@ import org.apache.karaf.shell.console.Co
 import org.apache.karaf.shell.console.Completer;
 import org.apache.karaf.shell.console.NameScoping;
 import org.osgi.service.command.CommandSession;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class ArgumentCompleter implements Completer {
+    private static final Logger LOGGER = LoggerFactory.getLogger(ArgumentCompleter.class);
+
     final Completer commandCompleter;
     final Completer optionsCompleter;
     final List<Completer> argsCompleters;
     final AbstractCommand function;
     final Map<Option, Field> fields = new HashMap<Option, Field>();
     final Map<String, Option> options = new HashMap<String, Option>();
+    final Map<Integer, Argument> arguments = new HashMap<Integer, Argument>();
     boolean strict = true;
 
     public ArgumentCompleter(CommandSession session, AbstractCommand function, String command) {
@@ -65,6 +76,15 @@ public class ArgumentCompleter implement
                         }
                     }
                 }
+                Argument argument = field.getAnnotation(Argument.class);
+                if (argument != null) {
+                    Integer key = argument.index();
+                    if (arguments.containsKey(key)) {
+                        LOGGER.warn("Duplicate @Argument annotations on class " + type.getName() + " for index: " + key + " see: " + field);
+                    } else {
+                        arguments.put(key, argument);
+                    }
+                }
             }
         }
         options.put(DefaultActionPreparator.HELP.name(), DefaultActionPreparator.HELP);
@@ -81,7 +101,55 @@ public class ArgumentCompleter implement
                 argsCompleters.add(NullCompleter.INSTANCE);
             }
         } else {
-            argsCompleters.add(NullCompleter.INSTANCE);
+            final Map<Integer, Method> methods = new HashMap<Integer, Method>();
+            for (Class type = function.getActionClass(); type != null; type = type.getSuperclass()) {
+                for (Method method : type.getDeclaredMethods()) {
+                    CompleterValues completerMethod = method.getAnnotation(CompleterValues.class);
+                    if (completerMethod != null) {
+                        Integer key = completerMethod.index();
+                        if (methods.containsKey(key)) {
+                            LOGGER.warn("Duplicate @CompleterMethod annotations on class " + type.getName() + " for index: " + key + " see: " + method);
+                        } else {
+                            methods.put(key, method);
+                        }
+                    }
+                }
+            }
+            for (int i = 0, size = arguments.size(); i < size; i++) {
+                Method method = methods.get(i);
+                Completer argCompleter = NullCompleter.INSTANCE;
+                if (method != null) {
+                    // lets invoke the method
+                    System.out.println("About to invoke method: " + method);
+                    Action action = function.createNewAction();
+                    try {
+                        Object value = method.invoke(action);
+                        if (value instanceof String[]) {
+                            argCompleter = new StringsCompleter((String[]) value);
+                        } else if (value instanceof Collection) {
+                            argCompleter = new StringsCompleter((Collection<String>) value);
+                        } else {
+                            LOGGER.warn("Could not use value " + value + " as set of completions!");
+                        }
+                    } catch (IllegalAccessException e) {
+                        LOGGER.warn("Could not invoke @CompleterMethod on " + function + ". " + e, e);
+                    } catch (InvocationTargetException e) {
+                        Throwable target = e.getTargetException();
+                        if (target == null) {
+                            target = e;
+                        }
+                        LOGGER.warn("Could not invoke @CompleterMethod on " + function + ". " + target, target);
+                    } finally {
+                        try {
+                            function.releaseAction(action);
+                        } catch (Exception e) {
+                            LOGGER.warn("Failed to release action: " + action + ". " + e, e);
+                        }
+                    }
+
+                }
+                argsCompleters.add(argCompleter);
+            }
         }
     }
 

Modified: karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/ArgumentCompleterTest.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/ArgumentCompleterTest.java?rev=997880&r1=997879&r2=997880&view=diff
==============================================================================
--- karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/ArgumentCompleterTest.java (original)
+++ karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/ArgumentCompleterTest.java Thu Sep 16 18:59:10 2010
@@ -16,8 +16,6 @@
  */
 package org.apache.karaf.shell.console.completer;
 
-import java.io.InputStream;
-import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -25,7 +23,6 @@ import java.util.List;
 import org.apache.felix.gogo.commands.Action;
 import org.apache.felix.gogo.commands.Option;
 import org.apache.felix.gogo.commands.basic.SimpleCommand;
-import org.apache.felix.gogo.runtime.shell.CommandSessionImpl;
 import org.apache.karaf.shell.console.CompletableFunction;
 import org.apache.karaf.shell.console.Completer;
 import org.junit.Test;
@@ -33,7 +30,7 @@ import org.osgi.service.command.CommandS
 
 import static org.junit.Assert.*;
 
-public class ArgumentCompleterTest {
+public class ArgumentCompleterTest extends CompleterTestSupport {
 
     @Test
     public void testParser1() throws Exception {
@@ -101,12 +98,6 @@ public class ArgumentCompleterTest {
         assertEquals(Arrays.asList("bar1", "bar2"), complete(comp, "action -f 2 --check foo1 "));
     }
 
-    protected List<String> complete(Completer completer, String buf) {
-        List<String> candidates = new ArrayList<String>();
-        completer.complete(buf, buf.length(), candidates);
-        return candidates;
-    }
-
     public static class MyFunction extends SimpleCommand implements CompletableFunction {
         public MyFunction() {
             super(MyAction.class);
@@ -130,29 +121,4 @@ public class ArgumentCompleterTest {
         }
     }
 
-    protected static class DummyCommandSession implements CommandSession {
-        public Object convert(Class<?> type, Object instance) {
-            return null;
-        }
-        public CharSequence format(Object target, int level) {
-            return null;
-        }
-        public void put(String name, Object value) {
-        }
-        public Object get(String name) {
-            return null;
-        }
-        public PrintStream getConsole() {
-            return null;
-        }
-        public InputStream getKeyboard() {
-            return null;
-        }
-        public void close() {
-        }
-        public Object execute(CharSequence commandline) throws Exception {
-            return null;
-        }
-    }
-
 }

Added: karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/CompleterTestSupport.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/CompleterTestSupport.java?rev=997880&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/CompleterTestSupport.java (added)
+++ karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/CompleterTestSupport.java Thu Sep 16 18:59:10 2010
@@ -0,0 +1,34 @@
+/**
+ *
+ * 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.karaf.shell.console.completer;
+
+import org.apache.karaf.shell.console.Completer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Useful base class for completion related test cases
+ */
+public abstract class CompleterTestSupport {
+    protected List<String> complete(Completer completer, String buf) {
+        List<String> candidates = new ArrayList<String>();
+        completer.complete(buf, buf.length(), candidates);
+        return candidates;
+    }
+}

Added: karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/CompleterValuesTest.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/CompleterValuesTest.java?rev=997880&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/CompleterValuesTest.java (added)
+++ karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/CompleterValuesTest.java Thu Sep 16 18:59:10 2010
@@ -0,0 +1,74 @@
+/**
+ *
+ * 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.karaf.shell.console.completer;
+
+import org.apache.felix.gogo.commands.Action;
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.CompleterValues;
+import org.apache.felix.gogo.commands.basic.SimpleCommand;
+import org.apache.karaf.shell.console.Completer;
+import org.junit.Test;
+import org.osgi.service.command.CommandSession;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+public class CompleterValuesTest extends CompleterTestSupport {
+
+    @Test
+    public void testCompleteArgumnets() throws Exception {
+        CommandSession session = new DummyCommandSession();
+        Completer comp = new ArgumentCompleter(session, new SimpleCommand(MyAction.class), "my:action");
+
+        // arg 0
+        assertEquals(Arrays.asList("a1", "a2", "a3"), complete(comp, "action a"));
+        assertEquals(Arrays.asList("b4", "b5"), complete(comp, "action b"));
+
+        // arg 1
+        assertEquals(Arrays.asList("c2", "c3"), complete(comp, "action a1 c"));
+        assertEquals(Arrays.asList("d5", "d6", "d7"), complete(comp, "action b4 d"));
+
+        // unknown args
+        assertEquals(Arrays.asList(), complete(comp, "action c"));
+        assertEquals(Arrays.asList(), complete(comp, "action a1 d5 a"));
+    }
+
+    public static class MyAction implements Action {
+        @Argument(index = 0)
+        String foo;
+        @Argument(index = 1)
+        String bar;
+
+        public Object execute(CommandSession session) throws Exception {
+            return null;
+        }
+
+        @CompleterValues(index = 0)
+        public String[] getFooValues() {
+            return new String[]{"a1", "a2", "a3", "b4", "b5"};
+        }
+
+        @CompleterValues(index = 1)
+        public List<String> getBarValues() {
+            return Arrays.asList("c2", "c3", "d5", "d6", "d7");
+        }
+    }
+
+}

Added: karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/DummyCommandSession.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/DummyCommandSession.java?rev=997880&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/DummyCommandSession.java (added)
+++ karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/DummyCommandSession.java Thu Sep 16 18:59:10 2010
@@ -0,0 +1,48 @@
+/**
+ *
+ * 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.karaf.shell.console.completer;
+
+import org.osgi.service.command.CommandSession;
+
+import java.io.InputStream;
+import java.io.PrintStream;
+
+public class DummyCommandSession implements CommandSession {
+    public Object convert(Class<?> type, Object instance) {
+        return null;
+    }
+    public CharSequence format(Object target, int level) {
+        return null;
+    }
+    public void put(String name, Object value) {
+    }
+    public Object get(String name) {
+        return null;
+    }
+    public PrintStream getConsole() {
+        return null;
+    }
+    public InputStream getKeyboard() {
+        return null;
+    }
+    public void close() {
+    }
+    public Object execute(CharSequence commandline) throws Exception {
+        return null;
+    }
+}