1
<Insert Picture Here>




OSGi and Java EE: A Hybrid Approach to Enterprise Java Application
Development
Sanjeeb Sahoo and Sivakumar Thyagarajan
Staff Engineers, Sun Microsystems India Pvt. Ltd.
JavaOne and Oracle Develop

Latin America 2010

December 7–9, 2010




                             3
JavaOne and Oracle Develop

Beijing 2010

December 13–16, 2010




                             4
Agenda


•   OSGi 101                                   <Insert Picture Here>

•   Basic OSGi Demo
•   OSGi meets Java EE – Hybrid Applications
•   Hybrid Application Demo
•   Advanced usecases
•   Q&A




                                                                  5
Agenda


•   OSGi 101                                   <Insert Picture Here>

•   Basic OSGi Demo
•   OSGi meets Java EE – Hybrid Applications
•   Hybrid Application Demo
•   Advanced usecases
•   Q&A




                                                                  6
OSGi



       Service oriented
          dynamic
       module system
           for Java


                          7
OSGi Layers




              8
Module Layer


• Bundle – a unit of modularity in OSGi
• Encapsulation
• Lifecycle independent of the JVM
• Versionable
• Packaged as a JAR (classes + Manifest with OSGi
  metadata)
• Very explicit dependency specification
• Well defined dependency resolution rules




                                                    9
OSGi Metadata



Bundle-SymbolicName: com.acme.hello.startup
Bundle-Name: Hello World OSGi Sample
Bundle-Version: 1.0.1
Bundle-ManifestVersion: 2
Bundle-Activator: com.acme.hello.startup.MyActivator
Export-Package: com.acme.hello.startup;version=1.0.0
Import-Package: org.osgi.framework;version=1.3




                                                       10
Classloading in OSGi




Image from the OSGi R4 Core Specification



                                            11
Lifecycle Layer


• Provides an API to manage bundles
  – BundleContext.install
  – BundleContext.uninstall
  – Bundle.update
• BundleActivator
• BundleListener – listen to BundleEvents
  – Synchronous
  – Asynchronous




                                            12
Bundle Lifecycle




Image from the OSGi R4 Core Specification



                                            13
Service Layer

• In-VM SOA
• Service Registry
  – Register
  – Unregister
• Service is a POJO
• Service Dynamism
  – Service tracking
• Service Discovery
  – LDAP filter based queries




                                14
Interactions between OSGi Layers




Image from the OSGi R4 Core Specification



                                            15
Simple OSGi Demo




                   16
OSGi benefits – a recap

• Layered approach
• Modularity
  –   Enforces modularity
  –   Ensures class-space consistency
  –   Enables component reuse
  –   Supports versioning and evolution
• Dynamism
  – Bundles
  – Services
• Ease of deployment and management
  – OBR
     • Bundle deployment order is not relevant
  – Faster deployment cycle




                                                 17
OSGi meets Java EE




                     18
Agenda


•   OSGi 101                                   <Insert Picture Here>

•   Basic OSGi Demo
•   OSGi meets Java EE – Hybrid Applications
•   Hybrid Application Demo
•   Advanced usecases
•   Q&A




                                                                 19
Java EE
Platform of choice for enterprise applications


• Widely used APIs (JPA, JTA, JAXB, JNDI)
• Component Models (Servlet, EJB, JAX-RS)
• Frameworks (JSF, CDI)
• Ease of Use (Annotations, Convention over
  Configuration)
• Platform provided infrastructure services
  (Transaction, Security, Persistence, Remoting)
• Tools (IDE, Management, Monitoring)
• Ubiquitous deployment platform for enterprise
  applications


                                                   20
Hybrid Applications
Meeting of the two worlds


• OSGi + Java EE = Hybrid application
• Hybrid application
   – An OSGi bundle
   – And a Java EE Archive
• Leverage the capabilities of both the platforms
   – Enterprise applications can now be built as OSGi bundles
   – OSGi bundles can now use Java EE services




                                                                21
Current State

• OSGi Enterprise Expert Group (EEG)
  – Relased OSGi Service Platform Enterprise Specification 4.2
    in 2010
