[jboss-osgi-commits] JBoss-OSGI SVN: r97473 - in projects/jboss-osgi/projects/runtime/framework/trunk/src: test/java/org/jboss/test/osgi/service and 1 other directories.

jboss-osgi-commits at lists.jboss.org jboss-osgi-commits at lists.jboss.org
Fri Dec 4 13:26:14 EST 2009


Author: alesj
Date: 2009-12-04 13:26:13 -0500 (Fri, 04 Dec 2009)
New Revision: 97473

Modified:
   projects/jboss-osgi/projects/runtime/framework/trunk/src/main/java/org/jboss/osgi/framework/bundle/OSGiBundleManager.java
   projects/jboss-osgi/projects/runtime/framework/trunk/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java
   projects/jboss-osgi/projects/runtime/framework/trunk/src/test/java/org/jboss/test/osgi/service/ServiceMixUnitTestCase.java
   projects/jboss-osgi/projects/runtime/framework/trunk/src/test/java/org/jboss/test/osgi/service/support/d/D.java
Log:
Re-fix the order of install/register/uninstall/unregister.

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/src/main/java/org/jboss/osgi/framework/bundle/OSGiBundleManager.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/src/main/java/org/jboss/osgi/framework/bundle/OSGiBundleManager.java	2009-12-04 17:35:06 UTC (rev 97472)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/src/main/java/org/jboss/osgi/framework/bundle/OSGiBundleManager.java	2009-12-04 18:26:13 UTC (rev 97473)
@@ -1395,6 +1395,7 @@
    OSGiServiceState registerService(AbstractBundleState bundleState, String[] clazzes, Object service, Dictionary properties)
    {
       OSGiServiceState result = new OSGiServiceState(bundleState, clazzes, service, properties);
+      result.internalRegister();
       try
       {
          Controller controller = kernel.getController();
@@ -1405,7 +1406,6 @@
          fireError(bundleState, "installing service to MC in", t);
          throw new RuntimeException(t);
       }
-      result.internalRegister();
 
       FrameworkEventsPlugin plugin = getPlugin(FrameworkEventsPlugin.class);
       plugin.fireServiceEvent(bundleState, ServiceEvent.REGISTERED, result);
@@ -1420,13 +1420,13 @@
     */
    void unregisterService(OSGiServiceState serviceState)
    {
+      Controller controller = kernel.getController();
+      controller.uninstall(serviceState.getName());
+
       serviceState.internalUnregister();
 
       FrameworkEventsPlugin plugin = getPlugin(FrameworkEventsPlugin.class);
       plugin.fireServiceEvent(serviceState.getBundleState(), ServiceEvent.UNREGISTERING, serviceState);
-
-      Controller controller = kernel.getController();
-      controller.uninstall(serviceState.getName());
    }
 
    /**

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java	2009-12-04 17:35:06 UTC (rev 97472)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java	2009-12-04 18:26:13 UTC (rev 97473)
@@ -300,6 +300,14 @@
          return getTarget();
    }
 
+   protected Object ungetTargetForActualUser(Object user)
+   {
+      if (user instanceof AbstractBundleState)
+         return ungetService(AbstractBundleState.class.cast(user));
+      else
+         return getTarget();
+   }
+
    public Object invoke(String name, Object[] parameters, String[] signature) throws Throwable
    {
       return getBeanInfo().invoke(getTarget(), name, signature, parameters);
@@ -402,6 +410,46 @@
    }
 
    /**
+    * Unget from cache.
+    *
+    * @param bundleState the bundle state
+    * @return ungot service
+    */
+   Object ungetService(AbstractBundleState bundleState)
+   {
+      if (isUnregistered())
+         return null;
+
+      Object service = serviceOrFactory;
+      if (isServiceFactory)
+      {
+         if (serviceCache == null)
+            return null;
+
+         ServiceFactory serviceFactory = (ServiceFactory)serviceOrFactory;
+         service = serviceCache.get(bundleState);
+         ContextTracker ct = getContextTracker();
+         int count = ct.getUsedByCount(this, bundleState);
+         if (count == 1) // remove
+            serviceCache.remove(bundleState);
+         if (count > 0) // unget
+         {
+            try
+            {
+               serviceFactory.ungetService(bundleState, getRegistration(), service);
+            }
+            catch (Throwable t)
+            {
+               log.warn("Error from ungetService for " + this, t);
+               FrameworkEventsPlugin plugin = bundleState.getBundleManager().getPlugin(FrameworkEventsPlugin.class);
+               plugin.fireFrameworkEvent(bundleState, FrameworkEvent.WARNING, new BundleException("Error using service factory:" + serviceFactory, t));
+            }
+         }
+      }
+      return service;
+   }
+
+   /**
     * Get the service registration
     * 
     * @return the service registration
@@ -633,25 +681,7 @@
                if (used.contains(using) == false)
                {
                   used.add(using); // add so we don't remove duplicates
-
-                  if (using.ungetContex(this) == false)
-                  {
-                     if (isServiceFactory)
-                     {
-                        ServiceFactory serviceFactory = (ServiceFactory)serviceOrFactory;
-                        try
-                        {
-                           Object service = serviceCache.remove(using);
-                           serviceFactory.ungetService(using.getBundle(), getRegistration(), service);
-                        }
-                        catch (Throwable t)
-                        {
-                           log.warn("Error from ungetService for " + this, t);
-                           FrameworkEventsPlugin plugin = bundleState.getBundleManager().getPlugin(FrameworkEventsPlugin.class);
-                           plugin.fireFrameworkEvent(bundleState, FrameworkEvent.WARNING, new BundleException("Error using service factory:" + serviceFactory, t));
-                        }
-                     }
-                  }
+                  using.ungetContex(this); // ungetService will cleanup service cache
                }
             }
          }

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/src/test/java/org/jboss/test/osgi/service/ServiceMixUnitTestCase.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/src/test/java/org/jboss/test/osgi/service/ServiceMixUnitTestCase.java	2009-12-04 17:35:06 UTC (rev 97472)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/src/test/java/org/jboss/test/osgi/service/ServiceMixUnitTestCase.java	2009-12-04 18:26:13 UTC (rev 97473)
@@ -24,7 +24,7 @@
 import java.lang.reflect.Method;
 import java.util.Dictionary;
 import java.util.Hashtable;
-import java.util.Set;
+import java.util.List;
 
 import junit.framework.Test;
 
@@ -279,8 +279,9 @@
                   reg1.unregister();
                }
 
-               Set as = assertInstanceOf(invoke(d, "getAs", "A"), Set.class);
+               List as = assertInstanceOf(invoke(d, "getAs", "A"), List.class);
                assertNotNull(as);
+               assertEquals(2, as.size());
                assertTrue(as.contains(a1));
                assertTrue(as.contains(a2));
             }

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/src/test/java/org/jboss/test/osgi/service/support/d/D.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/src/test/java/org/jboss/test/osgi/service/support/d/D.java	2009-12-04 17:35:06 UTC (rev 97472)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/src/test/java/org/jboss/test/osgi/service/support/d/D.java	2009-12-04 18:26:13 UTC (rev 97473)
@@ -21,8 +21,8 @@
  */
 package org.jboss.test.osgi.service.support.d;
 
-import java.util.Set;
-import java.util.HashSet;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.jboss.test.osgi.service.support.a.A;
 import org.osgi.framework.Bundle;
@@ -34,7 +34,7 @@
  */
 public class D implements ServiceFactory
 {
-   private Set<Object> as = new HashSet<Object>();
+   private List<Object> as = new ArrayList<Object>();
 
    public Object getService(Bundle bundle, ServiceRegistration registration)
    {
@@ -48,7 +48,7 @@
       as.add(service);
    }
 
-   public Set<Object> getAs()
+   public List<Object> getAs()
    {
       return as;
    }



More information about the jboss-osgi-commits mailing list