Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
© 2009 IBM Corporation
CDI Integration In OSGi


Emily Jiang, WebSphere Application Server Development, CDI Lead
Emily Jia...
© 2009 IBM Corporation2
Emily Jiang
▪ WebSphere Application Server Development, CDI Lead @IBM
▪ CDI Expert Group Member
▪ ...
© 2009 IBM Corporation
What is CDI?
▪ Contexts and Dependency Injection
– A framework to provide
• Type safe dependency in...
© 2009 IBM Corporation
CDI Implementations
▪ JBoss Weld (Reference Implementation)
– JBoss Enterprise Application Server
–...
© 2009 IBM Corporation
CDI bean
▪ What is CDI Bean?
– Java class
• Not non-static inner class
• Concrete class or is annot...
© 2009 IBM Corporation
Bean injections
@SessionScoped @Named
public class Login implements Serializable {
@Inject Credenti...
© 2009 IBM Corporation
Typesafe Injection
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, Elemen...
© 2009 IBM Corporation
Typesafe injection
public class BreakFastEnglishStyle extends BreakFast implements Serializable {
p...
© 2009 IBM Corporation
Interceptor binding
▪ Define the Interceptor Binding
@Inherited
@InterceptorBinding
@Target({ Eleme...
© 2009 IBM Corporation
Interceptor binding
▪ Bind the interceptor
@ApplicationScoped
public class OrangeSearcherService im...
© 2009 IBM Corporation
CDI Extension
▪ Use CDI Extension to
– Providing its own beans, interceptors and decorators to the ...
© 2009 IBM Corporation
CDI Extension
▪ How?
– Write an extension
public class CustomExtension implements Extension {
<X> v...
© 2009 IBM Corporation
CDI example
13
© 2009 IBM Corporation
Does OSGi need CDI?
14
© 2009 IBM Corporation
Why CDI support needed in OSGi?
▪ OSGi Dependency injection framework: DS, blueprint
– No standards...
© 2009 IBM Corporation
Terminologies
▪ CDI Provider – An implementation of the CDI 1.2 specification.
▪ CDI Bundle – A CDI...
© 2009 IBM Corporation
CDI extender, CDI bundle and CDI container
▪ CDI extenders provides the capability
Provide-Capabili...
© 2009 IBM Corporation
CDI Container Lifecycle
18
© 2009 IBM Corporation
Publishing services in OSGi
19
@Component(properties = {@ComponentProperty(key = "key",
value = "va...
© 2009 IBM Corporation
Publishing CDI services
▪ Via annotations
– Specify service properties
@Component(properties ={@Com...
© 2009 IBM Corporation
Supported scopes on @Component
▪ CDI builtin scopes
– @ApplicationScoped
– @SessionScoped
– @Conver...
© 2009 IBM Corporation
Consuming services in OSGi
22
CDI
Container
CDI bean
OSGi
Service
Registry
@Inject @Service @SomeKe...
© 2009 IBM Corporation
Mandatory Service dependencies
23
A B C
@Inject
@Service(“(required=true)”)
@Inject
@Service(“(requ...
© 2009 IBM Corporation
Optional Service dependencies
24
A B C
@Inject
@Service(“(required=true)”)
@Inject
@Service
© 2009 IBM Corporation
Allow Service Injections in vanilla CDI injection points
▪ Support injection to @Inject points with...
© 2009 IBM Corporation
BundleContext Injection
@Inject BundleContext context;
Bundle A:
public abstract class MyBaseClass ...
© 2009 IBM Corporation
BeanManager Service
27
CDI
Container
OSGi
Service
Registry
Register the BeanManager service
javax.e...
© 2009 IBM Corporation
CDI Events
▪ The CDI Event (CdiEvent) supports the following event types:
– CREATING - The CDI exte...
© 2009 IBM Corporation
CdiEvent class
public class CdiEvent {
public static final int CREATING = 1;
public static final in...
© 2009 IBM Corporation
Specification Implementation
▪ Should be able to work with both Weld and OpenWebBeans
▪ An example ...
© 2009 IBM Corporation
© 2009 IBM Corporation
Questions
Questions?
32
Upcoming SlideShare
Loading in …5
×

CDI Integration in OSGi - Emily Jiang

1,294 views

Published on

OSGi Community Event 2015

OSGi is a widely used modularity framework for Java, with rapid growth in the enterprise space. Since Java EE6, Contexts and Dependency Injection (CDI) also becomes very popular. One of the significant changes in Java EE7 is that CDI is now enabled by default. Uniting two powerful programming modes, CDI and dynamic services in OSGi, will make OSGi technology even more powerful. OSGi Alliance is working towards standardizing the specification of CDI Integration into OSGi. This session will explain how CDI Integration in OSGi works.

Published in: Technology
  • DOWNLOAD FULL BOOKS INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

CDI Integration in OSGi - Emily Jiang

  1. 1. © 2009 IBM Corporation CDI Integration In OSGi
 
Emily Jiang, WebSphere Application Server Development, CDI Lead Emily Jiang 4th Nov 2015
  2. 2. © 2009 IBM Corporation2 Emily Jiang ▪ WebSphere Application Server Development, CDI Lead @IBM ▪ CDI Expert Group Member ▪ One of the authors of RFC 193 CDI Integration In OSGi ▪ Apache Aries PMC member Source: If applicable, describe source origin CDI Integration In OSGi
  3. 3. © 2009 IBM Corporation What is CDI? ▪ Contexts and Dependency Injection – A framework to provide • Type safe dependency injection • A better approach to binding interceptors to objects, such as interceptors, decorators • An SPI for developing portable extensions to the containers 3 12/2009 CDI 1.0 (Java EE6) JSR299 CDI 1.1 (Java EE7) JSR346 06/2013 CDI 1.2 (1.1 MR) 04/2014 09/2014 CDI 2.0 (start) JSR365 CDI 2.0 (release) 2016
  4. 4. © 2009 IBM Corporation CDI Implementations ▪ JBoss Weld (Reference Implementation) – JBoss Enterprise Application Server – WildFly – GlassFish – Oracle WebLogic Server – WebSphere Application Server Liberty profile 8.5.5.6+, EE7 compliance ▪ Apache OpenWebBeans – TomEE – Geronimo – WebSphere Application Server Full Profile 8.0, 8.5 – EE6 compliance 4
  5. 5. © 2009 IBM Corporation CDI bean ▪ What is CDI Bean? – Java class • Not non-static inner class • Concrete class or is annotated @Decorator • Does not implement javax.enterprise.inject.spi.Extension • Has an appropriate constructor – either: Default constructor @Inject constructor ▪ What is special about the CDI Bean? – Allow the container to create and destroy the instances and associate them with a context – Inject them into other beans – Using them in EL expression – Specialising them with qualifier annotations – Adding interceptors and decorators 5
  6. 6. © 2009 IBM Corporation Bean injections @SessionScoped @Named public class Login implements Serializable { @Inject Credentials credentials; @Inject @UserDatabase EntityManager userDatabase; private User user; public void login() { … } public void logout() { user = null; } public boolean isLoggedIn() { return user != null; } } //jsf usage <h:panelGroup rendered="#{login.loggedIn}"> signed in as #{currentUser.username} </h:panelGroup> 6 • 3 types of injection points o Constructor injection o Field injection o Method injection
  7. 7. © 2009 IBM Corporation Typesafe Injection @Qualifier @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.PARAMETER, ElementType.METHOD,ElementType.CONSTRUCTOR,ElementType.FIELD, ElementType.LOCAL_VARIABLE}) public @interface QualifierStyle { String value(); } @SessionScoped public class BreadMaker { @Produces @QualifierStyle("English")@QualifierGrain IFood makeEnglishBread( EnglishBread bread) { bread.createdBy = "Making English bread"; return bread; } @Produces @QualifierStyle("French")@QualifierGrain @SessionScoped IFood makeFrenchBread(FrenchBread bread) { bread.createdBy = "Making French bread"; return bread; } } 7
  8. 8. © 2009 IBM Corporation Typesafe injection public class BreakFastEnglishStyle extends BreakFast implements Serializable { private static final long serialVersionUID = -4055192893910694993L; @Inject public BreakFastEnglishStyle(@QualifierStyle("English") @QualifierGrain IFood bread) { this.bread = bread; } } 8
  9. 9. © 2009 IBM Corporation Interceptor binding ▪ Define the Interceptor Binding @Inherited @InterceptorBinding @Target({ ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER }) @Retention(RetentionPolicy.RUNTIME) public @interface MyInterceptor {} ▪ Define the Interceptor @Interceptor @MyInterceptor @Priority(100) public class OrangeInterceptor { @AroundInvoke public Object handle(final InvocationContext ic) throws Exception { Orange o = new Orange(); o.setOrangeMsg("created by OrangeIntercepter.handle"); GlobalState.addOrange(o); return ic.proceed(); } } 9
  10. 10. © 2009 IBM Corporation Interceptor binding ▪ Bind the interceptor @ApplicationScoped public class OrangeSearcherService implements OrangesSearcher { List<Orange> oranges = new ArrayList<Orange>(); @Override @MyInterceptor public List<Orange> search(OrangesCriteria criteria) { Orange o = new Orange(); o.setOrangeMsg("created by OrangeSearcherService.search"); GlobalState.addOrange(o); return GlobalState.getOranges(); } } 10
  11. 11. © 2009 IBM Corporation CDI Extension ▪ Use CDI Extension to – Providing its own beans, interceptors and decorators to the container – Injecting dependencies into its own objects using the dependency injection service – Providing a context implementation for a custom scope – Augmenting or overriding the annotation-based metadata with metadata from some other source 11
  12. 12. © 2009 IBM Corporation CDI Extension ▪ How? – Write an extension public class CustomExtension implements Extension { <X> void processInjectionTarget(@Observes ProcessInjectionTarget<X> pit, BeanManager beanManager) { if ( !pit.getInjectionTarget().getInjectionPoints().isEmpty() ) { for ( InjectionPoint ip : pit.getInjectionTarget().getInjectionPoints() ) { System.out.println("Injection Point " + ip + "n"); } } } } – Package the extension • Package this portable extension in a jar containing a file named javax.enterprise.inject.spi.Extension in the META-INF/services directory • The file content is the fully qualified extension class name 12
  13. 13. © 2009 IBM Corporation CDI example 13
  14. 14. © 2009 IBM Corporation Does OSGi need CDI? 14
  15. 15. © 2009 IBM Corporation Why CDI support needed in OSGi? ▪ OSGi Dependency injection framework: DS, blueprint – No standards support for runtime annotations ▪ Similar complementary relationship between other containers and CDI containers ▪ Migrating ear files to esa files easier 15
  16. 16. © 2009 IBM Corporation Terminologies ▪ CDI Provider – An implementation of the CDI 1.2 specification. ▪ CDI Bundle – A CDI-enabled OSGi bundle. ▪ CDI Container – A container for managed beans in a CDI Bundle. Each CDI Bundle has its own CDI container. ▪ CDI Extender – An application of the extender pattern to discover CDI Bundles and to manage the CDI container life-cycle on behalf of CDI Bundles. 16
  17. 17. © 2009 IBM Corporation CDI extender, CDI bundle and CDI container ▪ CDI extenders provides the capability Provide-Capability = osgi.extender; osgi.extender=osgi.cdi; uses:=”org.osgi.service.cdi,javax.enterprise.inject.spi”; version:Version=”1.0” ▪ CDI bundle: Require-Capability: osgi.extender; filter:="(osgi.extender=osgi.cdi)“ Provide-Capability:osgi.cdi.beans;beans:List<String>=”foo.A,foo.B” ▪ CDI container: – Register a service with the following service properties: osgi.cdi.container.symbolicname :CDI bundle’s symbolic name osgi.cdi.container.version: CDI bundle version 17
  18. 18. © 2009 IBM Corporation CDI Container Lifecycle 18
  19. 19. © 2009 IBM Corporation Publishing services in OSGi 19 @Component(properties = {@ComponentProperty(key = "key", value = "value"), @ComponentProperty(key ="key2", value = "42", type = “long”)}) public class ExampleComponent {} CDI Container CDI bean OSGi Service Registry @Component
  20. 20. © 2009 IBM Corporation Publishing CDI services ▪ Via annotations – Specify service properties @Component(properties ={@ComponentProperty(key = "key", value = "value"),@ComponentProperty(key ="key2", value = "42", type = “long”)}) public class ExampleComponent {} – Configuration dependency @Component(requireConfiguration=”PID.of.configuration”) public class AnotherExampleComponent {} ▪ Via Bundle manifest Require-Capability: osgi.extender;filter:="(osgi.extender=osgi.cdi)"; components:="org.acme.MyComponent,org.acme.ExampleComponent" 20
  21. 21. © 2009 IBM Corporation Supported scopes on @Component ▪ CDI builtin scopes – @ApplicationScoped – @SessionScoped – @ConversationScoped – @RequestScoped – @Dependent ▪ New scopes – @BundleScoped – @PrototypeScoped – @SingletonScoped 21
  22. 22. © 2009 IBM Corporation Consuming services in OSGi 22 CDI Container CDI bean OSGi Service Registry @Inject @Service @SomeKey(“somevalue”) @SomeOtherKey(“someothervalue”) @Inject @Service(“(somekey=somevalue)”) @MyServiceProperty(“example”) <-> “(my.service.property=example)" @Service @Inject
  23. 23. © 2009 IBM Corporation Mandatory Service dependencies 23 A B C @Inject @Service(“(required=true)”) @Inject @Service(“(required=true)”)
  24. 24. © 2009 IBM Corporation Optional Service dependencies 24 A B C @Inject @Service(“(required=true)”) @Inject @Service
  25. 25. © 2009 IBM Corporation Allow Service Injections in vanilla CDI injection points ▪ Support injection to @Inject points without specifying @Service Require-Capability: osgi.extender; filter:="(osgi.extender=osgi.cdi)"; at-service:="optional" 25
  26. 26. © 2009 IBM Corporation BundleContext Injection @Inject BundleContext context; Bundle A: public abstract class MyBaseClass { @Inject BundleContext bundleContext; Bundle B’s bundle context } Bundle B: @Component public class MySubClass extends MyBaseClass { } 26
  27. 27. © 2009 IBM Corporation BeanManager Service 27 CDI Container OSGi Service Registry Register the BeanManager service javax.enterprise.inject.spi.BeanManager
  28. 28. © 2009 IBM Corporation CDI Events ▪ The CDI Event (CdiEvent) supports the following event types: – CREATING - The CDI extender has started creating a CDI Container for the bundle. – CREATED - The CDI Container is ready. The application is now running. – WAITING - A service reference is blocking because of unsatisfied mandatory dependencies. This event can happen multiple times in a row. – DESTROYING - The CDI Container is being destroyed because the CDI bundle or CDI extender has stopped. – DESTROYED - The CDI Container is completely destroyed. – FAILURE - An error occurred during the creation of the CDI Container. 28
  29. 29. © 2009 IBM Corporation CdiEvent class public class CdiEvent { public static final int CREATING = 1; public static final int CREATED = 2; public static final int DESTROYING = 3; public static final int DESTROYED = 4; public static final int FAILURE = 5; public static final int WAITING = 7; private final int type; private final Bundle bundle; private final Bundle extenderBundle; private final String[] dependencies; private final Throwable cause; private final boolean replay; public int getType() { return type;} public Bundle getBundle() { return bundle;} public Bundle getExtenderBundle() { return extenderBundle; } public String[] getDependencies() { return dependencies == null ? null : (String[]) dependencies.clone(); public Throwable getCause() { return cause; } public boolean isReplay() { return replay; } } 29
  30. 30. © 2009 IBM Corporation Specification Implementation ▪ Should be able to work with both Weld and OpenWebBeans ▪ An example of implementation: PAX CDI 30
  31. 31. © 2009 IBM Corporation
  32. 32. © 2009 IBM Corporation Questions Questions? 32

×