Class DependencyManager<S,​T>

  • All Implemented Interfaces:
    ReferenceManager<S,​T>

    public class DependencyManager<S,​T>
    extends java.lang.Object
    implements ReferenceManager<S,​T>
    The DependencyManager manages the references to services declared by a single <reference>
    • 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 dependency
        index - 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

        boolean cardinalitySatisfied()
      • 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 a ServiceReference instances for a service implementing the interface and complying to the (optional) target filter declared for this dependency. If no matching service can be found null is returned. If the configured target filter is syntactically incorrect an error message is logged with the LogService and null 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 the getBestRefPair() 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 dependency null 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 of ServiceReference instances of all services this instance is bound to or null if no services are actually bound.
        Specified by:
        getServiceReferences in interface ReferenceManager<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 or null 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 - TODO
        serviceReference - 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).
      • isSatisfied

        public boolean isSatisfied()
        Returns true 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 interface ReferenceManager<S,​T>
      • hasGetPermission

        public boolean hasGetPermission()
        Returns true if the component providing bundle has permission to get the service described by this reference.
      • open

        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.
      • 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 object

        If 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 to
        refPair - 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.
      • 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 service
        edgeInfo - 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 from
        refPair - A service reference, service pair that will be unbound
        trackingCount - service event count for this reference
        info - EdgeInfo for the combination of this instance and this dependency manager
      • canUpdateDynamically

        boolean canUpdateDynamically​(java.util.Map<java.lang.String,​java.lang.Object> properties)
        Returns true if the properties can be dynamically applied to the component to which the dependency manager belongs.

        This method applies the following heuristics (in the given order):

        1. If there is no change in the target filter for this dependency, the properties can be applied
        2. 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.
        3. If there is still at least one service matching the new target filter we can apply the configuration because the depdency is dynamic.
        4. If there are no more services matching the filter, we can still apply the configuration if the dependency is optional.
        5. 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 be null if no target filtering is to be used.
      • 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 or null if this dependency has no target filter set.
        Specified by:
        getTarget in interface ReferenceManager<S,​T>
        Returns:
        The target filter of this dependency or null if none is set.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • newRefPair

        RefPair<S,​T> newRefPair​(org.osgi.framework.ServiceReference<T> serviceReference)
      • deactivateComponentManager

        private void deactivateComponentManager()