SlideShare a Scribd company logo
1 of 15
Download to read offline
Everything can be a bundle
Automatically repairing pre-OSGi code
...or “How to run jEdit as a bundle”
Erik Wistrand, wistrand@makewave.com
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
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
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
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
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
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
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
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
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
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
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
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
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
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

More Related Content

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

Bytecode Weaving in OSGi – Enhance Your Classes, Not Your Dependency graph! ...
Bytecode Weaving in OSGi – Enhance Your Classes, Not Your Dependency graph!  ...Bytecode Weaving in OSGi – Enhance Your Classes, Not Your Dependency graph!  ...
Bytecode Weaving in OSGi – Enhance Your Classes, Not Your Dependency graph! ...
mfrancis
 
Laporan Praktikum Keamanan Siber - Tugas 5 -Kelas C - Kelompok 3.pdf
Laporan Praktikum Keamanan Siber - Tugas 5 -Kelas C - Kelompok 3.pdfLaporan Praktikum Keamanan Siber - Tugas 5 -Kelas C - Kelompok 3.pdf
Laporan Praktikum Keamanan Siber - Tugas 5 -Kelas C - Kelompok 3.pdf
IGedeArieYogantaraSu
 
Enterprise OSGi at eBay
Enterprise OSGi at eBayEnterprise OSGi at eBay
Enterprise OSGi at eBay
Tony Ng
 

Similar to Everything can be a bundle — automatically repairing pre-OSGi code - Erik Wistrand (20)

Third party libraries and OSGi - a complicated relationship
Third party libraries and OSGi - a complicated relationshipThird party libraries and OSGi - a complicated relationship
Third party libraries and OSGi - a complicated relationship
 
Bytecode Weaving in OSGi – Enhance Your Classes, Not Your Dependency graph! ...
Bytecode Weaving in OSGi – Enhance Your Classes, Not Your Dependency graph!  ...Bytecode Weaving in OSGi – Enhance Your Classes, Not Your Dependency graph!  ...
Bytecode Weaving in OSGi – Enhance Your Classes, Not Your Dependency graph! ...
 
Intro To OSGi
Intro To OSGiIntro To OSGi
Intro To OSGi
 
Java and the JVM
Java and the JVMJava and the JVM
Java and the JVM
 
Racing with Droids
Racing with DroidsRacing with Droids
Racing with Droids
 
OSGi bootcamp - part 1
OSGi bootcamp - part 1OSGi bootcamp - part 1
OSGi bootcamp - part 1
 
Cloud forensics putting the bits back together
Cloud forensics putting the bits back togetherCloud forensics putting the bits back together
Cloud forensics putting the bits back together
 
Tuscany : Applying OSGi After The Fact
Tuscany : Applying  OSGi After The FactTuscany : Applying  OSGi After The Fact
Tuscany : Applying OSGi After The Fact
 
Introduction to OSGi
Introduction to OSGiIntroduction to OSGi
Introduction to OSGi
 
CODE BLUE 2014 : BadXNU, A rotten apple! by PEDRO VILAÇA
CODE BLUE 2014 : BadXNU, A rotten apple! by PEDRO VILAÇACODE BLUE 2014 : BadXNU, A rotten apple! by PEDRO VILAÇA
CODE BLUE 2014 : BadXNU, A rotten apple! by PEDRO VILAÇA
 
It's always sunny with OpenJ9
It's always sunny with OpenJ9It's always sunny with OpenJ9
It's always sunny with OpenJ9
 
Laporan Praktikum Keamanan Siber - Tugas 5 -Kelas C - Kelompok 3.pdf
Laporan Praktikum Keamanan Siber - Tugas 5 -Kelas C - Kelompok 3.pdfLaporan Praktikum Keamanan Siber - Tugas 5 -Kelas C - Kelompok 3.pdf
Laporan Praktikum Keamanan Siber - Tugas 5 -Kelas C - Kelompok 3.pdf
 
Project Basecamp: News From Camp 4
Project Basecamp: News From Camp 4Project Basecamp: News From Camp 4
Project Basecamp: News From Camp 4
 
Javasession10
Javasession10Javasession10
Javasession10
 
Testing your infallibleness
Testing your infalliblenessTesting your infallibleness
Testing your infallibleness
 
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
 
Migrating to Java 9 Modules
Migrating to Java 9 ModulesMigrating to Java 9 Modules
Migrating to Java 9 Modules
 
MLSEC 2020
MLSEC 2020MLSEC 2020
MLSEC 2020
 
How Class Data Sharing Can Speed up Your Jakarta EE Application Startup
How Class Data Sharing Can Speed up Your Jakarta EE Application StartupHow Class Data Sharing Can Speed up Your Jakarta EE Application Startup
How Class Data Sharing Can Speed up Your Jakarta EE Application Startup
 
Enterprise OSGi at eBay
Enterprise OSGi at eBayEnterprise OSGi at eBay
Enterprise OSGi at eBay
 

More from mfrancis

Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
mfrancis
 
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
mfrancis
 
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
mfrancis
 
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
mfrancis
 

More from mfrancis (20)

Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...
Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...
Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...
 
OSGi and Java 9+ - BJ Hargrave (IBM)
OSGi and Java 9+ - BJ Hargrave (IBM)OSGi and Java 9+ - BJ Hargrave (IBM)
OSGi and Java 9+ - BJ Hargrave (IBM)
 
Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)
Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)
Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)
 
OSGi for the data centre - Connecting OSGi to Kubernetes - Frank Lyaruu
OSGi for the data centre - Connecting OSGi to Kubernetes - Frank LyaruuOSGi for the data centre - Connecting OSGi to Kubernetes - Frank Lyaruu
OSGi for the data centre - Connecting OSGi to Kubernetes - Frank Lyaruu
 
Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...
Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...
Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...
 
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
 
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
 
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
 
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
 
OSGi CDI Integration Specification - Ray Augé (Liferay)
OSGi CDI Integration Specification - Ray Augé (Liferay)OSGi CDI Integration Specification - Ray Augé (Liferay)
OSGi CDI Integration Specification - Ray Augé (Liferay)
 
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
 
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
 
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
 
Popular patterns revisited on OSGi - Christian Schneider (Adobe)
Popular patterns revisited on OSGi - Christian Schneider (Adobe)Popular patterns revisited on OSGi - Christian Schneider (Adobe)
Popular patterns revisited on OSGi - Christian Schneider (Adobe)
 
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
 
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
 
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
 
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
 
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
 
How to connect your OSGi application - Dirk Fauth (Bosch)
How to connect your OSGi application - Dirk Fauth (Bosch)How to connect your OSGi application - Dirk Fauth (Bosch)
How to connect your OSGi application - Dirk Fauth (Bosch)
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

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

  • 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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