Author: jstrachan
Date: Fri Sep 17 09:35:22 2010
New Revision: 998033
URL: http://svn.apache.org/viewvc?rev=998033&view=rev
Log:
fixes KARAF-213 to complete on Files; added boolean/Boolean completer too. Also
added extra warning if a @CompleterValues index is out of range from the
available argumentsgre
Added:
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/FileCompleter.java
karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/BooleanCompleterTest.java
karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/FileCompleterTest.java
Modified:
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/ArgumentCompleter.java
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=998033&r1=998032&r2=998033&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
Fri Sep 17 09:35:22 2010
@@ -24,6 +24,7 @@
*/
package org.apache.karaf.shell.console.completer;
+import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -55,7 +56,7 @@ public class ArgumentCompleter implement
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>();
+ final Map<Integer, Field> arguments = new HashMap<Integer, Field>();
boolean strict = true;
public ArgumentCompleter(CommandSession session, AbstractCommand function,
String command) {
@@ -82,7 +83,7 @@ public class ArgumentCompleter implement
if (arguments.containsKey(key)) {
LOGGER.warn("Duplicate @Argument annotations on class
" + type.getName() + " for index: " + key + " see: " + field);
} else {
- arguments.put(key, argument);
+ arguments.put(key, field);
}
}
}
@@ -106,7 +107,11 @@ public class ArgumentCompleter implement
for (Method method : type.getDeclaredMethods()) {
CompleterValues completerMethod =
method.getAnnotation(CompleterValues.class);
if (completerMethod != null) {
- Integer key = completerMethod.index();
+ int index = completerMethod.index();
+ Integer key = index;
+ if (index >= arguments.size() || index < 0) {
+ LOGGER.warn("Index out of range on
@CompleterValues on class " + type.getName() + " for index: " + key + " see: "
+ method);
+ }
if (methods.containsKey(key)) {
LOGGER.warn("Duplicate @CompleterMethod
annotations on class " + type.getName() + " for index: " + key + " see: " +
method);
} else {
@@ -116,8 +121,8 @@ public class ArgumentCompleter implement
}
}
for (int i = 0, size = arguments.size(); i < size; i++) {
- Method method = methods.get(i);
Completer argCompleter = NullCompleter.INSTANCE;
+ Method method = methods.get(i);
if (method != null) {
// lets invoke the method
Action action = function.createNewAction();
@@ -145,7 +150,16 @@ public class ArgumentCompleter implement
LOGGER.warn("Failed to release action: " + action
+ ". " + e, e);
}
}
-
+ } else {
+ Field field = arguments.get(i);
+ Class<?> type = field.getType();
+ if (type.isAssignableFrom(File.class)) {
+ argCompleter = new FileCompleter(session);
+ } else if (type.isAssignableFrom(Boolean.class) ||
type.isAssignableFrom(boolean.class)) {
+ argCompleter = new StringsCompleter(new String[]
{"false", "true"});
+ } else {
+ // TODO any other completers we can add?
+ }
}
argsCompleters.add(argCompleter);
}
Added:
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/FileCompleter.java
URL:
http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/FileCompleter.java?rev=998033&view=auto
==============================================================================
---
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/FileCompleter.java
(added)
+++
karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/completer/FileCompleter.java
Fri Sep 17 09:35:22 2010
@@ -0,0 +1,41 @@
+/**
+ *
+ * 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 jline.FileNameCompletor;
+import org.apache.karaf.shell.console.Completer;
+import org.osgi.service.command.CommandSession;
+
+import java.io.File;
+import java.util.List;
+
+/**
+ * A completer for file names
+ */
+public class FileCompleter implements Completer {
+ private final CommandSession session;
+ private FileNameCompletor completor = new FileNameCompletor();
+
+ public FileCompleter(CommandSession session) {
+ this.session = session;
+ }
+
+ public int complete(String buffer, int cursor, List<String> candidates) {
+ return completor.complete(buffer, cursor, candidates);
+ }
+}
Added:
karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/BooleanCompleterTest.java
URL:
http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/BooleanCompleterTest.java?rev=998033&view=auto
==============================================================================
---
karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/BooleanCompleterTest.java
(added)
+++
karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/BooleanCompleterTest.java
Fri Sep 17 09:35:22 2010
@@ -0,0 +1,65 @@
+/**
+ *
+ * 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.io.File;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+public class BooleanCompleterTest 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("true "), complete(comp, "action t"));
+ assertEquals(Arrays.asList("false "), complete(comp, "action f"));
+
+ // arg 1
+ assertEquals(Arrays.asList("true "), complete(comp, "action false t"));
+ assertEquals(Arrays.asList("false "), complete(comp, "action true f"));
+
+ // unknown args
+ assertEquals(Arrays.asList(), complete(comp, "action c"));
+ assertEquals(Arrays.asList(), complete(comp, "action true true a"));
+ }
+
+ public static class MyAction implements Action {
+ @Argument(index = 0)
+ Boolean foo;
+ @Argument(index = 1)
+ boolean bar;
+
+ public Object execute(CommandSession session) throws Exception {
+ return null;
+ }
+ }
+
+}
Added:
karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/FileCompleterTest.java
URL:
http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/FileCompleterTest.java?rev=998033&view=auto
==============================================================================
---
karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/FileCompleterTest.java
(added)
+++
karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/completer/FileCompleterTest.java
Fri Sep 17 09:35:22 2010
@@ -0,0 +1,58 @@
+/**
+ *
+ * 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.io.File;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+public class FileCompleterTest 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("src/"), complete(comp, "action s"));
+ assertEquals(Arrays.asList("main/"), complete(comp, "action src/m"));
+ assertEquals(Arrays.asList("java/"), complete(comp, "action
src/main/j"));
+ }
+
+ public static class MyAction implements Action {
+ @Argument(index = 0)
+ File foo;
+ @Argument(index = 1)
+ File bar;
+
+ public Object execute(CommandSession session) throws Exception {
+ return null;
+ }
+ }
+
+}