21
Using OSGi in Nakamura Carl Hall [email protected] Hallway Technologies 15 June 2010

Using OSGi in Nakamura

Embed Size (px)

Citation preview

Page 1: Using OSGi in Nakamura

Using OSGi in Nakamura

Carl [email protected] Technologies

15 June 2010

Page 2: Using OSGi in Nakamura

Overview

• Apache Felixo Dependency Injectiono Dynamic loading/unloadingo Instantiation by configuration

• BND plugin generates manifesto Export-Package, Private-Package, Include-Resource

• SCR plugin generates descriptorso Declarative Serviceso Configuration Admino Metatype services

11th Sakai Conference - June 15-17, 2010

Page 3: Using OSGi in Nakamura

Starting Lineup

• Bundles• Components• Dependency Injection• Configuration

11th Sakai Conference - June 15-17, 2010

Page 4: Using OSGi in Nakamura

Bundles

 

11th Sakai Conference - June 15-17, 2010

Page 5: Using OSGi in Nakamura

Bundle Life Cycle

• INSTALLEDo Successfully installed in container

• RESOLVEDo All resources available; ready to start or has stopped 

• STARTINGo Activator.start called; waiting for return

• ACTIVEo  No errors during start; activator has returned

• STOPPINGo  Activator.stop called; waiting for return

• UNINSTALLEDo  Cannot move to any other state

11th Sakai Conference - June 15-17, 2010

Page 6: Using OSGi in Nakamura

Bundle Activation

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

public class Activator implements BundleActivator {public void start(BundleContext context) {System.out.println("Starting: Hello World");}

public void stop(BundleContext context) {System.out.println("Stopping: Goodbye Cruel World");}}

Source: Wikipedia

11th Sakai Conference - June 15-17, 2010

Page 7: Using OSGi in Nakamura

Components

 

11th Sakai Conference - June 15-17, 2010

Page 8: Using OSGi in Nakamura

Types of Components

• Componento Singletono Consumes functionality

• Serviceo Singletono Consumes & offers functionality

• Service Factoryo Creates a service per bundleo Not configurable

• Component Factoryo Creates a service per callo Fully configurable

11th Sakai Conference - June 15-17, 2010

Page 9: Using OSGi in Nakamura

Service Example

@Component@Servicepublic class LdapAuthenticationPlugin implements AuthenticationPlugin {  @Property(value = "o=sakai")  static final String LDAP_BASE_DN = "sakai.auth.ldap.baseDn";  private String baseDn;

  @Reference  private LdapConnectionManager connMgr;

  @Activate  protected void activate(Map<?, ?> props) {    baseDn = OsgiUtil.toString(props.get(LDAP_BASE_DN), "o=sakai");  }}

11th Sakai Conference - June 15-17, 2010

Page 10: Using OSGi in Nakamura

Component Execution

@Activateprotected void activate(ComponentContext ctx) {}protected void activate(BundleContext ctx) {}protected void activate(Map<?, ?> props) {}

@Modifiedprotected void modified(...) {}

@Deactivateprotected void deactivate(...) {}protected void deactivate(int reasonCode) {}

11th Sakai Conference - June 15-17, 2010

Page 11: Using OSGi in Nakamura

OSGi Services

System Services• Logging• Configuration Admin• Device Access• User Admin• IO Connector• Preferences• Component Runtime• Deployment Admin• Event Admin• Application Admin

11th Sakai Conference - June 15-17, 2010

Protocol Services• HTTP Service• UPnP Device Service• DMT Admin 

Miscellaneous Services•  Wire Admin•  XML Parse•  Measurement and State

Page 12: Using OSGi in Nakamura

Dependency Injection

 

11th Sakai Conference - June 15-17, 2010

Page 13: Using OSGi in Nakamura

References

• Acquiringo Event-based using bind/unbind methodso Lookup using ComponentContext 

• Policyo Requiredo Optional

• Cardinalityo Singleo Multiple

11th Sakai Conference - June 15-17, 2010

Page 14: Using OSGi in Nakamura

References

public class GetAReference {  @Reference  ConnectionManager connMgr;}

-----------------------------------------------------------------

@Component@Servicepublic class LdapConnectionManager implements ConnectionManager { }

@Component@Servicepublic class JdbcConnectionManager implements ConnectionManager { }

11th Sakai Conference - June 15-17, 2010

Page 15: Using OSGi in Nakamura

Single Instance Reference

// OPTIONAL_UNARY == "0..1"// MANDATORY_UNARY == "1..1"

@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY,  policy = ReferencePolicy.STATIC)ConnectionManager connMgr;

protected void bindConnMgr(ConnectionManager mgr) {  this.connMgr = mgr;}

protected void unbindConnMgr(ConnectionManager mgr) {  this.connMgr = null;}

11th Sakai Conference - June 15-17, 2010

Page 16: Using OSGi in Nakamura

Multiple Instance Reference

// OPTIONAL_MULTIPLE == "0..n"// MANDATORY_MULTIPLE == "1..n"

@Reference(cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE,  policy = ReferencePolicy.DYNAMIC,  referenceInterface = ConnectionManager.class,  bind = "bindManager",  unbind = "unbindManager")List<ConnectionManager> managers;  // be sure to instantiate this

protected void bindManager(ConnectionManager mgr) {  managers.add(mgr);}

protected void unbindManager(ConnectionManager mgr) {  managers.remove(mgr);}

11th Sakai Conference - June 15-17, 2010

Page 17: Using OSGi in Nakamura

Reference Target

@ReferencePooledConnectionManager connMgr;  //boo!

@Reference(target = "(type=pooled)")ConnectionManager connMgr;  //yay!

-----------------------------------------------------------------

@Component @Service@Property(name = "type", value = "simple")public class SimpleCM implements ConnectionManager {}

@Component @Service@Property(name = "type", value = "pooled")public class PooledCM implements ConnectionManager {}

11th Sakai Conference - June 15-17, 2010

Page 18: Using OSGi in Nakamura

Configuration

 

11th Sakai Conference - June 15-17, 2010

Page 19: Using OSGi in Nakamura

Felix Admin Console

11th Sakai Conference - June 15-17, 2010

Page 20: Using OSGi in Nakamura

New Service Configuration

@ReferenceConfigurationAdmin cfgAdmin;

@Activateprotected void activate() {  Configuration cfg =    cfgAdmin.createFactoryConfiguration("some.factory.pid");

  Hashtable props = new Hashtable();  props.put("key1", "val1");  props.put("key2", "val2");

  config.update(props);}

11th Sakai Conference - June 15-17, 2010

Page 21: Using OSGi in Nakamura

Configure A Reference

@ReferenceConnectionManager connMgr;

-----------------------------------------------------------------

@ReferenceConfigurationAdmin cfgAdmin;

@Activateprotected void activate() {  Configuration cfg =    cfgAdmin.createFactoryConfiguration("some.factory.pid");

  Hashtable props = new Hashtable();  props.put("connMgr.target", "(type=pooled)");

  config.update(props);}

11th Sakai Conference - June 15-17, 2010