Changes

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''
</source>
<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 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>
 
-----

Navigation menu