Osgi – the first program
Here is the simple Osgi application with Equinox framework and I use Maven as build tool. The structure of this project is described in follow:
ows-osgi-tutorial
--src
|--main
|--java (contains all java source files)
|--resources
|--META-INF
|--MANIFEST.MF
The manifest file has content:
Manifest-Version: 1.0
Bundle-Name: Helloworld
Bundle-SymbolicName: Helloworld
Bundle-Version: 1.0.0
Bundle-Description: Hello world sample bundle
Bundle-Vendor: NextSS company
Bundle-Activator: ows.osgi.sample.Activator
Bundle-Category: Tutorial
Import-Package: org.osgi.framework
The POM file has content:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="<a href="http://maven.apache.org/POM/4.0.0">http://maven.apache.org/POM/4.0.0"</a> xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance"</a> xsi:schemaLocation="<a href="http://maven.apache.org/POM/4.0.0">http://maven.apache.org/POM/4.0.0</a> <a href="http://maven.apache.org/maven-v4_0_0.xsd">http://maven.apache.org/maven-v4_0_0.xsd"</a>> <modelVersion>4.0.0</modelVersion> <groupId>ows</groupId> <artifactId>ows-osgi-tutorial</artifactId> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.eclipse</groupId> <artifactId>osgi</artifactId> <version>3.2.1.R32x_v20060919</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <!--Maven generate the default manifest file, these statements in --> <!--plugin section will force Maven use your own manifest file--> <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile> </archive> </configuration> </plugin> </plugins> </build> </project>
And we write the simple Activator class for Helloworld bundle:
package ows.osgi.sample.Activator; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; public class Activator implements BundleActivator { public void start(BundleContext context) { System.out.println("Start helloworld service"); } public void stop(BundleContext context) { System.out.println("Stop hello world service"); } }
Open the console and type mvn package, you will have the jar file ows-osgi-tutorial-0.0.1.jar and it is done. You can deploy this bundle to any osgi-compliant framework as felix or equinox. I have tested on equinox by following commands:
- Start equinox: follow equinox guideline.
- Install the bundle: %osgi-console%> install <path of ows-osgi-tutorial-0.0.1.jar >
- Check whether the bundle install: %osgi-console%> ss (you will get the bundle of installed bundle by this command too).
- Start bundle: %osgi-console%>start <bundle id>
It is simple, right? It's time for you to create the more complicated tutorial, just modify the manifest file and Activator class. The build system is ready for use without any change.