Class ServiceTracker<S, T, U extends org.osgi.framework.ServiceEvent>
- Type Parameters:
S-T-
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) classAbstract class to track items.private classInner class which subclasses AbstractTracked. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanwhether the DependencyManager is getting the service immediately.protected final org.osgi.framework.BundleContextThe Bundle Context used by thisServiceTracker.(package private) final ServiceTrackerCustomizer<S, T, U> TheServiceTrackerCustomizerfor this tracker.(package private) static final booleanprivate ExtendedServiceListenerContext<U> (package private) final StringFilter string for use when adding the ServiceListener.private ServiceTracker<S,T, U>.Tracked Tracked services:ServiceReference-> customized Object andServiceListenerobject -
Constructor Summary
ConstructorsConstructorDescriptionServiceTracker(org.osgi.framework.BundleContext context, ServiceTrackerCustomizer<S, T, U> customizer, boolean initialActive, ExtendedServiceListenerContext<U> bundleComponentActivator, String initialReferenceFilterString) Create aServiceTrackeron the specifiedFilterobject. -
Method Summary
Modifier and TypeMethodDescriptionaddingService(org.osgi.framework.ServiceReference<S> reference, int trackingCount) Default implementation of theServiceTrackerCustomizer.addingServicemethod.close(AtomicInteger trackingCount) Close thisServiceTracker.voidcompleteClose(Map<org.osgi.framework.ServiceReference<S>, T> toUntrack) (package private) voidprivate org.osgi.framework.ServiceReference<S>[]getInitialReferences(String className, String filterString) Returns the list of initialServiceReferences that will be tracked by thisServiceTracker.getService(org.osgi.framework.ServiceReference<S> reference) Returns the service object for the specifiedServiceReferenceif the specified referenced service is being tracked by thisServiceTracker.intorg.osgi.framework.ServiceReference<S>[]Return an array ofServiceReferences for all services being tracked by thisServiceTracker.Object[]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.getTracked(Boolean activate, AtomicInteger trackingCount) Return aSortedMapof theServiceReferences and service objects for all services being tracked by thisServiceTracker.intReturns the tracking count for thisServiceTracker.booleanisActive()booleanisEmpty()Return if thisServiceTrackeris empty.(package private) voidmodified()Called by the Tracked object whenever the set of tracked services is modified.voidmodifiedService(org.osgi.framework.ServiceReference<S> reference, T service, int trackingCount) Default implementation of theServiceTrackerCustomizer.modifiedServicemethod.voidopen(boolean trackAllServices, AtomicInteger trackingCount) Open thisServiceTrackerand begin tracking services.voidopen(AtomicInteger trackingCount) Open thisServiceTrackerand begin tracking services.voidRemove a service from thisServiceTracker.voidremovedService(org.osgi.framework.ServiceReference<S> reference, T service, int trackingCount) Default implementation of theServiceTrackerCustomizer.removedServicemethod.intsize()Return the number of services being tracked by thisServiceTracker.tracked()Accessor method for the current Tracked object.
-
Field Details
-
DEBUG
static final boolean DEBUG- See Also:
-
context
protected final org.osgi.framework.BundleContext contextThe Bundle Context used by thisServiceTracker. -
customizer
TheServiceTrackerCustomizerfor this tracker. -
initialReferenceFilterString
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. -
tracked
Tracked services:ServiceReference-> customized Object andServiceListenerobject -
active
private boolean activewhether the DependencyManager is getting the service immediately. -
extendedServiceListenerContext
private ExtendedServiceListenerContext<U extends org.osgi.framework.ServiceEvent> extendedServiceListenerContext
-
-
Constructor Details
-
ServiceTracker
public ServiceTracker(org.osgi.framework.BundleContext context, ServiceTrackerCustomizer<S, T, U> customizer, boolean initialActive, ExtendedServiceListenerContext<U> bundleComponentActivator, String initialReferenceFilterString) Create aServiceTrackeron the specifiedFilterobject.Services which match the specified
Filterobject will be tracked by thisServiceTracker.- Parameters:
context- TheBundleContextagainst which the tracking is done.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.initialActive- Initial active state of the tracker.bundleComponentActivator- TODOinitialReferenceFilterString- TODO- Since:
- 1.1
-
-
Method Details
-
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
Open thisServiceTrackerand begin tracking services.This implementation calls
open(false).- Parameters:
trackingCount-- Throws:
IllegalStateException- If theBundleContextwith which thisServiceTrackerwas created is no longer valid.- See Also:
-
open
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.trackingCount-- Throws:
IllegalStateException- If theBundleContextwith which thisServiceTrackerwas created is no longer valid.- Since:
- 1.3
-
getInitialReferences
private org.osgi.framework.ServiceReference<S>[] getInitialReferences(String className, String filterString) throws org.osgi.framework.InvalidSyntaxException Returns the list of initialServiceReferences that will be tracked by thisServiceTracker.- Parameters:
className- The class name with which the service was registered, ornullfor all services.filterString- The filter criteria ornullfor all services.- Returns:
- The list of initial
ServiceReferences. - Throws:
org.osgi.framework.InvalidSyntaxException- If the specified filterString has an invalid syntax.
-
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.- Parameters:
trackingCount-
-
completeClose
-
addingService
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
getServiceon 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.- 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.
-
modifiedService
public void modifiedService(org.osgi.framework.ServiceReference<S> reference, T service, int trackingCount) Default implementation of theServiceTrackerCustomizer.modifiedServicemethod.This method is only called when this
ServiceTrackerhas been constructed with anull ServiceTrackerCustomizerargument.This implementation does nothing.
- Parameters:
reference- The reference to modified service.service- The service object for the modified service.- See Also:
-
removedService
public void removedService(org.osgi.framework.ServiceReference<S> reference, T service, int trackingCount) 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.- Parameters:
reference- The reference to removed service.service- The service object for the removed service.- See Also:
-
getServiceReferences
Return an array ofServiceReferences for all services being tracked by thisServiceTracker.- Returns:
- Array of
ServiceReferences ornullif no services are being tracked.
-
getService
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
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.
-
remove
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
-
modified
void modified()Called by the Tracked object whenever the set of tracked services is modified. Clears the cache. -
getTracked
public SortedMap<org.osgi.framework.ServiceReference<S>, T> getTracked(Boolean activate, AtomicInteger trackingCount) 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.- Parameters:
activate-trackingCount-- 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
-
deactivate
void deactivate() -
isEmpty
public boolean isEmpty()Return if thisServiceTrackeris empty.- Returns:
trueif thisServiceTrackeris not tracking any services.- Since:
- 1.5
-
getServiceCount
public int getServiceCount() -
isActive
public boolean isActive() -
getServices
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
-