Wednesday, February 21, 2007
Tuesday, February 06, 2007
Knopflerfish OSGi tutorial
Thanks to some Australian OSGi users for providing a nice link to a Knopflerfish OSGi tutorial.
My example of how to create an OSGi bundle in under 10 minutes still takes some beating for simplicity (or triviality, if you prefer), but I do believe in giving beginners a really easy way in. If they're smart, they'll soon catch on.
Posted by Glyn at 1:09 PM 7 comments Links to this post
Friday, January 05, 2007
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?
Posted by Glyn at 1:22 PM 10 comments Links to this post