diff --git a/scr/src/main/java/org/apache/felix/scr/impl/helper/ComponentServiceObjectsHelper.java b/scr/src/main/java/org/apache/felix/scr/impl/helper/ComponentServiceObjectsHelper.java index 82377d2d3fe..6d1418bff74 100644 --- a/scr/src/main/java/org/apache/felix/scr/impl/helper/ComponentServiceObjectsHelper.java +++ b/scr/src/main/java/org/apache/felix/scr/impl/helper/ComponentServiceObjectsHelper.java @@ -41,8 +41,6 @@ public class ComponentServiceObjectsHelper private final List closedServices = new ArrayList(); - private final ConcurrentMap prototypeInstances = new ConcurrentHashMap(); - public ComponentServiceObjectsHelper(final BundleContext bundleContext) { this.bundleContext = bundleContext; @@ -65,7 +63,6 @@ public void cleanup() { cso.deactivate(); } - prototypeInstances.clear(); } public ComponentServiceObjects getServiceObjects(final ServiceReference ref) @@ -97,24 +94,11 @@ public void closeServiceObjects(final ServiceReference ref) { } cso.close(); } - prototypeInstances.remove(ref); } - public T getPrototypeRefInstance(final ServiceReference ref, ServiceObjects serviceObjects) + public T getPrototypeRefInstance(final ServiceReference ref) { - 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; + return (T) getServiceObjects(ref).getService(); } private static final class ComponentServiceObjectsImpl implements ComponentServiceObjects diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java index f28cee0e322..8f496ef7da4 100644 --- a/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java +++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java @@ -1681,7 +1681,9 @@ void close(ComponentContextImpl componentContext, EdgeInfo edgeInfo) { invokeUnbindMethod(componentContext, boundRef, trackingCount.get(), edgeInfo); } - + + boundRef.unsetServiceObject(componentContext); + } latch.countDown(); } @@ -2320,9 +2322,9 @@ RefPair newRefPair(ServiceReference serviceReference) } if (m_componentManager.getComponentMetadata().getServiceScope() == Scope.singleton) { - return new SinglePrototypeRefPair<>(m_componentManager.getBundleContext(), serviceReference); + return new SinglePrototypeRefPair<>(serviceReference); } - return new MultiplePrototypeRefPair<>(m_componentManager.getBundleContext(), serviceReference); + return new MultiplePrototypeRefPair<>(serviceReference); } private void deactivateComponentManager() diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/MultiplePrototypeRefPair.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/MultiplePrototypeRefPair.java index fa683c153b9..4681de7b531 100644 --- a/scr/src/main/java/org/apache/felix/scr/impl/manager/MultiplePrototypeRefPair.java +++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/MultiplePrototypeRefPair.java @@ -21,11 +21,11 @@ package org.apache.felix.scr.impl.manager; import java.util.Iterator; +import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceObjects; import org.osgi.framework.ServiceReference; import org.osgi.service.log.LogService; @@ -34,19 +34,11 @@ */ public class MultiplePrototypeRefPair extends RefPair { - private final ServiceObjects serviceObjects; private final ConcurrentMap, T> instances = new ConcurrentHashMap<>(); - public MultiplePrototypeRefPair( BundleContext context, ServiceReference ref ) + public MultiplePrototypeRefPair( ServiceReference ref ) { super(ref); - this.serviceObjects = context.getServiceObjects(ref); - } - - @Override - public ServiceObjects getServiceObjects() - { - return serviceObjects; } @Override @@ -58,7 +50,7 @@ public T getServiceObject(ComponentContextImpl key) @Override public boolean setServiceObject(ComponentContextImpl key, T serviceObject) { - return instances.putIfAbsent(key, serviceObject) == null; + return instances.putIfAbsent( key, serviceObject ) == null; } @Override @@ -66,22 +58,20 @@ public T unsetServiceObject(ComponentContextImpl key) { if ( key == null ) { - try + final Iterator, T>> iter = instances.entrySet().iterator(); + while ( iter.hasNext() ) { - final Iterator iter = instances.values().iterator(); - while ( iter.hasNext() ) - { - this.serviceObjects.ungetService(iter.next()); - } - } - catch (final IllegalStateException ise) - { - // ignore - } + Entry, T> e = iter.next(); + doUngetService( e.getKey(), e.getValue() ); + } instances.clear(); return null ; } - return instances.remove(key); + T service = instances.remove( key ); + if(service != null) { + doUngetService( key, service ); + } + return service; } @Override @@ -93,7 +83,7 @@ public String toString() @Override public boolean getServiceObject(ComponentContextImpl key, BundleContext context) { - final T service = key.getComponentServiceObjectsHelper().getPrototypeRefInstance(this.getRef(), serviceObjects); + final T service = key.getComponentServiceObjectsHelper().getPrototypeRefInstance(this.getRef()); if ( service == null ) { setFailed(); @@ -105,8 +95,19 @@ public boolean getServiceObject(ComponentContextImpl key, BundleContext conte if (!setServiceObject(key, service)) { // Another thread got the service before, so unget our - serviceObjects.ungetService( service ); + doUngetService( key, service ); } return true; } + + private void doUngetService(ComponentContextImpl key, final T service) { + try + { + key.getComponentServiceObjectsHelper().getServiceObjects(getRef()).ungetService( service ); + } + catch ( final IllegalStateException ise ) + { + // ignore + } + } } diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/RefPair.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/RefPair.java index 1407940ab50..97c180c4d9e 100644 --- a/scr/src/main/java/org/apache/felix/scr/impl/manager/RefPair.java +++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/RefPair.java @@ -44,11 +44,6 @@ public ServiceReference getRef() return ref; } - public ServiceObjects getServiceObjects() - { - return null; - } - public abstract boolean getServiceObject( ComponentContextImpl key, BundleContext context ); public abstract T getServiceObject(ComponentContextImpl key); diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/SinglePrototypeRefPair.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/SinglePrototypeRefPair.java index bcd73f237e9..0e9754c8975 100644 --- a/scr/src/main/java/org/apache/felix/scr/impl/manager/SinglePrototypeRefPair.java +++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/SinglePrototypeRefPair.java @@ -21,7 +21,6 @@ package org.apache.felix.scr.impl.manager; import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceObjects; import org.osgi.framework.ServiceReference; import org.osgi.service.log.LogService; @@ -30,18 +29,9 @@ */ public class SinglePrototypeRefPair extends SingleRefPair { - private final ServiceObjects serviceObjects; - - public SinglePrototypeRefPair( BundleContext context, ServiceReference ref ) + public SinglePrototypeRefPair( ServiceReference ref ) { super(ref); - this.serviceObjects = context.getServiceObjects(ref); - } - - @Override - public ServiceObjects getServiceObjects() - { - return serviceObjects; } @Override @@ -53,7 +43,7 @@ public String toString() @Override public boolean getServiceObject(ComponentContextImpl key, BundleContext context) { - final T service = key.getComponentServiceObjectsHelper().getPrototypeRefInstance(this.getRef(), serviceObjects); + final T service = key.getComponentServiceObjectsHelper().getPrototypeRefInstance(this.getRef()); if ( service == null ) { setFailed(); @@ -65,9 +55,31 @@ public boolean getServiceObject(ComponentContextImpl key, BundleContext conte if (!setServiceObject(key, service)) { // Another thread got the service before, so unget our - serviceObjects.ungetService( service ); + doUngetService(key, service); } return true; } + @Override + public T unsetServiceObject(ComponentContextImpl key) + { + final T service = super.unsetServiceObject(key); + if ( service != null ) + { + doUngetService(key, service); + } + return null ; + } + + private void doUngetService(ComponentContextImpl key, final T service) { + try + { + key.getComponentServiceObjectsHelper().getServiceObjects(getRef()).ungetService(service); + } + catch (final IllegalStateException ise) + { + // ignore + } + } + }