Java Reference
In-Depth Information
the topic examples, this makes sense to be sure you always start with a clean frame-
work instance. Next, you get the framework factory service and use it to create a
framework instance using the configuration map. To get the framework factory ser-
vice, you use the getFrameworkFactory() method introduced in listing 13.2. Finally,
you start the framework.
13.2.4
Installing the bundles
Now you have a configured and started framework instance. Because you configured the
framework to clean its bundle cache on first initialization, you know your framework has
no bundles installed in it. You need to remedy that. The following snippet shows how
to install the bundles contained in the directory specified on the command line:
...
BundleContext ctxt = fwk.getBundleContext();
for (int i = 0; i < jars.size(); i++) {
Bundle b = ctxt.installBundle(
((File) jars.get(i)).toURI().toString());
bundleList.add(b);
if (b.getHeaders().get("Main-Class") != null) {
mainBundle = b;
}
}
...
You first get the BundleContext object associated with the system bundle; this is possi-
ble because the Framework object extends Bundle and represents the system bundle.
You loop through the JAR files discovered in the specified directory and install them
using the system bundle context; any exceptions cause the launcher to fail. After you
install a bundle, you add it to the list of installed bundles and probe to see if its mani-
fest contains a Main-Class header, which you'll use later. If there's more than one
bundle with a Main-Class header, you use the last one you discover.
13.2.5
Starting the bundles
You've installed all of the bundles, but they aren't doing anything yet. You need to
start them. You can accomplish this in a simple loop over all installed bundles, invok-
ing start() on each one:
...
for (int i = 0; i < bundleList.size(); i++) {
if (!isFragment((Bundle) bundleList.get(i))) {
((Bundle) bundleList.get(i)).start();
}
}
...
You may wonder why you don't start each installed bundle right after installing it. It's
better to install and start bundles in two passes: one pass for installing and one pass
for starting. This approach helps alleviate ordering issues when it comes to depen-
dency resolution. If you install a bundle and start it immediately, it may fail to resolve
B
Starts nonfragment
bundles
 
Search WWH ::




Custom Search