• Open source efforts
  – Project GlassFish
  – Project Aries
  – Eclipse Gemini




                                                                 22
Enterprise OSGi Specifications

•   OSGi/Web Application (RFC #66)
•   OSGi/JPA (RFC #143)
•   OSGi/JDBC (RFC #122)
•   OSGi/JTA (RFC #98)
•   OSGi/JNDI (RFC #142)
•   OSGi/HTTP Service




                                     23
Hybrid application bundle lifecycle




                                      24
OSGi/Web Application (WAB)

• Web Application Bundle (WAB)
  – WAR + OSGi metadata + Web-ContextPath header
• Can use all enterprise APIs (like JPA, JTA)
• Wrapped WAR support
  – webbundle: URL scheme




                                                   25
OSGi/EJB Application

• Allows you to develop managed, transactional, secure
  OSGi services with very little knowledge of OSGi
• Make your existing EJB service available to OSGi
  clients with little effort
• Achieved through simple manifest metadata
  – Export-EJB: ALL/None/<names of stateless local EJB>




                                                          26
EE APIs in OSGi

• OSGi/JDBC
  – JDBC driver as DataSourceFactory
  – Dynamic discovery of driver details
  – Multiple versions of same driver
• OSGi/JTA
  – JTA artifacts available as OSGi Services
• OSGi/JPA
  – Enhancement of JPA entities at runtime
  – Same packaging rules as JPA and also deploy entities as a
    bundle
  – Shared Persistence Unit and thereby shared second level
    cache



                                                                27
Demo: Putting them
together




                     28
Demo schematic


• WAB

• JPA- LAZY loading

• EJB as Service

• OBR

• Security/Transaction
 Context propagation




                         29
Advanced Usecases




                    30
Agenda


•   OSGi 101                                   <Insert Picture Here>

•   Basic OSGi Demo
•   OSGi meets Java EE – Hybrid Applications
•   Hybrid Application Demo
•   Advanced usecases
•   Q&A




                                                                 31
CDI

• Brings type-safe dependency injection to EE platform
  – Strong typing and loose coupling
• Well defined life cycle of stateful objects bound to life
  cycle contexts
• Ability to decorate and intercept injected Beans
• An event notification model
• An ability to develop portable extensions that
  integrates with the container to extend the runtime




                                                              32
GlassFish CDI/OSGi Portable Extension

• Use of CDI for type safe injection of OSGi services

@WebServlet(urlPatterns = “/login”)
public class FooServlet implements HttpServlet {
    @Inject 
    @org.glassfish.osgicdi.OSGiService(
          dynamic=”true”, 
          timeout = 200, 
          serviceCriteria=<an ldap query >
    FooService foo;
}




                                                        33
Demo: Service Injection




                          34
Call To Action

• Want to build hybrid applications? Come to our HOL
  – ID#: S313522, tomorrow at 12:00 in Plaza A of Hilton San
    Francisco
• Want to try out?
  – Use GlassFish 3.1 trunk builds from http://glassfish.org
• Questions?
  – users@glassfish.dev.java.net




                                                               35
References

• OSGi Service Platform Core and Enterprise
  Specification 4.2
  – http://www.osgi.org
• Project GlassFish
  – http://glassfish.org
• Hybrid application samples
  – http://wikis.sun.com/display/GlassFish/BlogsGfOsgi




                                                         36
Agenda


•   OSGi 101                                   <Insert Picture Here>

•   Basic OSGi Demo
•   OSGi meets Java EE – Hybrid Applications
•   Hybrid Application Demo
•   Advanced usecases
•   Q&A




                                                                 37
Safe Harbor Statements


The following is intended to outline our general
product direction. It is intended for information
purposes only, and may not be incorporated into any
contract. It is not a commitment to deliver any
material, code, or functionality, and should not be
relied upon in making purchasing decisions.
The development, release, and timing of any
features or functionality described for Oracle’s
products remains at the sole discretion of Oracle.




                                                      38
The preceding is intended to outline our general
product direction. It is intended for information
purposes only, and may not be incorporated into any
contract. It is not a commitment to deliver any
material, code, or functionality, and should not be
relied upon in making purchasing decisions.
The development, release, and timing of any
features or functionality described for Oracle’s
products remains at the sole discretion of Oracle.




                                                      39
<Insert Picture Here>




OSGi and Java EE: A Hybrid Approach to Enterprise Java Application
Development
{sahoo, sivakumart}@sun.com
Staff Engineers, Sun Microsystems India Pvt. Ltd.
41

OSGi and Java EE: A Hybrid Approach to Enterprise Java Application Development

  • 1.
  • 2.
    <Insert Picture Here> OSGiand Java EE: A Hybrid Approach to Enterprise Java Application Development Sanjeeb Sahoo and Sivakumar Thyagarajan Staff Engineers, Sun Microsystems India Pvt. Ltd.
  • 3.
    JavaOne and OracleDevelop Latin America 2010 December 7–9, 2010 3
  • 4.
    JavaOne and OracleDevelop Beijing 2010 December 13–16, 2010 4
  • 5.
    Agenda • OSGi 101 <Insert Picture Here> • Basic OSGi Demo • OSGi meets Java EE – Hybrid Applications • Hybrid Application Demo • Advanced usecases • Q&A 5
  • 6.
    Agenda • OSGi 101 <Insert Picture Here> • Basic OSGi Demo • OSGi meets Java EE – Hybrid Applications • Hybrid Application Demo • Advanced usecases • Q&A 6
  • 7.
    OSGi Service oriented dynamic module system for Java 7
  • 8.
  • 9.
    Module Layer • Bundle– a unit of modularity in OSGi • Encapsulation • Lifecycle independent of the JVM • Versionable • Packaged as a JAR (classes + Manifest with OSGi metadata) • Very explicit dependency specification • Well defined dependency resolution rules 9
  • 10.
    OSGi Metadata Bundle-SymbolicName: com.acme.hello.startup Bundle-Name:Hello World OSGi Sample Bundle-Version: 1.0.1 Bundle-ManifestVersion: 2 Bundle-Activator: com.acme.hello.startup.MyActivator Export-Package: com.acme.hello.startup;version=1.0.0 Import-Package: org.osgi.framework;version=1.3 10
  • 11.
    Classloading in OSGi Imagefrom the OSGi R4 Core Specification 11
  • 12.
    Lifecycle Layer • Providesan API to manage bundles – BundleContext.install – BundleContext.uninstall – Bundle.update • BundleActivator • BundleListener – listen to BundleEvents – Synchronous – Asynchronous 12
  • 13.
    Bundle Lifecycle Image fromthe OSGi R4 Core Specification 13
  • 14.
    Service Layer • In-VMSOA • Service Registry – Register – Unregister • Service is a POJO • Service Dynamism – Service tracking • Service Discovery – LDAP filter based queries 14
  • 15.
    Interactions between OSGiLayers Image from the OSGi R4 Core Specification 15
  • 16.
  • 17.
    OSGi benefits –a recap • Layered approach • Modularity – Enforces modularity – Ensures class-space consistency – Enables component reuse – Supports versioning and evolution • Dynamism – Bundles – Services • Ease of deployment and management – OBR • Bundle deployment order is not relevant – Faster deployment cycle 17
  • 18.
  • 19.
    Agenda • OSGi 101 <Insert Picture Here> • Basic OSGi Demo • OSGi meets Java EE – Hybrid Applications • Hybrid Application Demo • Advanced usecases • Q&A 19
  • 20.
    Java EE Platform ofchoice for enterprise applications • Widely used APIs (JPA, JTA, JAXB, JNDI) • Component Models (Servlet, EJB, JAX-RS) • Frameworks (JSF, CDI) • Ease of Use (Annotations, Convention over Configuration) • Platform provided infrastructure services (Transaction, Security, Persistence, Remoting) • Tools (IDE, Management, Monitoring) • Ubiquitous deployment platform for enterprise applications 20
  • 21.
    Hybrid Applications Meeting ofthe two worlds • OSGi + Java EE = Hybrid application • Hybrid application – An OSGi bundle – And a Java EE Archive • Leverage the capabilities of both the platforms – Enterprise applications can now be built as OSGi bundles – OSGi bundles can now use Java EE services 21
  • 22.
    Current State • OSGiEnterprise Expert Group (EEG) – Relased OSGi Service Platform Enterprise Specification 4.2 in 2010 • Open source efforts – Project GlassFish – Project Aries – Eclipse Gemini 22
  • 23.
    Enterprise OSGi Specifications • OSGi/Web Application (RFC #66) • OSGi/JPA (RFC #143) • OSGi/JDBC (RFC #122) • OSGi/JTA (RFC #98) • OSGi/JNDI (RFC #142) • OSGi/HTTP Service 23
  • 24.
  • 25.
    OSGi/Web Application (WAB) •Web Application Bundle (WAB) – WAR + OSGi metadata + Web-ContextPath header • Can use all enterprise APIs (like JPA, JTA) • Wrapped WAR support – webbundle: URL scheme 25
  • 26.
    OSGi/EJB Application • Allowsyou to develop managed, transactional, secure OSGi services with very little knowledge of OSGi • Make your existing EJB service available to OSGi clients with little effort • Achieved through simple manifest metadata – Export-EJB: ALL/None/<names of stateless local EJB> 26
  • 27.
    EE APIs inOSGi • OSGi/JDBC – JDBC driver as DataSourceFactory – Dynamic discovery of driver details – Multiple versions of same driver • OSGi/JTA – JTA artifacts available as OSGi Services • OSGi/JPA – Enhancement of JPA entities at runtime – Same packaging rules as JPA and also deploy entities as a bundle – Shared Persistence Unit and thereby shared second level cache 27
  • 28.
  • 29.
    Demo schematic • WAB •JPA- LAZY loading • EJB as Service • OBR • Security/Transaction Context propagation 29
  • 30.
  • 31.
    Agenda • OSGi 101 <Insert Picture Here> • Basic OSGi Demo • OSGi meets Java EE – Hybrid Applications • Hybrid Application Demo • Advanced usecases • Q&A 31
  • 32.
    CDI • Brings type-safedependency injection to EE platform – Strong typing and loose coupling • Well defined life cycle of stateful objects bound to life cycle contexts • Ability to decorate and intercept injected Beans • An event notification model • An ability to develop portable extensions that integrates with the container to extend the runtime 32
  • 33.
    GlassFish CDI/OSGi PortableExtension • Use of CDI for type safe injection of OSGi services @WebServlet(urlPatterns = “/login”) public class FooServlet implements HttpServlet {     @Inject      @org.glassfish.osgicdi.OSGiService(           dynamic=”true”,            timeout = 200,            serviceCriteria=<an ldap query >     FooService foo; } 33
  • 34.
  • 35.
    Call To Action •Want to build hybrid applications? Come to our HOL – ID#: S313522, tomorrow at 12:00 in Plaza A of Hilton San Francisco • Want to try out? – Use GlassFish 3.1 trunk builds from http://glassfish.org • Questions? – users@glassfish.dev.java.net 35
  • 36.
    References • OSGi ServicePlatform Core and Enterprise Specification 4.2 – http://www.osgi.org • Project GlassFish – http://glassfish.org • Hybrid application samples – http://wikis.sun.com/display/GlassFish/BlogsGfOsgi 36
  • 37.
    Agenda • OSGi 101 <Insert Picture Here> • Basic OSGi Demo • OSGi meets Java EE – Hybrid Applications • Hybrid Application Demo • Advanced usecases • Q&A 37
  • 38.
    Safe Harbor Statements Thefollowing is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 38
  • 39.
    The preceding isintended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 39
  • 40.
    <Insert Picture Here> OSGiand Java EE: A Hybrid Approach to Enterprise Java Application Development {sahoo, sivakumart}@sun.com Staff Engineers, Sun Microsystems India Pvt. Ltd.
  • 41.