FELIX-2330: Uninstall, refresh, and resolve osgi shell commands do not support multiple bundle ids

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@945083 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/BundlesCommandOptional.java b/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/BundlesCommandOptional.java
new file mode 100644
index 0000000..13974e0
--- /dev/null
+++ b/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/BundlesCommandOptional.java
@@ -0,0 +1,54 @@
+/*
+ * 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.osgi;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Option;
+import org.apache.felix.karaf.shell.console.OsgiCommandSupport;
+import org.osgi.framework.Bundle;
+
+public abstract class BundlesCommandOptional extends OsgiCommandSupport {
+
+    @Argument(index = 0, name = "ids", description = "The list of bundle IDs separated by whitespaces", required = false, multiValued = true)
+    List<Long> ids;
+
+    @Option(name = "--force", aliases = {}, description = "Forces the command to execute", required = false, multiValued = false)
+    boolean force;
+
+    protected Object doExecute() throws Exception {
+        List<Bundle> bundles = new ArrayList<Bundle>();
+        if (ids != null && !ids.isEmpty()) {
+            for (long id : ids) {
+                Bundle bundle = getBundleContext().getBundle(id);
+                if (bundle == null) {
+                    System.err.println("Bundle ID" + id + " is invalid");
+                } else {
+                    if (force || !Util.isASystemBundle(getBundleContext(), bundle) || Util.accessToSystemBundleIsAllowed(bundle.getBundleId(), session)) {
+                        bundles.add(bundle);
+                    }
+                }
+            }
+        }
+        doExecute(bundles);
+        return null;
+    }
+
+    protected abstract void doExecute(List<Bundle> bundles) throws Exception;
+}
\ No newline at end of file
diff --git a/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/RefreshBundle.java b/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/RefreshBundle.java
index 33f1db5..4bca7d0 100644
--- a/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/RefreshBundle.java
+++ b/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/RefreshBundle.java
@@ -16,6 +16,8 @@
  */
 package org.apache.felix.karaf.shell.osgi;
 
+import java.util.List;
+
 import org.apache.felix.karaf.shell.console.OsgiCommandSupport;
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
@@ -24,39 +26,30 @@
 import org.osgi.service.packageadmin.PackageAdmin;
 
 @Command(scope = "osgi", name = "refresh", description = "Refresh a bundle")
-public class RefreshBundle extends OsgiCommandSupport {
+public class RefreshBundle extends BundlesCommandOptional {
 
-    @Argument(index = 0, name = "id", description = "The ID of the bundle to refresh", required = false, multiValued = false)
-    Long id;
-
-    protected Object doExecute() throws Exception {
+    protected void doExecute(List<Bundle> bundles) throws Exception {
         // Get package admin service.
         ServiceReference ref = getBundleContext().getServiceReference(PackageAdmin.class.getName());
         if (ref == null) {
             System.out.println("PackageAdmin service is unavailable.");
-            return null;
+            return;
         }
         try {
             PackageAdmin pa = (PackageAdmin) getBundleContext().getService(ref);
             if (pa == null) {
                 System.out.println("PackageAdmin service is unavailable.");
-                return null;
+                return;
             }
-            if (id == null) {
+            if (bundles.isEmpty()) {
                 pa.refreshPackages(null);
             }
             else {
-                Bundle bundle = getBundleContext().getBundle(id);
-                if (bundle == null) {
-                    System.out.println("Bundle " + id + " not found");
-                    return null;
-                }
-                pa.refreshPackages(new Bundle[] { bundle });
+                pa.refreshPackages(bundles.toArray(new Bundle[bundles.size()]));
             }
         }
         finally {
             getBundleContext().ungetService(ref);
         }
-        return null;
     }
 }
diff --git a/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/ResolveBundle.java b/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/ResolveBundle.java
index 1251892..b717e9d 100644
--- a/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/ResolveBundle.java
+++ b/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/ResolveBundle.java
@@ -16,15 +16,17 @@
  */
 package org.apache.felix.karaf.shell.osgi;
 
+import java.util.List;
+
 import org.osgi.framework.Bundle;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.packageadmin.PackageAdmin;
 import org.apache.felix.gogo.commands.Command;
 
 @Command(scope = "osgi", name = "resolve", description = "Resolve bundle(s)")
-public class ResolveBundle extends BundleCommand {
+public class ResolveBundle extends BundlesCommandOptional {
 
-    protected void doExecute(Bundle bundle) throws Exception {
+    protected void doExecute(List<Bundle> bundles) throws Exception {
         // Get package admin service.
         ServiceReference ref = getBundleContext().getServiceReference(PackageAdmin.class.getName());
         if (ref == null) {
@@ -37,7 +39,11 @@
                 System.out.println("PackageAdmin service is unavailable.");
                 return;
             }
-            pa.resolveBundles(new Bundle[] { bundle });
+            if (bundles.isEmpty()) {
+                pa.resolveBundles(null);
+            } else {
+                pa.resolveBundles(bundles.toArray(new Bundle[bundles.size()]));
+            }
         }
         finally {
             getBundleContext().ungetService(ref);
diff --git a/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/RestartBundle.java b/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/RestartBundle.java
index eaee362..9085395 100644
--- a/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/RestartBundle.java
+++ b/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/RestartBundle.java
@@ -27,6 +27,8 @@
     protected void doExecute(List<Bundle> bundles) throws Exception {
         for (Bundle bundle : bundles) {
             bundle.stop();
+        }
+        for (Bundle bundle : bundles) {
             bundle.start();
         }
     }
diff --git a/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/UninstallBundle.java b/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/UninstallBundle.java
index 363f603..e8a8e51 100644
--- a/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/UninstallBundle.java
+++ b/karaf/shell/osgi/src/main/java/org/apache/felix/karaf/shell/osgi/UninstallBundle.java
@@ -16,14 +16,18 @@
  */
 package org.apache.felix.karaf.shell.osgi;
 
+import java.util.List;
+
 import org.osgi.framework.Bundle;
 import org.apache.felix.gogo.commands.Command;
 
-@Command(scope = "osgi", name = "uninstall", description = "Uninstall bundle")
-public class UninstallBundle extends BundleCommand {
+@Command(scope = "osgi", name = "uninstall", description = "Uninstall bundle(s)")
+public class UninstallBundle extends BundlesCommand {
 
-    protected void doExecute(Bundle bundle) throws Exception {
-        bundle.uninstall();
+    protected void doExecute(List<Bundle> bundles) throws Exception {
+        for (Bundle bundle : bundles) {
+            bundle.uninstall();
+        }
     }
 
 }