[jboss-osgi-commits] JBoss-OSGI SVN: r102500 - in projects/jboss-osgi: projects/bundles/jmx/trunk/jmx-bundle/src/main/java/org/jboss/osgi/jmx and 4 other directories.

jboss-osgi-commits at lists.jboss.org jboss-osgi-commits at lists.jboss.org
Wed Mar 17 00:16:29 EDT 2010


Author: thomas.diesler at jboss.com
Date: 2010-03-17 00:16:20 -0400 (Wed, 17 Mar 2010)
New Revision: 102500

Modified:
   projects/jboss-osgi/projects/bundles/jmx/trunk/jmx-bundle/pom.xml
   projects/jboss-osgi/projects/bundles/jmx/trunk/jmx-bundle/src/main/java/org/jboss/osgi/jmx/JMXCapability.java
   projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/capability/Capability.java
   projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiRuntimeHelper.java
   projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/OSGiRuntimeImpl.java
   projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/jbosgi298/OSGi298TestCase.java
Log:
[JBOSGI-298] InstanceAlreadyExistsException: osgi.core:type=framework,version=1.5 

Modified: projects/jboss-osgi/projects/bundles/jmx/trunk/jmx-bundle/pom.xml
===================================================================
--- projects/jboss-osgi/projects/bundles/jmx/trunk/jmx-bundle/pom.xml	2010-03-17 04:05:59 UTC (rev 102499)
+++ projects/jboss-osgi/projects/bundles/jmx/trunk/jmx-bundle/pom.xml	2010-03-17 04:16:20 UTC (rev 102500)
@@ -124,6 +124,7 @@
               <!-- jboss-osgi --> 
               org.jboss.osgi.jmx;version="[1.0,2.0)", 
               org.jboss.osgi.spi*;version="[1.0,2.0)", 
+              org.jboss.osgi.testing*;version="[1.0,2.0)", 
               
               <!-- osgi --> 
               org.osgi.framework;version="[1.5,2.0)",

Modified: projects/jboss-osgi/projects/bundles/jmx/trunk/jmx-bundle/src/main/java/org/jboss/osgi/jmx/JMXCapability.java
===================================================================
--- projects/jboss-osgi/projects/bundles/jmx/trunk/jmx-bundle/src/main/java/org/jboss/osgi/jmx/JMXCapability.java	2010-03-17 04:05:59 UTC (rev 102499)
+++ projects/jboss-osgi/projects/bundles/jmx/trunk/jmx-bundle/src/main/java/org/jboss/osgi/jmx/JMXCapability.java	2010-03-17 04:16:20 UTC (rev 102500)
@@ -24,10 +24,16 @@
 //$Id$
 
 import javax.management.MBeanServer;
+import javax.management.ObjectName;
 
+import org.jboss.logging.Logger;
 import org.jboss.osgi.spi.capability.Capability;
 import org.jboss.osgi.spi.capability.CompendiumCapability;
 import org.jboss.osgi.testing.OSGiRuntime;
+import org.osgi.framework.BundleException;
+import org.osgi.jmx.framework.BundleStateMBean;
+import org.osgi.jmx.framework.FrameworkMBean;
+import org.osgi.jmx.framework.ServiceStateMBean;
 
 /**
  * Adds the JMX capability to the {@link OSGiRuntime}
@@ -50,6 +56,9 @@
  */
 public class JMXCapability extends Capability
 {
+   // Provide logging
+   private static final Logger log = Logger.getLogger(Capability.class);
+   
    public JMXCapability()
    {
       super(MBeanServer.class.getName());
@@ -62,4 +71,49 @@
       addBundle("bundles/jboss-osgi-jmx.jar");
       addBundle("bundles/org.apache.aries.jmx.jar");
    }
+
+   @Override
+   public void start(OSGiRuntime runtime) throws BundleException
+   {
+      super.start(runtime);
+      assertMBeanRegistration(runtime, true);
+   }
+
+   @Override
+   public void stop(OSGiRuntime runtime)
+   {
+      super.stop(runtime);
+      assertMBeanRegistration(runtime, false);
+   }
+
+   private void assertMBeanRegistration(OSGiRuntime runtime, boolean state)
+   {
+      MBeanServer server = (MBeanServer)runtime.getMBeanServer();
+      ObjectName fwkName = ObjectNameFactory.create(FrameworkMBean.OBJECTNAME);
+      ObjectName bndName = ObjectNameFactory.create(BundleStateMBean.OBJECTNAME);
+      ObjectName srvName = ObjectNameFactory.create(ServiceStateMBean.OBJECTNAME);
+      
+      int timeout = 2000;
+      while ( 0 < (timeout -= 200))
+      {
+         if (server.isRegistered(fwkName) != state || server.isRegistered(bndName) != state || server.isRegistered(srvName) != state)
+         {
+            try
+            {
+               Thread.sleep(200);
+            }
+            catch (InterruptedException e)
+            {
+               // ignore
+            }
+         }
+      }
+      
+      if (server.isRegistered(fwkName) != state)
+         log.warn("FrameworkMBean " + (state ? "not" : "still") + " registered");
+      if (server.isRegistered(bndName) != state)
+         log.warn("BundleStateMBean " + (state ? "not" : "still") + " registered");
+      if (server.isRegistered(srvName) != state)
+         log.warn("ServiceStateMBean " + (state ? "not" : "still") + " registered");
+   }
 }
