SlideShare a Scribd company logo
Declarative Services
Dependency Injection OSGi Style
Felix Meschberger | Day Management AG
2
About Felix Meschberger
• Senior Developer at Day Management AG
• fmeschbe@day.com
• 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: Binding
package org.sample;
public class Component {
protected void bindLog(
LogService ls) {
this.logService = ls
}
protected void unbindLog(
LogService ls) {
this.logService = null;
}
}
13
Lifecycle: Configuration
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“);
}
}
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;
@Component
public 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!

More Related Content

PDF
What’s cool in the new and updated OSGi specs (DS, Cloud and more) - C Ziegel...
ODP
Declarative Services Dependency Injection OSGi style
PDF
What's cool in the new and updated OSGi Specs (2013)
ODP
Declarative Services - Dependency Injection OSGi Style
PDF
Monitoring OSGi Applications with the Web Console - Carsten Ziegeler
PDF
Field injection, type safe configuration, and more new goodies in Declarative...
PPT
Apache Aries: A blueprint for developing with OSGi and JEE
PPTX
Spring & Hibernate
What’s cool in the new and updated OSGi specs (DS, Cloud and more) - C Ziegel...
Declarative Services Dependency Injection OSGi style
What's cool in the new and updated OSGi Specs (2013)
Declarative Services - Dependency Injection OSGi Style
Monitoring OSGi Applications with the Web Console - Carsten Ziegeler
Field injection, type safe configuration, and more new goodies in Declarative...
Apache Aries: A blueprint for developing with OSGi and JEE
Spring & Hibernate

What's hot (20)

PDF
Apache Aries Overview
ODP
Spring 4 final xtr_presentation
ODP
Spring 4 advanced final_xtr_presentation
PDF
Indic threads pune12-java ee 7 platformsimplification html5
PDF
Akka Cluster in Java - JCConf 2015
PPTX
Jersey framework
PDF
GraphTour - Utilizing Powerful Extensions for Analytics & Operations
PDF
Java9 Beyond Modularity - Java 9 más allá de la modularidad
PDF
Apache Commons Pool and DBCP - Version 2 Update
PDF
Struts2 - 101
PDF
Introduction tomcat7 servlet3
PPTX
Java Play Restful JPA
PDF
Spring 4 on Java 8 by Juergen Hoeller
PDF
OrientDB - The 2nd generation of (multi-model) NoSQL
PPTX
Automated testing web services - part 1
PDF
the Spring 4 update
PDF
AAI 2236-Using the New Java Concurrency Utilities with IBM WebSphere
PDF
Integration tests: use the containers, Luke!
PDF
JavaCro'14 - Scala and Java EE 7 Development Experiences – Peter Pilgrim
PDF
JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović
Apache Aries Overview
Spring 4 final xtr_presentation
Spring 4 advanced final_xtr_presentation
Indic threads pune12-java ee 7 platformsimplification html5
Akka Cluster in Java - JCConf 2015
Jersey framework
GraphTour - Utilizing Powerful Extensions for Analytics & Operations
Java9 Beyond Modularity - Java 9 más allá de la modularidad
Apache Commons Pool and DBCP - Version 2 Update
Struts2 - 101
Introduction tomcat7 servlet3
Java Play Restful JPA
Spring 4 on Java 8 by Juergen Hoeller
OrientDB - The 2nd generation of (multi-model) NoSQL
Automated testing web services - part 1
the Spring 4 update
AAI 2236-Using the New Java Concurrency Utilities with IBM WebSphere
Integration tests: use the containers, Luke!
JavaCro'14 - Scala and Java EE 7 Development Experiences – Peter Pilgrim
JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović
Ad

Similar to Declarative Services (20)

