Difference between revisions of "OSGi : Lab Example"

From CDOT Wiki
Jump to: navigation, search
(First bundle)
 
(second bundle - provider)
Line 11: Line 11:
 
== Design and implementation ==
 
== Design and implementation ==
  
<h4> 1. Define the Bundle Service Interface</h4>
+
<h4> 1. Define the Service </h4>
 
Create the bundle ''cs.ecl.osgi.voting_1.0.1.qualifier''
 
Create the bundle ''cs.ecl.osgi.voting_1.0.1.qualifier''
  
Line 23: Line 23:
 
</source>
 
</source>
  
<h6> 1.2 Define the MANIFEST.MF  for the bundle interface </h6>
+
<h6> 1.2 Define the MANIFEST.MF  for the service </h6>
  
 
<source lang="xml">
 
<source lang="xml">
Line 37: Line 37:
  
 
Note: The bundle does not need an activator.
 
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 VotingActivator.java </h6>
 +
<source lang="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 !");
 +
}
 +
}
 +
</source>
 +
 +
<h6> 2.1 Create the SimpleVotingSystem.java </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;
 +
 +
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;
 +
}
 +
 +
}
 +
</source>
 +
 +
<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
 +
Import-Package: cs.ecl.osgi.voting,
 +
  org.osgi.framework;version="1.3.0"
 +
</source>
 +
 +
-----

Revision as of 22:27, 3 February 2011

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"