Declarative Services - Dependency Injection OSGi Style

4,839 views

Published on

This talk presents the simple yet powerful way of dependency injection as defined by the OSGi Compendium Declarative Services specification. Besides covering the basics of Declarative Services this talk also explains the many useful additions of the Declarative Services 1.1 in OSGi R 4.2.

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

  • Be the first to like this

No Downloads
Views
Total views
4,839
On SlideShare
0
From Embeds
0
Number of Embeds
403
Actions
Shares
0
Downloads
110
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Declarative Services - Dependency Injection OSGi Style

  1. 1. Declarative Services – Dependency Injection, OSGi Style Felix Meschberger | Sr. Computer Scientist, Adobe Systems Incorporated
  2. 2. About Felix Meschberger <ul><li>Senior Computer Scientist
  3. 3. fmeschbe@adobe.com
  4. 4. http://blog.meschberger.ch
  5. 5. OSGi Implementations @ Apache Felix </li><ul><li>Declarative Services
  6. 6. Configuration Admin
  7. 7. Metatype Services </li></ul><li>VP Apache Sling
  8. 8. OSGi Alliance Supporter </li></ul>
  9. 9. Contents <ul><li>Dependency Injection
  10. 10. Implementations for OSGi
  11. 11. Declarative Services 1.0
  12. 12. Issues
  13. 13. Declarative Services 1.1
  14. 14. Maven SCR Plugin
  15. 15. Apache Felix Extensions </li></ul>
  16. 16. Dependency Injection <ul><li>Loose Coupling </li><ul><li>“ Don't call us, we'll call you” </li></ul><li>Easier Testing </li><ul><li>Inject different implementations </li></ul><li>Popular in Spring Framework
  17. 17. Traditionally Descriptor Based
  18. 18. Current Trend: Java Annotations (JSR-330) </li></ul>
  19. 19. Implementations for OSGi <ul><li>Apache Felix Dependency Manager
  20. 20. OSGi Declarative Services
  21. 21. Apache Felix iPOJO (Evolution of Declarative Services)
  22. 22. Spring DM
  23. 23. OSGi Blueprint (Evolution of Spring DM)
  24. 24. Peaberry (based on Google Guice)
  25. 25. … probably more … </li></ul>
  26. 26. Declarative Services <ul><li>Version 1.0, Compendium R4
  27. 27. Version 1.1, Compendium R4.2
  28. 28. XML Descriptor Based
  29. 29. Lifecycle Management
  30. 30. Dependency Injection (Services)
  31. 31. Configuration Support </li></ul>
  32. 32. Lifecycle Management <ul><li>Load Descriptors on Bundle Start
  33. 33. Instantiation
  34. 34. Configuration
  35. 35. Activation
  36. 36. Dependency Injection
  37. 37. Deactivation
  38. 38. Unload on Bundle Stop </li></ul>
  39. 39. Component Descriptor <ul><li>XML
  40. 40. Descriptors may be embedded
  41. 41. Namespace for Component </li><ul><li>http://www.osgi.org/xmlns/scr/v1.0.0
  42. 42. http://www.osgi.org/xmlns/scr/v1.1.0 </li></ul><li>Descriptors listed in Bundle Manifest Header </li><ul><li>Service-Component </li></ul><li>Multiple Components per Document
  43. 43. Multiple Descriptors per Bundle </li></ul>
  44. 44. Component Descriptor <ul><scr:component name=“sample.component“ xmlns:scr=“ http://www.osgi.org/xmlns/scr/v1.1.0 “> <implementation class=“org.sample.Component“ /> <reference interface=“org.osgi.service.log.LogService“ bind=“bindLog unbind=“unbindLoad“ /> <property name=“p1“ value=“sample“ /> <property name=“p2“ type=“Integer“> 1 2 </property> </component> </ul>
  45. 45. Lifecycle: Activation <ul>package org.sample; public class Component { protected void activate ( ComponentContext c) { System.out.println(„Activating“); } protected void deactivate ( ComponentContext c) { System.out.println(„Deactivating“); } } </ul>
  46. 46. Lifecycle: Binding <ul>package org.sample; public class Component { protected void bindLog ( LogService ls) { this.logService = ls } protected void unbindLog ( LogService ls) { if (this.logService == ls) { this.logService = null; } } } </ul>
  47. 47. Lifecycle: Configuration <ul>package org.sample; public class Component { protected void activate ( ComponentContext c) { Dictionary props = c .getProperties() ; String p1 = toString(props.get(„p1“)); int[] p2 = toIntArray(props.get(„p2“)); } } </ul>
  48. 48. Component Types <ul><li>Regular Component (non-service)
  49. 49. Service
  50. 50. Service Factory
  51. 51. Component Factory </li></ul>
  52. 52. Dependency Injection <ul><li>Event-based using bind/unbind methods
  53. 53. Lookup oriented using ComponentContext
  54. 54. Optionality
  55. 55. Multiplicity
  56. 56. Binding Policy </li></ul>
  57. 57. Configuration <ul><li>Configuration from Configuration Admin
  58. 58. Properties from Descriptor
  59. 59. Provided through ComponentContext.getProperties() </li></ul>
  60. 60. Instantiation <ul><li>If Enabled and Satisfied
  61. 61. Single Instance </li><ul><li>No Configuration (unless required)
  62. 62. Singleton Configuration ( service.pid ) </li></ul><li>Multiple Instances </li><ul><li>Factory Configuration ( service.factoryPid ) </li></ul></ul>
  63. 63. Component Factory <ul><li>ComponentFactory.newInstance()
  64. 64. ComponentInstance.dispose()
  65. 65. Controlled by Application only
  66. 66. Global Singleton Configuration only </li></ul>
  67. 67. Descriptor Unvealed <ul><scr:component name= enabled= immediate= factory= configuration-policy= activate= deactivate= modified= > … Component Description … </scr:component> </ul>
  68. 68. Descriptor Unvealed <ul><implementation class= /> </ul>
  69. 69. Descriptor Unvealed <ul><property name= value= type= /> <property name= type= > … values … </property> <properties entry= /> </ul>
  70. 70. Descriptor Unvealed <ul><service servicefactory= > <provide interface= /> … More Provide Elements … </service> </ul>
  71. 71. Descriptor Unvealed <ul><reference name= interface= cardinality= policy= target= bind= unbind= /> </ul>
  72. 72. Issues <ul><li>Configuration Data Types
  73. 73. Components not really POJO (DS 1.0)
  74. 74. XML Descriptor </li></ul>
  75. 75. Configuration Data <ul><li>Wrapper of primitive types </li><ul><li>Byte, Short, Integer, Long, etc. </li></ul><li>String
  76. 76. Array or Vector </li><ul><li>Primitive types
  77. 77. Wrappers of primitive types
  78. 78. String </li></ul><li>Aligned with... </li><ul><li>Metatype Service Specification
  79. 79. Configuration Admin Specification </li></ul></ul>
  80. 80. DS 1.0 not really POJO <ul><li>Requires OSGi API for full functionality
  81. 81. Activate and Deactivate method names fixed and public or protected
  82. 82. Configuration through ComponentContext
  83. 83. Reference Service Properties through ServiceReference
  84. 84. Fixed in Declarative Services 1.1 </li></ul>
  85. 85. DS 1.1 <ul><li>Configurable names for (de)activator methods
  86. 86. More (de)activator method arguments </li><ul><li>ComponentContext
  87. 87. BundleContext
  88. 88. Map
  89. 89. int/Integer (deactivator only)
  90. 90. Any combination </li></ul></ul>
  91. 91. DS 1.1 (cont.) <ul><li>More (un)bind method arguments </li><ul><li>ServiceReference
  92. 92. Service instance
  93. 93. Service instance and Map </li></ul><li>Configuration Dependency </li><ul><li>Optional
  94. 94. Ignore
  95. 95. Require </li></ul><li>Support for private properties </li></ul>
  96. 96. DS 1.1 (cont.) <ul><li>Activator and bind methods may be </li><ul><li>public (discouraged)
  97. 97. protected
  98. 98. private (if in the component class)
  99. 99. default (if in the same package) </li></ul><li>New features require DS 1.1 Namespace
  100. 100. Service-Component supports Wildcards </li><ul><li>E.g. OSGi-INF/ds*.xml </li></ul></ul>
  101. 101. XML Descriptor <ul><li>Good to re-use legacy POJO
  102. 102. Problematic to keep in-sync with DS Classes
  103. 103. YAXF – Yet another XML File </li></ul>
  104. 104. Maven SCR Plugin <ul><li>Generates Descriptors from Java Source </li><ul><li>Java Annotations </li><ul><li>@Component, @Property, ... </li></ul><li>High Level Annotations </li><ul><li>@SlingServlet </li></ul><li>JavaDoc tags (deprecated) </li><ul><li>@scr.component, @scr.property, ... </li></ul></ul><li>Generates Metatype Service Descriptors
  105. 105. Alternative: Peter Kriens' BND library </li></ul>
  106. 106. Component Descriptor <ul><scr:component name=“sample.component“ xmlns:scr=“ http://www.osgi.org/xmlns/scr/v1.1.0 “> <implementation class=“org.sample.component“ /> <reference interface=“org.osgi.service.log.LogService“ bind=“bindLog unbind=“unbindLoad“ /> <property name=“p1“ value=“sample“ /> <property name=“p2“ type=“Integer“> 1 2 </property> </component> </ul>
  107. 107. SCR Annotations <ul>package org.sample; @Component public class Component { @Reference private LogService log; @Property(value = “sample“) private String p1; @Property(intValue = {1, 2}) private int[] p2; protected void activate( ComponentContext c) { System.out.println(„Activating“); } protected void deactivate( ComponentContext c) { System.out.println(„Deactivating“); } } </ul>
  108. 108. Apache Felix Extensions <ul><li>Management API </li><ul><li>Since Apache Felix SCR 1.0
  109. 109. Now also in Equinox DS 1.2.0v20100125 </li></ul><li>Service Reference: updated </li><ul><li>Since Apache Felix SCR 1.4.0
  110. 110. OSGi Bug #63 </li></ul></ul>
  111. 111. Questions
  112. 112. Thank You!

×