Declarative Services Dependency Injection OSGi style

14,760 views
14,595 views

Published on

About the standardized way of using Dependency Injection in OSGi applications

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

No Downloads
Views
Total views
14,760
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
220
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Declarative Services Dependency Injection OSGi style

  1. 1. Declarative Services Dependency Injection OSGi style Felix Meschberger Day Management AG [email_address] Zurich, September 22 nd 2009
  2. 2. About Felix Meschberger <ul><li>Senior Developer at Day Management AG
  3. 3. [email_address]
  4. 4. http://blog.meschberger.ch
  5. 5. OSGi Implementations @ Apache Felix </li><ul><li>Declarative Services
  6. 6. Configuration Admin
  7. 7. Metatype Service </li></ul></ul>
  8. 8. Contents <ul><li>Dependency Injection
  9. 9. Implementations for OSGi
  10. 10. Declarative Services
  11. 11. Issues
  12. 12. Maven SCR Plugin
  13. 13. Declarative Services 1.1 </li></ul>
  14. 14. Dependency Injection <ul><li>Loose Coupling </li><ul><li>„ Don't call use, we'll call you“ </li></ul><li>Easier Testing </li><ul><li>Inject different implementations </li></ul><li>Popular in Spring Framework
  15. 15. Traditionally Descriptor Based
  16. 16. Current Trend: Java Annotations </li></ul>
  17. 17. Implementations for OSGi <ul><li>Declarative Services
  18. 18. iPOJO (Evolution of Declarative Services)
  19. 19. Spring DM
  20. 20. Blueprint Service (Evolution of Spring DM)
  21. 21. Peaberry (based on Google Guice)
  22. 22. … possibly more … </li></ul>
  23. 23. Declarative Services <ul><li>Version 1.0
  24. 24. Part of Compendium Services since R4
  25. 25. XML Descriptor Based
  26. 26. Lifecycle Management
  27. 27. Dependency Injection (Services)
  28. 28. Configuration Support </li></ul>
  29. 29. Component Descriptor <ul><li>XML
  30. 30. Descriptors may be embedded
  31. 31. Namespace for Component </li><ul><li>http://www.osgi.org/xmlns/scr/v1.0.0 </li></ul><li>Descriptors listed in Bundle Manifest Header </li><ul><li>Service-Component </li></ul><li>Multiple Components per Document </li></ul>
  32. 32. Component Descriptor <ul><scr:component name=“sample.component“ xmlns:scr=“...“> <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>
  33. 33. Lifecycle Management <ul><li>Load Descriptors on Bundle Start
  34. 34. Instantiation
  35. 35. Configuration
  36. 36. Activation
  37. 37. Dependency Injection
  38. 38. Deactivation
  39. 39. Unload on Bundle Stop </li></ul>
  40. 40. Component Descriptor <ul><scr:component name=“sample.component“ xmlns:scr=“...“> <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>
  41. 41. Lifecycle Sample: 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>
  42. 42. Lifecycle Sample: Binding <ul>package org.sample; public class Component { protected void bindLog ( LogService ls) { this.logService = ls } protected void unbindLog ( LogService ls) { this.logService = null; } } </ul>
  43. 43. Lifecycle Sample: Configuration <ul>package org.sample; public class Component { protected void activate ( ComponentContext c) { Dictionary props = c .getProperties() ; String p1 = (String) props.get(„p1“); int[] p2 = (int[]) props.get(„p2“); } } </ul>
  44. 44. Component Types <ul><li>Regular Component (non-service)
  45. 45. Service
  46. 46. Service Factory
  47. 47. Component Factory </li></ul>
  48. 48. Dependency Injection <ul><li>Event-based using bind/unbind methods
  49. 49. Lookup oriented using ComponentContext
  50. 50. Optionality
  51. 51. Multiplicity
  52. 52. Binding Policy </li></ul>
  53. 53. Configuration <ul><li>Configuration from Configuration Admin
  54. 54. Properties from Descriptor
  55. 55. Provided through ComponentContext.getProperties() </li></ul>
  56. 56. Instantiation (non Factory) <ul><li>If Enabled and Satisfied
  57. 57. Single Instance </li><ul><li>No Configuration
  58. 58. Singleton Configuration ( service.pid ) </li></ul><li>Multiple Instances </li><ul><li>Factory Configuration ( service.factoryPid ) </li></ul></ul>
  59. 59. Instantiation (Component Factory) <ul><li>ComponentFactory.newInstance()
  60. 60. ComponentInstance.dispose()
  61. 61. Controlled by Application Only
  62. 62. Configuration may not be Factory Configuration </li></ul>
  63. 63. Descriptor Unvealed: Component <ul><scr:component name= enabled= immediate= factory= configuration-policy= activate= deactivate= > … Component Description … <li></scr:component> </li></ul>
  64. 64. Descriptor Unvealed: Implementation <ul><implementation class= /> </ul>
  65. 65. Descriptor Unvealed: Property <ul><property name= value= type= /> <property name= type= > … values … </property> </ul>
  66. 66. Descriptor Unvealed: Properties <ul><properties entry= /> </ul>
  67. 67. Descriptor Unvealed: Service <ul><service servicefactory= > <provide interface= /> … More Provide Elements … </service> </ul>
  68. 68. Descriptor Unvealed: Reference <ul><reference name= interface= cardinality= policy= target= bind= unbind= /> </ul>
  69. 69. Issue: Configuration Data Types <ul><li>Wrapper of primitive types </li><ul><li>Byte, Short, Integer, Long, etc. </li></ul><li>String
  70. 70. Array or Vector </li><ul><li>Primitive types
  71. 71. Wrappers of primitive types
  72. 72. String </li></ul></ul>
  73. 73. Issue: XML Descriptor <ul><li>Good to re-use legacy POJO
  74. 74. Problematic to keep in-sync with DS Classes
  75. 75. YAXF – Yet another XML File </li></ul>
  76. 76. Maven SCR Plugin <ul><li>Generates Descriptors from Java Source </li><ul><li>JavaDoc tags </li><ul><li>@scr.component, @scr.property, ... </li></ul><li>Java Annotations </li><ul><li>@Component, @Property, ... </li></ul><li>High Level Annotations </li><ul><li>@SlingServlet </li></ul></ul></ul>
  77. 77. Issue: Not really POJO <ul><li>Requires OSGi API for full functionality
  78. 78. Activate and Deactivate method names fixed
  79. 79. Configuration through ComponentContext
  80. 80. Service properties through ServiceReference
  81. 81. Fixed in Declarative Services 1.1 </li></ul>
  82. 82. Declarative Services 1.1 <ul><li>Scheduled for OSGi R 4.2
  83. 83. Configurable names for (de)activator methods
  84. 84. More (de)activator method arguments </li><ul><li>ComponentContext
  85. 85. BundleContext
  86. 86. Map
  87. 87. int/Integer (deactivator only)
  88. 88. Any combination </li></ul></ul>
  89. 89. Declarative Services 1.1 (cont.) <ul><li>More (un)bind method arguments </li><ul><li>ServiceReference
  90. 90. Service instance
  91. 91. Service instance and Map </li></ul><li>Configuration Dependency </li><ul><li>Optional
  92. 92. Ignore
  93. 93. Require </li></ul><li>Support for private properties </li></ul>
  94. 94. Declarative Services 1.1 (cont.) <ul><li>Activator and bind methods may be </li><ul><li>public (discouraged)
  95. 95. protected
  96. 96. private (if in the component class)
  97. 97. default (if in the same package) </li></ul><li>Wildcards for Service-Component header </li></ul>
  98. 98. Questions
  99. 99. Thank You!

×