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/04/30 18:47:19 UTC

svn commit: r770318 - in /felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi: BundleCommand.java BundlesCommand.java RestartBundle.java StartBundle.java StopBundle.java Util.java

Author: gnodet
Date: Thu Apr 30 16:47:19 2009
New Revision: 770318

URL: http://svn.apache.org/viewvc?rev=770318&view=rev
Log:
FELIX-1104, FELIX-1112: improve start/stop/restart to support multiple bundle ids

Modified:
    felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundleCommand.java
    felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundlesCommand.java
    felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/RestartBundle.java
    felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StartBundle.java
    felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StopBundle.java
    felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/Util.java

Modified: felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundleCommand.java
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundleCommand.java?rev=770318&r1=770317&r2=770318&view=diff
==============================================================================
--- felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundleCommand.java (original)
+++ felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundleCommand.java Thu Apr 30 16:47:19 2009
@@ -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;

Modified: felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundlesCommand.java
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundlesCommand.java?rev=770318&r1=770317&r2=770318&view=diff
==============================================================================
--- felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundlesCommand.java (original)
+++ felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/BundlesCommand.java Thu Apr 30 16:47:19 2009
@@ -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);
+                    }
                 }
             }
         }

Modified: felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/RestartBundle.java
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/RestartBundle.java?rev=770318&r1=770317&r2=770318&view=diff
==============================================================================
--- felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/RestartBundle.java (original)
+++ felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/RestartBundle.java Thu Apr 30 16:47:19 2009
@@ -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

Modified: felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StartBundle.java
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StartBundle.java?rev=770318&r1=770317&r2=770318&view=diff
==============================================================================
--- felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StartBundle.java (original)
+++ felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StartBundle.java Thu Apr 30 16:47:19 2009
@@ -16,12 +16,17 @@
  */
 package org.apache.servicemix.kernel.gshell.osgi;
 
-import org.osgi.framework.Bundle;
+import java.util.List;
 
-public class StartBundle extends BundleCommand {
+import org.apache.geronimo.gshell.clp.Option;
+import org.osgi.framework.Bundle;
 
-    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();
+        }
     }
 
 }

Modified: felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StopBundle.java
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StopBundle.java?rev=770318&r1=770317&r2=770318&view=diff
==============================================================================
--- felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StopBundle.java (original)
+++ felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/StopBundle.java Thu Apr 30 16:47:19 2009
@@ -16,12 +16,17 @@
  */
 package org.apache.servicemix.kernel.gshell.osgi;
 
-import org.osgi.framework.Bundle;
+import java.util.List;
 
-public class StopBundle extends BundleCommand {
+import org.apache.geronimo.gshell.clp.Option;
+import org.osgi.framework.Bundle;
 
-    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

Modified: felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/Util.java
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/Util.java?rev=770318&r1=770317&r2=770318&view=diff
==============================================================================
--- felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/Util.java (original)
+++ felix/trunk/karaf/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/Util.java Thu Apr 30 16:47:19 2009
@@ -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