Successfully reported this slideshow.

JavaOne 2010: OSGI Migrat


Published on

The session details are accessible over here:

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.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

JavaOne 2010: OSGI Migrat

  1. 1. S314613: OSGI Migration headaches Krasimir Semerdzhiev SAP Labs Bulgaria Development Architect Peter Peshev SAP Labs Bulgaria Development Architect
  2. 2. Agenda OSGI intro Headaches Painkiller options Q&A
  3. 3. Modularity
  4. 4. Alternative implementations VW Golf 91-97
  5. 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. 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. 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. 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. 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
  10. 10. Module Layer: Runtime class loading load class
  11. 11. Class Loading Architecture: Class Space
  12. 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. 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. 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. 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”
  16. 16. Agenda OSGI intro Headaches Painkiller options Q&A
  17. 17. Step 1: Choose an OSGi runtime
  18. 18. Step 2: How to create bundles? Manifest-Version: 1.0 Bundle-Name: My First Bundle Bundle-SymbolicName: com.acme.myfirstbundle Bundle-Version: 1.0.0 Bundle-Description: Small Bundle Bundle-Vendor: ACME Bundle-Activator: com.acme.myfirstbundle.impl.Activator Bundle-Category: example Import-Package: org.osgi.framework Export-Package: com.acme.myfirstbundle.api Export-Package ::= export(‘,’ export)* export ::= package-names(‘;’ parameter)* package-names ::= package-name(‘;’ package-name)* Import-Package ::= import(‘,’ import)* import ::= package-names(‘;’ parameter)* package-names ::= package-name(‘;’ package-name)* Require-Bundle ::= bundle-description ( ',' bundle-description )* bundle-description ::= symbolic-name (';' parameter )*
  19. 19. Step 2: How to create bundles? Let‘s take one example – Apache Derby org.apache.derby.* authentication impl.jdbc.authentication catalog impl.load catalog.types iapi.sql database iapi.sql.compile diag iapi.sql.conn iapi iapi.sql.depend iapi.db iapi.sql.dictionary iapi.error iapi.sql.execute iapi.jdbc info io jdbc impl.sql loc impl.sql.catalog mbeans impl.sql.compile osgi impl.sql.conn security impl.sql.depend shared impl.sql.execute shared.common iapi.types impl.sql.execute.rts shared.common.error iapi.util shared.common.sanity impl tools impl.db vti impl.jdbc
  20. 20. Step 2: How to create bundles? Apache Derby – impl.* gone org.apache.derby.* authentication osgi catalog security catalog.types iapi.sql shared database iapi.sql.compile shared.common diag iapi.sql.conn shared.common.error iapi iapi.sql.depend shared.common.sanity iapi.db iapi.sql.dictionary tools iapi.error iapi.sql.execute vti iapi.jdbc iapi.types iapi.util info io jdbc loc mbeans
  21. 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. 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. 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. 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. 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. 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. 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. 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. 29. Step 4: Design flaws Boot delegation specifics ■ org.osgi.framework.bootdelegation – shortcut the OSGi resolution ■ Adding* includes all sub-packages, but not the package itself. ■ osgi.compatibility.bootdelegation - true/false or in other words… ■Make sure that you understand the setup of your OSGi environment
  30. 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/ ■ Explicit import of the impl packages is required!
  31. 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. 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. 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
  34. 34. Agenda OSGI intro Headaches Painkiller options Q&A
  35. 35. Eclipse Virgo shell improvements Supportability commands ■ Nested OSGi frameworks ■ Classloading
  36. 36. SpringSource Tool Suite
  37. 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
  38. 38. Q&A?
  39. 39. Thank you! Krasimir Semerdzhiev Peter Peshev