|
OSGi™ Core Release 5 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.osgi.util.tracker.ServiceTracker<S,T>
S
- The type of the service being tracked.T
- The type of the tracked object.public class ServiceTracker<S,T>
The 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 ServiceEvent
s 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.
Field Summary | |
---|---|
protected BundleContext |
context
The Bundle Context used by this ServiceTracker . |
protected Filter |
filter
The Filter used by this ServiceTracker which specifies the search
criteria for the services to track. |
Constructor Summary | |
---|---|
ServiceTracker(BundleContext context,
java.lang.Class<S> clazz,
ServiceTrackerCustomizer<S,T> customizer)
Create a ServiceTracker on the specified class. |
|
ServiceTracker(BundleContext context,
Filter filter,
ServiceTrackerCustomizer<S,T> customizer)
Create a ServiceTracker on the specified Filter object. |
|
ServiceTracker(BundleContext context,
ServiceReference<S> reference,
ServiceTrackerCustomizer<S,T> customizer)
Create a ServiceTracker on the specified ServiceReference
. |
|
ServiceTracker(BundleContext context,
java.lang.String clazz,
ServiceTrackerCustomizer<S,T> customizer)
Create a ServiceTracker on the specified class name. |
Method Summary | |
---|---|
T |
addingService(ServiceReference<S> reference)
Default implementation of the ServiceTrackerCustomizer.addingService method. |
void |
close()
Close this ServiceTracker . |
T |
getService()
Returns a service object for one of the services being tracked by this ServiceTracker . |
T |
getService(ServiceReference<S> reference)
Returns the service object for the specified ServiceReference if
the specified referenced service is being tracked by this
ServiceTracker . |
ServiceReference<S> |
getServiceReference()
Returns a ServiceReference for one of the services being tracked
by this ServiceTracker . |
ServiceReference<S>[] |
getServiceReferences()
Return an array of ServiceReference s for all services being
tracked by this ServiceTracker . |
java.lang.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 . |
java.util.SortedMap<ServiceReference<S>,T> |
getTracked()
Return a SortedMap of the ServiceReference s and service
objects for all services being tracked by this ServiceTracker . |
int |
getTrackingCount()
Returns the tracking count for this ServiceTracker . |
boolean |
isEmpty()
Return if this ServiceTracker is empty. |
void |
modifiedService(ServiceReference<S> reference,
T service)
Default implementation of the ServiceTrackerCustomizer.modifiedService method. |
void |
open()
Open this ServiceTracker and begin tracking services. |
void |
open(boolean trackAllServices)
Open this ServiceTracker and begin tracking services. |
void |
remove(ServiceReference<S> reference)
Remove a service from this ServiceTracker . |
void |
removedService(ServiceReference<S> reference,
T service)
Default implementation of the ServiceTrackerCustomizer.removedService method. |
int |
size()
Return the number of services being tracked by this ServiceTracker . |
T |
waitForService(long timeout)
Wait for at least one service to be tracked by this ServiceTracker . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final BundleContext context
ServiceTracker
.
protected final Filter filter
ServiceTracker
which specifies the search
criteria for the services to track.
Constructor Detail |
---|
public ServiceTracker(BundleContext context, ServiceReference<S> reference, ServiceTrackerCustomizer<S,T> customizer)
ServiceTracker
on the specified ServiceReference
.
The service referenced by the specified ServiceReference
will be
tracked by this ServiceTracker
.
context
- The BundleContext
against which the tracking is
done.reference
- The ServiceReference
for the service to be
tracked.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.public ServiceTracker(BundleContext context, java.lang.String clazz, ServiceTrackerCustomizer<S,T> customizer)
ServiceTracker
on the specified class name.
Services registered under the specified class name will be tracked by
this ServiceTracker
.
context
- The BundleContext
against 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 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.public ServiceTracker(BundleContext context, Filter filter, ServiceTrackerCustomizer<S,T> customizer)
ServiceTracker
on the specified Filter
object.
Services which match the specified Filter
object will be tracked
by this ServiceTracker
.
context
- The BundleContext
against which the tracking is
done.filter
- The Filter
to select the services to be tracked.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.public ServiceTracker(BundleContext context, java.lang.Class<S> clazz, ServiceTrackerCustomizer<S,T> customizer)
ServiceTracker
on the specified class.
Services registered under the name of the specified class will be tracked
by this ServiceTracker
.
context
- The BundleContext
against 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 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.Method Detail |
---|
public void open()
ServiceTracker
and begin tracking services.
This implementation calls open(false)
.
java.lang.IllegalStateException
- If the BundleContext
with
which this ServiceTracker
was created is no longer valid.open(boolean)
public void open(boolean trackAllServices)
ServiceTracker
and begin tracking services.
Services which match the search criteria specified when this
ServiceTracker
was created are now tracked by this
ServiceTracker
.
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
.
java.lang.IllegalStateException
- If the BundleContext
with
which this ServiceTracker
was created is no longer valid.public void close()
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.
public T addingService(ServiceReference<S> reference)
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.
addingService
in interface ServiceTrackerCustomizer<S,T>
reference
- The reference to the service being added to this
ServiceTracker
.
ServiceTracker
.ServiceTrackerCustomizer.addingService(ServiceReference)
public void modifiedService(ServiceReference<S> reference, T service)
ServiceTrackerCustomizer.modifiedService
method.
This method is only called when this ServiceTracker
has been
constructed with a null ServiceTrackerCustomizer
argument.
This implementation does nothing.
modifiedService
in interface ServiceTrackerCustomizer<S,T>
reference
- The reference to modified service.service
- The service object for the modified service.ServiceTrackerCustomizer.modifiedService(ServiceReference, Object)
public void removedService(ServiceReference<S> reference, T service)
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.
removedService
in interface ServiceTrackerCustomizer<S,T>
reference
- The reference to removed service.service
- The service object for the removed service.ServiceTrackerCustomizer.removedService(ServiceReference, Object)
public T waitForService(long timeout) throws java.lang.InterruptedException
ServiceTracker
. This method will also return when this
ServiceTracker
is closed.
It is strongly recommended that waitForService
is not used during
the calling of the BundleActivator
methods.
BundleActivator
methods are expected to complete in a short
period of time.
This implementation calls getService()
to determine if a service
is being tracked.
timeout
- The time interval in milliseconds to wait. If zero, the
method will wait indefinitely.
getService()
.
java.lang.InterruptedException
- If another thread has interrupted the
current thread.
java.lang.IllegalArgumentException
- If the value of timeout is negative.public ServiceReference<S>[] getServiceReferences()
ServiceReference
s for all services being
tracked by this ServiceTracker
.
ServiceReference
s or null
if no services
are being tracked.public ServiceReference<S> getServiceReference()
ServiceReference
for one of the services being tracked
by this ServiceTracker
.
If multiple services are being tracked, the service with the highest
ranking (as specified in its service.ranking
property) is
returned. If there is a tie in ranking, the service with the lowest
service ID (as specified in its service.id
property); that is,
the service that was registered first is returned. This is the same
algorithm used by BundleContext.getServiceReference
.
This implementation calls getServiceReferences()
to get the list
of references for the tracked services.
ServiceReference
or null
if no services are
being tracked.public T getService(ServiceReference<S> reference)
ServiceReference
if
the specified referenced service is being tracked by this
ServiceTracker
.
reference
- The reference to the desired service.
null
if the service referenced by the
specified ServiceReference
is not being tracked.public java.lang.Object[] getServices()
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.
null
if no services are
being tracked.public T getService()
ServiceTracker
.
If any services are being tracked, this implementation returns the result
of calling getService(getServiceReference())
.
null
if no services are being
tracked.public void remove(ServiceReference<S> reference)
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.
reference
- The reference to the service to be removed.public int size()
ServiceTracker
.
public int getTrackingCount()
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.
ServiceTracker
or -1 if this
ServiceTracker
is not open.public java.util.SortedMap<ServiceReference<S>,T> getTracked()
SortedMap
of the ServiceReference
s 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.
SortedMap
with the ServiceReference
s and
service objects for all services being tracked by this
ServiceTracker
. If no services are being tracked, then
the returned map is empty.public boolean isEmpty()
ServiceTracker
is empty.
true
if this ServiceTracker
is not tracking any
services.public T[] getServices(T[] array)
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.
array
- An array into which the tracked service objects will be
stored, if the array is large enough.
null
. If the specified array is not large enough
to hold the result, a new array is created and returned.
|
OSGi™ Core Release 5 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |