Open main menu

CDOT Wiki β

Changes

OSGi : Develop Simple Apps

1,852 bytes added, 15:15, 22 January 2011
no edit summary
{{Ecl_activities|type=OSGi|type-repo=osgi}}
__NOTOC__
# 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]
</source>
<h5>2.2 Define the a Concrete Bundle Provider</h5><h6>2.2.1 Define the Activator class</h6><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> <h6>2.2.1 Define the internal implementation class (This class will not be exposed to the outside world)</h6> <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[] 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. </source>