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.
Everything can be a bundle
Automatically repairing pre-OSGi code
...or “How to run jEdit as a bundle”
Erik Wistrand, wistr...
2
Makewave AB
Erik Wistrand
Everything can be a bundle
What's the problem?
• Lots of Java code has been written taking stu...
3
Makewave AB
Erik Wistrand
Everything can be a bundle
Classloading problems
Old, non-OSGi-aware libraries can easily be w...
4
Makewave AB
Erik Wistrand
Everything can be a bundle
Why ClassNotFoundException
For the OSGi import/export mechanism to ...
5
Makewave AB
Erik Wistrand
Everything can be a bundle
package mypackage;
class Foo {
System classloader
Bundle classloade...
6
Makewave AB
Erik Wistrand
Everything can be a bundle
What can be done?
The Knopflerfish OSGi framework contains code tha...
7
Makewave AB
Erik Wistrand
Everything can be a bundle
Common workarounds to fix classloading
• Modifying the library sour...
8
Makewave AB
Erik Wistrand
Everything can be a bundle
A new approach – automatic byte code modification
Since all bundle ...
9
Makewave AB
Erik Wistrand
Everything can be a bundle
The Knopflerfish implementation
• Uses the ASM byte-code manipulati...
10
Makewave AB
Erik Wistrand
Everything can be a bundle
jEdit bundle
Demo – run jEdit as a bundle
Knopflerfish OSGi + ASM
...
11
Makewave AB
Erik Wistrand
Everything can be a bundle
Example patch
Let's patch all library calls to
Class.forName(Strin...
12
Makewave AB
Erik Wistrand
Everything can be a bundle
First, write the wrapper method
public static
Class forName3Wrappe...
13
Makewave AB
Erik Wistrand
Everything can be a bundle
Next, find signature strings
Find the string signature of the meth...
14
Makewave AB
Erik Wistrand
Everything can be a bundle
Create config file
patch.1.from= java/lang/Class.
forName(Ljava/la...
15
Makewave AB
Erik Wistrand
Everything can be a bundle
...and launch
java 
-Dorg.knopflerfish.framework.patch=true 
-Dorg...
Upcoming SlideShare
Loading in …5
×

Everything can be a bundle — automatically repairing pre-OSGi code - Erik Wistrand

404 views

Published on

OSGi DevCon 2008

The OSGi platform expects bundles to play nicely, e.g. only use the bundle class loader, use global resources as system properties wisely, and never-ever call System.exit(). However, a large number of useful libraries are not aware of these restrictions and are thus problematic to use without source code modifications and recompilation. This talk will explore means of automatically, at run-time, solve these problems using byte-code manipulation and automatic creation of bundle manifests.
The target audience of this talk are both experienced implementors who are interested in the inner workings of the OSGi framework and beginner OSGi programmers who are interested in why third-party libs "don't find their classes". A short demo based on the Knopflerfish OSGi framework will be provided and limitations of the methods will be mentioned.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Everything can be a bundle — automatically repairing pre-OSGi code - Erik Wistrand

  1. 1. Everything can be a bundle Automatically repairing pre-OSGi code ...or “How to run jEdit as a bundle” Erik Wistrand, wistrand@makewave.com
  2. 2. 2 Makewave AB Erik Wistrand Everything can be a bundle What's the problem? • Lots of Java code has been written taking stuff for granted • Just one classloader • System.exit() is OK to call • A file system is present • A static main() method is used for startup • ... ...this leads to classloading problems, premature exit of the entire framework and various internal bugs
  3. 3. 3 Makewave AB Erik Wistrand Everything can be a bundle Classloading problems Old, non-OSGi-aware libraries can easily be wrapped as an OSGi bundle Sounds easy, just create a manifest file and export/import the necessary stuff ...but they may still run into classloading problems ClassNotFoundException: Cannot find com.acme.ICustomer
  4. 4. 4 Makewave AB Erik Wistrand Everything can be a bundle Why ClassNotFoundException For the OSGi import/export mechanism to work, the bundle must use the Bundle classloader However, many libraries uses the system classloader or the thread's classloader • Works great in standalone apps • Does not work at all in typical OSGi settings
  5. 5. 5 Makewave AB Erik Wistrand Everything can be a bundle package mypackage; class Foo { System classloader Bundle classloader mypackage.Foo.class mypackage.Bar.class Class c = ...getContextClassLoader().loadClass(“mypackage.Bar”); Class c = Foo.class.getClassloader().loadClass(“mypackage.Bar”); OSGi framework Fails! ok!
  6. 6. 6 Makewave AB Erik Wistrand Everything can be a bundle What can be done? The Knopflerfish OSGi framework contains code that can • Start jar files with only a static main method • Automatically import/export packages • Patch code that uses bad methods regarding classloading etc. This session will focus on class patching
  7. 7. 7 Makewave AB Erik Wistrand Everything can be a bundle Common workarounds to fix classloading • Modifying the library source to be OSGi-aware and recompile • Impractical • License issues • Wrapping each library call in code that sets the thread's context class loader • Tricky to find all cases • Boilerplate code is a Bad Thing • Put all classes on the system class path! • Really just hurts
  8. 8. 8 Makewave AB Erik Wistrand Everything can be a bundle A new approach – automatic byte code modification Since all bundle classes are loaded via the OSGi framework, the framework can modify the bytecode on the fly to use the correct classloader 1. Load original byte code 2. Find occurances of wrong method calls 3. Replace with call to right method 4. Use the modified class (5. Profit!)
  9. 9. 9 Makewave AB Erik Wistrand Everything can be a bundle The Knopflerfish implementation • Uses the ASM byte-code manipulation library • A configuration file can specify which bundles/classes should be modified • Bundles are automatically modified as they are loaded Yes, this is a poor man's aspect oriented framework btw, LDAP filters are the best thing since sliced bread. Yes, they are!
  10. 10. 10 Makewave AB Erik Wistrand Everything can be a bundle jEdit bundle Demo – run jEdit as a bundle Knopflerfish OSGi + ASM Jedit 4.2 std distribution Std OSGi bundles Wrapper methods JVM http://www.knopflerfish.org/econ2008/knopflerfish-econ2008.zip
  11. 11. 11 Makewave AB Erik Wistrand Everything can be a bundle Example patch Let's patch all library calls to Class.forName(String name, boolean init, ClassLoader cl)
  12. 12. 12 Makewave AB Erik Wistrand Everything can be a bundle First, write the wrapper method public static Class forName3Wrapper(String name, boolean initialize, ClassLoader cl, long bid, Object context) throws ClassNotFoundException { return ... // use bundle classloader instead }
  13. 13. 13 Makewave AB Erik Wistrand Everything can be a bundle Next, find signature strings Find the string signature of the method to be patched java/lang/Class.forName(Ljava/lang/Strin g;ZLjava/lang/ClassLoader;)Ljava/lang/Cl ass; Find the string signature or the static wrapper method that should replace the above call org/knopflerfish/framework/ClassPatcherWr appers.forName3Wrapper
  14. 14. 14 Makewave AB Erik Wistrand Everything can be a bundle Create config file patch.1.from= java/lang/Class. forName(Ljava/lang/String;ZLjava/lang/ClassLoader;) Ljava/lang/Class; patch.1.to= org/knopflerfish/framework/ClassPatcherWrappers. forName3Wrapper patch.1.static=true
  15. 15. 15 Makewave AB Erik Wistrand Everything can be a bundle ...and launch java -Dorg.knopflerfish.framework.patch=true -Dorg.knopflerfish.framework.patch.configurl= file:patches.props -jar framework.jar:asm-3.1.jar org.knopflerfish.framework.Main

×