SlideShare une entreprise Scribd logo
1  sur  30
Télécharger pour lire hors ligne
OSGI
DECLARATIVE
SERVICES
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
2
Getting Started with OSGi Declarative Services
Speaker
Dirk Fauth
Software-Architect Rich Client Systeme
Eclipse Committer
Robert Bosch GmbH
Franz-Oechsle-Straße 4
73207 Plochingen
dirk.fauth@de.bosch.com
www.bosch.com
blog.vogella.com/author/fipro/
Twitter: fipro78
OVERVIEW
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
4
 Bundles register (publish) services
 Bundles get (bind) services
 Bundles listen (find) services
Publish-Find-Bind
A BS
register
(publish)
get
(bind)
listen
(find)
<<bundle>> <<bundle>><<service>>
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
5
 (Service) Component
‒ Java class contained in a bundle
‒ Declared via Component Description
 Component Description
‒ XML document to declare a Service Component
 Component Configuration
‒ Component Description that is parameterized with component properties
‒ Tracks the component dependencies and manages the component instance
 Component Instance
‒ Instance of the component implementation class
‒ Created when a Component Configuration is activated
‒ Discarded when the Component Configuration is deactivated
Components
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
6
References
 References
‒ The definition of dependencies to other services.
 Target Services
‒ The services that match the reference interface and target property filter.
 Bound Services
‒ The services that are bound to a Component Configuration.
 Access Strategies
‒ Event Strategy
‒ Lookup Strategy
‒ Field Strategy (1.3)
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
7
Component Types
 Delayed Component
‒ Activated when the service is requested
‒ Needs to specify a service
 Immediate Component
‒ Activated as soon as all dependencies are satisfied
‒ Does not need to specify a service
 Factory Component
‒ Creates and activates Component Configurations
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
8
Component Lifecycle – Immediate
UNSATISFIED
activate ACTIVE
deactivate
Bundle
started
Bundle
stopped
disable
load
enabled
becomes
satisfied
becomes
unsatisfied
if dynamic:
rebinding
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
9
Component Lifecycle – Delayed
UNSATISFIED
activate ACTIVE
deactivate
Bundle
started
Bundle
stopped
disable
load
enabled
becomes
satisfied
becomes
unsatisfied
if dynamic:
rebinding
REGISTERED
get service
becomes
unsatisfied
unget
service
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
10
 Activation consists of the following steps:
1. Load the component implementation class
2. Create the component instance and component context
3. Bind the target services
4. Call the activate method if present
 For Delayed Components the load time is moved to the first request
(including reference bindings)
(see Declarative Services Specification Version 1.3 – 112.5.6 Activation)
Component Lifecycle – Activation
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
11
Component Lifecycle – Factory
UNSATISFIED
activate
ACTIVE
deactivate
Bundle
started
Bundle
stopped
disable
load
enabled
becomes
satisfied
becomes
unsatisfied
if dynamic:
rebinding
FACTORY
newInstance
becomes
unsatisfieddispose
register
unregister
IMPLEMENT &
PUBLISH
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
13
 Create a bundle for the service API (e.g. org.fipro.inverter.api)
 Create the service interface
Service API
public interface StringInverter {
String invert(String input);
}
 Make the service implementation exchangeable
 Clean dependency hierarchy
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
14
Service API
ErrorHandler
ErrorHandlerImpl
<<consumer bundle>>
<<ErrorHandler Bundle>>
<<SWT>>
<<API>> <<Provider>>
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
15
Metadata
• Create/Modify .bnd file
• Describe OSGi meta-data
• MANIFEST.MF is generated
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
16
 Create a bundle for the service implementation
(e.g. org.fipro.inverter.provider)
 Create the service implementation
Service Provider
@Component
public class StringInverterImpl implements StringInverter {
@Override
public String invert(String input) {
return new StringBuilder(input).reverse().toString();
}
}
@Component
public class StringInverterImpl implements StringInverter {
@Override
public String invert(String input) {
return new StringBuilder(input).reverse().toString();
}
}
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
17
Metadata
• Add API bundle to the Build Path
• Select the annotations to be used
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
18
 Annotation to mark a Java class as a Service Component
 Generates
