Class DependencyManager<S,T>
- java.lang.Object
-
- org.apache.felix.scr.impl.manager.DependencyManager<S,T>
-
- All Implemented Interfaces:
ReferenceManager<S,T>
public class DependencyManager<S,T> extends java.lang.Object implements ReferenceManager<S,T>
TheDependencyManager
manages the references to services declared by a single<reference>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
DependencyManager.AbstractCustomizer
private static interface
DependencyManager.Customizer<S,T>
private class
DependencyManager.FactoryCustomizer
private class
DependencyManager.MultipleDynamicCustomizer
private class
DependencyManager.MultipleStaticGreedyCustomizer
private class
DependencyManager.MultipleStaticReluctantCustomizer
private class
DependencyManager.NoPermissionsCustomizer
static class
DependencyManager.OpenStatus<S,T>
private class
DependencyManager.SingleDynamicCustomizer
private class
DependencyManager.SingleStaticCustomizer
-
Field Summary
Fields Modifier and Type Field Description private ReferenceMethods
m_bindMethods
private AbstractComponentManager<S>
m_componentManager
private DependencyManager.Customizer<S,T>
m_customizer
private ReferenceMetadata
m_dependencyMetadata
private int
m_index
private int
m_minCardinality
private java.lang.String
m_target
private ServiceTracker<T,RefPair<S,T>,ExtendedServiceEvent>
m_tracker
private static java.lang.String
OBJECTCLASS_CLAUSE
private static java.lang.String
PROTOTYPE_SCOPE_CLAUSE
-
Constructor Summary
Constructors Constructor Description DependencyManager(AbstractComponentManager<S> componentManager, ReferenceMetadata dependency, int index)
Constructor that receives several parameters.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) boolean
bind(ComponentContextImpl<S> componentContext, DependencyManager.OpenStatus<S,T> status)
(package private) boolean
bindDependency(ComponentContextImpl<S> componentContext, ReferenceMethod bindMethod, DependencyManager.OpenStatus<S,T> status)
(package private) boolean
canUpdateDynamically(java.util.Map<java.lang.String,java.lang.Object> properties)
Returnstrue
if theproperties
can be dynamically applied to the component to which the dependency manager belongs.private boolean
cardinalityJustSatisfied(int serviceCount)
boolean
cardinalitySatisfied(int serviceCount)
(package private) void
close(ComponentContextImpl<S> componentContext, EdgeInfo edgeInfo)
Revoke the given bindings.(package private) void
deactivate()
private void
deactivateComponentManager()
private static int
defaultMinimumCardinality(ReferenceMetadata dependency)
private boolean
doInvokeBindMethod(ComponentContextImpl<S> componentContext, ReferenceMethod bindMethod, RefPair<S,T> refPair, int trackingCount)
private RefPair<S,T>
getBestRefPair()
Returns aServiceReference
instances for a service implementing the interface and complying to the (optional) target filter declared for this dependency.private org.osgi.framework.ServiceReference<T>[]
getFrameworkServiceReferences(java.lang.String targetFilter)
(package private) int
getIndex()
private int
getMinimumCardinality(java.util.Map<java.lang.String,java.lang.Object> properties)
java.lang.String
getName()
Returns the name of the service reference.ReferenceMetadata
getReferenceMetadata()
private RefPair<S,T>
getRefPair(org.osgi.framework.ServiceReference<T> serviceReference)
Returns the RefPair containing the given service reference and the bound service ornull
if this is instance is not currently bound to that service.(package private) T
getService(ComponentContextImpl<S> key)
Returns the service instance for the service reference returned by thegetBestRefPair()
method.private T
getService(ComponentContextImpl<S> key, RefPair<S,T> refPair)
(package private) T
getService(ComponentContextImpl<S> key, org.osgi.framework.ServiceReference<T> serviceReference)
Returns the service described by the ServiceReference.private java.lang.String
getServiceName()
(package private) boolean
getServiceObject(ComponentContextImpl<S> key, ReferenceMethod bindMethod, RefPair<S,T> refPair)
java.util.List<org.osgi.framework.ServiceReference<?>>
getServiceReferences()
Returns an array ofServiceReference
instances of all services this instance is bound to ornull
if no services are actually bound.(package private) T[]
getServices(ComponentContextImpl<S> key)
Returns an array of service instances for the service references returned by the customizer.java.lang.String
getTarget()
Returns the target filter of this dependency as a string ornull
if this dependency has no target filter set.boolean
hasGetPermission()
Returnstrue
if the component providing bundle has permission to get the service described by this reference.(package private) void
initBindingMethods(ReferenceMethods bindMethods)
Initialize binding methods.(package private) boolean
invokeBindMethod(ComponentContextImpl<S> componentContext, RefPair<S,T> refPair, int trackingCount, EdgeInfo info)
Calls the bind method.void
invokeBindMethodLate(org.osgi.framework.ServiceReference<T> ref, int trackingCount)
(package private) boolean
invokeInitMethod(ComponentContextImpl<S> componentContext)
Calls the optional init reference method.(package private) void
invokeUnbindMethod(ComponentContextImpl<S> componentContext, RefPair<S,T> refPair, int trackingCount, EdgeInfo info)
Calls the unbind method.(package private) boolean
invokeUpdatedMethod(ComponentContextImpl<S> componentContext, RefPair<S,T> refPair, int trackingCount, EdgeInfo info)
Calls the updated method.private boolean
isEffectivelyOptional()
private boolean
isMultiple()
(package private) boolean
isOptional()
private boolean
isReluctant()
boolean
isSatisfied()
Returnstrue
if this dependency manager is satisfied, that is if either the dependency is optional or the number of services registered in the framework and available to this dependency manager is not zero.private boolean
isStatic()
private DependencyManager.Customizer<S,T>
newCustomizer()
(package private) RefPair<S,T>
newRefPair(org.osgi.framework.ServiceReference<T> serviceReference)
(package private) DependencyManager.OpenStatus<S,T>
open(ComponentContextImpl<S> componentContext, EdgeInfo edgeInfo)
initializes a dependency.(package private) boolean
prebind(ComponentContextImpl<S> key)
private void
setTargetFilter(java.lang.String target, int minimumCardinality)
Sets the target filter of this dependency to the new filter value.(package private) void
setTargetFilter(java.util.Map<java.lang.String,java.lang.Object> properties)
Sets the target filter from target filter property contained in the properties.(package private) int
size()
Returns the number of services currently registered in the system, which match the service criteria (interface and optional target filter) configured for this dependency.java.lang.String
toString()
(package private) java.util.SortedMap<org.osgi.framework.ServiceReference<T>,RefPair<S,T>>
unregisterServiceListener(java.util.concurrent.atomic.AtomicInteger trackingCount)
-
-
-
Field Detail
-
m_componentManager
private final AbstractComponentManager<S> m_componentManager
-
m_dependencyMetadata
private final ReferenceMetadata m_dependencyMetadata
-
m_index
private final int m_index
-
m_customizer
private volatile DependencyManager.Customizer<S,T> m_customizer
-
m_bindMethods
private volatile ReferenceMethods m_bindMethods
-
m_tracker
private volatile ServiceTracker<T,RefPair<S,T>,ExtendedServiceEvent> m_tracker
-
m_target
private volatile java.lang.String m_target
-
m_minCardinality
private volatile int m_minCardinality
-
OBJECTCLASS_CLAUSE
private static final java.lang.String OBJECTCLASS_CLAUSE
- See Also:
- Constant Field Values
-
PROTOTYPE_SCOPE_CLAUSE
private static final java.lang.String PROTOTYPE_SCOPE_CLAUSE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DependencyManager
DependencyManager(AbstractComponentManager<S> componentManager, ReferenceMetadata dependency, int index)
Constructor that receives several parameters.- Parameters:
dependency
- An object that contains data about the dependencyindex
- index of the dependency manager in the metadata
-
-
Method Detail
-
defaultMinimumCardinality
private static int defaultMinimumCardinality(ReferenceMetadata dependency)
-
getIndex
int getIndex()
-
initBindingMethods
void initBindingMethods(ReferenceMethods bindMethods)
Initialize binding methods.
-
getServiceName
private java.lang.String getServiceName()
-
isOptional
boolean isOptional()
-
isEffectivelyOptional
private boolean isEffectivelyOptional()
-
cardinalitySatisfied
public boolean cardinalitySatisfied(int serviceCount)
-
cardinalityJustSatisfied
private boolean cardinalityJustSatisfied(int serviceCount)
-
isMultiple
private boolean isMultiple()
-
isStatic
private boolean isStatic()
-
isReluctant
private boolean isReluctant()
-
deactivate
void deactivate()
-
size
int size()
Returns the number of services currently registered in the system, which match the service criteria (interface and optional target filter) configured for this dependency. The number returned by this method has no correlation to the number of services bound to this dependency manager. It is actually the maximum number of services which may be bound to this dependency manager.- See Also:
isSatisfied()
-
getFrameworkServiceReferences
private org.osgi.framework.ServiceReference<T>[] getFrameworkServiceReferences(java.lang.String targetFilter)
-
getBestRefPair
private RefPair<S,T> getBestRefPair()
Returns aServiceReference
instances for a service implementing the interface and complying to the (optional) target filter declared for this dependency. If no matching service can be foundnull
is returned. If the configured target filter is syntactically incorrect an error message is logged with the LogService andnull
is returned. If multiple matching services are registered the service with the highest service.ranking value is returned. If multiple matching services have the same service.ranking value, the service with the lowest service.id is returned.
-
getService
T getService(ComponentContextImpl<S> key)
Returns the service instance for the service reference returned by thegetBestRefPair()
method. If this returns a non-null
service instance the service is then considered bound to this instance.- Parameters:
key
- TODO
-
getServices
T[] getServices(ComponentContextImpl<S> key)
Returns an array of service instances for the service references returned by the customizer. If no services match the criteria configured for this dependencynull
is returned. All services returned by this method will be considered bound after this method returns.- Parameters:
key
- TODO
-
getServiceReferences
public java.util.List<org.osgi.framework.ServiceReference<?>> getServiceReferences()
Description copied from interface:ReferenceManager
Returns an array ofServiceReference
instances of all services this instance is bound to ornull
if no services are actually bound.- Specified by:
getServiceReferences
in interfaceReferenceManager<S,T>
-
getRefPair
private RefPair<S,T> getRefPair(org.osgi.framework.ServiceReference<T> serviceReference)
Returns the RefPair containing the given service reference and the bound service ornull
if this is instance is not currently bound to that service.- Parameters:
serviceReference
- The reference to the bound service- Returns:
- RefPair the reference and service for the reference
if the service is bound or
null
if the service is not bound.
-
getService
T getService(ComponentContextImpl<S> key, org.osgi.framework.ServiceReference<T> serviceReference)
Returns the service described by the ServiceReference. If this instance is already bound the given service, that bound service instance is returned. Otherwise the service retrieved from the service registry and kept as a bound service for future use.- Parameters:
key
- TODOserviceReference
- The reference to the service to be returned- Returns:
- The requested service or
null
if no service is registered for the service reference (any more).
-
getName
public java.lang.String getName()
Description copied from interface:ReferenceManager
Returns the name of the service reference.- Specified by:
getName
in interfaceReferenceManager<S,T>
-
getReferenceMetadata
public ReferenceMetadata getReferenceMetadata()
-
isSatisfied
public boolean isSatisfied()
Returnstrue
if this dependency manager is satisfied, that is if either the dependency is optional or the number of services registered in the framework and available to this dependency manager is not zero.- Specified by:
isSatisfied
in interfaceReferenceManager<S,T>
-
hasGetPermission
public boolean hasGetPermission()
Returnstrue
if the component providing bundle has permission to get the service described by this reference.
-
prebind
boolean prebind(ComponentContextImpl<S> key)
-
open
DependencyManager.OpenStatus<S,T> open(ComponentContextImpl<S> componentContext, EdgeInfo edgeInfo)
initializes a dependency. This method binds all of the service occurrences to the instance object- Parameters:
edgeInfo
- Edge info for the combination of this component instance and this dependency manager.- Returns:
- true if the dependency is satisfied and at least the minimum number of services could be bound. Otherwise false is returned.
-
bind
boolean bind(ComponentContextImpl<S> componentContext, DependencyManager.OpenStatus<S,T> status)
-
bindDependency
boolean bindDependency(ComponentContextImpl<S> componentContext, ReferenceMethod bindMethod, DependencyManager.OpenStatus<S,T> status)
-
close
void close(ComponentContextImpl<S> componentContext, EdgeInfo edgeInfo)
Revoke the given bindings. This method cannot throw an exception since it must try to complete all that it can- Parameters:
componentContext
- instance we are unbinding from.edgeInfo
- EdgeInfo for the combination of this component instance and this dependency manager.
-
invokeBindMethodLate
public void invokeBindMethodLate(org.osgi.framework.ServiceReference<T> ref, int trackingCount)
-
invokeInitMethod
boolean invokeInitMethod(ComponentContextImpl<S> componentContext)
Calls the optional init reference method.
-
invokeBindMethod
boolean invokeBindMethod(ComponentContextImpl<S> componentContext, RefPair<S,T> refPair, int trackingCount, EdgeInfo info)
Calls the bind method. In case there is an exception while calling the bind method, the service is not considered to be bound to the instance objectIf the reference is singular and a service has already been bound to the component this method has no effect and just returns
true
.- Parameters:
componentContext
- instance we are binding torefPair
- the service reference, service object tuple.trackingCount
- service event counter for this service.edgeInfo
- EdgeInfo for the combination of this instance and this dependency manager.- Returns:
- true if the service should be considered bound. If no bind
method is found or the method call fails,
true
is returned.false
is only returned if the service must be handed over to the bind method but the service cannot be retrieved using the service reference.
-
doInvokeBindMethod
private boolean doInvokeBindMethod(ComponentContextImpl<S> componentContext, ReferenceMethod bindMethod, RefPair<S,T> refPair, int trackingCount)
-
invokeUpdatedMethod
boolean invokeUpdatedMethod(ComponentContextImpl<S> componentContext, RefPair<S,T> refPair, int trackingCount, EdgeInfo info)
Calls the updated method.- Parameters:
componentContext
- instance we are calling updated on.refPair
- A service reference corresponding to the service whose serviceedgeInfo
- EdgeInfo for the combination of this instance and this dependency manager.- Returns:
true
if reactivation is required.
-
invokeUnbindMethod
void invokeUnbindMethod(ComponentContextImpl<S> componentContext, RefPair<S,T> refPair, int trackingCount, EdgeInfo info)
Calls the unbind method.If the reference is singular and the given service is not the one bound to the component this method has no effect and just returns
true
.- Parameters:
componentContext
- instance we are unbinding fromrefPair
- A service reference, service pair that will be unboundtrackingCount
- service event count for this referenceinfo
- EdgeInfo for the combination of this instance and this dependency manager
-
canUpdateDynamically
boolean canUpdateDynamically(java.util.Map<java.lang.String,java.lang.Object> properties)
Returnstrue
if theproperties
can be dynamically applied to the component to which the dependency manager belongs.This method applies the following heuristics (in the given order):
- If there is no change in the target filter for this dependency, the properties can be applied
- If the dependency is static and there are changes in the target filter we cannot dynamically apply the configuration because the filter may (assume they do for simplicity here) cause the bindings to change.
- If there is still at least one service matching the new target filter we can apply the configuration because the depdency is dynamic.
- If there are no more services matching the filter, we can still apply the configuration if the dependency is optional.
- Ultimately, if all other checks do not apply we cannot dynamically apply.
-
setTargetFilter
void setTargetFilter(java.util.Map<java.lang.String,java.lang.Object> properties)
Sets the target filter from target filter property contained in the properties. The filter is taken from a property whose name is derived from the dependency name and the suffix.target
as defined for target properties on page 302 of the Declarative Services Specification, section 112.6.- Parameters:
properties
- The properties containing the optional target service filter property
-
getMinimumCardinality
private int getMinimumCardinality(java.util.Map<java.lang.String,java.lang.Object> properties)
-
setTargetFilter
private void setTargetFilter(java.lang.String target, int minimumCardinality)
Sets the target filter of this dependency to the new filter value. If the new target filter is the same as the old target filter, this method has not effect. Otherwise any services currently bound but not matching the new filter are unbound. Likewise any registered services not currently bound but matching the new filter are bound.- Parameters:
target
- The new target filter to be set. This may benull
if no target filtering is to be used.
-
newCustomizer
private DependencyManager.Customizer<S,T> newCustomizer()
-
unregisterServiceListener
java.util.SortedMap<org.osgi.framework.ServiceReference<T>,RefPair<S,T>> unregisterServiceListener(java.util.concurrent.atomic.AtomicInteger trackingCount)
-
getTarget
public java.lang.String getTarget()
Description copied from interface:ReferenceManager
Returns the target filter of this dependency as a string ornull
if this dependency has no target filter set.- Specified by:
getTarget
in interfaceReferenceManager<S,T>
- Returns:
- The target filter of this dependency or
null
if none is set.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
getServiceObject
boolean getServiceObject(ComponentContextImpl<S> key, ReferenceMethod bindMethod, RefPair<S,T> refPair)
-
deactivateComponentManager
private void deactivateComponentManager()
-
-