Java Reference
In-Depth Information
void addBundleListener(BundleListener listener);
void removeBundleListener(BundleListener listener);
void addFrameworkListener(FrameworkListener listener);
void removeFrameworkListener(FrameworkListener listener);
...
}
We'll cover most of these methods in this chapter. The second category of
Bundle-
Context
methods related to services will be covered in the next chapter.
Unique context
One important aspect of the bundle context object is its role as the unique execution
context of its associated bundle. Because it represents the execution context, it's
only valid while the associated bundle is active, which is explicitly from the moment
the activator
start()
method is invoked until the activator
stop()
method com-
pletes and the entire time in between. Most bundle context methods throw an excep-
tion if used when the associated bundle isn't active. It's a unique execution context
because each activated bundle receives its own context object. The framework uses
this context for security and resource allocation purposes for each individual bundle.
Given this capability of
BundleContext
objects, they should be treated as sensitive
or private objects and not passed freely among bundles.
The shell activator in listing 3.1 uses the bundle context to get its configuration prop-
erty values
C
. It also passes the context into the telnet binding
D
, which client con-
nections will use to interact with the running framework. Finally, it uses the context to
obtain the bundle's
Bundle
object to access the identification information. We'll look
at these details shortly, but for now we'll continue the top-down description by looking
at the final lifecycle layer interface:
org.osgi.framework.Bundle
.
BUNDLE
For each installed bundle, the framework creates a
Bundle
object to logically repre-
sent it. The
Bundle
interface defines the
API
to manage an installed bundle's lifecycle;
a portion of the interface is presented in the following listing. As we discuss the
Bundle
interface, you'll see that most lifecycle operations have a corresponding
method in it.
Listing 3.3
Bundle
interface methods related to lifecycle management
public interface Bundle {
...
BundleContext getBundleContext();
long getBundleId();
Dictionary getHeaders();
Dictionary getHeaders(String locale);
String getLocation();
int getState();
String getSymbolicName();