The guitool in OSGi snippets is a plain Java application. In order to use OSGi, the OSGi framework must be started somehow. This is usually called "embedding the OSGi framework". In the OSGi snippets project the class OSGiFwLoader takes care of that.
The class OSGiFwLoader is used like this:
Eventually the framework needs to be stopped:
Usually bundles should be installed:
Implementation of OSGiFwLoader
The constructor does nothing, so the first interesting method is start():
The relevant lines are in black. After the fw.start() call, the OSGi framework is up and running. The configMap contains configuration data, I will come back to that later.
In the guitool there is a button labelled "Start Framework". When that button is pressed, among a few other things, OSGIFwLoader.start() is called. After the framework has been started, the list of bundles has one entry:
As the text reads, it is the "System Bundle" which is the framework itself. An overview can be found here http://wiki.osgi.org/wiki/System_Bundle. The bundle id is 0 (zero) and the bundle symbolic name is "org.apache.felix,framework". The symbolic name of the system bundle is implementation dependent. Apache Felix is used here, hence the name.
Things are rather boring without other bundles, so the OSGi loader should be able to install bundles:
The relevant method call is BundleContext.installBundle() here. It takes the location of the bundle file as a parameter and returns a reference to the bundle instance.
If needed, the returned bundle reference is used to start the bundle later. So its time to look at this method:
More methods exist to stop and uninstall a bundle.
Eventually the framework should be stopped somehow. The first step is to tell the framework, that it should stop and the second step is to wait for the framework to have stopped.
The stop method returns immediately and the framework does the actual work in other threads. To wait for the framework to have stopped, the loader class offers the following method:
The two methods are basically wrappers around the Framework's methods to catch Exceptions. Since the Exceptions are only logged and the caller of the OSGiFwLoader's methods wouldn't know, if any were thrown, the implementation is by far not production ready. So if you use the code, you need to change the error handling.
Going back to starting the framework, there was configuration data involved. OWGiFwLoader.start() calls this method:
When embedding an OSGi framework into an application, that application usually provides services to bundles or uses services provided by bundles. The packages of these services must be accessible and this is accomplished with the entry Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA. In the OSGi snippets project, the slf4j logging library is used and is in the classpath of the application. The package org.slf4j is added to the configuration entry, so it is available to bundles.
The entry FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT makes sure, that the storage maintained by the OSGi implementation is cleaned each time it is run. Usually this is not needed, but for something which is used for playing around, I prefer to clean up frequently.
- Apache Felix: http://felix.apache.org/
Article about embedding at Apache Felix site:
Apache Felix Framework Launching and Embedding
Source code of OSGiFwLoader is at the project site: