Jump to: navigation, search

OSGi : Lab Example

2,266 bytes added, 22:27, 3 February 2011
second bundle - provider
== Design and implementation ==
<h4> 1. Define the Bundle Service Interface</h4>
Create the bundle ''cs.ecl.osgi.voting_1.0.1.qualifier''
<h6> 1.2 Define the MANIFEST.MF for the bundle interface service </h6>
<source lang="xml">
Note: The bundle does not need an activator.
<h4> 2. Define the Service Provider</h4>
Create the bundle ''cs.ecl.osgi.votingsystem.provider_1.0.0.qualifier''
<h6> 2.1 Create the </h6>
<source lang="java">
package cs.ecl.osgi.votingsystem.provider;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
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 !");
<h6> 2.1 Create the </h6>
This is the internal implementation of the service and should be done in a packet that will not be exported.
<source lang="java">
package cs.ecl.osgi.votingsystem.provider.internals;
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]))
return votes;
<h6> 2.3 Define the MANIFEST.MF for the service provider</h6>
<source lang="xml">
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

Navigation menu