FELIX-1104, FELIX-1112: improve start/stop/restart to support multiple bundle ids
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@770318 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundleCommand.java b/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundleCommand.java
index 5a3fd3f..2a20dbf 100644
--- a/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundleCommand.java
+++ b/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundleCommand.java
@@ -20,8 +20,6 @@
import org.apache.geronimo.gshell.clp.Option;
import org.apache.servicemix.kernel.gshell.core.OsgiCommandSupport;
import org.osgi.framework.Bundle;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.startlevel.StartLevel;
public abstract class BundleCommand extends OsgiCommandSupport {
@@ -37,42 +35,13 @@
io.out.println("Bundle " + id + " not found");
return Result.FAILURE;
}
- if (!force) {
- ServiceReference ref = getBundleContext().getServiceReference(StartLevel.class.getName());
- if (ref != null) {
- StartLevel sl = getService(StartLevel.class, ref);
- if (sl != null) {
- int level = sl.getBundleStartLevel(bundle);
- if (level < 50) {
- for (;;) {
- StringBuffer sb = new StringBuffer();
- io.err.print("You are about to access a system bundle. Do you want to continue (yes/no): ");
- io.err.flush();
- for (;;) {
- int c = io.in.read();
- if (c < 0) {
- return Result.FAILURE;
- }
- io.err.print((char) c);
- if (c == '\r' || c == '\n') {
- break;
- }
- sb.append((char) c);
- }
- String str = sb.toString();
- if ("yes".equals(str)) {
- break;
- }
- if ("no".equals(str)) {
- return Result.FAILURE;
- }
- }
- }
- }
- }
+
+ if (!force && Util.isASystemBundle(getBundleContext(), bundle) && !Util.accessToSystemBundleIsAllowed(bundle.getBundleId(), io)) {
+ return Result.FAILURE;
+ } else {
+ doExecute(bundle);
+ return Result.SUCCESS;
}
- doExecute(bundle);
- return Result.SUCCESS;
}
protected abstract void doExecute(Bundle bundle) throws Exception;
diff --git a/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundlesCommand.java b/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundlesCommand.java
index 7b937ee..776009a 100644
--- a/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundlesCommand.java
+++ b/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundlesCommand.java
@@ -20,14 +20,18 @@
import java.util.List;
import org.apache.geronimo.gshell.clp.Argument;
+import org.apache.geronimo.gshell.clp.Option;
import org.apache.servicemix.kernel.gshell.core.OsgiCommandSupport;
import org.osgi.framework.Bundle;
public abstract class BundlesCommand extends OsgiCommandSupport {
- @Argument(required = false, multiValued = true, description = "Bundle IDs")
+ @Argument(required = true, multiValued = true, description = "Bundle IDs")
List<Long> ids;
+ @Option(name = "--force")
+ boolean force;
+
protected Object doExecute() throws Exception {
List<Bundle> bundles = new ArrayList<Bundle>();
if (ids != null && !ids.isEmpty()) {
@@ -36,7 +40,9 @@
if (bundle == null) {
io.err.println("Bundle ID" + id + " is invalid");
} else {
- bundles.add(bundle);
+ if (force || !Util.isASystemBundle(getBundleContext(), bundle) || Util.accessToSystemBundleIsAllowed(bundle.getBundleId(), io)) {
+ bundles.add(bundle);
+ }
}
}
}
diff --git a/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/RestartBundle.java b/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/RestartBundle.java
index 6793ecb..888dfa8 100644
--- a/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/RestartBundle.java
+++ b/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/RestartBundle.java
@@ -16,13 +16,17 @@
*/
package org.apache.servicemix.kernel.gshell.osgi;
+import java.util.List;
+
import org.osgi.framework.Bundle;
-public class RestartBundle extends BundleCommand {
+public class RestartBundle extends BundlesCommand {
- protected void doExecute(Bundle bundle) throws Exception {
- bundle.stop();
- bundle.start();
+ protected void doExecute(List<Bundle> bundles) throws Exception {
+ for (Bundle bundle : bundles) {
+ bundle.stop();
+ bundle.start();
+ }
}
}
\ No newline at end of file
diff --git a/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StartBundle.java b/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StartBundle.java
index 6639802..38e33bd 100644
--- a/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StartBundle.java
+++ b/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StartBundle.java
@@ -16,12 +16,17 @@
*/
package org.apache.servicemix.kernel.gshell.osgi;
+import java.util.List;
+
+import org.apache.geronimo.gshell.clp.Option;
import org.osgi.framework.Bundle;
-public class StartBundle extends BundleCommand {
-
- protected void doExecute(Bundle bundle) throws Exception {
- bundle.start();
+public class StartBundle extends BundlesCommand {
+
+ protected void doExecute(List<Bundle> bundles) throws Exception {
+ for (Bundle bundle : bundles) {
+ bundle.start();
+ }
}
}
diff --git a/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StopBundle.java b/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StopBundle.java
index 867a0a0..96fa466 100644
--- a/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StopBundle.java
+++ b/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StopBundle.java
@@ -16,12 +16,17 @@
*/
package org.apache.servicemix.kernel.gshell.osgi;
+import java.util.List;
+
+import org.apache.geronimo.gshell.clp.Option;
import org.osgi.framework.Bundle;
-public class StopBundle extends BundleCommand {
-
- protected void doExecute(Bundle bundle) throws Exception {
- bundle.stop();
+public class StopBundle extends BundlesCommand {
+
+ protected void doExecute(List<Bundle> bundles) throws Exception {
+ for (Bundle bundle : bundles) {
+ bundle.stop();
+ }
}
}
\ No newline at end of file
diff --git a/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/Util.java b/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/Util.java
index c8481d6..fbe2f4c 100644
--- a/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/Util.java
+++ b/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/Util.java
@@ -18,8 +18,14 @@
*/
package org.apache.servicemix.kernel.gshell.osgi;
+import java.io.IOException;
+
+import org.apache.geronimo.gshell.io.IO;
import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.startlevel.StartLevel;
public class Util
{
@@ -106,4 +112,61 @@
}
}
}
+
+ /**
+ * Check if a bundle is a system bundle (start level < 50)
+ *
+ * @param bundleContext
+ * @param bundle
+ * @return true if the bundle has start level minor than 50
+ */
+ public static boolean isASystemBundle(BundleContext bundleContext, Bundle bundle) {
+ ServiceReference ref = bundleContext.getServiceReference(StartLevel.class.getName());
+ if (ref != null) {
+ StartLevel sl = (StartLevel) bundleContext.getService(ref);
+ if (sl != null) {
+ int level = sl.getBundleStartLevel(bundle);
+ if (level < 50)
+ return true;
+ else
+ return false;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Ask the user to confirm the access to a system bundle
+ *
+ * @param bundleId
+ * @param io
+ * @return true if the user confirm
+ * @throws IOException
+ */
+ public static boolean accessToSystemBundleIsAllowed(long bundleId, IO io) throws IOException {
+ for (;;) {
+ StringBuffer sb = new StringBuffer();
+ io.err.print("You are about to access system bundle " + bundleId + ". Do you want to continue (yes/no): ");
+ io.err.flush();
+ for (;;) {
+ int c = io.in.read();
+ if (c < 0) {
+ return false;
+ }
+ io.err.print((char) c);
+ if (c == '\r' || c == '\n') {
+ break;
+ }
+ sb.append((char) c);
+ }
+ String str = sb.toString();
+ if ("yes".equals(str)) {
+ return true;
+ }
+ if ("no".equals(str)) {
+ return false;
+ }
+ }
+ }
+
}
\ No newline at end of file