\ No newline at end of file

Modified: projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/capability/Capability.java
===================================================================
--- projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/capability/Capability.java	2010-03-17 04:05:59 UTC (rev 102499)
+++ projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/capability/Capability.java	2010-03-17 04:16:20 UTC (rev 102500)
@@ -29,8 +29,13 @@
 import java.util.List;
 import java.util.Map;
 
+import org.jboss.logging.Logger;
 import org.jboss.osgi.spi.util.BundleInfo;
+import org.jboss.osgi.testing.OSGiBundle;
+import org.jboss.osgi.testing.OSGiRuntime;
+import org.jboss.osgi.testing.OSGiRuntimeHelper;
 import org.osgi.framework.BundleException;
+import org.osgi.framework.Version;
 
 /**
  * An abstract OSGi capability that can be installed in an OSGiRuntime.
@@ -44,12 +49,16 @@
  */
 public abstract class Capability
 {
+   // Provide logging
+   private static final Logger log = Logger.getLogger(Capability.class);
+
    private String serviceName;
    private String filter;
    private Map<String, String> systemProperties;
 
    private List<Capability> dependencies;
    private List<BundleInfo> bundles;
+   private List<OSGiBundle> installed = new ArrayList<OSGiBundle>();
 
    /**
     * Construct a capability that is identified by the given service name. 
@@ -148,7 +157,7 @@
          Throwable cause = ex.getCause();
          if (cause instanceof RuntimeException)
             throw (RuntimeException)cause;
-         
+
          throw new IllegalArgumentException("Cannot create bundle info for: " + location, ex);
       }
       getBundlesInternal().add(info);
@@ -177,4 +186,59 @@
 
       return bundles;
    }
+
+   public List<OSGiBundle> getInstalledBundles()
+   {
+      return Collections.unmodifiableList(installed);
+   }
+
+   public void install(OSGiRuntime runtime) throws BundleException
+   {
+      for (BundleInfo info : getBundles())
+      {
+         String location = info.getLocation();
+         String symName = info.getSymbolicName();
+         Version version = info.getVersion();
+         if (runtime.getBundle(symName, version) == null)
+         {
+            OSGiBundle bundle = runtime.installBundle(location);
+            installed.add(bundle);
+         }
+         else
+         {
+            log.debug("Skip bundle: " + location);
+         }
+      }
+   }
+
+   public void start(OSGiRuntime runtime) throws BundleException
+   {
+      for (OSGiBundle bundle : getInstalledBundles())
+      {
+         bundle.start();
+      }
+   }
+
+   public void stop(OSGiRuntime runtime)
+   {
+      List<OSGiBundle> installedReverse = new ArrayList<OSGiBundle>(getInstalledBundles());
+      Collections.reverse(installedReverse);
+
+      for (OSGiBundle bundle : installedReverse)
+      {
+         OSGiRuntimeHelper.failsafeStop(bundle);
+      }
+   }
+
+   public void uninstall(OSGiRuntime runtime)
+   {
+      List<OSGiBundle> installedReverse = new ArrayList<OSGiBundle>(getInstalledBundles());
+      Collections.reverse(installedReverse);
+
+      for (OSGiBundle bundle : installedReverse)
+      {
+         OSGiRuntimeHelper.failsafeUninstall(bundle);
+         installed.remove(bundle);
+      }
+   }
 }
\ No newline at end of file

Modified: projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiRuntimeHelper.java
===================================================================
--- projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiRuntimeHelper.java	2010-03-17 04:05:59 UTC (rev 102499)
+++ projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiRuntimeHelper.java	2010-03-17 04:16:20 UTC (rev 102500)
@@ -21,10 +21,12 @@
  */
 package org.jboss.osgi.testing;
 
+import org.jboss.logging.Logger;
 import org.jboss.osgi.spi.framework.OSGiBootstrap;
 import org.jboss.osgi.spi.framework.OSGiBootstrapProvider;
 import org.jboss.osgi.testing.internal.EmbeddedRuntime;
 import org.jboss.osgi.testing.internal.RemoteRuntime;
+import org.osgi.framework.Bundle;
 
 /**
  * A helper for the OSGi runtime abstraction. 
@@ -34,6 +36,9 @@
  */
 public class OSGiRuntimeHelper extends OSGiTestHelper
 {
+   // Provide logging
+   private static final Logger log = Logger.getLogger(OSGiRuntimeHelper.class);
+   
    // The OSGiBootstrapProvider is a lazy property of the helper
    private OSGiBootstrapProvider bootProvider;
    private boolean skipBootstrap;
@@ -85,4 +90,35 @@
    {
       return new RemoteRuntime(this);
    }
+   
+   public static void failsafeStop(OSGiBundle bundle)
+   {
+      if (bundle != null)
+      {
+         try
+         {
+            bundle.stop();
+         }
+         catch (Exception ex)
+         {
+            log.warn("Cannot stop bundle: " + bundle, ex);
+         }
+      }
+   }
+   
+   public static void failsafeUninstall(OSGiBundle bundle)
+   {
+      if (bundle != null)
+      {
+         try
+         {
+            if (bundle.getState() != Bundle.UNINSTALLED)
+               bundle.uninstall();
+         }
+         catch (Exception ex)
+         {
+            log.warn("Cannot uninstall bundle: " + bundle, ex);
+         }
+      }
+   }
 }

Modified: projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/OSGiRuntimeImpl.java
===================================================================
--- projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/OSGiRuntimeImpl.java	2010-03-17 04:05:59 UTC (rev 102499)
+++ projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/OSGiRuntimeImpl.java	2010-03-17 04:16:20 UTC (rev 102500)
@@ -58,7 +58,6 @@
 import org.jboss.osgi.vfs.VirtualFile;
 import org.jboss.shrinkwrap.api.Archive;
 import org.jboss.shrinkwrap.api.exporter.ZipExporter;
-import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Version;
 import org.osgi.jmx.framework.BundleStateMBean;
@@ -112,30 +111,13 @@
       if (srefs == null || srefs.length == 0)
       {
          log.debug("Add capability: " + capability);
-
+         
          // Install the capability bundles 
-         List<OSGiBundle> installed = new ArrayList<OSGiBundle>();
-         for (BundleInfo info : capability.getBundles())
-         {
-            String location = info.getLocation();
-            String symName = info.getSymbolicName();
-            Version version = info.getVersion();
-            if (bundles.get(location) == null && getBundle(symName, version) == null)
-            {
-               OSGiBundle bundle = installBundle(location);
-               installed.add(bundle);
-            }
-            else
-            {
-               log.debug("Skip bundle: " + location);
-            }
-         }
-
-         // Start the capability bundles
-         for (OSGiBundle bundle : installed)
-         {
-            bundle.start();
-         }
+         capability.install(this);
+         
+         // Start the capability bundles 
+         capability.start(this);
+         
          capabilities.add(capability);
       }
       else
@@ -150,20 +132,12 @@
       {
          log.debug("Remove capability : " + capability);
 
-         List<BundleInfo> bundleInfos = new ArrayList<BundleInfo>(capability.getBundles());
-         Collections.reverse(bundleInfos);
-
-         for (BundleInfo info : bundleInfos)
-         {
-            OSGiBundle bundle = bundles.get(info.getLocation());
-            failsafeStop(bundle);
-         }
+         // Install the capability bundles 
+         capability.stop(this);
          
-         for (BundleInfo info : bundleInfos)
-         {
-            OSGiBundle bundle = bundles.remove(info.getLocation());
-            failsafeUninstall(bundle);
-         }
+         // Start the capability bundles 
+         capability.uninstall(this);
+         
       }
 
       List<Capability> dependencies = new ArrayList<Capability>(capability.getDependencies());
@@ -205,7 +179,8 @@
       while (locations.size() > 0)
       {
          String location = locations.remove(0);
-         failsafeUninstall(bundles.remove(location));
+         OSGiBundle bundle = bundles.remove(location);
+         OSGiRuntimeHelper.failsafeUninstall(bundle);
       }
 
       // Uninstall the capabilities
@@ -417,35 +392,4 @@
       
       return AbstractVFS.getRoot(target.toURI().toURL());
    }