‒ Component Description XML file
‒ Additional header in MANIFEST.MF
‒ Service-Component header
‒ Provide-Capability header for osgi.service
‒ Require-Capability header for osgi.extender (DS 1.3)
@Component
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
19
 Capability = non-code dependency
 osgi.extender=osgi.component
‒ added to spec with DS 1.3
‒ Equinox DS adapted for DS 1.2 with Eclipse Neon
 osgi.service
‒ specify the provided service implementations
Capabilities
Require-Capability: osgi.extender;
filter:="(&(osgi.extender=osgi.component)(version>=1.3)(!(version>=2.0)))"
Provide-Capability: osgi.service;
objectClass:List<String>="org.fipro.inverter.StringInverter"
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
20
@Component
Type Element Default Type Element Default
configurationPid full qualified class name of
the component
property empty
configurationPolicy optional service full qualified class names of all
directly implemented interfaces
enabled true servicefactory
(depr. in 1.3)
false
factory empty String xmlns lowest DS XML namespace that
supports used features
immediate false if factory or service
true otherwise
name full qualified class name of
the component
reference
(since 1.3)
empty
properties empty scope
(since 1.3)
SINGLETON
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
21
 Component life cycle methods
 Method parameters
‒ ComponentContext
‒ BundleContext
‒ Map<String, ?>
Map containing component properties
‒ int / Integer for (@Deactivate)
‒ <Component Property Type> (DS 1.3)
Type safe access to component properties
@Activate / @Modified / @Deactivate
@Activate
private void activate(
ComponentContext c,
BundleContext b,
Map<String, ?> properties) {
//do some initialization stuff
}
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
22
 Create a bundle for the service consumer
(e.g. org.fipro.inverter.command)
 Create the service consumer implementation
Service Consumer
@Component(property= {"osgi.command.scope:String=fipro",
"osgi.command.function:String=invert"},
service=StringInverterCommand.class
)
public class StringInverterCommand {
private StringInverter inverter;
@Reference
void bindStringInverter(StringInverter inverter) { this.inverter = inverter; }
public void invert(String input) { System.out.println(inverter.invert(input)); }
}
@Component(property= {"osgi.command.scope:String=fipro",
"osgi.command.function:String=invert"},
service=StringInverterCommand.class
)
public class StringInverterCommand {
private StringInverter inverter;
@Reference
void bindStringInverter(StringInverter inverter) { this.inverter = inverter; }
public void invert(String input) { System.out.println(inverter.invert(input)); }
}
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
23
 Specify dependency on other services
 Resolving references is required to satisfy a component
(if the reference is not optional)
 Different strategies for accessing services
‒ Event Strategy
‒ Field Strategy (DS 1.3)
‒ Lookup Strategy
@Reference
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
24
@Reference
 Event Strategy – using event methods for bind/updated/unbind
