Java Reference
In-Depth Information
fwk = getFrameworkFactory().newFramework(m);
fwk.start();
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);
}
for (int i = 0; i < bundleList.size(); i++) {
((Bundle) bundleList.get(i)).start();
}
} catch (Exception ex) {
System.err.println("Error starting framework: " + ex);
ex.printStackTrace();
System.exit(0);
}
C Starts all
installed bundles
return fwk;
}
...
As with the generic launcher, you configure the framework to clean its bundle cache
on first initialization. For performance reasons, you probably wouldn't want to do this
if you were using the framework as a plugin mechanism, because it's slower to repopu-
late the cache every time. You do it in this case to make sure you're starting from a
clean slate. An important difference from the launcher, which we alluded to previ-
ously, is at B . Here you configure the framework to export the org.foo.shape pack-
age from the class path via the system bundle. This allows bundles to import the
package from the application, thus ensuring that they're both using the same inter-
face definition for shape implementations. You also need to ensure that this package
is on the class path; but because you're going to package it in the application JAR file,
it should definitely be available.
Next, you create the framework with the defined configuration and start it. You get
the system bundle's bundle context, which you use to install the discovered bundles.
Finally, you start all installed bundles C . Any errors cause the JVM to exit.
Now let's look at how you publish an external service into the framework instance.
PUBLISHING AN EXTERNAL SERVICE
The publishTrapezoidService() method is simple, as the following code snippet
illustrates:
...
private static void publishTrapezoidService() {
Hashtable dict = new Hashtable();
dict.put(SimpleShape.NAME_PROPERTY, "Trapezoid");
dict.put(SimpleShape.ICON_PROPERTY,
new ImageIcon(Trapezoid.class.getResource("trapezoid.png")));
fwk.getBundleContext().registerService(
SimpleShape.class.getName(), new Trapezoid(), dict);
}
...
Search WWH ::




Custom Search