SlideShare uma empresa Scribd logo
1 de 26
Baixar para ler offline
Field injection, type safe configuration,
and more new goodies in Declarative
Services
BJ Hargrave, IBM
a Component
Impl
a Service Impl
Service
Component
Runtime Impl
a Servicea Component
Instance
Component
Description
a Component
Confguration
registered service
tracks
dependencies
declarescomponent
createdby
controls 0..n
0..n
0..n
references
1..n
1
Configuration
Admin
0..n
1
0..n
1
1
<<service>>
Service Component
Runtime
Declarative Services
• A declarative model for publishing and
consuming OSGi services

• Introduced in Release 4 in 2005, it greatly
simplified programming OSGi services

• Service Component Runtime, SCR, is the
runtime which implements the spec and
manages the service components
Service Component
• A Java class which can optionally be registered as a service and can optionally
use services
• Described by XML in the bundle which is processed at runtime by SCR
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
name="example.provider.ExampleImpl" activate="activate">
<implementation class="example.provider.ExampleImpl"/>
<service>
<provide interface="example.api.Example"/>
</service>
<reference name="Log" interface="org.osgi.service.log.LogService" bind="setLog"/>
</scr:component>
Service Component using annotations
• But who wants to write XML?
• So DS 1.2 added support for
programming with annotations
• Tools, like , process the
annotations into the XML used by
SCR at runtime
@Component
public class ExampleImpl implements Example {
private Map<String, Object> properties;
private LogService log;
@Activate
void activate(Map<String, Object> map) {
properties = map;
}
@Override
public boolean say(String message) {
log.log((int) properties.get("loglevel"), message);
return false;
}
@Reference
void setLog(LogService log) {
this.log = log;
}
}
Pretty
cool!
So what is new in Declarative Services 1.3?
• Field Injection
• Type Safe Configuration with MetaType integration
• Configuration Admin integration improvements
• Multiple PIDs
• Introspection
• And more small improvements we don’t have time to discuss today
• See 112.17 “Changes” in the DS 1.3 spec
Field Injection
Injection
• Method injection
• Constructor injection
• Field injection
@Reference
void setLog(LogService log) {
this.log = log;
}
Field Injection
• Scalar cardinality
• Static policy
• Dynamic policy
@Reference
private Example target;
@Reference
private volatile Example target;
https://github.com/bjhargrave/osgice2015/blob/master/1ScalarFieldRef/src/example/command/ExampleCommand.java#L17
Field Injection
• Multiple cardinality
• Service type from generic signature
• SCR-managed collection implementation
• Static policy
• Dynamic policy
@Reference
private List<Example> targets;
https://github.com/bjhargrave/osgice2015/blob/master/2MultipleFieldRef/src/example/command/ExampleCommand.java#L20
@Reference
private volatile List<Example> targets;
Field Injection
• User managed collection
• Can also inject fields of types
related to services and
collections of those types
@Reference(policy = ReferencePolicy.DYNAMIC)
private final List<Example> targets = new
CopyOnWriteArrayList<>();
@Reference
ServiceReference<Example> sr;
@Reference
ComponentServiceObjects<Example> so;
@Reference(service=Example.class)
Map<String,Object> props;
@Reference
Map.Entry<Map<String,Object>,Example> tuple;
Type Safe Configuration
Component Properties
• Component properties come from
• Component description
• Configuration Admin configuration
• ComponentFactory.newInstance
<property name=“myport" type="Integer"
value="8080"/>
Dictionary<String, Object> props = new Hashtable<>();
props.put("myport", Integer.valueOf(8081));
cm.getConfiguration("Example").update(props);
Dictionary<String, Object> props = new Hashtable<>();
props.put("myport", Integer.valueOf(8082));
cf.newInstance(props);
Getting the property values
• Activate method
• But what if the configuration value for myport was of type String? Or type Long?
• Type safety failure in your code
• You also need to handle the case where the property is not set
private int myport;
@Activate
void activate(Map<String, Object> props) {
myport = props.containsKey("myport")
? ((Integer) props.get("myport")).intValue() : 8080;
}
Component Property Types
• Define and use your component properties in a type safe manner using
annotations!
@interface Props {
int myport() default 8080;
}
@Component
public class ExampleImpl implements Example {
private int myport;
@Activate
void activate(Props props) {
myport = props.myport();
}
}
https://github.com/bjhargrave/osgice2015/blob/master/3TypeSafeConfig/src/example/provider/ExampleImpl.java#L14
Type Safe Configuration
and MetaType
Component Property Types integrate with MetaType Service
• Use the new MetaType annotations to define metatype resources and
associate them with your component
https://github.com/bjhargrave/osgice2015/blob/master/4TypeSafeConfigWithMetatype/src/example/provider/ExampleImpl.java#L14
@ObjectClassDefinition
@interface Props {
int myport() default 8080;
}
@Designate(ocd=Props.class)
@Component
public class ExampleImpl implements Example {
private int myport;
@Activate
void activate(Props props) {
myport = props.myport();
Multiple Configurations
Component properties from Configurations
• DS integrates with Configuration Admin and will get component properties
from a configuration with the configuration pid of the component
@ObjectClassDefinition(pid = "Example")
@interface Name {
String name() default "Default Name";
}
@Component(configurationPid = "Example")
public class ExampleImpl implements Example {
private String name;
@Activate
void activate(Name name) {
this.name = name.name();
}
}
Sometimes we need to use multiple configurations
• A component may want to use a “system” configuration as well as a specific
configuration
@ObjectClassDefinition(pid = “System")
@interface Name {
String name() default "Default Name";
}
@ObjectClassDefinition(pid = "Example")
@interface Words {
String hello() default "Hello";
String goodbye() default "Goodbye";
}
@Component(configurationPid = {“System", "Example"})
public class ExampleImpl implements Example {
private String name;
private Words words;
@Activate
void activate(Name name, Words words) {
this.name = name.name();
this.words = words;
}
https://github.com/bjhargrave/osgice2015/blob/master/6MultiplePIDs/src/example/provider/ExampleImpl.java#L13
Introspection
Introspecting the Service Components
• SCR now registers a ServiceComponentRuntime service which provides APIs
to introspect the service components managed by SCR
public interface ServiceComponentRuntime {
Collection<ComponentDescriptionDTO> getComponentDescriptionDTOs(Bundle... bundles);
ComponentDescriptionDTO getComponentDescriptionDTO(Bundle bundle, String name);
Collection<ComponentConfigurationDTO> getComponentConfigurationDTOs(ComponentDescriptionDTO
description);
boolean isComponentEnabled(ComponentDescriptionDTO description);
Promise<Void> enableComponent(ComponentDescriptionDTO description);
Promise<Void> disableComponent(ComponentDescriptionDTO description);
}
https://github.com/bjhargrave/osgice2015/blob/master/5Introspection/src/example/command/ExampleCommand.java#L25
SCR Data Transfer Objects
• DTOs are defined for
• Component descriptions - ComponentDescriptionDTO
• Component configurations - ComponentConfigurationDTO
• References - ReferenceDTO, SatisfiedReferenceDTO,
UnsatisfiedReferenceDTO
Why isn’t my component active?
• You can use the DTOs to figure out why
• It might be missing a required configuration or have an unsatisfied reference to
a service
public class ComponentConfigurationDTO extends DTO {
public static final int UNSATISFIED_CONFIGURATION= 1;
public static final int UNSATISFIED_REFERENCE = 2;
public static final int SATISFIED = 4;
public static final int ACTIVE = 8;
public ComponentDescriptionDTO description;
public int state;
public long id;
public Map<String, Object> properties;
public SatisfiedReferenceDTO[] satisfiedReferences;
public UnsatisfiedReferenceDTO[] unsatisfiedReferences;
}
Fin https://github.com/bjhargrave/osgice2015
Field injection, type safe configuration, and more new goodies in Declarative Services

Mais conteúdo relacionado

Mais procurados

Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)
Oro Inc.
 

Mais procurados (20)

Wcf data services
Wcf data servicesWcf data services
Wcf data services
 
.NET Core, ASP.NET Core Course, Session 17
.NET Core, ASP.NET Core Course, Session 17.NET Core, ASP.NET Core Course, Session 17
.NET Core, ASP.NET Core Course, Session 17
 
.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13
 
.NET Core, ASP.NET Core Course, Session 18
 .NET Core, ASP.NET Core Course, Session 18 .NET Core, ASP.NET Core Course, Session 18
.NET Core, ASP.NET Core Course, Session 18
 
.NET Core, ASP.NET Core Course, Session 14
.NET Core, ASP.NET Core Course, Session 14.NET Core, ASP.NET Core Course, Session 14
.NET Core, ASP.NET Core Course, Session 14
 
How to get full power from WebApi
How to get full power from WebApiHow to get full power from WebApi
How to get full power from WebApi
 
Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)
 
GraphQL 101
GraphQL 101GraphQL 101
GraphQL 101
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling Rewriter
 
.NET Core, ASP.NET Core Course, Session 6
.NET Core, ASP.NET Core Course, Session 6.NET Core, ASP.NET Core Course, Session 6
.NET Core, ASP.NET Core Course, Session 6
 
.NET Core, ASP.NET Core Course, Session 5
.NET Core, ASP.NET Core Course, Session 5.NET Core, ASP.NET Core Course, Session 5
.NET Core, ASP.NET Core Course, Session 5
 
Real World Asp.Net WebApi Applications
Real World Asp.Net WebApi ApplicationsReal World Asp.Net WebApi Applications
Real World Asp.Net WebApi Applications
 
Backbone js
Backbone jsBackbone js
Backbone js
 
Spray - Build RESTfull services in scala
Spray - Build RESTfull services in scalaSpray - Build RESTfull services in scala
Spray - Build RESTfull services in scala
 
Sling Models Using Sightly and JSP by Deepak Khetawat
Sling Models Using Sightly and JSP by Deepak KhetawatSling Models Using Sightly and JSP by Deepak Khetawat
Sling Models Using Sightly and JSP by Deepak Khetawat
 
Flask & Flask-restx
Flask & Flask-restxFlask & Flask-restx
Flask & Flask-restx
 
.NET Core, ASP.NET Core Course, Session 11
.NET Core, ASP.NET Core Course, Session 11.NET Core, ASP.NET Core Course, Session 11
.NET Core, ASP.NET Core Course, Session 11
 
.NET Core, ASP.NET Core Course, Session 12
.NET Core, ASP.NET Core Course, Session 12.NET Core, ASP.NET Core Course, Session 12
.NET Core, ASP.NET Core Course, Session 12
 
Unit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSUnit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJS
 
.NET Core, ASP.NET Core Course, Session 10
.NET Core, ASP.NET Core Course, Session 10.NET Core, ASP.NET Core Course, Session 10
.NET Core, ASP.NET Core Course, Session 10
 

Destaque

Destaque (10)

OSGi Specification Evolution - BJ Hargrave
OSGi Specification Evolution - BJ HargraveOSGi Specification Evolution - BJ Hargrave
OSGi Specification Evolution - BJ Hargrave
 
OSGi 4.3 Technical Update: What's New?
OSGi 4.3 Technical Update: What's New?OSGi 4.3 Technical Update: What's New?
OSGi 4.3 Technical Update: What's New?
 
Services-First Migration to OSGi
Services-First Migration to OSGiServices-First Migration to OSGi
Services-First Migration to OSGi
 
OSGi for Enterprises
OSGi for EnterprisesOSGi for Enterprises
OSGi for Enterprises
 
What's new in the OSGi Enterprise Release 5.0
What's new in the OSGi Enterprise Release 5.0What's new in the OSGi Enterprise Release 5.0
What's new in the OSGi Enterprise Release 5.0
 
Why OSGi?
Why OSGi?Why OSGi?
Why OSGi?
 
Avoid the chaos - Handling 100+ OSGi Components - Balázs Zsoldos
Avoid the chaos - Handling 100+ OSGi Components - Balázs ZsoldosAvoid the chaos - Handling 100+ OSGi Components - Balázs Zsoldos
Avoid the chaos - Handling 100+ OSGi Components - Balázs Zsoldos
 
Hands on with lightweight m2m and Eclipse Leshan
Hands on with lightweight m2m and Eclipse LeshanHands on with lightweight m2m and Eclipse Leshan
Hands on with lightweight m2m and Eclipse Leshan
 
Service oriented web development with OSGi
Service oriented web development with OSGiService oriented web development with OSGi
Service oriented web development with OSGi
 
OSGi Puzzlers
OSGi PuzzlersOSGi Puzzlers
OSGi Puzzlers
 

Semelhante a Field injection, type safe configuration, and more new goodies in Declarative Services

Declarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi StyleDeclarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi Style
Felix Meschberger
 
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
lennartkats
 
GDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App EngineGDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App Engine
Yared Ayalew
 

Semelhante a Field injection, type safe configuration, and more new goodies in Declarative Services (20)

Constructor injection and other new features for Declarative Services 1.4
Constructor injection and other new features for Declarative Services 1.4Constructor injection and other new features for Declarative Services 1.4
Constructor injection and other new features for Declarative Services 1.4
 
Declarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi StyleDeclarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi Style
 
Declarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi StyleDeclarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi Style
 
SCR Annotations for Fun and Profit
SCR Annotations for Fun and ProfitSCR Annotations for Fun and Profit
SCR Annotations for Fun and Profit
 
Liferay (DXP) 7 Tech Meetup for Developers
Liferay (DXP) 7 Tech Meetup for DevelopersLiferay (DXP) 7 Tech Meetup for Developers
Liferay (DXP) 7 Tech Meetup for Developers
 
AngularJS application architecture
AngularJS application architectureAngularJS application architecture
AngularJS application architecture
 
Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018
 
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
 
Struts2 - 101
Struts2 - 101Struts2 - 101
Struts2 - 101
 
ASP.Net 5 and C# 6
ASP.Net 5 and C# 6ASP.Net 5 and C# 6
ASP.Net 5 and C# 6
 
It depends: Loving .NET Core dependency injection or not
It depends: Loving .NET Core dependency injection or notIt depends: Loving .NET Core dependency injection or not
It depends: Loving .NET Core dependency injection or not
 
The Web on OSGi: Here's How
The Web on OSGi: Here's HowThe Web on OSGi: Here's How
The Web on OSGi: Here's How
 
Angular.js Primer in Aalto University
Angular.js Primer in Aalto UniversityAngular.js Primer in Aalto University
Angular.js Primer in Aalto University
 
GDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App EngineGDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App Engine
 
S313937 cdi dochez
S313937 cdi dochezS313937 cdi dochez
S313937 cdi dochez
 
OSGi ecosystems compared on Apache Karaf - Christian Schneider
OSGi ecosystems compared on Apache Karaf - Christian SchneiderOSGi ecosystems compared on Apache Karaf - Christian Schneider
OSGi ecosystems compared on Apache Karaf - Christian Schneider
 
Microservices and modularity with java
Microservices and modularity with javaMicroservices and modularity with java
Microservices and modularity with java
 
TechDays 2016 - Developing websites using asp.net core mvc6 and entity framew...
TechDays 2016 - Developing websites using asp.net core mvc6 and entity framew...TechDays 2016 - Developing websites using asp.net core mvc6 and entity framew...
TechDays 2016 - Developing websites using asp.net core mvc6 and entity framew...
 
Analytics Metrics delivery and ML Feature visualization: Evolution of Data Pl...
Analytics Metrics delivery and ML Feature visualization: Evolution of Data Pl...Analytics Metrics delivery and ML Feature visualization: Evolution of Data Pl...
Analytics Metrics delivery and ML Feature visualization: Evolution of Data Pl...
 
Scala and Spring
Scala and SpringScala and Spring
Scala and Spring
 

Último

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 

Último (20)

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 

Field injection, type safe configuration, and more new goodies in Declarative Services

  • 1. Field injection, type safe configuration, and more new goodies in Declarative Services BJ Hargrave, IBM
  • 2. a Component Impl a Service Impl Service Component Runtime Impl a Servicea Component Instance Component Description a Component Confguration registered service tracks dependencies declarescomponent createdby controls 0..n 0..n 0..n references 1..n 1 Configuration Admin 0..n 1 0..n 1 1 <<service>> Service Component Runtime Declarative Services • A declarative model for publishing and consuming OSGi services • Introduced in Release 4 in 2005, it greatly simplified programming OSGi services • Service Component Runtime, SCR, is the runtime which implements the spec and manages the service components
  • 3. Service Component • A Java class which can optionally be registered as a service and can optionally use services • Described by XML in the bundle which is processed at runtime by SCR <?xml version="1.0" encoding="UTF-8"?> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="example.provider.ExampleImpl" activate="activate"> <implementation class="example.provider.ExampleImpl"/> <service> <provide interface="example.api.Example"/> </service> <reference name="Log" interface="org.osgi.service.log.LogService" bind="setLog"/> </scr:component>
  • 4. Service Component using annotations • But who wants to write XML? • So DS 1.2 added support for programming with annotations • Tools, like , process the annotations into the XML used by SCR at runtime @Component public class ExampleImpl implements Example { private Map<String, Object> properties; private LogService log; @Activate void activate(Map<String, Object> map) { properties = map; } @Override public boolean say(String message) { log.log((int) properties.get("loglevel"), message); return false; } @Reference void setLog(LogService log) { this.log = log; } }
  • 6. So what is new in Declarative Services 1.3? • Field Injection • Type Safe Configuration with MetaType integration • Configuration Admin integration improvements • Multiple PIDs • Introspection • And more small improvements we don’t have time to discuss today • See 112.17 “Changes” in the DS 1.3 spec
  • 8. Injection • Method injection • Constructor injection • Field injection @Reference void setLog(LogService log) { this.log = log; }
  • 9. Field Injection • Scalar cardinality • Static policy • Dynamic policy @Reference private Example target; @Reference private volatile Example target; https://github.com/bjhargrave/osgice2015/blob/master/1ScalarFieldRef/src/example/command/ExampleCommand.java#L17
  • 10. Field Injection • Multiple cardinality • Service type from generic signature • SCR-managed collection implementation • Static policy • Dynamic policy @Reference private List<Example> targets; https://github.com/bjhargrave/osgice2015/blob/master/2MultipleFieldRef/src/example/command/ExampleCommand.java#L20 @Reference private volatile List<Example> targets;
  • 11. Field Injection • User managed collection • Can also inject fields of types related to services and collections of those types @Reference(policy = ReferencePolicy.DYNAMIC) private final List<Example> targets = new CopyOnWriteArrayList<>(); @Reference ServiceReference<Example> sr; @Reference ComponentServiceObjects<Example> so; @Reference(service=Example.class) Map<String,Object> props; @Reference Map.Entry<Map<String,Object>,Example> tuple;
  • 13. Component Properties • Component properties come from • Component description • Configuration Admin configuration • ComponentFactory.newInstance <property name=“myport" type="Integer" value="8080"/> Dictionary<String, Object> props = new Hashtable<>(); props.put("myport", Integer.valueOf(8081)); cm.getConfiguration("Example").update(props); Dictionary<String, Object> props = new Hashtable<>(); props.put("myport", Integer.valueOf(8082)); cf.newInstance(props);
  • 14. Getting the property values • Activate method • But what if the configuration value for myport was of type String? Or type Long? • Type safety failure in your code • You also need to handle the case where the property is not set private int myport; @Activate void activate(Map<String, Object> props) { myport = props.containsKey("myport") ? ((Integer) props.get("myport")).intValue() : 8080; }
  • 15. Component Property Types • Define and use your component properties in a type safe manner using annotations! @interface Props { int myport() default 8080; } @Component public class ExampleImpl implements Example { private int myport; @Activate void activate(Props props) { myport = props.myport(); } } https://github.com/bjhargrave/osgice2015/blob/master/3TypeSafeConfig/src/example/provider/ExampleImpl.java#L14
  • 17. Component Property Types integrate with MetaType Service • Use the new MetaType annotations to define metatype resources and associate them with your component https://github.com/bjhargrave/osgice2015/blob/master/4TypeSafeConfigWithMetatype/src/example/provider/ExampleImpl.java#L14 @ObjectClassDefinition @interface Props { int myport() default 8080; } @Designate(ocd=Props.class) @Component public class ExampleImpl implements Example { private int myport; @Activate void activate(Props props) { myport = props.myport();
  • 19. Component properties from Configurations • DS integrates with Configuration Admin and will get component properties from a configuration with the configuration pid of the component @ObjectClassDefinition(pid = "Example") @interface Name { String name() default "Default Name"; } @Component(configurationPid = "Example") public class ExampleImpl implements Example { private String name; @Activate void activate(Name name) { this.name = name.name(); } }
  • 20. Sometimes we need to use multiple configurations • A component may want to use a “system” configuration as well as a specific configuration @ObjectClassDefinition(pid = “System") @interface Name { String name() default "Default Name"; } @ObjectClassDefinition(pid = "Example") @interface Words { String hello() default "Hello"; String goodbye() default "Goodbye"; } @Component(configurationPid = {“System", "Example"}) public class ExampleImpl implements Example { private String name; private Words words; @Activate void activate(Name name, Words words) { this.name = name.name(); this.words = words; } https://github.com/bjhargrave/osgice2015/blob/master/6MultiplePIDs/src/example/provider/ExampleImpl.java#L13
  • 22. Introspecting the Service Components • SCR now registers a ServiceComponentRuntime service which provides APIs to introspect the service components managed by SCR public interface ServiceComponentRuntime { Collection<ComponentDescriptionDTO> getComponentDescriptionDTOs(Bundle... bundles); ComponentDescriptionDTO getComponentDescriptionDTO(Bundle bundle, String name); Collection<ComponentConfigurationDTO> getComponentConfigurationDTOs(ComponentDescriptionDTO description); boolean isComponentEnabled(ComponentDescriptionDTO description); Promise<Void> enableComponent(ComponentDescriptionDTO description); Promise<Void> disableComponent(ComponentDescriptionDTO description); } https://github.com/bjhargrave/osgice2015/blob/master/5Introspection/src/example/command/ExampleCommand.java#L25
  • 23. SCR Data Transfer Objects • DTOs are defined for • Component descriptions - ComponentDescriptionDTO • Component configurations - ComponentConfigurationDTO • References - ReferenceDTO, SatisfiedReferenceDTO, UnsatisfiedReferenceDTO
  • 24. Why isn’t my component active? • You can use the DTOs to figure out why • It might be missing a required configuration or have an unsatisfied reference to a service public class ComponentConfigurationDTO extends DTO { public static final int UNSATISFIED_CONFIGURATION= 1; public static final int UNSATISFIED_REFERENCE = 2; public static final int SATISFIED = 4; public static final int ACTIVE = 8; public ComponentDescriptionDTO description; public int state; public long id; public Map<String, Object> properties; public SatisfiedReferenceDTO[] satisfiedReferences; public UnsatisfiedReferenceDTO[] unsatisfiedReferences; }