Declarative Services

38
Declarative Services Dependency Injection OSGi Style Felix Meschberger | Day Management AG

Transcript of Declarative Services

Declarative Services

Dependency Injection OSGi Style

Felix Meschberger | Day Management AG

2

About Felix Meschberger

• Senior Developer at Day Management AG

[email protected]

• http://blog.meschberger.ch

• OSGi Implementations @ Apache Felix– Declarative Services

– Configuration Admin

– Metatype Service

3

Contents• Dependency Injection

• Implementations for OSGi

• Declarative Services

• Issues

• Declarative Services 1.1

• Maven SCR Plugin

• Apache Felix Extensions

4

Dependency Injection• Loose Coupling

– „Don't call use, we'll call you“

• Easier Testing– Inject different implementations

• Popular in Spring Framework

• Traditionally Descriptor Based

• Current Trend: Java Annotations

5

Implementations for OSGi• Dependency Manager

• Declarative Services

• iPOJO (Evolution of Declarative Services)

• Spring DM

• Blueprint Service (Evolution of Spring DM)

• Peaberry (based on Google Guice)

• … possibly more …

6

Declarative Services• Version 1.0, Compendium R4

• Version 1.1, Comendium R4.2

• XML Descriptor Based

• Lifecycle Management

• Dependency Injection (Services)

• Configuration Support

7

Component Descriptor• XML

• Descriptors may be embedded

• Namespace for Component

– http://www.osgi.org/xmlns/scr/v1.0.0

– http://www.osgi.org/xmlns/scr/v1.1.0

• Descriptors listed in Bundle Manifest Header

– Service-Component

• Multiple Components per Document

8

Component Descriptor

<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>

9

Lifecycle Management• Load Descriptors on Bundle Start

• Instantiation

• Configuration

• Activation

• Dependency Injection

• Deactivation

• Unload on Bundle Stop

10

Component Descriptor

<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>

11

Lifecycle: Activation

package org.sample;public class Component { protected void activate( ComponentContext c) { System.out.println(„Activating“); } protected void deactivate( ComponentContext c) { System.out.println(„Deactivating“); }}

12

Lifecycle: Bindingpackage org.sample;public class Component { protected void bindLog( LogService ls) { this.logService = ls } protected void unbindLog( LogService ls) { this.logService = null; }}

13

Lifecycle: Configurationpackage 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“); }}

14

Component Types• Regular Component (non-service)

• Service

• Service Factory

• Component Factory

15

Dependency Injection• Event-based using bind/unbind methods

• Lookup oriented using ComponentContext

• Optionality

• Multiplicity

• Binding Policy

16

Configuration• Configuration from Configuration Admin

• Properties from Descriptor

• Provided through ComponentContext.getProperties()

17

Instantiation• If Enabled and Satisfied

• Single Instance– No Configuration (unless required)

– Singleton Configuration (service.pid)

• Multiple Instances– Factory Configuration (service.factoryPid)

18

Component Factory• ComponentFactory.newInstance()

• ComponentInstance.dispose()

• Controlled by Application only

• Global Singleton Configuration only

19

Descriptor Unvealed<scr:component name= enabled= immediate= factory= configuration-policy= activate= deactivate= modified=> … Component Description …

• </scr:component>

20

Descriptor Unvealed<implementation class=/>

21

Descriptor Unvealed<property name= value= type=/>

<property name= type=> … values …</property>

22

Descriptor Unvealed<properties entry=/>

23

Descriptor Unvealed<service servicefactory=> <provide interface= /> … More Provide Elements …</service>

24

Descriptor Unvealed<reference name= interface= cardinality= policy= target= bind= unbind=/>

25

Issues• Configuration Data Types

• Components not really POJO (DS 1.0)

• XML Descriptor

26

Configuration Data• Wrapper of primitive types

– Byte, Short, Integer, Long, etc.

• String

• Array or Vector

– Primitive types

– Wrappers of primitive types

– String

• Aligned with...

– Metatype Service Specification

– Configuration Admin Specification

27

DS 1.0 not really POJO• Requires OSGi API for full functionality

• Activate and Deactivate method names fixed and public or protected

• Configuration through ComponentContext

• Reference Service Properties through ServiceReference

• Fixed in Declarative Services 1.1

28

DS 1.1• Configurable names for (de)activator

methods

• More (de)activator method arguments– ComponentContext

– BundleContext

– Map

– int/Integer (deactivator only)

– Any combination

29

DS 1.1 (cont.)

• More (un)bind method arguments

– ServiceReference

– Service instance

– Service instance and Map

• Configuration Dependency

– Optional

– Ignore

– Require

• Support for private properties

30

DS 1.1 (cont.)

• Activator and bind methods may be– public (discouraged)

– protected

– private (if in the component class)

– default (if in the same package)

• New features require DS 1.1 Namespace

• Service-Component supports Wildcards– E.g. OSGi-INF/ds*.xml

31

XML Descriptor• Good to re-use legacy POJO

• Problematic to keep in-sync with DS Classes

• YAXF – Yet another XML File

32

Maven SCR Plugin• Generates Descriptors from Java Source

– JavaDoc tags• @scr.component, @scr.property, ...

– Java Annotations• @Component, @Property, ...

– High Level Annotations• @SlingServlet

• Alternative: Peter Kriens' BND library

33

Component Descriptor

<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>

34

SCR Annotations

package org.sample;@Componentpublic class Component { @Reference private LogService log; @Property(value=“sample“) private String p1; @Property(value=“1,2“) private int[] p2; protected void activate( ComponentContext c) { System.out.println(„Activating“); } protected void deactivate( ComponentContext c) { System.out.println(„Deactivating“); }}

35

SCR JavaDoc Tags

package org.sample;/** @scr.component */ public class Component { /** @scr.reference / private LogService log; /** @scr.property value=“sample“ */ private String p1; /** @scr.property values.0=“1“ values.1=“2“ */ private int[] p2; protected void activate( ComponentContext c) { System.out.println(„Activating“); } protected void deactivate( ComponentContext c) { System.out.println(„Deactivating“); }}

36

Apache Felix Extensions• Management API

– Since Apache Felix SCR 1.0

– Now also in Equinox DS 1.2.0v20100125

• Service Reference: updated– Since Apache Felix SCR 1.4.0

– OSGi Bug #63

37

Questions

38

Thank You!