Creating an OSGi bundle
Newcomers to OSGi may like a simple guide to developing an bundle. Peter Kriens has provided an extensive tutorial which is a must for anyone serious about learning OSGi. However, it has a long introduction and assumes you are happy to run Eclipse. So I thought I would provide a trival example that people could get going using only a Java SDK and their favourite text editor.
Here are instructions to create and run a trivial bundle, hopefully in about 10 minutes.
1. Download an OSGi framework (I used Equinox v3.2.1).
2. Paste the following code into the file org/foo/example/Example.java:
package org.foo.example;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Example implements BundleActivator {
public void start(BundleContext bc) {
System.out.println("Hello world");
}
public void stop(BundleContext bc) {
System.out.println("Goodbye world");
}
}
3. Compile the code:
> javac -cp org.eclipse.osgi_3.2.1.R32x_v20060919.jar org/foo/example/Example.java
4. Paste the following bundle manifest into the file MANIFEST.MF:
Make sure you have a carriage return and/or newline character at the end of the last line as this is required (see manifest specification in the JAR specification).
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.foo.example.Example
Bundle-Version: 1
Bundle-Activator: org.foo.example.Example
Import-Package: org.osgi.framework;version="1.3.0"
5. Package the manifest and code into a bundle:
> jar cvfm example.jar MANIFEST.MF org
Note that the order of the flags is important - the f and m options need to appear in the same order as the jar file and manifest file parameters.
6. Run the resultant bundle example.jar interactively using the Equinox console, e.g.:
> java -jar org.eclipse.osgi_3.2.1.R32x_v20060919.jar -console
osgi> install file:///osgi/blog/example/example.jar
Bundle id is 6
osgi> start 6
Hello world
osgi> stop 6
Goodbye world
osgi> close
(close exits the framework).
That's it! Anyone care to comment how long it took them?
10 comments:
This is good stuff, Glyn. This is the kind of thing that people need to be aware of to levarage OSGi outside of Eclipse. Keep up the good work.
Thanks for this good tutorial. We're using OSGi on the SimpleCenter project (http://www.simplecenter.org), and we're definitely finding people struggling with the OSGi learning curve. Hopefully tutorials like this will help.
Thanks for the feedback. Are you aware of Neil Bartlett's series of OSGi tutorials on EclipseZone? These should help the people you have in mind.
I heard of SimpleCenter via a recent blog,
but I'm glad you made direct contact. Always interesting to hear about projects adopting OSGi!
8 seconds :)
Nice.
A simple startup to the OSGi framework. Helped a lot and very nice.
HI,
Nice tutorial to go for the beginners.
I just wanted to ask one question. If I wanted to included third party libraty (for example jidc.jar ) to my bundle...how should I proceed ???
THanks
You can include a third party jar inside your bundle by adding the third party jar to the root directory of the bundle jar file and then adding a bundle classpath header to the bundle's manifest, e.g.:
Bundle-ClassPath: .;jidc.jar
The result is a bundle containing the nested third party jar.
Alternatively, you could convert the third party jar into a bundle with particular exported packages which your application bundle could then import. You could either add a manifest to the third party jar or you could wrap the third party jar in a bundle containing just a manifest and the nested third party jar. The latter approach avoids having to crack open the third party jar if this would invalidate the license.
Putting the third party jar in a separate bundle would allow you to control the packages available to your application from the third party jar so, for example, you could protect your application from being sensitive to changes in internal packages of the third party jar.
Well, Thanks for the valuable comment.
But still, say I have third party jars jetty-6.1.7.jar, servlet-api-2.5-6.1.7.jar and my application imports packages from these jars like org.mortbay.jetty,org.mortbay.jetty.servlet
So, I think the new menifest file would be like
Manifest-Version: 1.0
Bundle-Description:
Bundle-Name: jetty
Bundle-ClassPath: .;jetty-6.1.7.jar;servlet-api-2.5-6.1.7.jar;jetty-util-6.1.7.jar
Export-Package: org.mortbay.jetty,org.mortbay.jetty.servlet,
Bundle-ManifestVersion: 2
Bundle-Vendor: NTNU
Bundle-SymbolicName: jetty
Bundle-Version: 1.0.1
But the Question is how about
Bundle-Activator:..... ???
The manifest is approximately correct, but note that bundle classpath entries should be separated by commas (',') rather than semicolons (';').
The bundle activator would presumably have to start up Jetty, but I don't profess any Jetty expertise, so I'll say no more. Sorry!
Post a Comment