Difference between revisions of "OSGi : Develop Simple Apps"

From CDOT Wiki
Jump to: navigation, search
Line 169: Line 169:
 
The bundle export the package cs.ecl.osgi.simple.bookfinderservice Meanwhile, the implementation package - cs.ecl.osgi.simple.bookfinderservice.internals -  <strong>is hidden to the outside world and can be change dynamically</strong> and can be changed dynamically anytime a better implementation is created.
 
The bundle export the package cs.ecl.osgi.simple.bookfinderservice Meanwhile, the implementation package - cs.ecl.osgi.simple.bookfinderservice.internals -  <strong>is hidden to the outside world and can be change dynamically</strong> and can be changed dynamically anytime a better implementation is created.
  
 +
The bundle requires the osgi framework and the interface bundle (e.g. bookfinder bundle)
  
The bundle requires the generic bundle service definition: cs.ecl.osgi.simple.bookfinder;bundle-version="1.0.0"
 
The bundle requires the osgi framework by asking for the org.osgi.framework;version="1.3.0" package
 
 
: Require-Bundle: cs.ecl.osgi.simple.bookfinder;bundle-version="1.0.0"
 
: Require-Bundle: cs.ecl.osgi.simple.bookfinder;bundle-version="1.0.0"
 
: Import-Package: org.osgi.framework;version="1.3.0"
 
: Import-Package: org.osgi.framework;version="1.3.0"
 +
 +
The bundle export its service (for instance, the bood finder service - cs.ecl.osgi.simple.bookfinderservice)
 +
 +
: Export-Package: cs.ecl.osgi.simple.bookfinderservice

Revision as of 16:33, 22 January 2011


OSGi Activities



Start the lab activities in the order defined below:

  1. Check out the Service Interface
  2. Check out the Service Provider
  3. Check out the Service Consumer

Steps for Building Bundels

1. Study the Interfaces

2. Define the Bundle Service Provider

2.1 Define the Generic Bundle Service Provider
2.1.1 Define the Java Interface that exposes the services

Let us suppose that you want to define a service that allows the user to find a book from the book isbn. Then the interface should look similare with this one:

package cs.ecl.osgi.simple.bookfinder;

public interface BookFinder {
	Book findBook(int isbn) throws BookNotFoundException;
}

Define in the same bundle the classes that you need such as: Book and BookNotFoundException

package cs.ecl.osgi.simple.bookfinder;

public class Book {
	private int isbn;
	private String title;
	
	public Book(int isbn, String title) {
		super();
		this.isbn = isbn;
		this.title = title;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getIsbn() {
		return isbn;
	}

	public void setIsbn(int isbn) {
		this.isbn = isbn;
	}
}

package cs.ecl.osgi.simple.bookfinder;

public class BookNotFoundException extends Exception {
	private static final long serialVersionUID = -6184839510952070091L;

	public BookNotFoundException() {
		super();
	}

	public BookNotFoundException(String arg0) {
		super(arg0);
	}
}


Define in the MANIFEST.MF that cs.ecl.osgi.simple.bookfinder is the Exported-Package

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Bookfinder
Bundle-SymbolicName: cs.ecl.osgi.simple.bookfinder
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-Vendor: Seneca College - Eclipse Course
Export-Package: cs.ecl.osgi.simple.bookfinder
2.2 Define a Concrete Bundle Provider
2.2.1 Define the Activator class
package cs.ecl.osgi.simple.bookfinderservice;

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

import cs.ecl.osgi.simple.bookfinder.BookFinder;
import cs.ecl.osgi.simple.bookfinderservice.internals.BookFinderImplementation;


public class Activator implements BundleActivator {

	private static BundleContext context;

	static BundleContext getContext() {
		return context;
	}
	
	public void start(BundleContext bundleContext) throws Exception {
		Activator.context = bundleContext;
		BookFinder bookService = new BookFinderImplementation();
		
		context.registerService(BookFinder.class.getName(), bookService, null);
		System.out.println(" Bookfinder service registered ");
	}
	
	public void stop(BundleContext bundleContext) throws Exception {
		Activator.context = null;
		System.out.println(" Bookfinder service stopped ");
	}
}
2.2.1 Define the internal implementation class (the class BookFinderImplementation will not be exposed to the outside world)
package cs.ecl.osgi.simple.bookfinderservice.internals;

import cs.ecl.osgi.simple.bookfinder.Book;
import cs.ecl.osgi.simple.bookfinder.BookFinder;
import cs.ecl.osgi.simple.bookfinder.BookNotFoundException;

public class BookFinderImplementation implements BookFinder {
	private static final Book[] BOOKS = new Book[] {
			new Book(1234, "Java Programming Language"), 
			new Book(5678, "OSGi") };

	public Book findBook(int isbn) throws BookNotFoundException {
		Book found = null;

		for (Book b : BOOKS)
			if (b.getIsbn() == isbn) {
				found = b;
				break;
			}
		if (found == null)
			throw new BookNotFoundException("No book with isbn = " + isbn);

		return found;
	}
}
//This could be any implementation: database, file system, distributed, etc.

Define in the MANIFEST.MF for cs.ecl.osgi.simple.bookfinderservice

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Bookfinderservice
Bundle-SymbolicName: cs.ecl.osgi.simple.bookfinderservice
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: cs.ecl.osgi.simple.bookfinderservice.Activator
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version="1.3.0"
Bundle-Vendor: Seneca College -  Eclipse Course
Export-Package: cs.ecl.osgi.simple.bookfinderservice
Require-Bundle: cs.ecl.osgi.simple.bookfinder;bundle-version="1.0.0"

The bundle export the package cs.ecl.osgi.simple.bookfinderservice Meanwhile, the implementation package - cs.ecl.osgi.simple.bookfinderservice.internals - is hidden to the outside world and can be change dynamically and can be changed dynamically anytime a better implementation is created.

The bundle requires the osgi framework and the interface bundle (e.g. bookfinder bundle)

Require-Bundle: cs.ecl.osgi.simple.bookfinder;bundle-version="1.0.0"
Import-Package: org.osgi.framework;version="1.3.0"

The bundle export its service (for instance, the bood finder service - cs.ecl.osgi.simple.bookfinderservice)

Export-Package: cs.ecl.osgi.simple.bookfinderservice