@Component(...)
public class StringInverterCommand {
private StringInverter inverter;
@Reference
void bindStringInverter(StringInverter inverter) { this.inverter = inverter; }
void updatedStringInverter(
StringInverter inverter, Map<String, ?> properties) { //do something }
void unbindStringInverter(StringInverter inverter) { this.inverter = null; }
public void invert(String input) { System.out.println(inverter.invert(input)); }
}
@Component(...)
public class StringInverterCommand {
private StringInverter inverter;
@Reference
void bindStringInverter(StringInverter inverter) { this.inverter = inverter; }
void updatedStringInverter(
StringInverter inverter, Map<String, ?> properties) { //do something }
void unbindStringInverter(StringInverter inverter) { this.inverter = null; }
public void invert(String input) { System.out.println(inverter.invert(input)); }
}
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
25
 ServiceReference
 <service type>
 <service type> + Map<String, ?>
With DS 1.3
 ComponentServiceObjects
 Different variations of the parameter list
@Reference - Event Method Parameter
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
26
@Reference
 Field Strategy (DS 1.3) – using instance fields
@Component(...)
public class StringInverterCommand {
@Reference
private StringInverter inverter;
public void invert(String input) {
System.out.println(inverter.invert(input));
}
}
@Component(...)
public class StringInverterCommand {
@Reference
private StringInverter inverter;
public void invert(String input) {
System.out.println(inverter.invert(input));
}
}
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
27
@Reference
 Lookup Strategy (DS 1.2) – lookup everytime needed, do not store
@Component(...)
public class StringInverterCommand {
private ComponentContext context;
private ServiceReference<StringInverter> reference;
@Activate
void activate(ComponentContext context) { this.context = context; }
@Reference
void setStringInverter(ServiceReference<StringInverter> reference) { this.reference = reference; }
public void invert(String input) {
StringInverter inverter =
(StringInverter) context.locateService("StringInverter", reference);
...
}
}
@Component(...)
public class StringInverterCommand {
private ComponentContext context;
private ServiceReference<StringInverter> reference;
@Activate
void activate(ComponentContext context) { this.context = context; }
@Reference
void setStringInverter(ServiceReference<StringInverter> reference) { this.reference = reference; }
public void invert(String input) {
StringInverter inverter =
(StringInverter) context.locateService("StringInverter", reference);
...
}
}
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
28
@Reference
 Lookup Strategy (DS 1.3) – lookup everytime needed, do not store
@Component(...
reference=@Reference(name="inverter", service=StringInverter.class)
)
public class StringInverterCommand {
private ComponentContext context;
@Activate
void activate(ComponentContext context) { this.context = context; }
public void invert(String input) {
StringInverter inverter = (StringInverter) context.locateService("inverter");
...
}
}
@Component(...
reference=@Reference(name="inverter", service=StringInverter.class)
)
public class StringInverterCommand {
private ComponentContext context;
@Activate
void activate(ComponentContext context) { this.context = context; }
public void invert(String input) {
StringInverter inverter = (StringInverter) context.locateService("inverter");
...
}
}
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
29
@Reference
Type Element Default Type Element Default
cardinality • 1:1 for event methods
• 1:1 for non-collection
fields, 0..n for collections
unbind unbind<name>
unset<name>
remove<name>
name • bind event method name
without bind prefix
• name of the field
updated updated<name>
if such a method exists
policy STATIC
policyOption RELUCTANT bind
(since 1.3)
the name of the annotated
method or empty
service full qualified class name of
the referenced service
field
(since 1.3)
the name of the annotated field
or empty
target empty String fieldOption
(since 1.3)
REPLACE
scope
(since 1.3)
BUNDLE
OSGi Declarative Services
Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016
© Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen.
30
Further information
 OSGi Compendium Specification
https://www.osgi.org/developer/specifications/
 enRoute Documentation
http://enroute.osgi.org/book/210-doc.html
 Blog posts
Getting Started with OSGi Declarative Services
http://blog.vogella.com/2016/06/21/getting-started-with-osgi-declarative-services/
OSGi Component Testing
http://blog.vogella.com/2016/07/04/osgi-component-testing/
Configuring OSGi Declarative Services
http://blog.vogella.com/2016/09/26/configuring-osgi-declarative-services/

Contenu connexe

En vedette

OSGi Specification Evolution - BJ Hargrave
OSGi Specification Evolution - BJ HargraveOSGi Specification Evolution - BJ Hargrave
OSGi Specification Evolution - BJ Hargravemfrancis
 
RESTful Web Applications with Apache Sling
RESTful Web Applications with Apache SlingRESTful Web Applications with Apache Sling
RESTful Web Applications with Apache SlingBertrand Delacretaz
 
OSGi, Scripting and REST, Building Webapps With Apache Sling
OSGi, Scripting and REST, Building Webapps With Apache SlingOSGi, Scripting and REST, Building Webapps With Apache Sling
OSGi, Scripting and REST, Building Webapps With Apache SlingCarsten Ziegeler
 
Use Case: Building OSGi Enterprise Applications (QCon 14)
Use Case: Building OSGi Enterprise Applications (QCon 14)Use Case: Building OSGi Enterprise Applications (QCon 14)
Use Case: Building OSGi Enterprise Applications (QCon 14)Carsten Ziegeler
 
Service oriented web development with OSGi
Service oriented web development with OSGiService oriented web development with OSGi
Service oriented web development with OSGiCarsten Ziegeler
 
Monitoring OSGi Applications with the Web Console
Monitoring OSGi Applications with the Web ConsoleMonitoring OSGi Applications with the Web Console
Monitoring OSGi Applications with the Web ConsoleCarsten Ziegeler
 
OSGi Best Practices - Tim Ward
OSGi Best Practices - Tim WardOSGi Best Practices - Tim Ward
OSGi Best Practices - Tim Wardmfrancis
 
Infrastructure as code: running microservices on AWS using Docker, Terraform,...
Infrastructure as code: running microservices on AWS using Docker, Terraform,...Infrastructure as code: running microservices on AWS using Docker, Terraform,...
Infrastructure as code: running microservices on AWS using Docker, Terraform,...Yevgeniy Brikman
 

En vedette (10)

OSGi Specification Evolution - BJ Hargrave
OSGi Specification Evolution - BJ HargraveOSGi Specification Evolution - BJ Hargrave
OSGi Specification Evolution - BJ Hargrave
 
Reactive applications
Reactive applicationsReactive applications
Reactive applications
 
RESTful Web Applications with Apache Sling
RESTful Web Applications with Apache SlingRESTful Web Applications with Apache Sling
RESTful Web Applications with Apache Sling
 
OSGi, Scripting and REST, Building Webapps With Apache Sling
OSGi, Scripting and REST, Building Webapps With Apache SlingOSGi, Scripting and REST, Building Webapps With Apache Sling
OSGi, Scripting and REST, Building Webapps With Apache Sling
 
Use Case: Building OSGi Enterprise Applications (QCon 14)
Use Case: Building OSGi Enterprise Applications (QCon 14)Use Case: Building OSGi Enterprise Applications (QCon 14)
Use Case: Building OSGi Enterprise Applications (QCon 14)
 
Service oriented web development with OSGi
Service oriented web development with OSGiService oriented web development with OSGi
Service oriented web development with OSGi
 
Monitoring OSGi Applications with the Web Console
Monitoring OSGi Applications with the Web ConsoleMonitoring OSGi Applications with the Web Console
Monitoring OSGi Applications with the Web Console
 
OSGi Best Practices - Tim Ward
OSGi Best Practices - Tim WardOSGi Best Practices - Tim Ward
OSGi Best Practices - Tim Ward
 
Infrastructure as code: running microservices on AWS using Docker, Terraform,...
Infrastructure as code: running microservices on AWS using Docker, Terraform,...Infrastructure as code: running microservices on AWS using Docker, Terraform,...
Infrastructure as code: running microservices on AWS using Docker, Terraform,...
 
The AI Rush
The AI RushThe AI Rush
The AI Rush
 

Plus de mfrancis

Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...
Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...
Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...mfrancis
 
OSGi and Java 9+ - BJ Hargrave (IBM)
OSGi and Java 9+ - BJ Hargrave (IBM)OSGi and Java 9+ - BJ Hargrave (IBM)
OSGi and Java 9+ - BJ Hargrave (IBM)mfrancis
 
Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)
Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)
Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)mfrancis
 
