felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1689302 - in /felix/trunk/scr/src/main/java/org/apache/felix/scr/impl: helper/ComponentServiceObjectsHelper.java manager/ComponentContextImpl.java manager/MultiplePrototypeRefPair.java manager/RefPair.java manager/SinglePrototypeRefPair.java
Date Mon, 06 Jul 2015 01:31:40 GMT
Author: cziegeler
Date: Mon Jul  6 01:31:39 2015
New Revision: 1689302

URL: http://svn.apache.org/r1689302
Log:
FELIX-4950 : [DS][RFC-190] Within a component instance for each reference to the same service
the same object needs to be injected

Modified:
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/ComponentServiceObjectsHelper.java
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentContextImpl.java
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/MultiplePrototypeRefPair.java
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/RefPair.java
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/SinglePrototypeRefPair.java

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/ComponentServiceObjectsHelper.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/ComponentServiceObjectsHelper.java?rev=1689302&r1=1689301&r2=1689302&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/ComponentServiceObjectsHelper.java
(original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/ComponentServiceObjectsHelper.java
Mon Jul  6 01:31:39 2015
@@ -23,6 +23,8 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceObjects;
@@ -31,7 +33,8 @@ import org.osgi.service.component.Compon
 
 
 /**
- * Utility methods for class handling used by method and field references.
+ * Utility class for handling references using a ComponentServiceObjects
+ * to get services.
  */
 public class ComponentServiceObjectsHelper
 {
@@ -41,6 +44,8 @@ public class ComponentServiceObjectsHelp
 
     private final Map<ServiceObjects, List<Object>> services = new HashMap<ServiceObjects,
List<Object>>();
 
+    private final ConcurrentMap<ServiceReference, Object> prototypeInstances = new
ConcurrentHashMap<ServiceReference, Object>();
+
     public ComponentServiceObjectsHelper(final BundleContext bundleContext)
     {
         this.bundleContext = bundleContext;
@@ -60,6 +65,7 @@ public class ComponentServiceObjectsHelp
             services.clear();
             serviceObjectsMap.clear();
         }
+        prototypeInstances.clear();
     }
 
     public ComponentServiceObjects getServiceObjects(final ServiceReference<?> ref)
@@ -87,9 +93,11 @@ public class ComponentServiceObjectsHelp
                 final ServiceObjects serviceObjects = so;
                 final List<Object> serviceList = services;
 
-                return new ComponentServiceObjects() {
+                return new ComponentServiceObjects() 
+                {
 
-                    public Object getService() {
+                    public Object getService() 
+                    {
                         final Object service = serviceObjects.getService();
                         if ( service != null )
                         {
@@ -101,7 +109,8 @@ public class ComponentServiceObjectsHelp
                         return service;
                     }
 
-                    public void ungetService(final Object service) {
+                    public void ungetService(final Object service) 
+                    {
                         boolean remove;
                         synchronized ( serviceList )
                         {
@@ -112,7 +121,8 @@ public class ComponentServiceObjectsHelp
                         }
                     }
 
-                    public ServiceReference<?> getServiceReference() {
+                    public ServiceReference<?> getServiceReference() 
+                    {
                         return ref;
                     }
                 };
@@ -120,4 +130,23 @@ public class ComponentServiceObjectsHelp
         }
         return null;
     }
-}
+
+    
+    public <T> T getPrototypeRefInstance(final ServiceReference<T> ref, ServiceObjects<T>
serviceObjects) 
+    {
+    	T service = (T) prototypeInstances.get(ref);
+    	if ( service == null )
+    	{
+    		service = serviceObjects.getService();
+    		T oldService = (T)prototypeInstances.putIfAbsent(ref, service);
+    		if ( oldService != null )
+    		{
+    			// another thread created the instance already
+    			serviceObjects.ungetService(service);
+    			service = oldService;
+    		}
+    	}
+    	return service;
+    }
+   
+ }

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentContextImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentContextImpl.java?rev=1689302&r1=1689301&r2=1689302&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentContextImpl.java
(original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentContextImpl.java
Mon Jul  6 01:31:39 2015
@@ -20,6 +20,8 @@ package org.apache.felix.scr.impl.manage
 
 
 import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
@@ -29,6 +31,7 @@ import org.apache.felix.scr.impl.helper.
 import org.apache.felix.scr.impl.helper.ReadOnlyDictionary;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceObjects;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.component.ComponentInstance;
 import org.osgi.service.log.LogService;
@@ -44,11 +47,11 @@ public class ComponentContextImpl<S> imp
 
     private final EdgeInfo[] edgeInfos;
 
-    private final ComponentInstance m_componentInstance = new ComponentInstanceImpl(this);
+    private final ComponentInstance m_componentInstance = new ComponentInstanceImpl<S>(this);
 
     private final Bundle m_usingBundle;
 
-    private S m_implementationObject;
+    private volatile S m_implementationObject;
 
     private volatile boolean m_implementationAccessible;
 
@@ -56,7 +59,7 @@ public class ComponentContextImpl<S> imp
 
     private final ComponentServiceObjectsHelper serviceObjectsHelper;
 
-    public ComponentContextImpl( SingleComponentManager<S> componentManager, Bundle
usingBundle )
+    public ComponentContextImpl( final SingleComponentManager<S> componentManager,
final Bundle usingBundle )
     {
         m_componentManager = componentManager;
         m_usingBundle = usingBundle;
@@ -73,7 +76,7 @@ public class ComponentContextImpl<S> imp
         this.serviceObjectsHelper.cleanup();
     }
 
-    public Object getComponentServiceObjectsHelper()
+    public ComponentServiceObjectsHelper getComponentServiceObjectsHelper()
     {
         return this.serviceObjectsHelper;
     }
@@ -99,7 +102,7 @@ public class ComponentContextImpl<S> imp
         return edgeInfos[index];
     }
 
-    protected SingleComponentManager<S> getComponentManager()
+   protected SingleComponentManager<S> getComponentManager()
     {
         return m_componentManager;
     }

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/MultiplePrototypeRefPair.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/MultiplePrototypeRefPair.java?rev=1689302&r1=1689301&r2=1689302&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/MultiplePrototypeRefPair.java
(original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/MultiplePrototypeRefPair.java
Mon Jul  6 01:31:39 2015
@@ -44,13 +44,11 @@ public class MultiplePrototypeRefPair<S,
     }
 
     @Override
-    public Object getServiceObjects()
+    public ServiceObjects<T> getServiceObjects()
     {
         return serviceObjects;
     }
 
-
-
     @Override
     public T getServiceObject(ComponentContextImpl<S> key)
     {
@@ -79,7 +77,7 @@ public class MultiplePrototypeRefPair<S,
     public boolean getServiceObject(ComponentContextImpl<S> key, BundleContext context,
         SimpleLogger logger)
     {
-        T service = serviceObjects.getService();
+    	final T service = key.getComponentServiceObjectsHelper().getPrototypeRefInstance(this.getRef(),
serviceObjects);
         if ( service == null )
         {
             setFailed();
@@ -95,6 +93,4 @@ public class MultiplePrototypeRefPair<S,
         }
         return true;
     }
-
-
 }

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/RefPair.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/RefPair.java?rev=1689302&r1=1689301&r2=1689302&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/RefPair.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/RefPair.java Mon Jul 
6 01:31:39 2015
@@ -22,10 +22,11 @@ package org.apache.felix.scr.impl.manage
 
 import org.apache.felix.scr.impl.helper.SimpleLogger;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceObjects;
 import org.osgi.framework.ServiceReference;
 
 /**
- * @version $Rev:$ $Date:$
+ * @version $Rev$ $Date$
  */
 public abstract class RefPair<S, T>
 {
@@ -44,7 +45,7 @@ public abstract class RefPair<S, T>
         return ref;
     }
 
-    public Object getServiceObjects()
+    public ServiceObjects<T> getServiceObjects()
     {
         return null;
     }

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/SinglePrototypeRefPair.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/SinglePrototypeRefPair.java?rev=1689302&r1=1689301&r2=1689302&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/SinglePrototypeRefPair.java
(original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/SinglePrototypeRefPair.java
Mon Jul  6 01:31:39 2015
@@ -40,7 +40,7 @@ public class SinglePrototypeRefPair<S, T
     }
 
     @Override
-    public Object getServiceObjects()
+    public ServiceObjects<T> getServiceObjects()
     {
         return serviceObjects;
     }
@@ -55,7 +55,7 @@ public class SinglePrototypeRefPair<S, T
     public boolean getServiceObject(ComponentContextImpl<S> key, BundleContext context,
         SimpleLogger logger)
     {
-        T service = serviceObjects.getService();
+    	final T service = key.getComponentServiceObjectsHelper().getPrototypeRefInstance(this.getRef(),
serviceObjects);
         if ( service == null )
         {
             setFailed();



Mime
View raw message