ServiceTracker class simplifies using services from the
Framework's service registry.
A ServiceTracker object is constructed with search criteria and a
ServiceTrackerCustomizer object. A ServiceTracker can use a
ServiceTrackerCustomizer to customize the service objects to be
tracked. The ServiceTracker can then be opened to begin tracking all
services in the Framework's service registry that match the specified search
criteria. The ServiceTracker correctly handles all of the details of
listening to ServiceEvents and getting and ungetting services.
The getServiceReferences method can be called to get references to
the services being tracked. The getService and getServices
methods can be called to get the service objects for the tracked service.
The ServiceTracker class is thread-safe. It does not call a
ServiceTrackerCustomizer while holding any locks.
ServiceTrackerCustomizer implementations must also be thread-safe.
| Modifier and Type | Class and Description |
|---|---|
| private class | ServiceTracker.
Subclass of Tracked which implements the AllServiceListener interface. |
| private class | ServiceTracker.
Inner class which subclasses AbstractTracked. |
| Modifier and Type | Field and Description |
|---|---|
| private volatile ServiceReference | cachedReference
Cached ServiceReference for getServiceReference. |
| private volatile T | cachedService
Cached service object for getService. |
| protected final BundleContext | context
The Bundle Context used by this |
| pack-priv final ServiceTrackerCustomizer | customizer
The |
| pack-priv static final boolean | |
| protected final Filter | filter
The Filter used by this |
| pack-priv final String | listenerFilter
Filter string for use when adding the ServiceListener. |
| private final String | trackClass
Class name to be tracked. |
| private volatile ServiceTracker | tracked
Tracked services: |
| private final ServiceReference | trackReference
Reference to be tracked. |
| Access | Constructor and Description |
|---|---|
| public | ServiceTracker(final BundleContext
The context, final ServiceReference<S> BundleContext against which the tracking is
done.The reference, final ServiceTrackerCustomizer<S, T> ServiceReference for the service to be
tracked.The customizer object to call when services are added,
modified, or removed in this customizer)ServiceTracker. If customizer
is null, then this ServiceTracker will be used as
the ServiceTrackerCustomizer and this
ServiceTracker will call the
ServiceTrackerCustomizer methods on itself.Create a |
| public | ServiceTracker(final BundleContext
The context, final String BundleContext against which the tracking is
done.The class name of the services to be tracked. clazz, final ServiceTrackerCustomizer<S, T> The customizer object to call when services are added,
modified, or removed in this customizer)ServiceTracker. If customizer
is null, then this ServiceTracker will be used as
the ServiceTrackerCustomizer and this
ServiceTracker will call the
ServiceTrackerCustomizer methods on itself.Create a |
| public | ServiceTracker(final BundleContext
The context, final Filter BundleContext against which the tracking is
done.The filter, final ServiceTrackerCustomizer<S, T> Filter to select the services to be tracked.The customizer object to call when services are added,
modified, or removed in this customizer)ServiceTracker. If customizer
is null, then this ServiceTracker will be used as the
ServiceTrackerCustomizer and this ServiceTracker
will call the ServiceTrackerCustomizer methods on itself.Create a |
| public | ServiceTracker(final BundleContext
The context, final Class<S> BundleContext against which the tracking is
done.The class of the services to be tracked. clazz, final ServiceTrackerCustomizer<S, T> The customizer object to call when services are added,
modified, or removed in this customizer)ServiceTracker. If customizer
is null, then this ServiceTracker will be used as
the ServiceTrackerCustomizer and this
ServiceTracker will call the
ServiceTrackerCustomizer methods on itself.Create a |
| Modifier and Type | Method and Description |
|---|---|
| public T | Returns: The service object to be tracked for the service added to thisServiceTracker.The reference to the service being added to this
reference)ServiceTracker.Implements org. Default implementation of the
|
| public void | |
| private ServiceReference | Returns: The list of initialServiceReferences.If trackAllServices, String true, use
getAllServiceReferences.The class name with which the service was registered, or
className, String null for all services.The filter criteria or filterString)null for all services.Returns the list of initial |
| public T | Returns: A service object ornull if the service referenced by the
specified ServiceReference is not being tracked.The reference to the desired service. reference)Returns the service object for the specified |
| public T | Returns: A service object ornull if no services are being
tracked.Returns a service object for one of the services being tracked by this
|
| public ServiceReference | Returns: AServiceReference or null if no services are
being tracked.Returns a |
| public ServiceReference | Returns: Array ofServiceReferences or null if no services
are being tracked.Return an array of |
| public Object[] | Returns: An array of service objects ornull if no services are
being tracked.Return an array of service objects for all services being tracked by this
|
| public T[] | 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 tonull. If the specified array is not large enough
to hold the result, a new array is created and returned.An array into which the tracked service objects will be
stored, if the array is large enough. array)Return an array of service objects for all services being tracked by this
|
| public SortedMap | Returns: ASortedMap 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.Return a |
| public int | Returns: The tracking count for thisServiceTracker or -1 if this
ServiceTracker is not open.Returns the tracking count for this |
| public boolean | Returns: true if this ServiceTracker is not tracking any
services.Return if this |
| pack-priv void | |
| public void | modifiedService(ServiceReference<S>
The reference to modified service. reference, T The service object for the modified service. service)Implements org. Default implementation of the
|
| public void | |
| public void | open(boolean
If trackAllServices)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.Open this |
| public void | remove(ServiceReference<S>
The reference to the service to be removed. reference)Remove a service from this |
| public void | removedService(ServiceReference<S>
The reference to removed service. reference, T The service object for the removed service. service)Implements org. Default implementation of the
|
| public int | Returns: The number of services being tracked.Return the number of services being tracked by this
|
| private ServiceTracker | |
| public T | Returns: Returns the result ofgetService().The time interval in milliseconds to wait. If zero, the
method will wait indefinitely. timeout)Wait for at least one service to be tracked by this
|
| cachedReference | back to summary |
|---|---|
| private volatile ServiceReference<S> cachedReference Cached ServiceReference for getServiceReference. This field is volatile since it is accessed by multiple threads. | |
| cachedService | back to summary |
|---|---|
| private volatile T cachedService Cached service object for getService. This field is volatile since it is accessed by multiple threads. | |
| context | back to summary |
|---|---|
| protected final BundleContext context The Bundle Context used by this | |
| customizer | back to summary |
|---|---|
| pack-priv final ServiceTrackerCustomizer<S, T> customizer The | |
| DEBUG | back to summary |
|---|---|
| pack-priv static final boolean DEBUG | |
| filter | back to summary |
|---|---|
| protected final Filter filter The Filter used by this
| |
| listenerFilter | back to summary |
|---|---|
| pack-priv final String listenerFilter Filter string for use when adding the ServiceListener. If this field is set, then certain optimizations can be taken since we don't have a user supplied filter. | |
| trackClass | back to summary |
|---|---|
| private final String trackClass Class name to be tracked. If this field is set, then we are tracking by class name. | |
| tracked | back to summary |
|---|---|
| private volatile ServiceTracker<S, T>. Tracked services: | |
| trackReference | back to summary |
|---|---|
| private final ServiceReference<S> trackReference Reference to be tracked. If this field is set, then we are tracking a single ServiceReference. | |
| ServiceTracker | back to summary |
|---|---|
| public ServiceTracker(final BundleContext context, final ServiceReference<S> reference, final ServiceTrackerCustomizer<S, T> customizer) Create a
The service referenced by the specified
| |
| ServiceTracker | back to summary |
|---|---|
| public ServiceTracker(final BundleContext context, final String clazz, final ServiceTrackerCustomizer<S, T> customizer) Create a
Services registered under the specified class name will be tracked by
this
| |
| ServiceTracker | back to summary |
|---|---|
| public ServiceTracker(final BundleContext context, final Filter filter, final ServiceTrackerCustomizer<S, T> customizer) Create a
Services which match the specified
| |
| ServiceTracker | back to summary |
|---|---|
| public ServiceTracker(final BundleContext context, final Class<S> clazz, final ServiceTrackerCustomizer<S, T> customizer) Create a
Services registered under the name of the specified class will be tracked
by this
| |
| addingService | back to summary |
|---|---|
| public T addingService(ServiceReference<S> reference) Implements org. Default implementation of the
This method is only called when this
This implementation returns the result of calling
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
| |
| close | back to summary |
|---|---|
| public void close() Close this
This method should be called when this
This implementation calls | |
| getInitialReferences | back to summary |
|---|---|
| private ServiceReference Returns the list of initial
| |
| getService | back to summary |
|---|---|
| public T getService(ServiceReference<S> reference) Returns the service object for the specified
| |
| getService | back to summary |
|---|---|
| public T getService() Returns a service object for one of the services being tracked by this
If any services are being tracked, this implementation returns the result
of calling
| |
| getServiceReference | back to summary |
|---|---|
| public ServiceReference Returns a
If multiple services are being tracked, the service with the highest
ranking (as specified in its
This implementation calls
| |
| getServiceReferences | back to summary |
|---|---|
| public ServiceReference Return an array of
| |
| getServices | back to summary |
|---|---|
| public Object[] getServices() Return an array of service objects for all services being tracked by this
This implementation calls
| |
| getServices | back to summary |
|---|---|
| public T[] getServices(T[] array) Return an array of service objects for all services being tracked by this
This implementation calls
| |
| getTracked | back to summary |
|---|---|
| public SortedMap Return a
| |
| getTrackingCount | back to summary |
|---|---|
| public int getTrackingCount() Returns the tracking count for this
The tracking count can be used to determine if this
| |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() Return if this
| |
| modified | back to summary |
|---|---|
| pack-priv void modified() Called by the Tracked object whenever the set of tracked services is modified. Clears the cache. | |
| modifiedService | back to summary |
|---|---|
| public void modifiedService(ServiceReference<S> reference, T service) Implements org. Default implementation of the
This method is only called when this This implementation does nothing.
| |
| open | back to summary |
|---|---|
| public void open() Open this
This implementation calls
| |
| open | back to summary |
|---|---|
| public void open(boolean trackAllServices) Open this
Services which match the search criteria specified when this
| |
| remove | back to summary |
|---|---|
| public void remove(ServiceReference<S> reference) Remove a service from this
| |
| removedService | back to summary |
|---|---|
| public void removedService(ServiceReference<S> reference, T service) Implements org. Default implementation of the
This method is only called when this
This implementation calls
This method can be overridden in a subclass. If the default
implementation of
| |
| size | back to summary |
|---|---|
| public int size() Return the number of services being tracked by this
| |
| tracked | back to summary |
|---|---|
| private ServiceTracker 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.
| |
| waitForService | back to summary |
|---|---|
| public T waitForService(long timeout) throws InterruptedException Wait for at least one service to be tracked by this
It is strongly recommended that
This implementation calls
| |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| AllTracked | back to summary |
|---|---|
| pack-priv AllTracked() AllTracked constructor. | |
ServiceListener object for the tracker.
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv final T | Returns: Customized object for the tracked item ornull if the
item is not to be tracked.Item to be tracked. item, final ServiceEvent Action related object. related)Implements abstract org. Call the specific customizer adding method. |
| pack-priv final void | customizerModified(final ServiceReference<S>
Tracked item. item, final ServiceEvent Action related object. related, final T Customized object for the tracked item. object)Implements abstract org. Call the specific customizer modified method. |
| pack-priv final void | customizerRemoved(final ServiceReference<S>
Tracked item. item, final ServiceEvent Action related object. related, final T Customized object for the tracked item. object)Implements abstract org. Call the specific customizer removed method. |
| pack-priv final void | modified()
Overrides org. Increment the tracking count and tell the tracker there was a modification. |
| public final void | serviceChanged(final ServiceEvent
ServiceEvent object from the framework.Implements org.
|
| Tracked | back to summary |
|---|---|
| pack-priv Tracked() Tracked constructor. | |
| customizerAdding | back to summary |
|---|---|
| pack-priv final T customizerAdding(final ServiceReference<S> item, final ServiceEvent related) Implements abstract org. Call the specific customizer adding method. This method must not be called while synchronized on this object.
| |
| customizerModified | back to summary |
|---|---|
| pack-priv final void customizerModified(final ServiceReference<S> item, final ServiceEvent related, final T object) Implements abstract org. Call the specific customizer modified method. This method must not be called while synchronized on this object.
| |
| customizerRemoved | back to summary |
|---|---|
| pack-priv final void customizerRemoved(final ServiceReference<S> item, final ServiceEvent related, final T object) Implements abstract org. Call the specific customizer removed method. This method must not be called while synchronized on this object.
| |
| modified | back to summary |
|---|---|
| pack-priv final void modified() Overrides org. Increment the tracking count and tell the tracker there was a modification.
| |
| serviceChanged | back to summary |
|---|---|
| public final void serviceChanged(final ServiceEvent event) Implements org.
| |