ODP
Declarative Services - Dependency Injection OSGi Style
PDF
Constructor injection and other new features for Declarative Services 1.4
PDF
OSGi compendium
PPTX
Introduction to OSGi - Part-2
PDF
Dependencies, dependencies, dependencies
PDF
The Future Of Service-Oriented Component Models for the OSGi Framework - Clém...
PPT
Service oriented component model
PDF
JAX 09 - OSGi Service Components Models
ODP
Introduction to Everit Component Registry - B Zsoldos
PDF
The Ultimate Dependency Manager Shootout (QCon NY 2014)
PDF
Dynamic and modular Web Applications with Equinox and Vaadin
PDF
iPOJO - The Simple Life - Richard Hall, Visiting Assistant Professor at Tufts...
PDF
OSGi Community Event 2010 - Dependencies, dependencies, dependencies
PDF
Dynamic Guice Applications
PDF
OSGi bootcamp - part 2
PDF
Popular patterns revisited on OSGi - Christian Schneider (Adobe)
PDF
Liberate your components with OSGi services - Graham Charters
PPTX
PDF
DS, BP, EJB, CDI, WTF!? - Graham Charters
PDF
DS, BP, EJB, CDI, WTF!? - Graham Charters
Declarative Services - Dependency Injection OSGi Style
Constructor injection and other new features for Declarative Services 1.4
OSGi compendium
Introduction to OSGi - Part-2
Dependencies, dependencies, dependencies
The Future Of Service-Oriented Component Models for the OSGi Framework - Clém...
Service oriented component model
JAX 09 - OSGi Service Components Models
Introduction to Everit Component Registry - B Zsoldos
The Ultimate Dependency Manager Shootout (QCon NY 2014)
Dynamic and modular Web Applications with Equinox and Vaadin
iPOJO - The Simple Life - Richard Hall, Visiting Assistant Professor at Tufts...
OSGi Community Event 2010 - Dependencies, dependencies, dependencies
Dynamic Guice Applications
OSGi bootcamp - part 2
Popular patterns revisited on OSGi - Christian Schneider (Adobe)
Liberate your components with OSGi services - Graham Charters
DS, BP, EJB, CDI, WTF!? - Graham Charters
DS, BP, EJB, CDI, WTF!? - Graham Charters
Ad

More from Felix Meschberger (7)

KEY
Apache Felix Web Console
ODP
Server-side OSGi with Apache Sling (OSGiDevCon 2011)
ODP
Server-side OSGi with Apache Sling
ODP
Server-side OSGi with Apache Sling (Jazoon 2010)
ODP
Managing an OSGi Framework with Apache Felix Web Console
ODP
Apache Sling Server Seitiges OSGi
ODP
Rapid JCR Applications Development with Sling
Apache Felix Web Console
Server-side OSGi with Apache Sling (OSGiDevCon 2011)
Server-side OSGi with Apache Sling
Server-side OSGi with Apache Sling (Jazoon 2010)
Managing an OSGi Framework with Apache Felix Web Console
Apache Sling Server Seitiges OSGi
Rapid JCR Applications Development with Sling

Recently uploaded (20)

PDF
project resource management chapter-09.pdf
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Getting Started with Data Integration: FME Form 101
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PPTX
Tartificialntelligence_presentation.pptx
PPTX
OMC Textile Division Presentation 2021.pptx
PPTX
1. Introduction to Computer Programming.pptx
PPTX
Chapter 5: Probability Theory and Statistics
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Encapsulation theory and applications.pdf
project resource management chapter-09.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Getting Started with Data Integration: FME Form 101
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
A Presentation on Artificial Intelligence
A novel scalable deep ensemble learning framework for big data classification...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Approach and Philosophy of On baking technology
Zenith AI: Advanced Artificial Intelligence
Building Integrated photovoltaic BIPV_UPV.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MIND Revenue Release Quarter 2 2025 Press Release
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
SOPHOS-XG Firewall Administrator PPT.pptx
Tartificialntelligence_presentation.pptx
OMC Textile Division Presentation 2021.pptx
1. Introduction to Computer Programming.pptx
Chapter 5: Probability Theory and Statistics
WOOl fibre morphology and structure.pdf for textiles
Encapsulation theory and applications.pdf

Declarative Services