OSGi for the data centre - Connecting OSGi to Kubernetes - Frank Lyaruu
OSGi for the data centre - Connecting OSGi to Kubernetes - Frank LyaruuOSGi for the data centre - Connecting OSGi to Kubernetes - Frank Lyaruu
OSGi for the data centre - Connecting OSGi to Kubernetes - Frank Lyaruumfrancis
 
Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...
Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...
Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...mfrancis
 
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...mfrancis
 
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...mfrancis
 
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)mfrancis
 
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...mfrancis
 
OSGi CDI Integration Specification - Ray Augé (Liferay)
OSGi CDI Integration Specification - Ray Augé (Liferay)OSGi CDI Integration Specification - Ray Augé (Liferay)
OSGi CDI Integration Specification - Ray Augé (Liferay)mfrancis
 
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...mfrancis
 
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...mfrancis
 
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...mfrancis
 
Popular patterns revisited on OSGi - Christian Schneider (Adobe)
Popular patterns revisited on OSGi - Christian Schneider (Adobe)Popular patterns revisited on OSGi - Christian Schneider (Adobe)
Popular patterns revisited on OSGi - Christian Schneider (Adobe)mfrancis
 
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)mfrancis
 
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)mfrancis
 
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...mfrancis
 
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)mfrancis
 
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...mfrancis
 
How to connect your OSGi application - Dirk Fauth (Bosch)
How to connect your OSGi application - Dirk Fauth (Bosch)How to connect your OSGi application - Dirk Fauth (Bosch)
How to connect your OSGi application - Dirk Fauth (Bosch)mfrancis
 

