Using OSGi in Nakamura Carl Hall [email_address] Hallway Technologies 15 June 2010
Overview <ul><ul><li>Apache Felix </li></ul></ul><ul><ul><ul><li>Dependency Injection </li></ul></ul></ul><ul><ul><ul><li>...
Starting Lineup <ul><ul><li>Bundles </li></ul></ul><ul><ul><li>Components </li></ul></ul><ul><ul><li>Dependency Injection ...
Bundles   11th Sakai Conference - June 15-17, 2010
Bundle Life Cycle <ul><ul><li>INSTALLED </li></ul></ul><ul><ul><ul><li>Successfully installed in container </li></ul></ul>...
Bundle Activation import   org.osgi.framework.BundleActivator ; import   org.osgi.framework.BundleContext ; public   class...
Components   11th Sakai Conference - June 15-17, 2010
Types of Components <ul><ul><li>Component </li></ul></ul><ul><ul><ul><li>Singleton </li></ul></ul></ul><ul><ul><ul><li>Con...
Service Example @Component @Service public class LdapAuthenticationPlugin implements AuthenticationPlugin {    @Property(v...
Component Execution @Activate protected void activate(ComponentContext ctx) {} protected void activate(BundleContext ctx) ...
OSGi Services <ul><li>System Services </li></ul><ul><ul><li>Logging </li></ul></ul><ul><ul><li>Configuration Admin </li></...
Dependency Injection   11th Sakai Conference - June 15-17, 2010
References <ul><ul><li>Acquiring </li></ul></ul><ul><ul><ul><li>Event-based using bind/unbind methods </li></ul></ul></ul>...
References public class GetAReference {    @Reference    ConnectionManager connMgr; } ------------------------------------...
Single Instance Reference // OPTIONAL_UNARY == &quot;0..1&quot; // MANDATORY_UNARY == &quot;1..1&quot; @Reference(cardinal...
Multiple Instance Reference // OPTIONAL_MULTIPLE == &quot;0..n&quot; // MANDATORY_MULTIPLE == &quot;1..n&quot; @Reference(...
Reference Target @Reference PooledConnectionManager connMgr;  //boo! @Reference(target = &quot;(type=pooled)&quot;) Connec...
Configuration   11th Sakai Conference - June 15-17, 2010
Felix Admin Console 11th Sakai Conference - June 15-17, 2010
New Service Configuration @Reference ConfigurationAdmin cfgAdmin; @Activate protected void activate() {    Configuration c...
Configure A Reference @Reference ConnectionManager connMgr; --------------------------------------------------------------...
Upcoming SlideShare
Loading in …5
×

Using OSGi in Nakamura

1,741
-1

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,741
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Using OSGi in Nakamura

  1. 1. Using OSGi in Nakamura Carl Hall [email_address] Hallway Technologies 15 June 2010
  2. 2. Overview <ul><ul><li>Apache Felix </li></ul></ul><ul><ul><ul><li>Dependency Injection </li></ul></ul></ul><ul><ul><ul><li>Dynamic loading/unloading </li></ul></ul></ul><ul><ul><ul><li>Instantiation by configuration </li></ul></ul></ul><ul><ul><li>BND plugin generates manifest </li></ul></ul><ul><ul><ul><li>Export-Package, Private-Package, Include-Resource </li></ul></ul></ul><ul><ul><li>SCR plugin generates descriptors </li></ul></ul><ul><ul><ul><li>Declarative Services </li></ul></ul></ul><ul><ul><ul><li>Configuration Admin </li></ul></ul></ul><ul><ul><ul><li>Metatype services </li></ul></ul></ul>11th Sakai Conference - June 15-17, 2010
  3. 3. Starting Lineup <ul><ul><li>Bundles </li></ul></ul><ul><ul><li>Components </li></ul></ul><ul><ul><li>Dependency Injection </li></ul></ul><ul><ul><li>Configuration </li></ul></ul>11th Sakai Conference - June 15-17, 2010
  4. 4. Bundles   11th Sakai Conference - June 15-17, 2010
  5. 5. Bundle Life Cycle <ul><ul><li>INSTALLED </li></ul></ul><ul><ul><ul><li>Successfully installed in container </li></ul></ul></ul><ul><ul><li>RESOLVED </li></ul></ul><ul><ul><ul><li>All resources available; ready to start or has stopped  </li></ul></ul></ul><ul><ul><li>STARTING </li></ul></ul><ul><ul><ul><li>Activator.start called; waiting for return </li></ul></ul></ul><ul><ul><li>ACTIVE </li></ul></ul><ul><ul><ul><li>  No errors during start; activator has returned </li></ul></ul></ul><ul><ul><li>STOPPING </li></ul></ul><ul><ul><ul><li>  Activator.stop called; waiting for return </li></ul></ul></ul><ul><ul><li>UNINSTALLED </li></ul></ul><ul><ul><ul><li>  Cannot move to any other state </li></ul></ul></ul>11th Sakai Conference - June 15-17, 2010
  6. 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 ( &quot;Starting: Hello World&quot; ) ; } public void stop ( BundleContext context ) { System . out . println ( &quot;Stopping: Goodbye Cruel World&quot; ) ; } } Source: Wikipedia 11th Sakai Conference - June 15-17, 2010
  7. 7. Components   11th Sakai Conference - June 15-17, 2010
  8. 8. Types of Components <ul><ul><li>Component </li></ul></ul><ul><ul><ul><li>Singleton </li></ul></ul></ul><ul><ul><ul><li>Consumes functionality </li></ul></ul></ul><ul><ul><li>Service </li></ul></ul><ul><ul><ul><li>Singleton </li></ul></ul></ul><ul><ul><ul><li>Consumes & offers functionality </li></ul></ul></ul><ul><ul><li>Service Factory </li></ul></ul><ul><ul><ul><li>Creates a service per bundle </li></ul></ul></ul><ul><ul><ul><li>Not configurable </li></ul></ul></ul><ul><ul><li>Component Factory </li></ul></ul><ul><ul><ul><li>Creates a service per call </li></ul></ul></ul><ul><ul><ul><li>Fully configurable </li></ul></ul></ul>11th Sakai Conference - June 15-17, 2010
  9. 9. Service Example @Component @Service public class LdapAuthenticationPlugin implements AuthenticationPlugin {    @Property(value = &quot;o=sakai&quot;)    static final String LDAP_BASE_DN = &quot;sakai.auth.ldap.baseDn&quot;;    private String baseDn;    @Reference    private LdapConnectionManager connMgr;    @Activate    protected void activate(Map<?, ?> props) {      baseDn = OsgiUtil.toString(props.get(LDAP_BASE_DN), &quot;o=sakai&quot;);    } } 11th Sakai Conference - June 15-17, 2010
  10. 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
  11. 11. OSGi Services <ul><li>System Services </li></ul><ul><ul><li>Logging </li></ul></ul><ul><ul><li>Configuration Admin </li></ul></ul><ul><ul><li>Device Access </li></ul></ul><ul><ul><li>User Admin </li></ul></ul><ul><ul><li>IO Connector </li></ul></ul><ul><ul><li>Preferences </li></ul></ul><ul><ul><li>Component Runtime </li></ul></ul><ul><ul><li>Deployment Admin </li></ul></ul><ul><ul><li>Event Admin </li></ul></ul><ul><ul><li>Application Admin </li></ul></ul>11th Sakai Conference - June 15-17, 2010 <ul><li>Protocol Services </li></ul><ul><ul><li>HTTP Service </li></ul></ul><ul><ul><li>UPnP Device Service </li></ul></ul><ul><ul><li>DMT Admin  </li></ul></ul><ul><li>Miscellaneous Services </li></ul><ul><ul><li>  Wire Admin </li></ul></ul><ul><ul><li>  XML Parse </li></ul></ul><ul><ul><li>  Measurement and State </li></ul></ul>
  12. 12. Dependency Injection   11th Sakai Conference - June 15-17, 2010
  13. 13. References <ul><ul><li>Acquiring </li></ul></ul><ul><ul><ul><li>Event-based using bind/unbind methods </li></ul></ul></ul><ul><ul><ul><li>Lookup using ComponentContext  </li></ul></ul></ul><ul><ul><li>Policy </li></ul></ul><ul><ul><ul><li>Required </li></ul></ul></ul><ul><ul><ul><li>Optional </li></ul></ul></ul><ul><ul><li>Cardinality </li></ul></ul><ul><ul><ul><li>Single </li></ul></ul></ul><ul><ul><ul><li>Multiple </li></ul></ul></ul>11th Sakai Conference - June 15-17, 2010
  14. 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. 15. Single Instance Reference // OPTIONAL_UNARY == &quot;0..1&quot; // MANDATORY_UNARY == &quot;1..1&quot; @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. 16. Multiple Instance Reference // OPTIONAL_MULTIPLE == &quot;0..n&quot; // MANDATORY_MULTIPLE == &quot;1..n&quot; @Reference(cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE,    policy = ReferencePolicy.DYNAMIC,    referenceInterface = ConnectionManager.class,    bind = &quot;bindManager&quot;,    unbind = &quot;unbindManager&quot;) 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. 17. Reference Target @Reference PooledConnectionManager connMgr;  //boo! @Reference(target = &quot;(type=pooled)&quot;) ConnectionManager connMgr;  //yay! ----------------------------------------------------------------- @Component @Service @Property(name = &quot;type&quot;, value = &quot;simple&quot;) public class SimpleCM implements ConnectionManager {} @Component @Service @Property(name = &quot;type&quot;, value = &quot;pooled&quot;) public class PooledCM implements ConnectionManager {} 11th Sakai Conference - June 15-17, 2010
  18. 18. Configuration   11th Sakai Conference - June 15-17, 2010
  19. 19. Felix Admin Console 11th Sakai Conference - June 15-17, 2010
  20. 20. New Service Configuration @Reference ConfigurationAdmin cfgAdmin; @Activate protected void activate() {    Configuration cfg =      cfgAdmin.createFactoryConfiguration(&quot;some.factory.pid&quot;);    Hashtable props = new Hashtable();    props.put(&quot;key1&quot;, &quot;val1&quot;);    props.put(&quot;key2&quot;, &quot;val2&quot;);    config.update(props); } 11th Sakai Conference - June 15-17, 2010
  21. 21. Configure A Reference @Reference ConnectionManager connMgr; ----------------------------------------------------------------- @Reference ConfigurationAdmin cfgAdmin; @Activate protected void activate() {    Configuration cfg =      cfgAdmin.createFactoryConfiguration(&quot;some.factory.pid&quot;);    Hashtable props = new Hashtable();    props.put(&quot;connMgr.target&quot;, &quot;(type=pooled)&quot;);    config.update(props); } 11th Sakai Conference - June 15-17, 2010
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×