Declarative Services - Dependency Injection OSGi Style

Embed Size (px)

Citation preview

Declarative Services Dependency Injection, OSGi Style

Felix Meschberger | Sr. Computer Scientist, Adobe Systems Incorporated

About Felix Meschberger

Senior Computer Scientist

[email protected]

http://blog.meschberger.ch

OSGi Implementations @ Apache FelixDeclarative Services

Configuration Admin

Metatype Services

VP Apache Sling

OSGi Alliance Supporter

Contents

Dependency Injection

Implementations for OSGi

Declarative Services 1.0

Issues

Declarative Services 1.1

Maven SCR Plugin

Apache Felix Extensions

Dependency Injection

Loose CouplingDon't call us, we'll call you

Easier TestingInject different implementations

Popular in Spring Framework

Traditionally Descriptor Based

Current Trend: Java Annotations (JSR-330)

Implementations for OSGi

Apache Felix Dependency Manager

OSGi Declarative Services

Apache Felix iPOJO (Evolution of Declarative Services)

Spring DM

OSGi Blueprint (Evolution of Spring DM)

Peaberry (based on Google Guice)

probably more

Declarative Services

Version 1.0, Compendium R4

Version 1.1, Compendium R4.2

XML Descriptor Based

Lifecycle Management

Dependency Injection (Services)

Configuration Support

Lifecycle Management

Load Descriptors on Bundle Start

Instantiation

Configuration

Activation

Dependency Injection

Deactivation

Unload on Bundle Stop

Component Descriptor

XML

Descriptors may be embedded

Namespace for Componenthttp://www.osgi.org/xmlns/scr/v1.0.0

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

Descriptors listed in Bundle Manifest HeaderService-Component

Multiple Components per Document

Multiple Descriptors per Bundle

Component Descriptor






1
2

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);
}
}

Lifecycle: Binding

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

Lifecycle: Configuration

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));
}
}

Component Types

Regular Component (non-service)

Service

Service Factory

Component Factory

Dependency Injection

Event-based using bind/unbind methods

Lookup oriented using ComponentContext

Optionality

Multiplicity

Binding Policy

Configuration

Configuration from Configuration Admin

Properties from Descriptor

Provided through

ComponentContext.getProperties()

Instantiation

If Enabled and Satisfied

Single InstanceNo Configuration (unless required)

Singleton Configuration (service.pid)

Multiple InstancesFactory Configuration (service.factoryPid)

Component Factory

ComponentFactory.newInstance()

ComponentInstance.dispose()

Controlled by Application only

Global Singleton Configuration only

Descriptor Unvealed


Component Description

Descriptor Unvealed

Descriptor Unvealed


values


Descriptor Unvealed



More Provide Elements

Descriptor Unvealed

Issues

Configuration Data Types

Components not really POJO (DS 1.0)

XML Descriptor

Configuration Data

Wrapper of primitive typesByte, Short, Integer, Long, etc.

String

Array or VectorPrimitive types

Wrappers of primitive types

String

Aligned with...Metatype Service Specification

Configuration Admin Specification

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

DS 1.1

Configurable names for (de)activator methods

More (de)activator method argumentsComponentContext

BundleContext

Map

int/Integer (deactivator only)

Any combination

DS 1.1 (cont.)

More (un)bind method argumentsServiceReference

Service instance

Service instance and Map

Configuration DependencyOptional

Ignore

Require

Support for private properties

DS 1.1 (cont.)

Activator and bind methods may bepublic (discouraged)

protected

private (if in the component class)

default (if in the same package)

New features require DS 1.1 Namespace

Service-Component supports WildcardsE.g. OSGi-INF/ds*.xml

XML Descriptor

Good to re-use legacy POJO

Problematic to keep in-sync with DS Classes

YAXF Yet another XML File

Maven SCR Plugin

Generates Descriptors from Java SourceJava Annotations@Component, @Property, ...

High Level Annotations@SlingServlet

JavaDoc tags (deprecated)@scr.component, @scr.property, ...

Generates Metatype Service Descriptors

Alternative: Peter Kriens' BND library

Component Descriptor






1
2

SCR Annotations

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);
}
}

Apache Felix Extensions

Management APISince Apache Felix SCR 1.0

Now also in Equinox DS 1.2.0v20100125

Service Reference: updatedSince Apache Felix SCR 1.4.0

OSGi Bug #63

Questions

Thank You!

2011 Adobe Systems Incorporated. All Rights Reserved. Click to edit the title text formatClick to edit Master title style

Click to edit the outline text formatSecond Outline LevelThird Outline LevelFourth Outline LevelFifth Outline LevelSixth Outline LevelSeventh Outline LevelEighth Outline Level

Ninth Outline LevelClick to edit Master text styles

Second level

Third level

Fourth level

Fifth level

22.03.11

2011 Adobe Systems Incorporated. All Rights Reserved.

Click to edit the title text formatClick to edit Master title style

2011 Adobe Systems Incorporated. All Rights Reserved. Click to edit the title text formatClick to edit Master title style

22.03.11

2011 Adobe Systems Incorporated. All Rights Reserved.Click to edit the title text formatClick to edit Master title style

22.03.11

2011 Adobe Systems Incorporated. All Rights Reserved.Click to edit the title text formatClick to edit Master title style

22.03.11

2011 Adobe Systems Incorporated. All Rights Reserved.

Click to edit Master title style

Click to edit Master text styles
Second level
Third level
Fourth level
Fifth level

3/22/11

3/22/11

Click to edit Master text stylesSecond levelThird levelFourth levelFifth level