OSGi : Lab Example

From CDOT Wiki
Revision as of 22:27, 3 February 2011 by Jordan.anastasiade (talk | contribs) (second bundle - provider)
Jump to: navigation, search

Example of OSGi service system

Let us suppose that you were asked to implement a voting system as an OSGi service system.

The system should be generic enough so that it could be used for any election. Therefore you will not be using specific candidate names, as they can change for different elections.

As a proof of concept, you will be given three candidates and as the election takes place you have to print the number of votes for each candidate.

Design and implementation

1. Define the Service

Create the bundle cs.ecl.osgi.voting_1.0.1.qualifier

1.1 Create the VotingSystem.java
package cs.ecl.osgi.voting;

public interface VotingSystem {
	int[] countVotesFor(String code);
}
1.2 Define the MANIFEST.MF for the service
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Voting System
Bundle-SymbolicName: cs.ecl.osgi.voting
Bundle-Version: 1.0.1.qualifier
Bundle-Vendor: Seneca College - Eclipse Course
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: cs.ecl.osgi.voting

Note: The bundle does not need an activator.


2. Define the Service Provider

Create the bundle cs.ecl.osgi.votingsystem.provider_1.0.0.qualifier

2.1 Create the VotingActivator.java
package cs.ecl.osgi.votingsystem.provider;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import cs.ecl.osgi.voting.VotingSystem;
import cs.ecl.osgi.votingsystem.provider.internals.SimpleVotingSystem;

public class VotingActivator implements BundleActivator {

	private static BundleContext context;

	static BundleContext getContext() {
		return context;
	}
	
	public void start(BundleContext bundleContext) throws Exception {
		VotingActivator.context = bundleContext;
		
		VotingSystem vs = new SimpleVotingSystem();
		context.registerService(VotingSystem.class.getName(), vs, null);
		System.out.println("Voting System Registered !");		
	}

	public void stop(BundleContext bundleContext) throws Exception {
		VotingActivator.context = null;
		System.out.println("Voting System Stopped !");
	}
}
2.1 Create the SimpleVotingSystem.java

This is the internal implementation of the service and should be done in a packet that will not be exported.

package cs.ecl.osgi.votingsystem.provider.internals;

import cs.ecl.osgi.voting.VotingSystem;

public class SimpleVotingSystem implements VotingSystem {

	public static int[] votes = { 0, 0, 0, 0 }; // the test for 3 candidates +
								      // the invalid vote

	public int[] countVotesFor(String code) {

		// dirty and trivial implementation of a voting system
		String[] votCodes = { "default", "yesno", "yeahnah", "ync" };
		for (int i = 0; i < votes.length; i++) {
			if (code.equals(votCodes[i]))
				++votes[i];
		}
		return votes;
	}

}
2.3 Define the MANIFEST.MF for the service provider
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Voting System Provider
Bundle-SymbolicName: cs.ecl.osgi.votingsystem.provider
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: cs.ecl.osgi.votingsystem.provider.VotingActivator
Bundle-Vendor: Seneca College - Eclipse Course
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: cs.ecl.osgi.voting,
   org.osgi.framework;version="1.3.0"