...
Note that we do not need to unget or release the service in the stop()
method, because the OSGi framework will automatically do so for us. We must create a manifest.mf
file that contains the meta-data for our bundle; the manifest file contains the following:
Code Block |
---|
Bundle-Activator: tutorial.example3.Activator Import-Package: tutorial.example2.service Bundle-Name: Dictionary client Bundle-Description: A bundle that uses the dictionary service if it finds it at startup Bundle-Vendor: Apache Felix Bundle-Version: 1.0.0 Bundle-Activator: tutorial.example3.Activator Import-Package: org.osgi.framework, tutorial.example2.service |
We specify the bundle's activator class via the Bundle-Activator
attribute and also specify that our class imports the shared OSGi core framework package of and the dictionary service interface using package via the Import-Package
attribute. This import clause The dictionary service interface package will be fulfilled by the bundle in Example 2, which exports the this package that this bundle is importing. The OSGi framework will automatically handle the details of resolving import packages. (Note: Make sure your manifest file ends in a trailing carriage return or else the last line will be ignored.)
To compile our source code, we need to have the felix.jar
file (found in Felix' bin
directory) and the example2.jar
file in our class path. We compile the source file using a command like:
...
To run Felix, we follow the instructions described in usage.html. When we start Felix, it asks for a profile name, we will put all of our bundles in a profile named tutorial
. After running Felix, we should check that all tutorial bundles are stopped, except for the English dictionary service bundle from Example 2. We can use the Felix ps
lb
shell command to get a list of all bundles, their state, and their bundle identifier number. If the Example 2 bundle is not active, we should start the bundle using the start
command along with the bundle's identifier number displayed by the ps
lb
command and stop
any other unneeded tutorial bundles using the stop
command. (Note: Felix uses some bundles to provide its command shell, so do not stop these bundles.) Now we can install and start our dictionary client bundle. Assuming that we created our bundle in the directory c:\tutorial
, we can install and start it in Felix' shell using the following command:
...
The above command installs and starts the bundle in a single step; it is also possible to install and start the bundle in two steps by using the Felix install
and start shell commands. When we start the bundle, it will use the shell thread to prompt us for words. Enter one word at a time to check the words and enter a blank line to stop checking words. To restart the bundle, we must use the Felix shell ps
lb
command to get the bundle identifier number for the bundle and first use the stop
command to stop the bundle, then the start
command to restart it. To test the dictionary service, enter any of the words in the dictionary (e.g., "welcome", "to", "the", "OSGi", "tutorial") or any word not in the dictionary.
...