Open main menu

CDOT Wiki β

Changes

Sudoku Generator (Example for the OSGi Lab)

2,579 bytes added, 20:52, 13 February 2011
Design and Implementation
First, let me say that I followed Jordan's Voting System example, for both the code as well as the writeup on this wiki.
<h4> 1. Define the Service </h4>
Create the bundle ''ca.on.senecac.scs.sudoku''
<h6> 1.1 Create SudokuGenerator.java </h6>
<source lang="java">
</source>
<h6> 1.2 Define the MANIFEST.MF for the service </h6>
<source lang="xml">
</source>
<h4> 2. Define the Service Provider </h4>
Create the bundle ''ca.on.senecac.scs.sudoku.provider''
<h6> 2.1 Create SudokuActivator.java </h6>
<source lang="java">
</source>
<h6> 2.2 Create SimpleSudokuGenerator.java </h6>
<source lang="java">
</source>
<h6> 2.3 Create Grid.java </h6>
<source lang="java">
}
</source>
 
<h6> Define the MANIFEST.MF </h6>
 
<source lang="xml">
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Provider
Bundle-SymbolicName: ca.on.senecac.scs.sudoku.provider
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: ca.on.senecac.scs.sudoku.provider.SudokuActivator
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: ca.on.senecac.scs.sudoku,
org.osgi.framework;version="1.3.0"
</source>
 
<h4> Define the Service Consumer </h4>
 
Create the bundle ''ca.on.senecac.scs.sudoku.client''
 
<h6> Create ClientActivator.java </h6>
 
<source lang="java">
package ca.on.senecac.scs.sudoku.client;
 
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
 
import ca.on.senecac.scs.sudoku.*;
 
/**
* Sudoku Generator client starts computing the Sudoku grid
* with a call to generate(0,0)
*
* @author John Selmys
*
*/
public class ClientActivator implements BundleActivator {
 
private static BundleContext context;
private SudokuGenerator sg;
static BundleContext getContext() {
return context;
}
 
public void start(BundleContext bundleContext) throws Exception {
ClientActivator.context = bundleContext;
ServiceReference reference = context
.getServiceReference(SudokuGenerator.class.getName());
if (reference != null) {
sg = (SudokuGenerator) context.getService(reference);
if (sg != null) {
sg.generate(0,0);
context.ungetService(reference);
} else
System.err.println("The Sudoku Generator cannot be used !!!");
} else
System.err.println("The Sudoku Generator cannot be found !!!");
}
 
public void stop(BundleContext bundleContext) throws Exception {
ClientActivator.context = null;
}
 
}
</source>
 
<h6> Define the MANIFEST.MF </h6>
 
<source lang="xml">
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Client
Bundle-SymbolicName: ca.on.senecac.scs.sudoku.client
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: ca.on.senecac.scs.sudoku.client.ClientActivator
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: ca.on.senecac.scs.sudoku,
org.osgi.framework;version="1.3.0"
</source>
 
<h4> Configure the Sudoku Generator for running as an OSGi Implementation in the Eclipse Equinox container. Then run the application. </h4>
 
Note: In the console window you should see a valid Sudoku Puzzle printed out.
63
edits