Class ServiceTracker<S,​T,​U extends org.osgi.framework.ServiceEvent>

  • Type Parameters:
    S -
    T -

    public class ServiceTracker<S,​T,​U extends org.osgi.framework.ServiceEvent>
    extends Object
    changes from osgi service tracker: - included AbstractTracked as an inner class. - addedService method on customizer called after the object is tracked. - we always track all matching services.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected org.osgi.framework.BundleContext context
      The Bundle Context used by this ServiceTracker.
      protected org.osgi.framework.Filter eventFilter
      The Filter used by this ServiceTracker which specifies the search criteria for the services to track.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T addingService​(org.osgi.framework.ServiceReference<S> reference, int trackingCount)
      Default implementation of the ServiceTrackerCustomizer.addingService method.
      SortedMap<org.osgi.framework.ServiceReference<S>,​T> close​(AtomicInteger trackingCount)
      Close this ServiceTracker.
      void completeClose​(Map<org.osgi.framework.ServiceReference<S>,​T> toUntrack)  
      T getService​(org.osgi.framework.ServiceReference<S> reference)
      Returns the service object for the specified ServiceReference if the specified referenced service is being tracked by this ServiceTracker.
      int getServiceCount()  
      org.osgi.framework.ServiceReference<S>[] getServiceReferences()
      Return an array of ServiceReferences for all services being tracked by this ServiceTracker.
      Object[] getServices()
      Return an array of service objects for all services being tracked by this ServiceTracker.
      T[] getServices​(T[] array)
      Return an array of service objects for all services being tracked by this ServiceTracker.
      SortedMap<org.osgi.framework.ServiceReference<S>,​T> getTracked​(Boolean activate, AtomicInteger trackingCount)
      Return a SortedMap of the ServiceReferences and service objects for all services being tracked by this ServiceTracker.
      int getTrackingCount()
      Returns the tracking count for this ServiceTracker.
      boolean isActive()  
      boolean isEmpty()
      Return if this ServiceTracker is empty.
      void modifiedService​(org.osgi.framework.ServiceReference<S> reference, T service, int trackingCount)
      Default implementation of the ServiceTrackerCustomizer.modifiedService method.
      void open​(boolean trackAllServices, AtomicInteger trackingCount)
      Open this ServiceTracker and begin tracking services.
      void open​(AtomicInteger trackingCount)
      Open this ServiceTracker and begin tracking services.
      void remove​(org.osgi.framework.ServiceReference<S> reference)
      Remove a service from this ServiceTracker.
      void removedService​(org.osgi.framework.ServiceReference<S> reference, T service, int trackingCount)
      Default implementation of the ServiceTrackerCustomizer.removedService method.
      int size()
      Return the number of services being tracked by this ServiceTracker.
      org.apache.felix.scr.impl.manager.ServiceTracker.Tracked tracked()
      Accessor method for the current Tracked object.
    • Field Detail

      • context

        protected final org.osgi.framework.BundleContext context
        The Bundle Context used by this ServiceTracker.
      • eventFilter

        protected final org.osgi.framework.Filter eventFilter
        The Filter used by this ServiceTracker which specifies the search criteria for the services to track.
        Since:
        1.1
    • Constructor Detail

      • ServiceTracker

        public ServiceTracker​(org.osgi.framework.BundleContext context,
                              ServiceTrackerCustomizer<S,​T,​U> customizer,
                              boolean initialActive,
                              ExtendedServiceListenerContext<U> bundleComponentActivator,
                              org.osgi.framework.Filter eventFilter,
                              String classFilterString,
                              String initialReferenceFilterString)
        Create a ServiceTracker on the specified Filter object.

        Services which match the specified Filter object will be tracked by this ServiceTracker.

        Parameters:
        context - The BundleContext against which the tracking is done.
        customizer - The customizer object to call when services are added, modified, or removed in this ServiceTracker. If customizer is null, then this ServiceTracker will be used as the ServiceTrackerCustomizer and this ServiceTracker will call the ServiceTrackerCustomizer methods on itself.
        initialActive - Initial active state of the tracker.
        bundleComponentActivator - TODO
        eventFilter - The Filter to select the services to be tracked.
        classFilterString - TODO
        initialReferenceFilterString - TODO
        Since:
        1.1
    • Method Detail

      • tracked

        public org.apache.felix.scr.impl.manager.ServiceTracker.Tracked tracked()
        Accessor method for the current Tracked object. This method is only intended to be used by the unsynchronized methods which do not modify the tracked field.
        Returns:
        The current Tracked object.
      • open

        public void open​(boolean trackAllServices,
                         AtomicInteger trackingCount)
        Open this ServiceTracker and begin tracking services.

        Services which match the search criteria specified when this ServiceTracker was created are now tracked by this ServiceTracker.

        Parameters:
        trackAllServices - If true, then this ServiceTracker will track all matching services regardless of class loader accessibility. If false, then this ServiceTracker will only track matching services which are class loader accessible to the bundle whose BundleContext is used by this ServiceTracker.
        trackingCount -
        Throws:
        IllegalStateException - If the BundleContext with which this ServiceTracker was created is no longer valid.
        Since:
        1.3
      • close

        public SortedMap<org.osgi.framework.ServiceReference<S>,​T> close​(AtomicInteger trackingCount)
        Close this ServiceTracker.

        This method should be called when this ServiceTracker should end the tracking of services.

        This implementation calls getServiceReferences() to get the list of tracked services to remove.

        Parameters:
        trackingCount -
      • completeClose

        public void completeClose​(Map<org.osgi.framework.ServiceReference<S>,​T> toUntrack)
      • addingService

        public T addingService​(org.osgi.framework.ServiceReference<S> reference,
                               int trackingCount)
        Default implementation of the ServiceTrackerCustomizer.addingService method.

        This method is only called when this ServiceTracker has been constructed with a null ServiceTrackerCustomizer argument.

        This implementation returns the result of calling getService on the BundleContext with which this ServiceTracker was created passing the specified ServiceReference.

        This method can be overridden in a subclass to customize the service object to be tracked for the service being added. In that case, take care not to rely on the default implementation of removedService to unget the service.

        Parameters:
        reference - The reference to the service being added to this ServiceTracker.
        Returns:
        The service object to be tracked for the service added to this ServiceTracker.
      • modifiedService

        public void modifiedService​(org.osgi.framework.ServiceReference<S> reference,
                                    T service,
                                    int trackingCount)
        Default implementation of the ServiceTrackerCustomizer.modifiedService method.

        This method is only called when this ServiceTracker has been constructed with a null ServiceTrackerCustomizer argument.

        This implementation does nothing.

        Parameters:
        reference - The reference to modified service.
        service - The service object for the modified service.
        See Also:
        ServiceTrackerCustomizer.modifiedService(org.osgi.framework.ServiceReference, Object, int, ServiceEvent)
      • removedService

        public void removedService​(org.osgi.framework.ServiceReference<S> reference,
                                   T service,
                                   int trackingCount)
        Default implementation of the ServiceTrackerCustomizer.removedService method.

        This method is only called when this ServiceTracker has been constructed with a null ServiceTrackerCustomizer argument.

        This implementation calls ungetService, on the BundleContext with which this ServiceTracker was created, passing the specified ServiceReference.

        This method can be overridden in a subclass. If the default implementation of addingService method was used, this method must unget the service.

        Parameters:
        reference - The reference to removed service.
        service - The service object for the removed service.
        See Also:
        ServiceTrackerCustomizer.removedService(org.osgi.framework.ServiceReference, Object, int, ServiceEvent)
      • getServiceReferences

        public org.osgi.framework.ServiceReference<S>[] getServiceReferences()
        Return an array of ServiceReferences for all services being tracked by this ServiceTracker.
        Returns:
        Array of ServiceReferences or null if no services are being tracked.
      • getService

        public T getService​(org.osgi.framework.ServiceReference<S> reference)
        Returns the service object for the specified ServiceReference if the specified referenced service is being tracked by this ServiceTracker.
        Parameters:
        reference - The reference to the desired service.
        Returns:
        A service object or null if the service referenced by the specified ServiceReference is not being tracked.
      • getServices

        public Object[] getServices()
        Return an array of service objects for all services being tracked by this ServiceTracker.

        This implementation calls getServiceReferences() to get the list of references for the tracked services and then calls getService(ServiceReference) for each reference to get the tracked service object.

        Returns:
        An array of service objects or null if no services are being tracked.
      • remove

        public void remove​(org.osgi.framework.ServiceReference<S> reference)
        Remove a service from this ServiceTracker. The specified service will be removed from this ServiceTracker. If the specified service was being tracked then the ServiceTrackerCustomizer.removedService method will be called for that service.
        Parameters:
        reference - The reference to the service to be removed.
      • size

        public int size()
        Return the number of services being tracked by this ServiceTracker.
        Returns:
        The number of services being tracked.
      • getTrackingCount

        public int getTrackingCount()
        Returns the tracking count for this ServiceTracker. The tracking count is initialized to 0 when this ServiceTracker is opened. Every time a service is added, modified or removed from this ServiceTracker, the tracking count is incremented.

        The tracking count can be used to determine if this ServiceTracker has added, modified or removed a service by comparing a tracking count value previously collected with the current tracking count value. If the value has not changed, then no service has been added, modified or removed from this ServiceTracker since the previous tracking count was collected.

        Returns:
        The tracking count for this ServiceTracker or -1 if this ServiceTracker is not open.
        Since:
        1.2
      • getTracked

        public SortedMap<org.osgi.framework.ServiceReference<S>,​T> getTracked​(Boolean activate,
                                                                                    AtomicInteger trackingCount)
        Return a SortedMap of the ServiceReferences and service objects for all services being tracked by this ServiceTracker. The map is sorted in reverse natural order of ServiceReference. That is, the first entry is the service with the highest ranking and the lowest service id.
        Parameters:
        activate -
        trackingCount -
        Returns:
        A SortedMap with the ServiceReferences and service objects for all services being tracked by this ServiceTracker. If no services are being tracked, then the returned map is empty.
        Since:
        1.5
      • isEmpty

        public boolean isEmpty()
        Return if this ServiceTracker is empty.
        Returns:
        true if this ServiceTracker is not tracking any services.
        Since:
        1.5
      • getServiceCount

        public int getServiceCount()
      • isActive

        public boolean isActive()
      • getServices

        public T[] getServices​(T[] array)
        Return an array of service objects for all services being tracked by this ServiceTracker. The runtime type of the returned array is that of the specified array.

        This implementation calls getServiceReferences() to get the list of references for the tracked services and then calls getService(ServiceReference) for each reference to get the tracked service object.

        Parameters:
        array - An array into which the tracked service objects will be stored, if the array is large enough.
        Returns:
        An array of service objects being tracked. If the specified array is large enough to hold the result, then the specified array is returned. If the specified array is longer then necessary to hold the result, the array element after the last service object is set to null. If the specified array is not large enough to hold the result, a new array is created and returned.
        Since:
        1.5