You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2015/02/23 02:05:11 UTC

svn commit: r1661592 - in /felix/trunk/framework/src/main/java/org/apache/felix/framework: BundleContextImpl.java Felix.java ServiceRegistry.java

Author: cziegeler
Date: Mon Feb 23 01:05:10 2015
New Revision: 1661592

URL: http://svn.apache.org/r1661592
Log:
FELIX-4806 : Ungetting service through ServiceObjects might throw IllegalArgumentException

Modified:
    felix/trunk/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java
    felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java
    felix/trunk/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java?rev=1661592&r1=1661591&r2=1661592&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java Mon Feb 23 01:05:10 2015
@@ -20,12 +20,10 @@ package org.apache.felix.framework;
 
 import java.io.File;
 import java.io.InputStream;
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Dictionary;
-import java.util.List;
 
 import org.apache.felix.framework.ext.FelixBundleContext;
 import org.osgi.framework.AdminPermission;
@@ -553,88 +551,37 @@ class BundleContextImpl implements Felix
     {
         private final ServiceReference<S> m_ref;
 
-        private final List<S> srvObjects = new ArrayList<S>();
-
         public ServiceObjectsImpl(final ServiceReference<S> ref)
         {
             this.m_ref = ref;
         }
 
         public S getService() {
-        	S srvObj = null;
-            // special handling for prototype scope
-            if ( m_ref.getProperty(Constants.SERVICE_SCOPE) == Constants.SCOPE_PROTOTYPE )
-            {
-                checkValidity();
-
-                // CONCURRENCY NOTE: This is a check-then-act situation,
-                // but we ignore it since the time window is small and
-                // the result is the same as if the calling thread had
-                // won the race condition.
-
-                Object sm = System.getSecurityManager();
+            checkValidity();
 
-                if (sm != null)
-                {
-                   ((SecurityManager) sm).checkPermission(new ServicePermission(m_ref, ServicePermission.GET));
-                }
+            // CONCURRENCY NOTE: This is a check-then-act situation,
+            // but we ignore it since the time window is small and
+            // the result is the same as if the calling thread had
+            // won the race condition.
 
-                srvObj = m_felix.getService(m_bundle, m_ref, true);
-            }
-            else
-            {
-            	// getService handles singleton and bundle scope
-            	srvObj = BundleContextImpl.this.getService(m_ref);
-            }
+            final Object sm = System.getSecurityManager();
 
-            if ( srvObj != null )
+            if (sm != null)
             {
-            	synchronized ( srvObjects )
-            	{
-            		srvObjects.add(srvObj);
-            	}
+               ((SecurityManager) sm).checkPermission(new ServicePermission(m_ref, ServicePermission.GET));
             }
 
-            return srvObj;
+            return m_felix.getService(m_bundle, m_ref, true);
         }
 
         public void ungetService(final S srvObj)
         {
-        	if ( srvObj != null )
-        	{
-                // check if this object was returned by this service objects
-                synchronized ( srvObjects )
-                {
-	                boolean found = false;
-	                int i = 0;
-	                while ( !found && i < srvObjects.size() )
-	                {
-	                	found = srvObjects.get(i) == srvObj;
-	                	i++;
-	                }
-	                if ( !found )
-	                {
-	                	throw new IllegalArgumentException();
-	                }
-	                srvObjects.remove(i-1);
-                }
-
-        	}
-            // special handling for prototype scope
-            if ( m_ref.getProperty(Constants.SERVICE_SCOPE) == Constants.SCOPE_PROTOTYPE )
-            {
-                checkValidity();
+            checkValidity();
 
-                // Unget the specified service.
-                if ( !m_felix.ungetService(m_bundle, m_ref, srvObj) )
-                {
-                	throw new IllegalArgumentException();
-                }
-            }
-            else
+            // Unget the specified service.
+            if ( !m_felix.ungetService(m_bundle, m_ref, srvObj) )
             {
-                // ungetService handles singleton and bundle scope
-                BundleContextImpl.this.ungetService(m_ref);
+            	throw new IllegalArgumentException();
             }
         }
 

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java?rev=1661592&r1=1661591&r2=1661592&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java Mon Feb 23 01:05:10 2015
@@ -3664,11 +3664,11 @@ public class Felix extends BundleImpl im
 
     }
 
