Java Reference
In-Depth Information
}
public synchronized Bundle[] getBundles() {
return (Bundle[]) m_bundleSet.toArray(
new Bundle[m_bundleSet.size()]);
}
protected abstract void addedBundle(Bundle bundle);
protected abstract void removedBundle(Bundle bundle);
To s t a r t a
BundleTracker
instance tracking bundles, you must invoke its
open()
method. This methods registers a bundle event listener and processes any existing
ACTIVE
bundles by adding them to its bundle list and invoking the abstract
added-
Bundle()
method. The
getBundles()
method provides access to the current list of
active bundles being tracked. Because
BundleTracker
is abstract, subclasses must pro-
vide implementations of
addedBundle()
and
removedBundle()
to perform custom
processing of added and removed bundles, respectively.
The last portion of the
BundleTracker
is as follows.
Listing 3.16 Disposing of a
BundleTracker
public synchronized void close() {
if (m_open) {
m_open = false;
m_context.removeBundleListener(m_listener);
Bundle[] bundles = (Bundle[])
m_bundleSet.toArray(new Bundle[m_bundleSet.size()]);
for (int i = 0; i < bundles.length; i++) {
if (m_bundleSet.remove(bundles[i])) {
removedBundle(bundles[i]);
}
}
}
}
}
Calling
BundleTracker.close()
stops it from tracking bundles. This removes its bun-
dle listener, removes each currently tracked bundle from its bundle list, and invokes
the abstract
removedBundle()
method.
Standardizing bundle trackers
Tracking bundles is a useful building block. It's so useful that the OSGi Alliance de-
cided to create a standard
BundleTracker
for the R4.2 specification. The R4.2
BundleTracker
is more complicated than the one presented here, but it follows the
same basic principles; we'll discuss it in chapter 15.
Now that you know how the
BundleTracker
works, let's return to its subclass,
Shape-
Tracker
. The heart of this subclass is the
processBundle()
method shown next, which
processes added and removed bundles.