Skip to content

Commit

Permalink
FELIX-4950 : [DS][RFC-190] Within a component instance for each refer…
Browse files Browse the repository at this point in the history
…ence to the same service the same object needs to be injected

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1689302 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
cziegeler committed Jul 6, 2015
1 parent 12bd5f3 commit 926414d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
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;
Expand All @@ -31,7 +33,8 @@


/**
* 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
{
Expand All @@ -41,6 +44,8 @@ public class ComponentServiceObjectsHelper

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;
Expand All @@ -60,6 +65,7 @@ public void cleanup()
services.clear();
serviceObjectsMap.clear();
}
prototypeInstances.clear();
}

public ComponentServiceObjects getServiceObjects(final ServiceReference<?> ref)
Expand Down Expand Up @@ -87,9 +93,11 @@ public ComponentServiceObjects getServiceObjects(final ServiceReference<?> ref)
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 )
{
Expand All @@ -101,7 +109,8 @@ public Object getService() {
return service;
}

public void ungetService(final Object service) {
public void ungetService(final Object service)
{
boolean remove;
synchronized ( serviceList )
{
Expand All @@ -112,12 +121,32 @@ public void ungetService(final Object service) {
}
}

public ServiceReference<?> getServiceReference() {
public ServiceReference<?> getServiceReference()
{
return ref;
}
};
}
}
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@


import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand All @@ -29,6 +31,7 @@
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;
Expand All @@ -44,19 +47,19 @@ public class ComponentContextImpl<S> implements ExtComponentContext {

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;

private final CountDownLatch accessibleLatch = new CountDownLatch(1);

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;
Expand All @@ -73,7 +76,7 @@ public void cleanup()
this.serviceObjectsHelper.cleanup();
}

public Object getComponentServiceObjectsHelper()
public ComponentServiceObjectsHelper getComponentServiceObjectsHelper()
{
return this.serviceObjectsHelper;
}
Expand All @@ -99,7 +102,7 @@ EdgeInfo getEdgeInfo(DependencyManager<S, ?> dm)
return edgeInfos[index];
}

protected SingleComponentManager<S> getComponentManager()
protected SingleComponentManager<S> getComponentManager()
{
return m_componentManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ public MultiplePrototypeRefPair( BundleContext context, ServiceReference<T> ref
}

@Override
public Object getServiceObjects()
public ServiceObjects<T> getServiceObjects()
{
return serviceObjects;
}



@Override
public T getServiceObject(ComponentContextImpl<S> key)
{
Expand Down Expand Up @@ -79,7 +77,7 @@ public String toString()
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();
Expand All @@ -95,6 +93,4 @@ public boolean getServiceObject(ComponentContextImpl<S> key, BundleContext conte
}
return true;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@

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>
{
Expand All @@ -44,7 +45,7 @@ public ServiceReference<T> getRef()
return ref;
}

public Object getServiceObjects()
public ServiceObjects<T> getServiceObjects()
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public SinglePrototypeRefPair( BundleContext context, ServiceReference<T> ref )
}

@Override
public Object getServiceObjects()
public ServiceObjects<T> getServiceObjects()
{
return serviceObjects;
}
Expand All @@ -55,7 +55,7 @@ public String toString()
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();
Expand Down

0 comments on commit 926414d

Please sign in to comment.