You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by gn...@apache.org on 2009/10/09 18:25:22 UTC

svn commit: r823609 - in /felix/trunk/karaf/shell: commands/ commands/src/main/java/org/apache/felix/karaf/shell/commands/ commands/src/main/resources/OSGI-INF/blueprint/ console/src/main/java/org/apache/felix/karaf/shell/console/commands/

Author: gnodet
Date: Fri Oct  9 16:25:22 2009
New Revision: 823609

URL: http://svn.apache.org/viewvc?rev=823609&view=rev
Log:
FELIX-1729, FELIX-1730: add the each / if commands, make sure the arguments are displayed in the correct order in the help system

Added:
    felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/EachAction.java
    felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/IfAction.java
Modified:
    felix/trunk/karaf/shell/commands/pom.xml
    felix/trunk/karaf/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
    felix/trunk/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/commands/BlueprintCommand.java

Modified: felix/trunk/karaf/shell/commands/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/shell/commands/pom.xml?rev=823609&r1=823608&r2=823609&view=diff
==============================================================================
--- felix/trunk/karaf/shell/commands/pom.xml (original)
+++ felix/trunk/karaf/shell/commands/pom.xml Fri Oct  9 16:25:22 2009
@@ -46,6 +46,10 @@
             <artifactId>org.apache.felix.karaf.shell.console</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.apache.felix.gogo</groupId>
+            <artifactId>org.apache.felix.gogo.runtime</artifactId>
+        </dependency>
+        <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.osgi.core</artifactId>
             <scope>provided</scope>

Added: felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/EachAction.java
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/EachAction.java?rev=823609&view=auto
==============================================================================
--- felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/EachAction.java (added)
+++ felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/EachAction.java Fri Oct  9 16:25:22 2009
@@ -0,0 +1,46 @@
+/*
+ * 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.karaf.shell.commands;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.karaf.shell.console.OsgiCommandSupport;
+import org.osgi.service.command.Function;
+
+/**
+ * Execute a closure on a list of arguments.
+ */
+@Command(scope = "shell", name = "each", description = "Execute a closure on a list of arguments.")
+public class EachAction extends OsgiCommandSupport {
+
+    @Argument(name = "values", index = 0, multiValued = false, required = true, description = "The collection of arguments to iterate on")
+    Collection<Object> values;
+
+    @Argument(name = "function", index = 1, multiValued = false, required = true, description = "The function to execute")
+    Function function;
+
+    @Override
+    protected Object doExecute() throws Exception {
+        for (Object v : values) {
+            function.execute(session, Collections.singletonList(v));
+        }
+        return null;
+    }
+}

Added: felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/IfAction.java
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/IfAction.java?rev=823609&view=auto
==============================================================================
--- felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/IfAction.java (added)
+++ felix/trunk/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/IfAction.java Fri Oct  9 16:25:22 2009
@@ -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.felix.karaf.shell.commands;
+
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.karaf.shell.console.OsgiCommandSupport;
+import org.osgi.service.command.Function;
+
+/**
+ * Execute a closure on a list of arguments.
+ */
+@Command(scope = "shell", name = "if", description = "If/Then/Else block.")
+public class IfAction extends OsgiCommandSupport {
+
+    @Argument(name = "condition", index = 0, multiValued = false, required = true, description = "The condition")
+    Function condition;
+
+    @Argument(name = "ifTrue", index = 1, multiValued = false, required = true, description = "The function to execute if the condition is true")
+    Function ifTrue;
+
+    @Argument(name = "ifFalse", index = 2, multiValued = false, required = false, description = "The function to execute if the condition is false")
+    Function ifFalse;
+
+    @Override
+    protected Object doExecute() throws Exception {
+        Object result = condition.execute(session, null);
+        if (isTrue(result)) {
+            return ifTrue.execute(session, null);
+        } else {
+            if (ifFalse != null) {
+                return ifFalse.execute(session, null);
+            }
+        }
+        return null;
+    }
+
+    private boolean isTrue(Object result) {
+        if (result == null) {
+            return false;
+        }
+        if (result instanceof String && ((String) result).equals("")) {
+            return false;
+        }
+        if (result instanceof Boolean) {
+            return ((Boolean) result).booleanValue();
+        }
+        return true;
+    }
+
+}
\ No newline at end of file

Modified: felix/trunk/karaf/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml?rev=823609&r1=823608&r2=823609&view=diff
==============================================================================
--- felix/trunk/karaf/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml (original)
+++ felix/trunk/karaf/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml Fri Oct  9 16:25:22 2009
@@ -23,6 +23,9 @@
         <command name="shell/cat">
             <action class="org.apache.felix.karaf.shell.commands.CatAction"/>
         </command>
+        <command name="shell/each">
+            <action class="org.apache.felix.karaf.shell.commands.EachAction"/>
+        </command>
         <command name="shell/echo">
             <action class="org.apache.felix.karaf.shell.commands.EchoAction"/>
         </command>
@@ -37,6 +40,9 @@
             <action class="org.apache.felix.karaf.shell.commands.HistoryAction"/>
         </command>
         -->
+        <command name="shell/if">
+            <action class="org.apache.felix.karaf.shell.commands.IfAction"/>
+        </command>
         <command name="shell/info">
             <action class="org.apache.felix.karaf.shell.commands.InfoAction"/>
         </command>

Modified: felix/trunk/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/commands/BlueprintCommand.java
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/commands/BlueprintCommand.java?rev=823609&r1=823608&r2=823609&view=diff
==============================================================================
--- felix/trunk/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/commands/BlueprintCommand.java (original)
+++ felix/trunk/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/commands/BlueprintCommand.java Fri Oct  9 16:25:22 2009
@@ -18,6 +18,9 @@
 package org.apache.felix.karaf.shell.console.commands;
 
 import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Set;
 import java.util.HashSet;
@@ -82,8 +85,14 @@
         }
 
         @Override
-        protected void printUsage(Command command, Set<Option> options, Set<Argument> arguments, PrintStream out)
+        protected void printUsage(Command command, Set<Option> options, Set<Argument> args, PrintStream out)
         {
+            List<Argument> arguments = new ArrayList<Argument>(args);
+            Collections.sort(arguments, new Comparator<Argument>() {
+                public int compare(Argument o1, Argument o2) {
+                    return Integer.valueOf(o1.index()).compareTo(Integer.valueOf(o2.index()));
+                }
+            });
             options = new HashSet<Option>(options);
             options.add(HELP);
             if (command != null && (command.description() != null || command.name() != null))