Mais conteúdo relacionado
Semelhante a Using OSGi in Nakamura (20)
Using OSGi in Nakamura
- 1. Using OSGi in Nakamura Carl Hall [email_address] Hallway Technologies 15 June 2010
- 6. 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
- 9. Service Example @Component @Service public 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
- 10. Component Execution @Activate protected void activate(ComponentContext ctx) {} protected void activate(BundleContext ctx) {} protected void activate(Map<?, ?> props) {} @Modified protected void modified(...) {} @Deactivate protected void deactivate(...) {} protected void deactivate(int reasonCode) {} 11th Sakai Conference - June 15-17, 2010
- 14. References public class GetAReference { @Reference ConnectionManager connMgr; } ----------------------------------------------------------------- @Component @Service public class LdapConnectionManager implements ConnectionManager { } @Component @Service public class JdbcConnectionManager implements ConnectionManager { } 11th Sakai Conference - June 15-17, 2010
- 15. 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
- 16. 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
- 17. Reference Target @Reference PooledConnectionManager 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
- 20. New Service Configuration @Reference ConfigurationAdmin cfgAdmin; @Activate protected 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
- 21. Configure A Reference @Reference ConnectionManager connMgr; ----------------------------------------------------------------- @Reference ConfigurationAdmin cfgAdmin; @Activate protected 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