-    <S> S getService(Bundle bundle, ServiceReference<S> ref, boolean isPrototype)
+    <S> S getService(Bundle bundle, ServiceReference<S> ref, boolean isServiceObjetcs)
     {
         try
         {
-            return m_registry.getService(bundle, ref, isPrototype);
+            return m_registry.getService(bundle, ref, isServiceObjetcs);
         }
         catch (ServiceException ex)
         {

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java?rev=1661592&r1=1661591&r2=1661592&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java Mon Feb 23 01:05:10 2015
@@ -281,13 +281,15 @@ public class ServiceRegistry
     }
 
     @SuppressWarnings("unchecked")
-    public <S> S getService(Bundle bundle, ServiceReference<S> ref, boolean isPrototype)
+    public <S> S getService(final Bundle bundle, final ServiceReference<S> ref, final boolean isServiceObjects)
     {
+    	// prototype scope is only possible if called from ServiceObjects
+    	final boolean isPrototype = isServiceObjects && ref.getProperty(Constants.SERVICE_SCOPE) == Constants.SCOPE_PROTOTYPE;
         UsageCount usage = null;
         Object svcObj = null;
 
         // Get the service registration.
-        ServiceRegistrationImpl reg =
+        final ServiceRegistrationImpl reg =
             ((ServiceRegistrationImpl.ServiceReferenceImpl) ref).getRegistration();
 
         synchronized (this)
@@ -312,6 +314,7 @@ public class ServiceRegistry
                 }
                 catch (InterruptedException ex)
                 {
+                	Thread.currentThread().interrupt();
                 }
             }
 
@@ -337,6 +340,10 @@ public class ServiceRegistry
                 // service object, if one exists.
                 usage.m_count++;
                 svcObj = usage.m_svcObj;
+                if ( isServiceObjects )
+                {
+                	usage.m_serviceObjectsCount++;
+                }
             }
         }
 
@@ -380,7 +387,10 @@ public class ServiceRegistry
 
     public boolean ungetService(Bundle bundle, ServiceReference<?> ref, Object svcObj)
     {
-        UsageCount usage = null;
+    	// prototype scope is only possible if called from ServiceObjects
+    	final boolean isPrototype = svcObj != null && ref.getProperty(Constants.SERVICE_SCOPE) == Constants.SCOPE_PROTOTYPE;
+
+    	UsageCount usage = null;
         ServiceRegistrationImpl reg =
             ((ServiceRegistrationImpl.ServiceReferenceImpl) ref).getRegistration();
 
@@ -414,7 +424,19 @@ public class ServiceRegistry
             {
                 return false;
             }
-
+            // if this is a call from service objects and the service was not fetched from
+            // there, return false
+            if ( svcObj != null )
+            {
+            	if ( usage.m_serviceObjectsCount > 0 )
+            	{
+            		usage.m_serviceObjectsCount--;
+            	}
+            	else
+            	{
+            		return false;
+            	}
+            }
             // Lock the service registration.
             m_lockedRegsMap.put(reg, Thread.currentThread());
         }
@@ -601,8 +623,7 @@ public class ServiceRegistry
         for (int i = 0; (usages != null) && (i < usages.length); i++)
         {
             if (usages[i].m_ref.equals(ref) 
-               && ((svcObj == null && !usages[i].m_prototype) 
-            		|| (usages[i].m_svcObj == svcObj && usages[i].m_prototype)))
+               && ((svcObj == null && !usages[i].m_prototype) || usages[i].m_svcObj == svcObj))
             {
                 return usages[i];
             }
@@ -838,6 +859,7 @@ public class ServiceRegistry
         public ServiceReference<?> m_ref;
         public Object m_svcObj;
         public boolean m_prototype;
+        public int m_serviceObjectsCount;
     }
 
     public interface ServiceRegistryCallbacks