-   
-   private void failsafeStop(OSGiBundle bundle)
-   {
-      if (bundle != null)
-      {
-         try
-         {
-            bundle.stop();
-         }
-         catch (Exception ex)
-         {
-            log.warn("Cannot stop bundle: " + bundle, ex);
-         }
-      }
-   }
-   
-   private void failsafeUninstall(OSGiBundle bundle)
-   {
-      if (bundle != null)
-      {
-         try
-         {
-            if (bundle.getState() != Bundle.UNINSTALLED)
-               bundle.uninstall();
-         }
-         catch (Exception ex)
-         {
-            log.warn("Cannot uninstall bundle: " + bundle, ex);
-         }
-      }
-   }
 }

Modified: projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/jbosgi298/OSGi298TestCase.java
===================================================================
--- projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/jbosgi298/OSGi298TestCase.java	2010-03-17 04:05:59 UTC (rev 102499)
+++ projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/jbosgi298/OSGi298TestCase.java	2010-03-17 04:16:20 UTC (rev 102500)
@@ -36,7 +36,6 @@
 import org.jboss.osgi.testing.OSGiRuntimeTest;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.osgi.jmx.framework.BundleStateMBean;
 import org.osgi.jmx.framework.FrameworkMBean;
@@ -50,7 +49,6 @@
  * @author thomas.diesler at jboss.com
  * @since 05-Mar-2010
  */
