The "One Laptop Per Child" project has a great device ready to ship, but there's no Java on there. Let's think about working together to put Java on OLPC!
Welcome back to the EclipseZone OSGi tutorial series.
Last time we looked at a simple Hello World bundle that printed a message when starting and stopping. It did that by implementing the
BundleActivator
interface and providing
start
and
stop
methods. Take another look at the code now, in particular the method signature of
start
and
stop
, and you'll notice that we were passed a parameter, the
BundleContext
. In this installment of the tutorial we will be looking at
BundleContext
and what we can do with it.
BundleContext
is a magic ticket that the OSGi framework passes to our bundle. When code needs to interact with the framework in any way, you will use the
BundleContext
. In fact this is the
only
way to interact with the OSGi API, and the framework issues one of these tickets to each bundle through its
BundleActivator
when the bundle is started.
If you still have Equinox running from last time then you don't need to restart it. If it's not running, then remember the command to start it is
> java -jar equinox.jar -console
Type
ss
at the prompt and you should see that the Hello World bundle from last time is still installed. That's the case even if you have shut down and restarted Equinox since then, because the OSGi framework persists its state between runs.
For this exercise we will write a bundle that searches out and uninstalls Hello World. We could do this easily from the console using the
uninstall
command, but we want to see how it can be done programmatically using the OSGi API. So, create a new file called
HelloWorldKiller.java
and copy in the following code:
Back at the OSGi console, install the new bundle using
install file:HelloWorldKiller.jar
, and then type
ss
. The status listing should now look like this:
id State Bundle
0 ACTIVE system.bundle_3.2.1.R32x_v20060919
1 ACTIVE HelloWorld_1.0.0
2 INSTALLED HelloWorldKiller_1.0.0
Let's run the Hello World Killer by typing
start 2
. You should see the following output:
HelloWorldKiller searching...
Hello World found, destroying!
Goodbye EclipseZone Readers!
Notice that the last line of output comes from our original Hello World bundle. Because it was in the active state before we ran the Killer, it had to be stopped before being uninstalled, and that caused the
stop
method of its
BundleActivator
to run.
Taking another look at the output of
ss
, Hello World has disappeared:
id State Bundle
0 ACTIVE system.bundle_3.2.1.R32x_v20060919
2 ACTIVE HelloWorldKiller_1.0.0
You might wonder if there is a security problem here. It appears that any bundle can uninstall any other bundle! Fortunately OSGi has a comprehensive security layer which gives fine-grained control over all interaction with the framework, so for example you could limit the right to uninstall bundles to a particular "management" bundle. However, getting security working is mostly a configuration issue, and in this series we're going to focus on the code.
That's it for this installment. Until next time, why not take a look at the
BundleContext
interface and see what else you can do with it? For example, you could try programmatically installing a new bundle using the
installBundle
method. Or you could get a list of all the currently installed bundles and print out the time and date they were last modified. To help you get started, check out the
Javadocs for the OSGi Release 4 APIs
.
Re: Getting started with OSGi: Interacting with the Framework
You might also like to try using
stop
instead of
uninstall
for a
HelloWorldStopper
. That way, you can go into the console, and when you start the
HelloWorldStopper
bundle, you'll cause the
HelloWorld
bundle to stop. It's easier to then restart and try again than installing the bundle each time ...
Re: Getting started with OSGi: Interacting with the Framework
Hi !
You say that security to prevent a bundle to interact with another bundle's lifecycle event is just a matter of configuration.
But I'm fighting with google, policy files and Javadoc to try to prevent my helloWorldRemoteController to actually stop the HelloWorld bundle, with no success.
Do you have a tutorial or a quick explanation about this, either using policy files or API (PermissionInfo/AdminPermission)?
Getting started with OSGi: Interacting with the Framework
At 9:05 AM on Feb 8, 2007, Neil Bartlett
wrote:
Last time we looked at a simple Hello World bundle that printed a message when starting and stopping. It did that by implementing the
BundleActivatorinterface and providingstartandstopmethods. Take another look at the code now, in particular the method signature ofstartandstop, and you'll notice that we were passed a parameter, theBundleContext. In this installment of the tutorial we will be looking atBundleContextand what we can do with it.BundleContextis a magic ticket that the OSGi framework passes to our bundle. When code needs to interact with the framework in any way, you will use theBundleContext. In fact this is the only way to interact with the OSGi API, and the framework issues one of these tickets to each bundle through itsBundleActivatorwhen the bundle is started.If you still have Equinox running from last time then you don't need to restart it. If it's not running, then remember the command to start it is
Type
ssat the prompt and you should see that the Hello World bundle from last time is still installed. That's the case even if you have shut down and restarted Equinox since then, because the OSGi framework persists its state between runs.For this exercise we will write a bundle that searches out and uninstalls Hello World. We could do this easily from the console using the
uninstallcommand, but we want to see how it can be done programmatically using the OSGi API. So, create a new file calledHelloWorldKiller.javaand copy in the following code:import org.osgi.framework.*; public class HelloWorldKiller implements BundleActivator { public void start(BundleContext context) { System.out.println("HelloWorldKiller searching..."); Bundle[] bundles = context.getBundles(); for(int i=0; i<bundles.length; i++) { if("HelloWorld".equals(bundles[i] .getSymbolicName())) { try { System.out.println("Hello World found, " + "destroying!"); bundles[i].uninstall(); return; } catch (BundleException e) { System.err.println("Failed: " + e.getMessage()); } } } System.out.println("Hello World bundle not found"); } public void stop(BundleContext context) { System.out.println("HelloWorldKiller shutting down"); } }Now create the manifest. Again, remember the blank line at the end is very important. Copy the following into HelloWorldKiller.mf:
Now compile and build the Jar:
Back at the OSGi console, install the new bundle using
install file:HelloWorldKiller.jar, and then typess. The status listing should now look like this:Let's run the Hello World Killer by typing
start 2. You should see the following output:Notice that the last line of output comes from our original Hello World bundle. Because it was in the active state before we ran the Killer, it had to be stopped before being uninstalled, and that caused the
stopmethod of itsBundleActivatorto run.Taking another look at the output of
ss, Hello World has disappeared:You might wonder if there is a security problem here. It appears that any bundle can uninstall any other bundle! Fortunately OSGi has a comprehensive security layer which gives fine-grained control over all interaction with the framework, so for example you could limit the right to uninstall bundles to a particular "management" bundle. However, getting security working is mostly a configuration issue, and in this series we're going to focus on the code.
That's it for this installment. Until next time, why not take a look at the
BundleContextinterface and see what else you can do with it? For example, you could try programmatically installing a new bundle using theinstallBundlemethod. Or you could get a list of all the currently installed bundles and print out the time and date they were last modified. To help you get started, check out the Javadocs for the OSGi Release 4 APIs .2 replies so far (
Post your own)
Re: Getting started with OSGi: Interacting with the Framework
You might also like to try usingstopinstead ofuninstallfor aHelloWorldStopper. That way, you can go into the console, and when you start theHelloWorldStopperbundle, you'll cause theHelloWorldbundle to stop. It's easier to then restart and try again than installing the bundle each time ...Alex.
Re: Getting started with OSGi: Interacting with the Framework
Hi !You say that security to prevent a bundle to interact with another bundle's lifecycle event is just a matter of configuration.
But I'm fighting with google, policy files and Javadoc to try to prevent my helloWorldRemoteController to actually stop the HelloWorld bundle, with no success.
Do you have a tutorial or a quick explanation about this, either using policy files or API (PermissionInfo/AdminPermission)?
I'd be grateful for this.
Btw, thanks for the great work you did there.