[jboss-cvs] JBossAS SVN: r96969 - projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Nov 25 11:17:38 EST 2009


Author: alesj
Date: 2009-11-25 11:17:37 -0500 (Wed, 25 Nov 2009)
New Revision: 96969

Modified:
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiBundleManager.java
Log:
[JBOSGI-141]; remove the need for additional service registry, controller keeps it already.

Modified: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiBundleManager.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiBundleManager.java	2009-11-25 16:11:46 UTC (rev 96968)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiBundleManager.java	2009-11-25 16:17:37 UTC (rev 96969)
@@ -62,6 +62,7 @@
 import org.jboss.deployers.vfs.spi.client.VFSDeployment;
 import org.jboss.deployers.vfs.spi.client.VFSDeploymentFactory;
 import org.jboss.kernel.Kernel;
+import org.jboss.kernel.spi.dependency.KernelController;
 import org.jboss.logging.Logger;
 import org.jboss.osgi.deployment.deployer.Deployment;
 import org.jboss.osgi.framework.metadata.OSGiMetaData;
@@ -76,7 +77,6 @@
 import org.jboss.osgi.framework.plugins.ServicePlugin;
 import org.jboss.osgi.framework.util.NoFilter;
 import static org.jboss.osgi.spi.OSGiConstants.PROPERTY_AUTO_START;
-import org.jboss.util.collection.ConcurrentSet;
 import org.jboss.virtual.VFS;
 import org.jboss.virtual.VFSUtils;
 import org.jboss.virtual.VirtualFile;
@@ -133,9 +133,6 @@
    /** The bundles by id */
    private List<AbstractBundleState> bundles = new CopyOnWriteArrayList<AbstractBundleState>();
 
-   /** The registered services */
-   private Set<OSGiServiceState> registeredServices = new ConcurrentSet<OSGiServiceState>();
-
    /** The kernel */
    private Kernel kernel;
 
@@ -1056,30 +1053,51 @@
     */
    Collection<OSGiServiceState> getServices(AbstractBundleState bundle, String clazz, Filter filter, boolean checkAssignable)
    {
-      if (filter == null)
-         filter = NoFilter.INSTANCE;
+      Set<ControllerContext> contexts;
+      KernelController controller = kernel.getController();
 
-      if (registeredServices.isEmpty())
-         return null;
-
       // Don't check assignabilty for the system bundle
-      if (bundle.getBundleId() == 0)
+      boolean isSystemBundle = (bundle.getBundleId() == 0);
+      if (isSystemBundle)
          checkAssignable = false;
 
+      // TODO - a bit slow for system bundle
+      if (clazz != null && isSystemBundle == false)
+      {
+         Class<?> type = loadClass(bundle, clazz);
+         if (type == null)
+            return null;
+
+         contexts = controller.getInstantiatedContexts(type);
+      }
+      else
+      {
+         contexts = controller.getContextsByState(ControllerState.INSTALLED);
+      }
+
+      if (contexts == null || contexts.isEmpty())
+         return null;
+
+      if (filter == null)
+         filter = NoFilter.INSTANCE;
+
       // review: optimise this, e.g. index by class
       // Use a sorted set to order services according to spec
       Set<OSGiServiceState> result = new TreeSet<OSGiServiceState>(ServiceComparator.INSTANCE);
-      for (OSGiServiceState service : registeredServices)
+      for (ControllerContext context : contexts)
       {
-         // Check the state, filter and permission
-         if (service.isUnregistered() == false && filter.match(service) && service.hasPermission())
+         if (context instanceof OSGiServiceState)
          {
-            // Check any passed class matches
-            if (clazz == null || service.matchClass(clazz))
+            OSGiServiceState service = OSGiServiceState.class.cast(context);
+            // Check the state, filter and permission
+            if (service.isUnregistered() == false && filter.match(service) && service.hasPermission())
             {
-               // Check the assignability
-               if (checkAssignable == false || service.isAssignable(bundle))
-                  result.add(service);
+               if (clazz == null || isSystemBundle == false || service.matchClass(clazz))
+               {
+                  // Check the assignability
+                  if (checkAssignable == false || service.isAssignable(bundle))
+                     result.add(service);
+               }
             }
          }
       }
@@ -1087,6 +1105,26 @@
    }
 
    /**
+    * Load class from a bundle.
+    * If it cannot be loaded, return null.
+    *
+    * @param bundle the bundle to load from
+    * @param clazz the class
+    * @return class or null
+    */
+   private Class<?> loadClass(Bundle bundle, String clazz)
+   {
+      try
+      {
+         return bundle.loadClass(clazz);
+      }
+      catch (ClassNotFoundException e)
+      {
+         return null;
+      }
+   }
+
+   /**
     * Get service reference
     * 
     * @param bundle the referencing bundle
@@ -1178,7 +1216,6 @@
    {
       OSGiServiceState result = new OSGiServiceState(bundleState, clazzes, service, properties);
       result.internalRegister();
-      registeredServices.add(result);
 
       try
       {
@@ -1188,10 +1225,7 @@
       catch (Throwable t)
       {
          fireError(bundleState, "installing service to MC in", t);
-
-         registeredServices.remove(result);
          result.internalUnregister();
-
          throw new RuntimeException(t);
       }
 
@@ -1214,7 +1248,6 @@
       FrameworkEventsPlugin plugin = getPlugin(FrameworkEventsPlugin.class);
       plugin.fireServiceEvent(serviceState.getBundleState(), ServiceEvent.UNREGISTERING, serviceState);
 
-      registeredServices.remove(serviceState);
       serviceState.internalUnregister();
    }
 




More information about the jboss-cvs-commits mailing list