Post on 10-May-2015
transcript
Using OSGi in Nakamura
Carl Hallcarl@hallwaytech.comHallway Technologies
15 June 2010
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
Starting Lineup
• Bundles• Components• Dependency Injection• Configuration
11th Sakai Conference - June 15-17, 2010
Bundles
11th Sakai Conference - June 15-17, 2010
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
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
Components
11th Sakai Conference - June 15-17, 2010
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
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
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
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
Dependency Injection
11th Sakai Conference - June 15-17, 2010
References
• Acquiringo Event-based using bind/unbind methodso Lookup using ComponentContext
• Policyo Requiredo Optional
• Cardinalityo Singleo Multiple
11th Sakai Conference - June 15-17, 2010
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
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
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
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
Configuration
11th Sakai Conference - June 15-17, 2010
Felix Admin Console
11th Sakai Conference - June 15-17, 2010
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
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