58
Building OSGi Components Carsten Ziegeler | [email protected] 1 ApacheCon NA 2014

Building OSGi Components - · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

  • Upload
    lehuong

  • View
    241

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Building OSGi Components Carsten Ziegeler | [email protected]

1

ApacheCon NA 2014

Page 2: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

About [email protected] @cziegeler

•  RnD Team at Adobe Research Switzerland

•  Member of the Apache Software Foundation

•  Apache Felix and Apache Sling (PMC and committer)

•  And other Apache projects

•  OSGi Core Platform and Enterprise Expert Groups

•  Member of the OSGi Board

•  Book / article author, technical reviewer, conference speaker

2

Page 3: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Agenda

§  1 OSGi Service Registry

§  2 Components

§  3 Declarative Services Today

§  4 Next version of Declarative Services

3

Page 4: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Component and Service

§  Component §  Piece of software managed by a (component) container

§  Java: instances created and managed by a container

§  Container provides configuration and used services

4

Page 5: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Component and Service

§  Service §  A component providing a service

§  Java:

§  Defined through an interface

§  A component implementing one or more interfaces (= services)

§  Usable by components and other services

§  Clients act on the service (interface)

5

Page 6: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Foreword

§  Many component frameworks for OSGi exist today

§  Difficulty of choosing §  For OSGi based component development it’s more important to focus on the

components than on the components framework

§  Focus is on developing components §  Developers choice

§  Declarative Services is very good but it’s not the only solution

6

Page 7: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Example Application �

����

7

Page 8: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

OSGi Service Registry

§  Service oriented architecture

§  Publish/find/bind

8

Service Registry

Service Provider

Service Consumer Interact

Service Description

Publish Find

Page 9: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Registering a Service

§  Each bundle has access to its bundle context object

§  Using bundle activator

§  Bundle context:

§  registerService(String, Object, Dictionary)

§  registerService(String[], Object, Dictionary)

9

Page 10: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Registering a Service

§  Each bundle has access to its bundle context object

§  Using bundle activator

§  Bundle context:

§  registerService(String, Object, Dictionary)

§  registerService(String[], Object, Dictionary)

10

Service name(s)

Page 11: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Registering a Service

§  Each bundle has access to its bundle context object

§  Using bundle activator

§  Bundle context:

§  registerService(String, Object, Dictionary)

§  registerService(String[], Object, Dictionary)

11

Service name(s)

Service instance

Page 12: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Registering a Service

§  Each bundle has access to its bundle context object

§  Using bundle activator

§  Bundle context:

§  registerService(String, Object, Dictionary)

§  registerService(String[], Object, Dictionary)

12

Service name(s)

Service instance

Service properties

Page 13: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Registering a Service

§  Each bundle has access to its bundle context object

§  Using bundle activator

§  Bundle context:

§  registerService(String, Object, Dictionary)

§  registerService(String[], Object, Dictionary)

13

import org.osgi.framework.Constants;import org.osgi.framework.ServiceRegistration;…BundleContext bc = …;final Dictionary<String, Object> props = new Hashtable<String, Object>();props.put(Constants.SERVICE_DESCRIPTION, "Greatest Service on Earth");props.put(Constants.SERVICE_VENDOR, "Adobe Systems Incorporated"); final Scheduler service = new MyScheduler();this.bundleContext.registerService( new String[] {Scheduler.class.getName()}, service, props);

Page 14: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Getting a Service from the Service Registry

14

BundleContext bundleContext = ...;final ServiceReference sr = bundleContext.getServiceReference( Scheduler.class.getName());if ( sr != null ) { final Scheduler s = (Scheduler) bundleContext.getService(sr); if ( s != null ) { s.doSomething(); } bundleContext.ungetService(sr);}

Page 15: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Getting Service Properties

15