- at Ignore
 public class OSGi298TestCase extends OSGiRuntimeTest
 {
    private OSGiRuntime runtime;
@@ -72,21 +70,17 @@
    public void testJMXCapability() throws Exception
    {
       JMXCapability capability = new JMXCapability();
+      runtime.addCapability(capability);
 
-      for (int i = 0; i < 10; i++)
-      {
-         runtime.addCapability(capability);
+      assertTrue("FrameworkMBean registered", isRegistered(FrameworkMBean.OBJECTNAME));
+      assertTrue("BundleStateMBean registered", isRegistered(BundleStateMBean.OBJECTNAME));
+      assertTrue("ServiceStateMBean registered", isRegistered(ServiceStateMBean.OBJECTNAME));
 
-         assertTrue("FrameworkMBean registered", isRegistered(FrameworkMBean.OBJECTNAME));
-         assertTrue("BundleStateMBean registered", isRegistered(BundleStateMBean.OBJECTNAME));
-         assertTrue("ServiceStateMBean registered", isRegistered(ServiceStateMBean.OBJECTNAME));
+      runtime.removeCapability(capability);
 
-         runtime.removeCapability(capability);
-
-         assertFalse("FrameworkMBean registered", isRegistered(FrameworkMBean.OBJECTNAME));
-         assertFalse("BundleStateMBean registered", isRegistered(BundleStateMBean.OBJECTNAME));
-         assertFalse("ServiceStateMBean registered", isRegistered(ServiceStateMBean.OBJECTNAME));
-      }
+      assertFalse("FrameworkMBean registered", isRegistered(FrameworkMBean.OBJECTNAME));
+      assertFalse("BundleStateMBean registered", isRegistered(BundleStateMBean.OBJECTNAME));
+      assertFalse("ServiceStateMBean registered", isRegistered(ServiceStateMBean.OBJECTNAME));
    }
 
    private boolean isRegistered(String oname) throws MalformedObjectNameException



More information about the jboss-osgi-commits mailing list