Changes

Jump to: navigation, search

OSGi : Develop Simple Apps

5,136 bytes removed, 23:56, 24 January 2011
no edit summary
{{Ecl_activities|type=OSGi|type-repo=osgi}}__NOTOC____NOTOC__<h2>Bundels and Java Applications</h2>
# Check out the [https://guest:1673852@zenit.senecac.on.ca/svn/ecl500/Lectures/trunk/w11-osgi-simple-interface/ Service Interface]
# Check out the [https://guest:1673852@zenit.senecac.on.ca/svn/ecl500/Lectures/trunk/w11-osgi-simple-serviceprovider/ Service Provider]
# Check out the [https://guest:1673852@zenit.senecac.on.ca/svn/ecl500/Lectures/trunk/w11-osgi-simple-serviceconsumer/ Service Consumer]
<h3>[[Steps for Building Applications from Bundels]]</h3>
<strong>Steps for Building Bundels</strong>[[File:OSGi-DS.png|border|SOA in JVM|400px| ]]
<h4h2>1. Study Bundels and the InterfacesDeclarative Services</h4h2* # Check out the [httphttps://www.osgi.org/javadoc/r4v42/org/osgi/framework/Bundle.html Bundle]* [httpguest://www1673852@zenit.osgisenecac.org/javadoc/r4v42/org/osgi/framework/BundleContexton.html BundleContext]* [http:ca/svn/www.osgi.org/javadocecl500/r4v42Lectures/orgtrunk/w11-osgi/framework/BundleActivator.html BundleActivator]--------simple-declarativeservice-<h4>2. Define the Bundle consumer Declarative Service Interface</h4>Consumer] <h5>2.1 Define # Check out the Java Interface that exposes the services<[https:/h5>Let us suppose that one wants to define a service that allows the user to find a book using its isbn. <source lang="java">package cs.ecl.osgi.simple.bookfinder; public interface BookFinder { Book findBook(int isbn) throws BookNotFoundException;}</source>Define in the same bundle the classes that you need such asguest: Book and BookNotFoundException<source lang="java">package cs.ecl1673852@zenit.osgisenecac.simpleon.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); }}<ca/svn/ecl500/Lectures/source> <h5>2.2 Define in the MANIFEST.MF cs.ecl.osgi.simple.bookfinder <trunk/h5><source lang="xml">Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-Name: BookfinderBundlew11-SymbolicName: cs.ecl.osgi.simple.bookfinderBundle-Version: 1.0.0.qualifierBundle-RequiredExecutionEnvironment: JavaSE-1.6Bundle-Vendor: Seneca College - Eclipse CourseExport-Package: cs.ecl.osgi.simple.bookfinder</source>-declarativeservice--------- <h4>3. Define the Bundle provider Declarative Service Provider</h4>The service ''bookfinder'' could be used only if there is a bundle that implements the service. The bundle ''cs.ecl.osgi.simple.bookfinderservice'' implements ''BundleActivator'' interface in the ''Activator'' class. This bundle will be called 'service provider'.]<h5h3>3.1 Define the Activator class [[Steps for the service provider</h5><source lang="java">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 "); }}</source> <h5>3.2 Define the implementation of the service in a class BookFinderImplementation. The class must be defined in an internal package ''cs.ecl.osgi.simple.bookfinderservice.internals'' <u>The package will not be exposed to the outside world</u>)</h5> <source lang="java">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[Building Declarative Services] 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 is a simple and trivial implementation. //There real implementation should use database, distributed system, etc.</source> <h5>3.3 Define the MANIFEST.MF for the ''cs.ecl.osgi.simple.bookfinderservice'' bundle<source lang="xml">Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-Name: BookfinderserviceBundle-SymbolicName: cs.ecl.osgi.simple.bookfinderserviceBundle-Version: 1.0.0.qualifierBundle-Activator: cs.ecl.osgi.simple.bookfinderservice.ActivatorBundle-ActivationPolicy: lazyBundle-RequiredExecutionEnvironment: JavaSE-1.6Import-Package: org.osgi.framework;version="1.3.0"Bundle-Vendor: Seneca College - Eclipse CourseExport-Package: cs.ecl.osgi.simple.bookfinderserviceRequire-Bundle: cs.ecl.osgi.simple.bookfinder;bundle-version="1.0.0"</source> The bundle export only 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 changed dynamically any time a better implementation is created</strongh3>. The bundle provider requires the osgi framework and the interface bundle (e.g. ''cs.ecl.osgi.simple.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'' cs.ecl.osgi.simple.bookfinderservice'' export its service, namely the bood finder service: : ''Export-Package'': cs.ecl.osgi.simple.bookfinderservice ----------

Navigation menu