The session details are accessible over here: http://www.eventreg.com/cc250/sessionDetail.jsp?SID=314613
Session abstract
Nowadays, every platform considers having the "OSGi supported" checkbox ticked and offers "easy" and "smooth" migration from the classical Java EE world to the new "modular" future. In spite of the multiple guidelines and best practices, migrating from a monolithic Java EE application to OSGi remains a pain and reveals a number of hidden and time-consuming pitfalls and hurdles. The session will guide you through the usual problems that every project faces on its way to holy modularity, providing helpful tips and tricks on how to quickly find your way through the maze of NoClassDeffFound, ClassCast, and other OSGi-specific errors.
5. Good architecture and interchangeable
minilang party pos. product workeffort workflow …
accounting assetmaint content bi ebay ecommerce googlebase
guiapp hhfacility manufact. marketing minerva oagis order
Application layer
common catalina base datafile entity widget …
resolver rome serializer servlets tomcat xalan bsf
mail mx4j naming jetty poi wsdl4j bsh
jenks jpos18 jython looks lucene xerces velocity
jakarta jcl json jdbm jdom xmlapis ws-commons
log4j barcode4j axis batik commons xmlrpc geronimo
httpunit freemarker ezmorph fop derby xmlgraphics Libs..
Java VM
rt jce jsse plugin sunjce_prov. dnsns ..
6. Problem: CLASSPATH
Java VM party common resolver axis xerces
Begin rt
jce
assetmaint
hhfacility
catalina
base
mail
jenks
ezmorph
servlets
xmlapis
xmlrpc
Here jsse pos. datafile jakarta jetty xmlgraphics
plugin content entity log4j looks
sunjce_prov manufact. widget httpunit jdbm
.
dnsns product … mx4j bsf
.. bi rome batik bsh Class
marketing workflow jpos18 fop velocity
workeffort ecommerce jcl tomcat ws-commons
Not
ebay oagis barcode4j poi geronimo Found
minerva … freemarker lucene ..
Exception
minilang googlebase serializer jdom json
accounting order naming commons xalan
guiapp ofbiz jython derby wsdl4j
7. The OSGi Alliance
Started March 1999 as an initiative from ERICSSON, SUN and IBM
Today: A stable Alliance with almost 100 members
Now practically all of the Java Server vendors are represented there
A standardization body…
8. OSGi Framework Layers
Staircase
Applications can make use
of different layers directly
Module Layer defines
packaging , deployment
dependancies among
modules
Lifecycle Layer controls
lifecycyle operations of a
bundle
9. Module Layer: Bundles
Bundles
Are JAR files
Contain classes, other resources like HTML, images, specific metadata
Contain
manifest file (META-INF/MANIFEST.MF) with OSGI specific
headers used to manage the bundle
Describes the content of the JAR file
Export Import package
Require-Bundle
12. Class Loading Architecture: Delegation
Basic properties
Parent delegation
Wire importers and exporters
There can be fragment bundles
Class loading traversal based on importers and their wires
13. Lifecycle Layer
Runtime representation of bundles
Forevery bundle, the framework creates a Bundle java object
Bundles are identified by Bundle ID, Bundle Symbolic Name and
Bundle Location
Bundle has a runtime state:
14. What is the story?
Web Web Module bundle
Web Module
Web Modules Modules (war)
(war) bundle
Web Modules bundle
Web Modules
State 2
bundle
Lib Lib Lib Lib EAR file
EAR file OSGi framework
Java EE compliant runtime
bundle bundle bundle
bundle bundle bundle
bundle bundle bundle
bundle bundle bundle
bundle bundle bundle
OSGi framework
15. What do we aim to get from OSGi?
■ Standardized execution environment
■ Java-based modularization
■ Well defined module contract
■ Strict dependency management at build and run time
■ Manifest as a single point of truth
■ Deterministic declarative runtime resolution
or in other words…
■ Evolve from JARs/modules to “interchangeable/reusable parts”
21. Step 2: How to create bundles?
Apache Derby – what Eclipse uses out of it :-)
org.apache.derby.*
authentication
database
io
jdbc
vti How to create manifests?
By hand
BND tool
Apache Felix Maven plugin
Eclipse PDE
Bundlor
or in other words…
■ Don’t rush to define manifests, for modules you know nothing of!
22. Step 3: Let‘s try to activate the bundle.
java.lang.NoClassDefFoundException Class definition is
missing!
The forName() method in class Class.
Possible causes? The findSystemClass method() in class ClassLoader.
The loadClass() method in class ClassLoader
Missing import-package
Missing export-package
Import version mismatch [1.2.3, 2.0); “1.2.3”
Forgotten/missing component (really missing!)
Wrong boot class delegation property
Missing file system permissions (can’t read the JARs)
23. Step 3: Separate a the first library as a bundle
java.lang.NoClassDefFoundError Class definition is
found, but
instantiation failed!
Possible causes?
Exception in static block
Missing imported class (NoClassDefFoundException for it)
Class version mismatch
Hotspot reflection JIT optimizations (BugID 6265952 )
(injection of sun.misc.Unsafe.defineClass)
or in other words…
■Don’t rush blaming the framework! Classloading there is quite solid!
24. Step 4: Iterative refactoring
My bundle is not updated?!
Possible causes?
Didyou update the version?
You’re replacing your bundle directly in the equinox plugins folder
Package-imports do not fit to the new bundle version?
or in other words…
■Don’t rush blaming the framework! ;)
25. Step 4: Iterative refactoring
java.lang.ClassCastError
Possible causes?
Additionalcopies (besides the system bundle) of the
OSGi framework classes
Undesired additional JARs in classpath
■ Clash between RT.jar and application libs
(previously used embedded jars)
■ Java SE 5 vs. Java SE 6 does make a difference…
(a bunch of new packages added (StAX, JDBC 4.0, JAXB 2.0, etc.)
or in other words…
■Make sure you understand the root cause. (try using Eclipse Memory Analyzer)
26. Step 4: Iterative refactoring
Works in Eclipse, but doesn‘t work in Equinox
Possible issues
■ Different setting of org.osgi.parentClassloader
■ app (default for Eclipse)
■ boot
■ ext
■ fwk
Certain expectations of org.osgi.framework.system.packages
Different values of org.osgi.framework.bootdelegation
■ property osgi.compatibility.bootdelegation (default for Eclipse is false)
or in other words…
■Don’t be fooled by configuration differences
27. Step 4: Design flaws
Dependencies on startup order
Possible causes?
■ Avoid lookup-error-fail patterns
■ As a last resort – you can use start levels
■ Keep Activators really lean.
■ Typically seen when extracting code from a web module with fixed
Servlet.init() order.
or in other words…
■Don’t do anything needless in your activator. That’s not init()/start()!
28. Step 4: Design flaws
Classloader resource assumptions
Possible issues
■ JAR files
■ Folder containing class files/packages
■ URLs (to any of the above)
■ new File(<resource>), after loader resources iteration
“bundle:xxx” as a resource
or in other words…
■Don’t make assumptions on the classloader resources!
29. Step 4: Design flaws
Boot delegation specifics
■ org.osgi.framework.bootdelegation – shortcut the OSGi resolution
■ Adding com.foo.* includes all sub-packages, but not the com.foo
package itself.
■ osgi.compatibility.bootdelegation - true/false
or in other words…
■Make sure that you understand the setup of your OSGi environment
30. Step 5: Design flaws
SPI pattern limitations
Possible issues
■ Load a class, described in some meta-data file, used via interface
■ META-INF/services/javax.xml.ws.spi.Provider
■ Explicit import of the impl packages is required!
31. Step 5: Design flaws
My process exits unexpectedly?!
Possible issues
■ Lots of code is written with the assumption that it owns the world and not
that it lives in a small corner of it.
■ Enabling a Security Manager which traces System.exit() and Runtime.halt()
calls might help
32. Step 5: Design flaws
Registering objects in the VM has direct impact on the
ability to stop a bundle
Some of those
have no
Possible issues unregister methods!
■ System in and out streams
■ Shutdown hooks
■ MBean server factory
■ Log4J formatters
■ JNDI provider/factory
or in other words…
■Don’t try to do the job of the underlying middleware.
33. Step 5: Design flaws
ContextClassLoader usage
Possible issues
■ Some code written to run in a web app might make some assumptions on
the Context classloader.
■ One example here is JNDI.
or in other words…
■Consider environment dependencies when extracting code into bundles
37. Other relevant sessions
Tue 12:00 PM Developing OSGi-Enabled Java EE Applications
Tue 6:00 PM Patterns for modularity
Tue 6:00 PM OSGI BOF
Tue 9:00 PM OSGi at a Large-Scale Enterprise: Lessons from eBay
Wed 10:00 AM Java EE OSGi applications : Design and Deployment for On-Premises and Cloud
Wed 1:00 PM OSGi and JSR 294: Module Keyword
Wed 4:45 PM Visualizing the Science of Conversation with JavaFX and OSGi to Save Lives
Thu 3:30 PM Creating Modular Applications with Apache Aries and OSGi
Thu 3:30 PM Developing Rich Modular Clients with Java, JavaFX and OSGi Technology