Class ServiceTracker<S,T>
- java.lang.Object
-
- org.osgi.util.tracker.ServiceTracker<S,T>
-
- Type Parameters:
S- The type of the service being tracked.T- The type of the tracked object.
- All Implemented Interfaces:
ServiceTrackerCustomizer<S,T>
- Direct Known Subclasses:
Logger,SortingServiceTracker
@ConsumerType public class ServiceTracker<S,T> extends java.lang.Object implements ServiceTrackerCustomizer<S,T>
TheServiceTrackerclass simplifies using services from the Framework's service registry.A
ServiceTrackerobject is constructed with search criteria and aServiceTrackerCustomizerobject. AServiceTrackercan use aServiceTrackerCustomizerto customize the service objects to be tracked. TheServiceTrackercan then be opened to begin tracking all services in the Framework's service registry that match the specified search criteria. TheServiceTrackercorrectly handles all of the details of listening toServiceEvents and getting and ungetting services.The
getServiceReferencesmethod can be called to get references to the services being tracked. ThegetServiceandgetServicesmethods can be called to get the service objects for the tracked service.The
ServiceTrackerclass is thread-safe. It does not call aServiceTrackerCustomizerwhile holding any locks.ServiceTrackerCustomizerimplementations must also be thread-safe.
-
-
Constructor Summary
Constructors Constructor Description ServiceTracker(BundleContext context, java.lang.Class<S> clazz, ServiceTrackerCustomizer<S,T> customizer)Create aServiceTrackeron the specified class.ServiceTracker(BundleContext context, java.lang.String clazz, ServiceTrackerCustomizer<S,T> customizer)Create aServiceTrackeron the specified class name.ServiceTracker(BundleContext context, Filter filter, ServiceTrackerCustomizer<S,T> customizer)Create aServiceTrackeron the specifiedFilterobject.ServiceTracker(BundleContext context, ServiceReference<S> reference, ServiceTrackerCustomizer<S,T> customizer)Create aServiceTrackeron the specifiedServiceReference.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TaddingService(ServiceReference<S> reference)Default implementation of theServiceTrackerCustomizer.addingServicemethod.voidclose()Close thisServiceTracker.TgetService()Returns a service object for one of the services being tracked by thisServiceTracker.TgetService(ServiceReference<S> reference)Returns the service object for the specifiedServiceReferenceif the specified referenced service is being tracked by thisServiceTracker.ServiceReference<S>getServiceReference()Returns aServiceReferencefor one of the services being tracked by thisServiceTracker.ServiceReference<S>[]getServiceReferences()Return an array ofServiceReferences for all services being tracked by thisServiceTracker.java.lang.Object[]getServices()Return an array of service objects for all services being tracked by thisServiceTracker.T[]getServices(T[] array)Return an array of service objects for all services being tracked by thisServiceTracker.java.util.SortedMap<ServiceReference<S>,T>getTracked()Return aSortedMapof theServiceReferences and service objects for all services being tracked by thisServiceTracker.intgetTrackingCount()Returns the tracking count for thisServiceTracker.booleanisEmpty()Return if thisServiceTrackeris empty.voidmodifiedService(ServiceReference<S> reference, T service)Default implementation of theServiceTrackerCustomizer.modifiedServicemethod.voidopen()Open thisServiceTrackerand begin tracking services.voidopen(boolean trackAllServices)Open thisServiceTrackerand begin tracking services.voidremove(ServiceReference<S> reference)Remove a service from thisServiceTracker.voidremovedService(ServiceReference<S> reference, T service)Default implementation of theServiceTrackerCustomizer.removedServicemethod.intsize()Return the number of services being tracked by thisServiceTracker.TwaitForService(long timeout)Wait for at least one service to be tracked by thisServiceTracker.
-
-
-
Constructor Detail
-
ServiceTracker
public ServiceTracker(BundleContext context, ServiceReference<S> reference, ServiceTrackerCustomizer<S,T> customizer)
Create aServiceTrackeron the specifiedServiceReference.The service referenced by the specified
ServiceReferencewill be tracked by thisServiceTracker.- Parameters:
context- TheBundleContextagainst which the tracking is done.reference- TheServiceReferencefor the service to be tracked.customizer- The customizer object to call when services are added, modified, or removed in thisServiceTracker. If customizer isnull, then thisServiceTrackerwill be used as theServiceTrackerCustomizerand thisServiceTrackerwill call theServiceTrackerCustomizermethods on itself.
-
ServiceTracker
public ServiceTracker(BundleContext context, java.lang.String clazz, ServiceTrackerCustomizer<S,T> customizer)
Create aServiceTrackeron the specified class name.Services registered under the specified class name will be tracked by this
ServiceTracker.- Parameters:
context- TheBundleContextagainst which the tracking is done.clazz- The class name of the services to be tracked.customizer- The customizer object to call when services are added, modified, or removed in thisServiceTracker. If customizer isnull, then thisServiceTrackerwill be used as theServiceTrackerCustomizerand thisServiceTrackerwill call theServiceTrackerCustomizermethods on itself.
-
ServiceTracker
public ServiceTracker(BundleContext context, Filter filter, ServiceTrackerCustomizer<S,T> customizer)
Create aServiceTrackeron the specifiedFilterobject.Services which match the specified
Filterobject will be tracked by thisServiceTracker.- Parameters:
context- TheBundleContextagainst which the tracking is done.filter- TheFilterto select the services to be tracked.customizer- The customizer object to call when services are added, modified, or removed in thisServiceTracker. If customizer is null, then thisServiceTrackerwill be used as theServiceTrackerCustomizerand thisServiceTrackerwill call theServiceTrackerCustomizermethods on itself.- Since:
- 1.1
-
ServiceTracker
public ServiceTracker(BundleContext context, java.lang.Class<S> clazz, ServiceTrackerCustomizer<S,T> customizer)
Create aServiceTrackeron the specified class.Services registered under the name of the specified class will be tracked by this
ServiceTracker.- Parameters:
context- TheBundleContextagainst which the tracking is done.clazz- The class of the services to be tracked.customizer- The customizer object to call when services are added, modified, or removed in thisServiceTracker. If customizer isnull, then thisServiceTrackerwill be used as theServiceTrackerCustomizerand thisServiceTrackerwill call theServiceTrackerCustomizermethods on itself.- Since:
- 1.5
-
-
Method Detail
-
open
public void open()
Open thisServiceTrackerand begin tracking services.This implementation calls
open(false).- Throws:
java.lang.IllegalStateException- If theBundleContextwith which thisServiceTrackerwas created is no longer valid.- See Also:
open(boolean)
-
open
public void open(boolean trackAllServices)
Open thisServiceTrackerand begin tracking services.Services which match the search criteria specified when this
ServiceTrackerwas created are now tracked by thisServiceTracker.- Parameters:
trackAllServices- Iftrue, then thisServiceTrackerwill track all matching services regardless of class loader accessibility. Iffalse, then thisServiceTrackerwill only track matching services which are class loader accessible to the bundle whoseBundleContextis used by thisServiceTracker.- Throws:
java.lang.IllegalStateException- If theBundleContextwith which thisServiceTrackerwas created is no longer valid.- Since:
- 1.3
-
close
public void close()
Close thisServiceTracker.This method should be called when this
ServiceTrackershould end the tracking of services.This implementation calls
getServiceReferences()to get the list of tracked services to remove.
-
addingService
public T addingService(ServiceReference<S> reference)
Default implementation of theServiceTrackerCustomizer.addingServicemethod.This method is only called when this
ServiceTrackerhas been constructed with anull ServiceTrackerCustomizerargument.This implementation returns the result of calling
getService, on theBundleContextwith which thisServiceTrackerwas created, passing the specifiedServiceReference.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
removedServiceto unget the service.- Specified by:
addingServicein interfaceServiceTrackerCustomizer<S,T>- Parameters:
reference- The reference to the service being added to thisServiceTracker.- Returns:
- The service object to be tracked for the service added to this
ServiceTracker. - See Also:
ServiceTrackerCustomizer.addingService(ServiceReference)
-
modifiedService
public void modifiedService(ServiceReference<S> reference, T service)
Default implementation of theServiceTrackerCustomizer.modifiedServicemethod.This method is only called when this
ServiceTrackerhas been constructed with anull ServiceTrackerCustomizerargument.This implementation does nothing.
- Specified by:
modifiedServicein interfaceServiceTrackerCustomizer<S,T>- Parameters:
reference- The reference to modified service.service- The service object for the modified service.- See Also:
ServiceTrackerCustomizer.modifiedService(ServiceReference, Object)
-
removedService
public void removedService(ServiceReference<S> reference, T service)
Default implementation of theServiceTrackerCustomizer.removedServicemethod.This method is only called when this
ServiceTrackerhas been constructed with anull ServiceTrackerCustomizerargument.This implementation calls
ungetService, on theBundleContextwith which thisServiceTrackerwas created, passing the specifiedServiceReference.This method can be overridden in a subclass. If the default implementation of
addingServicemethod was used, this method must unget the service.- Specified by:
removedServicein interfaceServiceTrackerCustomizer<S,T>- Parameters:
reference- The reference to removed service.service- The service object for the removed service.- See Also:
ServiceTrackerCustomizer.removedService(ServiceReference, Object)
-
waitForService
public T waitForService(long timeout) throws java.lang.InterruptedException
Wait for at least one service to be tracked by thisServiceTracker. This method will also return when thisServiceTrackeris closed.It is strongly recommended that
waitForServiceis not used during the calling of theBundleActivatormethods.BundleActivatormethods are expected to complete in a short period of time.This implementation calls
getService()to determine if a service is being tracked.- Parameters:
timeout- The time interval in milliseconds to wait. If zero, the method will wait indefinitely.- Returns:
- Returns the result of
getService(). - Throws:
java.lang.InterruptedException- If another thread has interrupted the current thread.java.lang.IllegalArgumentException- If the value of timeout is negative.
-
getServiceReferences
public ServiceReference<S>[] getServiceReferences()
Return an array ofServiceReferences for all services being tracked by thisServiceTracker.- Returns:
- Array of
ServiceReferences ornullif no services are being tracked.
-
getServiceReference
public ServiceReference<S> getServiceReference()
Returns aServiceReferencefor one of the services being tracked by thisServiceTracker.If multiple services are being tracked, the service with the highest ranking (as specified in its
service.rankingproperty) is returned. If there is a tie in ranking, the service with the lowest service id (as specified in itsservice.idproperty); that is, the service that was registered first is returned. This is the same algorithm used byBundleContext.getServiceReference.This implementation calls
getServiceReferences()to get the list of references for the tracked services.- Returns:
- A
ServiceReferenceornullif no services are being tracked. - Since:
- 1.1
-
getService
public T getService(ServiceReference<S> reference)
Returns the service object for the specifiedServiceReferenceif the specified referenced service is being tracked by thisServiceTracker.- Parameters:
reference- The reference to the desired service.- Returns:
- A service object or
nullif the service referenced by the specifiedServiceReferenceis not being tracked.
-
getServices
public java.lang.Object[] getServices()
Return an array of service objects for all services being tracked by thisServiceTracker.This implementation calls
getServiceReferences()to get the list of references for the tracked services and then callsgetService(ServiceReference)for each reference to get the tracked service object.- Returns:
- An array of service objects or
nullif no services are being tracked.
-
getService
public T getService()
Returns a service object for one of the services being tracked by thisServiceTracker.If any services are being tracked, this implementation returns the result of calling
getService(getServiceReference()).- Returns:
- A service object or
nullif no services are being tracked.
-
remove
public void remove(ServiceReference<S> reference)
Remove a service from thisServiceTracker. The specified service will be removed from thisServiceTracker. If the specified service was being tracked then theServiceTrackerCustomizer.removedServicemethod 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 thisServiceTracker.- Returns:
- The number of services being tracked.
-
getTrackingCount
public int getTrackingCount()
Returns the tracking count for thisServiceTracker. The tracking count is initialized to 0 when thisServiceTrackeris opened. Every time a service is added, modified or removed from thisServiceTracker, the tracking count is incremented.The tracking count can be used to determine if this
ServiceTrackerhas 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 thisServiceTrackersince the previous tracking count was collected.- Returns:
- The tracking count for this
ServiceTrackeror -1 if thisServiceTrackeris not open. - Since:
- 1.2
-
getTracked
public java.util.SortedMap<ServiceReference<S>,T> getTracked()
Return aSortedMapof theServiceReferences and service objects for all services being tracked by thisServiceTracker. The map is sorted in reverse natural order ofServiceReference. That is, the first entry is the service with the highest ranking and the lowest service id.- Returns:
- A
SortedMapwith theServiceReferences and service objects for all services being tracked by thisServiceTracker. If no services are being tracked, then the returned map is empty. - Since:
- 1.5
-
isEmpty
public boolean isEmpty()
Return if thisServiceTrackeris empty.- Returns:
trueif thisServiceTrackeris not tracking any services.- Since:
- 1.5
-
getServices
public T[] getServices(T[] array)
Return an array of service objects for all services being tracked by thisServiceTracker. 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 callsgetService(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
-
-