Difference between revisions of "Opentaps OSGI Proof Of Concept Tutorial"

From Opentaps Wiki
Jump to navigationJump to search
m (Protected "Opentaps OSGI Proof Of Concept Tutorial": Sysop page [edit=sysop:move=sysop])
Line 20: Line 20:
 
* Register services, which binds implementation to interfaces.  For example, we can bind <tt>org.opentaps.core.security.useradmin.internal.UserAdminImpl</tt> to <tt>org.osgi.service.useradmin.UserAdmin</tt>
 
* Register services, which binds implementation to interfaces.  For example, we can bind <tt>org.opentaps.core.security.useradmin.internal.UserAdminImpl</tt> to <tt>org.osgi.service.useradmin.UserAdmin</tt>
 
  context.registerService(UserAdmin.class.getName(), new UserAdminImpl(), null);
 
  context.registerService(UserAdmin.class.getName(), new UserAdminImpl(), null);
 +
* Open a <tt>ServiceTracker</tt> for to watch a service's lifecycle.  We cannot guarantee the loading order of bundles by osgi, so the service tracker helps to perform necessary actions when services are added, removed, or modified, independent of the loading details.  For example,
 +
/* ServiceTracker */ userAdminTrckr = new ServiceTracker(context, UserAdmin.class.getName(), null);
 +
        userAdminTrckr.open();

Revision as of 22:51, 6 April 2010

war/pom.xml defines the bundles which are used, including both remote bundles

     <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>com.springsource.javax.servlet</artifactId>
     </dependency>

and our bundles:

   <dependency>
   	<groupId>org.opentaps</groupId>
   	<artifactId>org.opentaps.core.persistence</artifactId>
   	<version>0.0.1-SNAPSHOT</version>
   	<scope>provided</scope>
   </dependency>

Each bundle in turn contains its own pom.xml which defines itself, its version, its build process (using Maven), and its dependencies.

The pom.xml defines an Activator class to manage the bundle:

   <Bundle-Activator>org.opentaps.core.persistence.Activator</Bundle-Activator>

This class has the start(BundleContext context) and stop(BundleContext context) methods which controls what happens when the bundle is started and stopped. The important things it does are:

  • Register services, which binds implementation to interfaces. For example, we can bind org.opentaps.core.security.useradmin.internal.UserAdminImpl to org.osgi.service.useradmin.UserAdmin
context.registerService(UserAdmin.class.getName(), new UserAdminImpl(), null);
  • Open a ServiceTracker for to watch a service's lifecycle. We cannot guarantee the loading order of bundles by osgi, so the service tracker helps to perform necessary actions when services are added, removed, or modified, independent of the loading details. For example,
/* ServiceTracker */ userAdminTrckr = new ServiceTracker(context, UserAdmin.class.getName(), null);
       userAdminTrckr.open();