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>
TheDependencyManagermanages the references to services declared by a single<reference>-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classDependencyManager.AbstractCustomizerprivate static interfaceDependencyManager.Customizer<S,T>private classDependencyManager.FactoryCustomizerprivate classDependencyManager.MultipleDynamicCustomizerprivate classDependencyManager.MultipleStaticGreedyCustomizerprivate classDependencyManager.MultipleStaticReluctantCustomizerprivate classDependencyManager.NoPermissionsCustomizerstatic classDependencyManager.OpenStatus<S,T>private classDependencyManager.SingleDynamicCustomizerprivate classDependencyManager.SingleStaticCustomizer
-
Field Summary
Fields Modifier and Type Field Description private ReferenceMethodsm_bindMethodsprivate AbstractComponentManager<S>m_componentManagerprivate DependencyManager.Customizer<S,T>m_customizerprivate ReferenceMetadatam_dependencyMetadataprivate intm_indexprivate intm_minCardinalityprivate java.lang.Stringm_targetprivate ServiceTracker<T,RefPair<S,T>,ExtendedServiceEvent>m_trackerprivate static java.lang.StringOBJECTCLASS_CLAUSEprivate static java.lang.StringPROTOTYPE_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) booleanbind(ComponentContextImpl<S> componentContext, DependencyManager.OpenStatus<S,T> status)(package private) booleanbindDependency(ComponentContextImpl<S> componentContext, ReferenceMethod bindMethod, DependencyManager.OpenStatus<S,T> status)(package private) booleancanUpdateDynamically(java.util.Map<java.lang.String,java.lang.Object> properties)Returnstrueif thepropertiescan be dynamically applied to the component to which the dependency manager belongs.private booleancardinalityJustSatisfied(int serviceCount)booleancardinalitySatisfied(int serviceCount)(package private) voidclose(ComponentContextImpl<S> componentContext, EdgeInfo edgeInfo)Revoke the given bindings.(package private) voiddeactivate()private voiddeactivateComponentManager()private static intdefaultMinimumCardinality(ReferenceMetadata dependency)private booleandoInvokeBindMethod(ComponentContextImpl<S> componentContext, ReferenceMethod bindMethod, RefPair<S,T> refPair, int trackingCount)private RefPair<S,T>getBestRefPair()Returns aServiceReferenceinstances 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) intgetIndex()private intgetMinimumCardinality(java.util.Map<java.lang.String,java.lang.Object> properties)java.lang.StringgetName()Returns the name of the service reference.ReferenceMetadatagetReferenceMetadata()private RefPair<S,T>getRefPair(org.osgi.framework.ServiceReference<T> serviceReference)Returns the RefPair containing the given service reference and the bound service ornullif this is instance is not currently bound to that service.(package private) TgetService(ComponentContextImpl<S> key)Returns the service instance for the service reference returned by thegetBestRefPair()method.private TgetService(ComponentContextImpl<S> key, RefPair<S,T> refPair)(package private) TgetService(ComponentContextImpl<S> key, org.osgi.framework.ServiceReference<T> serviceReference)Returns the service described by the ServiceReference.private java.lang.StringgetServiceName()(package private) booleangetServiceObject(ComponentContextImpl<S> key, ReferenceMethod bindMethod, RefPair<S,T> refPair)java.util.List<org.osgi.framework.ServiceReference<?>>getServiceReferences()Returns an array ofServiceReferenceinstances of all services this instance is bound to ornullif 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.StringgetTarget()Returns the target filter of this dependency as a string ornullif this dependency has no target filter set.booleanhasGetPermission()Returnstrueif the component providing bundle has permission to get the service described by this reference.(package private) voidinitBindingMethods(ReferenceMethods bindMethods)Initialize binding methods.(package private) booleaninvokeBindMethod(ComponentContextImpl<S> componentContext, RefPair<S,T> refPair, int trackingCount, EdgeInfo info)Calls the bind method.voidinvokeBindMethodLate(org.osgi.framework.ServiceReference<T> ref, int trackingCount)(package private) booleaninvokeInitMethod(ComponentContextImpl<S> componentContext)Calls the optional init reference method.(package private) voidinvokeUnbindMethod(ComponentContextImpl<S> componentContext, RefPair<S,T> refPair, int trackingCount, EdgeInfo info)Calls the unbind method.(package private) booleaninvokeUpdatedMethod(ComponentContextImpl<S> componentContext, RefPair<S,T> refPair, int trackingCount, EdgeInfo info)Calls the updated method.private booleanisEffectivelyOptional()private booleanisMultiple()(package private) booleanisOptional()private booleanisReluctant()booleanisSatisfied()Returnstrueif 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 booleanisStatic()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) booleanprebind(ComponentContextImpl<S> key)private voidsetTargetFilter(java.lang.String target, int minimumCardinality)Sets the target filter of this dependency to the new filter value.(package private) voidsetTargetFilter(java.util.Map<java.lang.String,java.lang.Object> properties)Sets the target filter from target filter property contained in the properties.(package private) intsize()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.StringtoString()(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 aServiceReferenceinstances for a service implementing the interface and complying to the (optional) target filter declared for this dependency. If no matching service can be foundnullis returned. If the configured target filter is syntactically incorrect an error message is logged with the LogService andnullis 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-nullservice 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 dependencynullis 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:ReferenceManagerReturns an array ofServiceReferenceinstances of all services this instance is bound to ornullif no services are actually bound.- Specified by:
getServiceReferencesin 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 ornullif 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
nullif 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
nullif no service is registered for the service reference (any more).
-
getName
public java.lang.String getName()
Description copied from interface:ReferenceManagerReturns the name of the service reference.- Specified by:
getNamein interfaceReferenceManager<S,T>
-
getReferenceMetadata
public ReferenceMetadata getReferenceMetadata()
-
isSatisfied
public boolean isSatisfied()
Returnstrueif 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:
isSatisfiedin interfaceReferenceManager<S,T>
-
hasGetPermission
public boolean hasGetPermission()
Returnstrueif 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,
trueis returned.falseis 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:
trueif 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)
Returnstrueif thepropertiescan 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.targetas 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 benullif 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:ReferenceManagerReturns the target filter of this dependency as a string ornullif this dependency has no target filter set.- Specified by:
getTargetin interfaceReferenceManager<S,T>- Returns:
- The target filter of this dependency or
nullif none is set.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getServiceObject
boolean getServiceObject(ComponentContextImpl<S> key, ReferenceMethod bindMethod, RefPair<S,T> refPair)
-
deactivateComponentManager
private void deactivateComponentManager()
-
-