BundleContext bundleContext = ...;final ServiceReference sr = bundleContext.getServiceReference( Scheduler.class.getName());if ( sr != null ) { // access properties final Object value = sr.getProperty(Constants.SERVICE_VENDOR); bundleContext.ungetService(sr);}

Page 16: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Service Properties

16

import org.osgi.framework.Constants;Constants.SERVICE_ID - set by the framework (long) id of the service increased for each registration dynamic - not persisted!Constants.SERVICE_DESCRIPTION - optional description (string)Constants.SERVICE_VENDOR - optional vendor (string)Constants.SERVICE_PID - persistence identifier (string) optional, unique identifierConstants.SERVICE_RANKING - ordering of registrations

Page 17: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Multiple Registrations for a Service

17

BundleContext bundleContext = ...;final ServiceReference[] refs = bundleContext.getServiceReferences( Scheduler.class.getName(), null);if ( refs != null ) { // iterate over references, maybe sort by ranking etc.}

Page 18: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Getting a Service from the Service Registry

18

BundleContext bundleContext = ...;final ServiceReference sr = bundleContext.getServiceReference( Scheduler.class.getName());if ( sr != null ) { final Scheduler s = (Scheduler) bundleContext.getService(sr); if ( s != null ) { s.doSomething(); } bundleContext.ungetService(sr);} Highest Ranking

Page 19: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Lazy Service Creation / Bundle Scope

•  Register service factory instead of service •  Framework calls factory once per client bundle

19

public interface org.osgi.framework.ServiceFactory {

Object getService(Bundle bundle, ServiceRegistration registration); void ungetService(Bundle bundle, ServiceRegistration registration, service);}

Page 20: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Registering a Service Factory

20

import org.osgi.framework.Constants;import org.osgi.framework.ServiceRegistration;…BundleContext bc = …;final Dictionary<String, Object> props = new Hashtable<String, Object>();props.put(Constants.SERVICE_DESCRIPTION, "Greatest service on Earth");props.put(Constants.SERVICE_VENDOR, "Adobe Systems Incorporated"); final ServiceFactory factory = new MySchedulerFactory();this.bundleContext.registerService( new String[] {Scheduler.class.getName()}, factory, props);

Page 21: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Service Event Listener

•  Notification of registration / unregistrations

•  Registered to the bundle context •  Filter for service name, properties etc.

21

package org.osgi.framework;public interface ServiceListener extends EventListener {

void serviceChanged(ServiceEvent event);}

Page 22: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

OSGi Service Registry

•  Lightweight services •  Lookup is based on interface name

•  Direct method invocation

•  Scopes: singleton, bundle, prototype (R6)

•  Good design practice •  Separates interface from implementation

•  Separates registration from usage

•  Enables reuse, substitutability, loose coupling, and late binding

22

Page 23: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Example Application �

����

23

Page 24: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

OSGi Service Registry

§  Powerful but "complicated" to use directly

§  Requires a different way of thinking

§  Dynamic §  Packages/Bundles might come and go

§  Services might appear/disappear

§  Manually resolve and track services

§  Doable, but requires "work"

24

Page 25: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Components and Services with OSGi

§  Service interface §  Public (if exported for other bundles)

§  Versioned through package version (Semantic versioning)

§  Private for internal services (sometimes useful)

§  Component / service implementation §  Always private

25

Page 26: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Component Container Interaction

26

OSGi Service Registry

Declarative Services

Blueprint iPojo

"Manual Access"

Page 27: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Advanced OSGi Development Solutions

•  Service Tracker

•  Still somewhat of a manual approach

•  Declarative Services, Blueprint, iPOJO

•  Declarative

•  Sophisticated service oriented component frameworks

•  Automated dependency injection and more

•  More modern, POJO oriented approaches

•  Straight forward with Declarative Services, Annotations, Maven/Ant/Bndtools...

27

Page 28: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Example Application �

����

28

Page 29: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Component Development with Declarative Services

§  Declarative Services (OSGi Compendium Spec) §  Defines Service Component Runtime (SCR)

§  Apache Felix SCR Annotations (DS annotations)

§  Available tooling: Maven/Ant/Bndtools...

§  Some advantages (in combination with the tooling) §  POJO style

§  Declarative

§  Single source: just the Java code, no XML etc.

§  "Integration" with Configuration Admin and Metatype Service

29

Page 30: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

My First Component

30

package com.adobe.osgitraining.impl;import org.apache.felix.scr.annotations.Component;@Componentpublic class MyComponent {}

Page 31: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Component Lifecycle

31

package com.adobe.osgitraining.impl;import org.apache.felix.scr.annotations.Activate;import org.apache.felix.scr.annotations.Component;import org.apache.felix.scr.annotations.Deactivate;@Componentpublic class MyComponent { @Activate protected void activate() { // do something } @Deactivate protected void deactivate() { // do something }}

Page 32: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Providing a Service

32

package com.adobe.osgitraining.impl;import org.apache.felix.scr.annotations.Component;import org.apache.felix.scr.annotations.Service;import org.osgi.service.event.EventHandler;@Component@Service(value=EventHandler.class)public class MyComponent implements EventHandler { …

Page 33: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Providing Several Services

33

package com.adobe.osgitraining.impl;import org.apache.felix.scr.annotations.Component;import org.apache.felix.scr.annotations.Service;import org.osgi.service.event.EventHandler;@Component@Service(value={EventHandler.class, Runnable.class})public class MyComponent implements EventHandler, Runnable { …

Page 34: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Using a Service

34

package com.adobe.osgitraining.impl;import org.apache.felix.scr.annotations.Component;import org.apache.felix.scr.annotations.Service;import org.osgi.service.event.EventHandler;@Component@Service(value=EventHandler.class)public class MyComponent implements EventHandler { @Reference privateTThreadPool threadPool; …

Page 35: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Using an optional Service

35

package com.adobe.osgitraining.impl;import org.apache.felix.scr.annotations.Component;import org.apache.felix.scr.annotations.Service;import org.osgi.service.event.EventHandler;@Component@Service(value=EventHandler.class)public class MyComponent implements EventHandler { @Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY, policy=ReferencePolicy.DYNAMIC) privateTThreadPool threadPool; @Reference(cardinality=ReferenceCardinality.MANDATORY_UNARY) privateTDistributor distributor;

Page 36: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Component Properties -> Service Properties

36

import org.apache.sling.commons.osgi.PropertiesUtil;@Component@Service(value=EventHandler.class)@Properties({ @Property(name="service.vendor", value="Who?”), @Property(name="service.ranking", intValue=500)})public class DistributingEventHandler implements EventHandler {

Page 37: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Configuration Admin

•  OSGi Configuration Admin •  “The” solution to handle configurations

•  Configuration Manager

•  Persistence storage

•  Service API to retrieve/update/remove configuration

•  Integration with Declarative Services •  Configuration changes are propagated to the components

•  Configurations are stored using the PID

37

Page 38: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Configuration – Supports Configuration Admin

38

import org.apache.sling.commons.osgi.PropertiesUtil;@Component@Service(value=EventHandler.class)@Properties({ @Property(name="event.topics", value="*", propertyPrivate=true), @Property(name="event.filter", value="(event.distribute=*)", propertyPrivate=true)})public class DistributingEventHandler implements EventHandler { private static final int DEFAULT_CLEANUP_PERIOD = 15; @Property(intValue=DEFAULT_CLEANUP_PERIOD) private static final String PROP_CLEANUP_PERIOD ="cleanup.period"; private int cleanupPeriod; @Activate protected void activate(final Map<String, Object> props) { this.cleanupPeriod = PropertiesUtil.toInteger(props.get(PROP_CLEANUP_PERIOD)); }

Page 39: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Configuration Update

39

import org.apache.sling.commons.osgi.OsgiUtil;public class DistributingEventHandler implements EventHandler { … @Modified protected void update(final Map<String, Object> props) { this.cleanupPeriod = PropertiesUtil.toInteger(props.get(PROP_CLEANUP_PERIOD)); }

Without update: Component is restarted on

config change!

Page 40: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Configuration – Supports Configuration Admin

§  Provided map contains §  Configuration properties from Configuration Admin

§  Defined component properties

40

@Activate protected void activate(final Map<String, Object> props) { … }

Page 41: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Metatype and Web Console

•  OSGi Metatype Service •  Description of bundle metadata

•  Description of service configurations

•  Property type, name, and description

•  Apache Felix Web Console •  Great solution to configure the system

•  Especially component configurations

•  Uses metatype description

41

Page 42: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Configuration – Supports Metatype

42

import org.apache.sling.commons.osgi.PropertiesUtil;@Component(metatype=true, label="Distributing Event Handler", description="This handler is awesome.")@Properties({ @Property(name="event.topics", value="*", propertyPrivate=true)})public class DistributingEventHandler implements EventHandler { private static final int DEFAULT_CLEANUP_PERIOD = 15; @Property(intValue=DEFAULT_CLEANUP_PERIOD, label="Cleanup Period", description="This is the cleanup period in seconds.") private static final String PROP_CLEANUP_PERIOD ="cleanup.period";

Page 43: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Lifecycle Methods

§  Signatures for activate and deactivate:

43

protected void activate(); protected void activate(final Map<String, Object> properties); protected void activate(final ComponentContext cc); protected void activate(final BundleContext cc); protected void activate(final Map<String, Object> properties, final ComponentContext cc); protected void activate(final Map<String, Object> properties, final BundleContext cc);

Page 44: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Declarative Services

•  A service is by default only started if someone else uses it! •  Lazy is always good and usually sufficient!

•  Immediate flag on @Component forces a service start (use with care!)

•  References are always bound through methods •  SCR Plugin generates methods for unary references at built time

44

Page 45: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Unary References - Revisited

45

package com.adobe.osgitraining.impl;import org.apache.felix.scr.annotations.Component;import org.apache.felix.scr.annotations.Service;import org.osgi.service.event.EventHandler;@Component@Service(value=EventHandler.class)public class MyComponent implements EventHandler { @Reference privateTDistributor distributor; protected void bindDistributor(Distributor d) { this.distributor = d; } protected void unbindDistributor(Distributor d) { if ( this.distributor == d ) { this.distributor = null; } }

Generated

Generated

Page 46: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

References to Multiple Services

§  Create bind / unbind methods

46

@Reference(name="AdapterFactory", referenceInterface=AdapterFactory.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)public class AdapterManagerImpl implements AdapterManager protected void bindAdapterFactory(ServiceReference reference) { // use component context to get the service } protected void bindAdapterFactory(AdapterFactory factory) { } protected void bindAdapterFactory(AdapterFactory factory, Map<String, Object> serviceProps) { }

Page 47: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Apache Felix SCR Tooling

•  Combines everything (DS, Configuration Admin, Metatype, Maven/Ant)

•  Annotation-based

•  Single-source development = only java code

•  Annotate components

•  Properties with default values and metatype info

•  Provided services

•  Services references (policy and cardinality)

•  Generates DS XML

•  Generates Metatype descriptors

•  Generates Java code (for reference handling)

•  Extensible by “annotation plugins”

47

Page 48: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Component Specification

•  XML Configuration •  Contained in bundle

•  Manifest entry pointing to config(s)

•  Publishing services (through OSGi registry)

•  Consuming services

•  Reference policy (static,dynamic),

•  Reference cardinality (0..1, 1..1, 0..n)

•  Default configuration

•  Service lifecycle management

48

Page 49: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Declarative Services

•  Reads XML configs on bundle start

•  Registers services (service factories)

•  Keeps track of dependencies •  Starts/stops services

•  Invokes optional activation and deactivation method •  Provides access to configuration

•  Leverages OSGi service registry •  Plays well with other component management approaches!

49

Page 50: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Example Application �

����

50

Page 51: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Configuring A Component

§  Today’s problems

§  Property definitions are lengthy…

§  ..and scattered across the code…

§  Conversion of configuration values

§  A lot of boilerplate code

51

Page 52: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Complex Sample

52

@Component@Property(name="service.ranking", intValue=15)public class MyComponent { private static final boolean DEFAULT_ENABLED = true; @Property(boolValue=DEFAULT_ENABLED) private static final String PROP_ENABLED = "enabled"; @Property(value = {"topicA", "topicB"}) private static final String PROP_TOPIC = "enabled"; @Property private static final String PROP_USERNAME = "userName"; String userName; String[] topics; @Activate protected void activate(final Map<String, Object> config) { final boolean enabled = PropertiesUtil.toBoolean(config.get(PROP_ENABLED), DEFAULT_ENABLED); if ( enabled ) { this.userName = PropertiesUtil.toString(config.get(PROP_USERNAME), null); this.topics = PropertiesUtil.toStringArray(config.get(PROP_TOPIC)); } }}

Page 53: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Define Configuration Annotation….

53

@interface MyConfig { boolean enabled() default true; String[] topic() default {"topicA", "topicB"}; String userName(); int service_ranking() default 15;}

Page 54: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

..and use in lifecycle method

54

@Component public class MyComponent { String userName; String[] topics; @Activate protected void activate(final MyConfig config) { // note: annotation MyConfig used as interface if ( config.enabled() ) { this.userName = config.userName(); this.topics = config.topic(); } }

Page 55: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Or even simpler…

55

@Componentpublic class MyComponent { private MyConfig configuration; @Activate protected void activate(final MyConfig config) { // note: annotation MyConfig used as interface if ( config.enabled() ) { this.configuration = config; } }}

Page 56: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

In the works: Metatype Support (RFC 208)

56

@ObjectClassDefinition(label="My Component", description="Coolest component in the world.")@interface MyConfig { @AttributeDefinition(label="Enabled", description="Topic and user name are used if enabled") boolean enabled() default true; @AttributeDefinition(...) String[] topic() default {"topicA", "topicB"}; @AttributeDefinition(...) String userName(); int service_ranking() default 15; // maps to service.ranking}

Page 57: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

Declarative Service Enhancements (RFC 190)

§  Annotation Configuration Support

§  Support for service scopes (prototypes)

§  Introspection API

57

Page 58: Building OSGi Components -   · PDF fileAgenda ! 1 OSGi Service Registry ! 2 Components ! 3 Declarative Services Today ! 4 Next version of Declarative Services 3

QnA

58