Plus de mfrancis (20)

Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...
Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...
Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...
 
OSGi and Java 9+ - BJ Hargrave (IBM)
OSGi and Java 9+ - BJ Hargrave (IBM)OSGi and Java 9+ - BJ Hargrave (IBM)
OSGi and Java 9+ - BJ Hargrave (IBM)
 
Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)
Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)
Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)
 
OSGi for the data centre - Connecting OSGi to Kubernetes - Frank Lyaruu
OSGi for the data centre - Connecting OSGi to Kubernetes - Frank LyaruuOSGi for the data centre - Connecting OSGi to Kubernetes - Frank Lyaruu
OSGi for the data centre - Connecting OSGi to Kubernetes - Frank Lyaruu
 
Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...
Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...
Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...
 
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
 
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
 
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
 
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
 
OSGi CDI Integration Specification - Ray Augé (Liferay)
OSGi CDI Integration Specification - Ray Augé (Liferay)OSGi CDI Integration Specification - Ray Augé (Liferay)
OSGi CDI Integration Specification - Ray Augé (Liferay)
 
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
 
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
 
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
 
Popular patterns revisited on OSGi - Christian Schneider (Adobe)
Popular patterns revisited on OSGi - Christian Schneider (Adobe)Popular patterns revisited on OSGi - Christian Schneider (Adobe)
Popular patterns revisited on OSGi - Christian Schneider (Adobe)
 
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
 
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
 
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
 
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
 
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
 
How to connect your OSGi application - Dirk Fauth (Bosch)
How to connect your OSGi application - Dirk Fauth (Bosch)How to connect your OSGi application - Dirk Fauth (Bosch)
How to connect your OSGi application - Dirk Fauth (Bosch)
 

