Declarative Services
Dependency Injection OSGi style
Felix MeschbergerDay Management [email protected]
Zurich, September 22nd 2009
About Felix Meschberger
Senior Developer at Day Management AG
http://blog.meschberger.ch
OSGi Implementations @ Apache FelixDeclarative Services
Configuration Admin
Metatype Service
Contents
Dependency Injection
Implementations for OSGi
Declarative Services
Issues
Maven SCR Plugin
Declarative Services 1.1
Dependency Injection
Loose CouplingDon't call use, we'll call you
Easier TestingInject different implementations
Popular in Spring Framework
Traditionally Descriptor Based
Current Trend: Java Annotations
Implementations for OSGi
Declarative Services
iPOJO (Evolution of Declarative Services)
Spring DM
Blueprint Service (Evolution of Spring DM)
Peaberry (based on Google Guice)
possibly more
Declarative Services
Version 1.0
Part of Compendium Services since R4
XML Descriptor Based
Lifecycle Management
Dependency Injection (Services)
Configuration Support
Component Descriptor
XML
Descriptors may be embedded
Namespace for Componenthttp://www.osgi.org/xmlns/scr/v1.0.0
Descriptors listed in Bundle Manifest HeaderService-Component
Multiple Components per Document
Component Descriptor
1
2
Lifecycle Management
Load Descriptors on Bundle Start
Instantiation
Configuration
Activation
Dependency Injection
Deactivation
Unload on Bundle Stop
Component Descriptor
1
2
Lifecycle Sample: 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 Sample: Binding
package org.sample;
public class Component {
protected void bindLog(
LogService ls) {
this.logService = ls
}
protected void unbindLog(
LogService ls) {
this.logService = null;
}
}
Lifecycle Sample: 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);
}
}
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 (non Factory)
If Enabled and Satisfied
Single InstanceNo Configuration
Singleton Configuration (service.pid)
Multiple InstancesFactory Configuration (service.factoryPid)
Instantiation (Component Factory)
ComponentFactory.newInstance()
ComponentInstance.dispose()
Controlled by Application Only
Configuration may not be Factory Configuration
Descriptor Unvealed: Component
Component Description
Descriptor Unvealed: Implementation
Descriptor Unvealed: Property
values
Descriptor Unvealed: Properties
Descriptor Unvealed: Service
More Provide Elements
Descriptor Unvealed: Reference
Issue: Configuration Data Types
Wrapper of primitive typesByte, Short, Integer, Long, etc.
String
Array or VectorPrimitive types
Wrappers of primitive types
String
Issue: 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 SourceJavaDoc [email protected], @scr.property, ...
Java Annotations@Component, @Property, ...
High Level Annotations@SlingServlet
Issue: Not really POJO
Requires OSGi API for full functionality
Activate and Deactivate method names fixed
Configuration through ComponentContext
Service properties through ServiceReference
Fixed in Declarative Services 1.1
Declarative Services 1.1
Scheduled for OSGi R 4.2
Configurable names for (de)activator methods
More (de)activator method argumentsComponentContext
BundleContext
Map
int/Integer (deactivator only)
Any combination
Declarative Services 1.1 (cont.)
More (un)bind method argumentsServiceReference
Service instance
Service instance and Map
Configuration DependencyOptional
Ignore
Require
Support for private properties
Declarative Services 1.1 (cont.)
Activator and bind methods may bepublic (discouraged)
protected
private (if in the component class)
default (if in the same package)
Wildcards for Service-Component header
Questions
Thank You!