1. Open Your Eyes
To OSGi
By : Yanai Franchi
Chief Architect , Tikal
Eclipse DemoCamps Galileo 2009/Tel Aviv
2. Agenda
Introduction
– What is OSGi ?
– What problems does it help us to solve ?
– How does it work ?
OSGi programming model
– Live Demo
OSGi in Enterprise Applications
– Spring DM
Copyright 2009 Tikal Knowledge, Ltd. | 2 |
32. A Little Open-Heart Surgery
• Equinox project
started February 2003
• Replace old Eclipse
runtime with OSGi
• Enhance OSGi
standard
• Eclipse 3.0 released
June 2004
Copyright 2009 Tikal Knowledge, Ltd. | 32 |
34. Bundle
By default a bundle is a black box
» Completely protected
» You can NOT see inside it
• Not even by reflection
• Or any other class loading trickery
Copyright 2009 Tikal Knowledge, Ltd. | 34 |
35. How Does It Look Like ?
Its a jar file!
No complicated tools or sophisticated
packaging required.
META-INF/MANIFEST.MF
Bundle-Name: Hello World
Bundle-SymbolicName: com.tikal.osgi.sample.helloworld
Bundle-Version: 2.1.0
Export-Package: com.tikal.osgi.sample
Import-Package: com.tikal.osgi.weather ,org.osgi.framework
Bundle-Activator: com.tikal.osgi.sample.HelloActivator
Copyright 2009 Tikal Knowledge, Ltd. | 35 |
36. Bundle Visibility
Exposing Types can be done explicitly
» A bundle can export one or more packages
» Optionally with versioning information
Only exported packages are visibly outside of
the exporting bundle.
» Stops unintended coupling between bundles
» Enable independent development
» Faster development cycles
Copyright 2009 Tikal Knowledge, Ltd. | 36 |
37. Bundle Visibility Cont.
Service interface
types exported
with version
information
Export-Packages: a,b,c
private
packages
“Passive” contribution
Service Types added to type space
implementation bundles see new version on
locked away resolution after
install/refresh
Copyright 2009 Tikal Knowledge, Ltd. | 37 |
43. Versioning
MVC
Two versions of
the same service
type... at the same Service Service '
time!
Repository '
Copyright 2009 Tikal Knowledge, Ltd. | 43 |
45. Operational Control
See all bundles and their status
» OSGi console
» Web Console
» JMX
Get information on wiring
Install new bundles
Activate/Deactivate bundles (and publish services)
Refresh bundles
Stop bundles
Uninstall bundles
All without stop or restart the application
Copyright 2009 Tikal Knowledge, Ltd. | 45 |
47. OSGi Programming
All access to OSGi is
through
BundleContext
This is supplied to our
BundleActivator
Copyright 2009 Tikal Knowledge, Ltd. | 47 |
48. BundleActivator
Bundle-Name: Hello World
Bundle-SymbolicName: com.tikal.osgi.sample.helloworld
Bundle-Activator: com.tikal.osgi.sample.HelloActivator
public class HelloActivator implements BundleActivator {
public void start(BundleContext context) throws Exception {
System.out.println(“Hello, world!”);
System.out.println(Arrays.asList(context.getBundles()));
}
public void stop(BundleContext context) throws Exception {
System.out.println(“Goodbye, world!”);
}
}
Copyright 2009 Tikal Knowledge, Ltd. | 48 |
51. SOA Architecture
The OSGi framework promotes a service-oriented
interaction pattern among bundles
Copyright 2009 Tikal Knowledge, Ltd. | 51 |
52. SOA For The JVM
Published
Services
“Active” contribution
Private Services published in
implementation registry
objects bundles see service
changes immediately
Copyright 2009 Tikal Knowledge, Ltd. | 52 |
68. ServiceTracker
Shape Tracker goal – Use Inversion of Control
principles to inject shapes into application
» Created by the BundleActivator
» Puts tracking logic in one place
» Isolates application from OSGi API
User “Observer” design pattern to track services
for clients.
Copyright 2009 Tikal Knowledge, Ltd. | 68 |
75. A Typical JEE Application
D
Web Application D Lib
Lib Context
D
D Lib
Lib
D
D Middle-tier Lib
Context
Lib Lib
D
Copyright 2009 Tikal Knowledge, Ltd. | 75 |
76. JEE Application Base on OSGi
Each bundle is a segregated class
space
MVC MVC MVC
Lib
Lib Service Service
Lib
Domain
Repository Repository
Copyright 2009 Tikal Knowledge, Ltd. | 76 |
77. JEE Application on OSGi ?
How can we exploit OSGi
» Without adding complexity ?
» Retaining our programming model?
We want to treat bundles as beans
» Instantiated, Configure, Assemble, Decorate
» We want to easy way to publish/consume services
• Dynamic management
» Should we code this ourselves?
» Preserve ability to test
Copyright 2009 Tikal Knowledge, Ltd. | 77 |
78. Spring-DM Kernel
Integrates the simplicity and power of Spring...
...with the dynamic module system of OSGi
– Use spring container to configure bundles
– Make it easy to publish/subscribe services
– Application can be coded with minimal OSGi code
• Also easy test/integration-test
POJO development on OSGi
Copyright 2009 Tikal Knowledge, Ltd. | 78 |
79. Spring-DM Kernel Cont.
NO OSGi code & NO Spring code
» No BundleActivator
» No ServiceTracker
» No Spring invasive code
A Spring application context based on OSGi
bundle.
» OsgiBundleApplicationContext
Enables UnitTest and Integration Test
Copyright 2009 Tikal Knowledge, Ltd. | 79 |
83. Summary
OSGi is a dynamic module system for Java
» Proven
» Scalable (up and down)
OSGi Offer benefits in terms of
» Modularity (visibility)
» Versioning
» Operational Control
Spring dm kernel/server combines the simplicity
and power of Spring with sophistication of OSGi
platform.
Copyright 2009 Tikal Knowledge, Ltd. | 83 |