Getting started with OSGi declarative services - Dirk Fauth

  • 2. Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 2 Getting Started with OSGi Declarative Services Speaker Dirk Fauth Software-Architect Rich Client Systeme Eclipse Committer Robert Bosch GmbH Franz-Oechsle-Straße 4 73207 Plochingen dirk.fauth@de.bosch.com www.bosch.com blog.vogella.com/author/fipro/ Twitter: fipro78
  • 4. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 4  Bundles register (publish) services  Bundles get (bind) services  Bundles listen (find) services Publish-Find-Bind A BS register (publish) get (bind) listen (find) <<bundle>> <<bundle>><<service>>
  • 5. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 5  (Service) Component ‒ Java class contained in a bundle ‒ Declared via Component Description  Component Description ‒ XML document to declare a Service Component  Component Configuration ‒ Component Description that is parameterized with component properties ‒ Tracks the component dependencies and manages the component instance  Component Instance ‒ Instance of the component implementation class ‒ Created when a Component Configuration is activated ‒ Discarded when the Component Configuration is deactivated Components
  • 6. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 6 References  References ‒ The definition of dependencies to other services.  Target Services ‒ The services that match the reference interface and target property filter.  Bound Services ‒ The services that are bound to a Component Configuration.  Access Strategies ‒ Event Strategy ‒ Lookup Strategy ‒ Field Strategy (1.3)
  • 7. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 7 Component Types  Delayed Component ‒ Activated when the service is requested ‒ Needs to specify a service  Immediate Component ‒ Activated as soon as all dependencies are satisfied ‒ Does not need to specify a service  Factory Component ‒ Creates and activates Component Configurations
  • 8. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 8 Component Lifecycle – Immediate UNSATISFIED activate ACTIVE deactivate Bundle started Bundle stopped disable load enabled becomes satisfied becomes unsatisfied if dynamic: rebinding
  • 9. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 9 Component Lifecycle – Delayed UNSATISFIED activate ACTIVE deactivate Bundle started Bundle stopped disable load enabled becomes satisfied becomes unsatisfied if dynamic: rebinding REGISTERED get service becomes unsatisfied unget service
  • 10. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 10  Activation consists of the following steps: 1. Load the component implementation class 2. Create the component instance and component context 3. Bind the target services 4. Call the activate method if present  For Delayed Components the load time is moved to the first request (including reference bindings) (see Declarative Services Specification Version 1.3 – 112.5.6 Activation) Component Lifecycle – Activation
  • 11. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 11 Component Lifecycle – Factory UNSATISFIED activate ACTIVE deactivate Bundle started Bundle stopped disable load enabled becomes satisfied becomes unsatisfied if dynamic: rebinding FACTORY newInstance becomes unsatisfieddispose register unregister
  • 13. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 13  Create a bundle for the service API (e.g. org.fipro.inverter.api)  Create the service interface Service API public interface StringInverter { String invert(String input); }  Make the service implementation exchangeable  Clean dependency hierarchy
  • 14. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 14 Service API ErrorHandler ErrorHandlerImpl <<consumer bundle>> <<ErrorHandler Bundle>> <<SWT>> <<API>> <<Provider>>
  • 15. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 15 Metadata • Create/Modify .bnd file • Describe OSGi meta-data • MANIFEST.MF is generated
  • 16. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 16  Create a bundle for the service implementation (e.g. org.fipro.inverter.provider)  Create the service implementation Service Provider @Component public class StringInverterImpl implements StringInverter { @Override public String invert(String input) { return new StringBuilder(input).reverse().toString(); } } @Component public class StringInverterImpl implements StringInverter { @Override public String invert(String input) { return new StringBuilder(input).reverse().toString(); } }
  • 17. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 17 Metadata • Add API bundle to the Build Path • Select the annotations to be used
  • 18. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 18  Annotation to mark a Java class as a Service Component  Generates ‒ Component Description XML file ‒ Additional header in MANIFEST.MF ‒ Service-Component header ‒ Provide-Capability header for osgi.service ‒ Require-Capability header for osgi.extender (DS 1.3) @Component
  • 19. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 19  Capability = non-code dependency  osgi.extender=osgi.component ‒ added to spec with DS 1.3 ‒ Equinox DS adapted for DS 1.2 with Eclipse Neon  osgi.service ‒ specify the provided service implementations Capabilities Require-Capability: osgi.extender; filter:="(&(osgi.extender=osgi.component)(version>=1.3)(!(version>=2.0)))" Provide-Capability: osgi.service; objectClass:List<String>="org.fipro.inverter.StringInverter"
  • 20. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 20 @Component Type Element Default Type Element Default configurationPid full qualified class name of the component property empty configurationPolicy optional service full qualified class names of all directly implemented interfaces enabled true servicefactory (depr. in 1.3) false factory empty String xmlns lowest DS XML namespace that supports used features immediate false if factory or service true otherwise name full qualified class name of the component reference (since 1.3) empty properties empty scope (since 1.3) SINGLETON
  • 21. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 21  Component life cycle methods  Method parameters ‒ ComponentContext ‒ BundleContext ‒ Map<String, ?> Map containing component properties ‒ int / Integer for (@Deactivate) ‒ <Component Property Type> (DS 1.3) Type safe access to component properties @Activate / @Modified / @Deactivate @Activate private void activate( ComponentContext c, BundleContext b, Map<String, ?> properties) { //do some initialization stuff }
  • 22. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 22  Create a bundle for the service consumer (e.g. org.fipro.inverter.command)  Create the service consumer implementation Service Consumer @Component(property= {"osgi.command.scope:String=fipro", "osgi.command.function:String=invert"}, service=StringInverterCommand.class ) public class StringInverterCommand { private StringInverter inverter; @Reference void bindStringInverter(StringInverter inverter) { this.inverter = inverter; } public void invert(String input) { System.out.println(inverter.invert(input)); } } @Component(property= {"osgi.command.scope:String=fipro", "osgi.command.function:String=invert"}, service=StringInverterCommand.class ) public class StringInverterCommand { private StringInverter inverter; @Reference void bindStringInverter(StringInverter inverter) { this.inverter = inverter; } public void invert(String input) { System.out.println(inverter.invert(input)); } }
  • 23. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 23  Specify dependency on other services  Resolving references is required to satisfy a component (if the reference is not optional)  Different strategies for accessing services ‒ Event Strategy ‒ Field Strategy (DS 1.3) ‒ Lookup Strategy @Reference
  • 24. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 24 @Reference  Event Strategy – using event methods for bind/updated/unbind @Component(...) public class StringInverterCommand { private StringInverter inverter; @Reference void bindStringInverter(StringInverter inverter) { this.inverter = inverter; } void updatedStringInverter( StringInverter inverter, Map<String, ?> properties) { //do something } void unbindStringInverter(StringInverter inverter) { this.inverter = null; } public void invert(String input) { System.out.println(inverter.invert(input)); } } @Component(...) public class StringInverterCommand { private StringInverter inverter; @Reference void bindStringInverter(StringInverter inverter) { this.inverter = inverter; } void updatedStringInverter( StringInverter inverter, Map<String, ?> properties) { //do something } void unbindStringInverter(StringInverter inverter) { this.inverter = null; } public void invert(String input) { System.out.println(inverter.invert(input)); } }
  • 25. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 25  ServiceReference  <service type>  <service type> + Map<String, ?> With DS 1.3  ComponentServiceObjects  Different variations of the parameter list @Reference - Event Method Parameter
  • 26. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 26 @Reference  Field Strategy (DS 1.3) – using instance fields @Component(...) public class StringInverterCommand { @Reference private StringInverter inverter; public void invert(String input) { System.out.println(inverter.invert(input)); } } @Component(...) public class StringInverterCommand { @Reference private StringInverter inverter; public void invert(String input) { System.out.println(inverter.invert(input)); } }
  • 27. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 27 @Reference  Lookup Strategy (DS 1.2) – lookup everytime needed, do not store @Component(...) public class StringInverterCommand { private ComponentContext context; private ServiceReference<StringInverter> reference; @Activate void activate(ComponentContext context) { this.context = context; } @Reference void setStringInverter(ServiceReference<StringInverter> reference) { this.reference = reference; } public void invert(String input) { StringInverter inverter = (StringInverter) context.locateService("StringInverter", reference); ... } } @Component(...) public class StringInverterCommand { private ComponentContext context; private ServiceReference<StringInverter> reference; @Activate void activate(ComponentContext context) { this.context = context; } @Reference void setStringInverter(ServiceReference<StringInverter> reference) { this.reference = reference; } public void invert(String input) { StringInverter inverter = (StringInverter) context.locateService("StringInverter", reference); ... } }
  • 28. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 28 @Reference  Lookup Strategy (DS 1.3) – lookup everytime needed, do not store @Component(... reference=@Reference(name="inverter", service=StringInverter.class) ) public class StringInverterCommand { private ComponentContext context; @Activate void activate(ComponentContext context) { this.context = context; } public void invert(String input) { StringInverter inverter = (StringInverter) context.locateService("inverter"); ... } } @Component(... reference=@Reference(name="inverter", service=StringInverter.class) ) public class StringInverterCommand { private ComponentContext context; @Activate void activate(ComponentContext context) { this.context = context; } public void invert(String input) { StringInverter inverter = (StringInverter) context.locateService("inverter"); ... } }
  • 29. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 29 @Reference Type Element Default Type Element Default cardinality • 1:1 for event methods • 1:1 for non-collection fields, 0..n for collections unbind unbind<name> unset<name> remove<name> name • bind event method name without bind prefix • name of the field updated updated<name> if such a method exists policy STATIC policyOption RELUCTANT bind (since 1.3) the name of the annotated method or empty service full qualified class name of the referenced service field (since 1.3) the name of the annotated field or empty target empty String fieldOption (since 1.3) REPLACE scope (since 1.3) BUNDLE
  • 30. OSGi Declarative Services Automotive Service Solutions | AA-AS/EIS2-EU | 27.10.2016 © Robert Bosch GmbH 2016. Alle Rechte vorbehalten, auch bzgl. jeder Verfügung, Verwertung, Reproduktion, Bearbeitung, Weitergabe sowie für den Fall von Schutzrechtsanmeldungen. 30 Further information  OSGi Compendium Specification https://www.osgi.org/developer/specifications/  enRoute Documentation http://enroute.osgi.org/book/210-doc.html  Blog posts Getting Started with OSGi Declarative Services http://blog.vogella.com/2016/06/21/getting-started-with-osgi-declarative-services/ OSGi Component Testing http://blog.vogella.com/2016/07/04/osgi-component-testing/ Configuring OSGi Declarative Services http://blog.vogella.com/2016/09/26/configuring-osgi-declarative-services/