Class BundleTracker<T>
- java.lang.Object
-
- org.osgi.util.tracker.BundleTracker<T>
-
- Type Parameters:
T
- The type of the tracked object.
- All Implemented Interfaces:
BundleTrackerCustomizer<T>
public class BundleTracker<T> extends java.lang.Object implements BundleTrackerCustomizer<T>
TheBundleTracker
class simplifies tracking bundles much like theServiceTracker
simplifies tracking services.A
BundleTracker
is constructed with state criteria and aBundleTrackerCustomizer
object. ABundleTracker
can use theBundleTrackerCustomizer
to select which bundles are tracked and to create a customized object to be tracked with the bundle. TheBundleTracker
can then be opened to begin tracking all bundles whose state matches the specified state criteria.The
getBundles
method can be called to get theBundle
objects of the bundles being tracked. ThegetObject
method can be called to get the customized object for a tracked bundle.The
BundleTracker
class is thread-safe. It does not call aBundleTrackerCustomizer
while holding any locks.BundleTrackerCustomizer
implementations must also be thread-safe.- Since:
- 1.4
- Author:
- $Id: 9a1b57568a8be735292700584f4eb936fc80f2ff $
-
-
Field Summary
Fields Modifier and Type Field Description protected BundleContext
context
The Bundle Context used by thisBundleTracker
.
-
Constructor Summary
Constructors Constructor Description BundleTracker(BundleContext context, int stateMask, BundleTrackerCustomizer<T> customizer)
Create aBundleTracker
for bundles whose state is present in the specified state mask.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description T
addingBundle(Bundle bundle, BundleEvent event)
Default implementation of theBundleTrackerCustomizer.addingBundle
method.void
close()
Close thisBundleTracker
.Bundle[]
getBundles()
Return an array ofBundle
s for all bundles being tracked by thisBundleTracker
.T
getObject(Bundle bundle)
Returns the customized object for the specifiedBundle
if the specified bundle is being tracked by thisBundleTracker
.java.util.Map<Bundle,T>
getTracked()
Return aMap
with theBundle
s and customized objects for all bundles being tracked by thisBundleTracker
.int
getTrackingCount()
Returns the tracking count for thisBundleTracker
.boolean
isEmpty()
Return if thisBundleTracker
is empty.void
modifiedBundle(Bundle bundle, BundleEvent event, T object)
Default implementation of theBundleTrackerCustomizer.modifiedBundle
method.void
open()
Open thisBundleTracker
and begin tracking bundles.void
remove(Bundle bundle)
Remove a bundle from thisBundleTracker
.void
removedBundle(Bundle bundle, BundleEvent event, T object)
Default implementation of theBundleTrackerCustomizer.removedBundle
method.int
size()
Return the number of bundles being tracked by thisBundleTracker
.
-
-
-
Field Detail
-
context
protected final BundleContext context
The Bundle Context used by thisBundleTracker
.
-
-
Constructor Detail
-
BundleTracker
public BundleTracker(BundleContext context, int stateMask, BundleTrackerCustomizer<T> customizer)
Create aBundleTracker
for bundles whose state is present in the specified state mask.Bundles whose state is present on the specified state mask will be tracked by this
BundleTracker
.- Parameters:
context
- TheBundleContext
against which the tracking is done.stateMask
- The bit mask of theOR
ing of the bundle states to be tracked.customizer
- The customizer object to call when bundles are added, modified, or removed in thisBundleTracker
. If customizer isnull
, then thisBundleTracker
will be used as theBundleTrackerCustomizer
and thisBundleTracker
will call theBundleTrackerCustomizer
methods on itself.- See Also:
Bundle.getState()
-
-
Method Detail
-
open
public void open()
Open thisBundleTracker
and begin tracking bundles.Bundle which match the state criteria specified when this
BundleTracker
was created are now tracked by thisBundleTracker
.- Throws:
java.lang.IllegalStateException
- If theBundleContext
with which thisBundleTracker
was created is no longer valid.java.lang.SecurityException
- If the caller and this class do not have the appropriateAdminPermission[context bundle,LISTENER]
, and the Java Runtime Environment supports permissions.
-
close
public void close()
Close thisBundleTracker
.This method should be called when this
BundleTracker
should end the tracking of bundles.This implementation calls
getBundles()
to get the list of tracked bundles to remove.
-
addingBundle
public T addingBundle(Bundle bundle, BundleEvent event)
Default implementation of theBundleTrackerCustomizer.addingBundle
method.This method is only called when this
BundleTracker
has been constructed with anull BundleTrackerCustomizer
argument.This implementation simply returns the specified
Bundle
.This method can be overridden in a subclass to customize the object to be tracked for the bundle being added.
- Specified by:
addingBundle
in interfaceBundleTrackerCustomizer<T>
- Parameters:
bundle
- TheBundle
being added to thisBundleTracker
object.event
- The bundle event which caused this customizer method to be called ornull
if there is no bundle event associated with the call to this method.- Returns:
- The specified bundle.
- See Also:
BundleTrackerCustomizer.addingBundle(Bundle, BundleEvent)
-
modifiedBundle
public void modifiedBundle(Bundle bundle, BundleEvent event, T object)
Default implementation of theBundleTrackerCustomizer.modifiedBundle
method.This method is only called when this
BundleTracker
has been constructed with anull BundleTrackerCustomizer
argument.This implementation does nothing.
- Specified by:
modifiedBundle
in interfaceBundleTrackerCustomizer<T>
- Parameters:
bundle
- TheBundle
whose state has been modified.event
- The bundle event which caused this customizer method to be called ornull
if there is no bundle event associated with the call to this method.object
- The customized object for the specified Bundle.- See Also:
BundleTrackerCustomizer.modifiedBundle(Bundle, BundleEvent, Object)
-
removedBundle
public void removedBundle(Bundle bundle, BundleEvent event, T object)
Default implementation of theBundleTrackerCustomizer.removedBundle
method.This method is only called when this
BundleTracker
has been constructed with anull BundleTrackerCustomizer
argument.This implementation does nothing.
- Specified by:
removedBundle
in interfaceBundleTrackerCustomizer<T>
- Parameters:
bundle
- TheBundle
being removed.event
- The bundle event which caused this customizer method to be called ornull
if there is no bundle event associated with the call to this method.object
- The customized object for the specified bundle.- See Also:
BundleTrackerCustomizer.removedBundle(Bundle, BundleEvent, Object)
-
getBundles
public Bundle[] getBundles()
Return an array ofBundle
s for all bundles being tracked by thisBundleTracker
.- Returns:
- An array of
Bundle
s ornull
if no bundles are being tracked.
-
getObject
public T getObject(Bundle bundle)
Returns the customized object for the specifiedBundle
if the specified bundle is being tracked by thisBundleTracker
.- Parameters:
bundle
- TheBundle
being tracked.- Returns:
- The customized object for the specified
Bundle
ornull
if the specifiedBundle
is not being tracked.
-
remove
public void remove(Bundle bundle)
Remove a bundle from thisBundleTracker
. The specified bundle will be removed from thisBundleTracker
. If the specified bundle was being tracked then theBundleTrackerCustomizer.removedBundle
method will be called for that bundle.- Parameters:
bundle
- TheBundle
to be removed.
-
size
public int size()
Return the number of bundles being tracked by thisBundleTracker
.- Returns:
- The number of bundles being tracked.
-
getTrackingCount
public int getTrackingCount()
Returns the tracking count for thisBundleTracker
. The tracking count is initialized to 0 when thisBundleTracker
is opened. Every time a bundle is added, modified or removed from thisBundleTracker
the tracking count is incremented.The tracking count can be used to determine if this
BundleTracker
has added, modified or removed a bundle by comparing a tracking count value previously collected with the current tracking count value. If the value has not changed, then no bundle has been added, modified or removed from thisBundleTracker
since the previous tracking count was collected.- Returns:
- The tracking count for this
BundleTracker
or -1 if thisBundleTracker
is not open.
-
getTracked
public java.util.Map<Bundle,T> getTracked()
Return aMap
with theBundle
s and customized objects for all bundles being tracked by thisBundleTracker
.- Returns:
- A
Map
with theBundle
s and customized objects for all services being tracked by thisBundleTracker
. If no bundles are being tracked, then the returned map is empty. - Since:
- 1.5
-
isEmpty
public boolean isEmpty()
Return if thisBundleTracker
is empty.- Returns:
true
if thisBundleTracker
is not tracking any bundles.- Since:
- 1.5
-
-