<ul>OSGi-friendly bytecode weaving –  enhance your classes, not your dependency graph! </ul><ul>Tim Ward </ul>
<ul>What we're going to cover </ul><ul><li>What weaving is
How weaving is done in Java SE (10,000 foot view)
Why OSGi needs a different approach
How to use OSGi weaving hooks
How to do classpath scanning in OSGi
A real world demo using classpath scanning and weaving </li></ul>
<ul>What is bytecode weaving? </ul><ul><li>Simply put, bytecode weaving involves generating or editing code by directly mo...
This can occur at different points in the application lifecycle </li><ul><li>At compile time
At packaging time
At class load time
After a class has been loaded </li></ul><li>Weaving later is more flexible (to a point) </li></ul>Outside a VM Inside a VM
<ul>How do we normally weave bytecode? </ul><ul><li>In Java 1.4 you had to create your own ClassLoader and mess about with...
Upcoming SlideShare
Loading in...5
×

OSGi-friendly bytecode weaving – enhance your classes, not your dependency graph!

3,613

Published on

Presentation by Tim Ward (IBM) at OSGi DevCon/EclipseCon 2011.

For many years bytecode weaving in OSGi has been a cumbersome process. Using it's internal hooks Equinox has supported basic transformation since version 3.2, but there has still been no simple, standard way to enhance classes or add new dependencies to the bundle that contains them. The Equinox 3.7 release includes support for the new OSGi WeavingHook whiteboard pattern, which allows any OSGi bundle, not just an Equinox framework extension, to weave classes from, and add dependencies to, other OSGi bundles.

Also new in OSGi is the opportunity to scan the classpath of an OSGi bundle. Whilst the isolated, modular classpath of an OSGi bundle provides an excellent system for building Java applications, many extenders and libraries need to be able to search for classes or resources within the bundle. This function further aids weaving implementations by allowing them to search for resources, class and package name patterns, and other configuration without requiring specific metadata to be present.

As the lead author for the Bytecode Weaving design within the OSGi Alliance, and a committer in the Apache Aries project, Tim will use his expertise to demonstrate the power and flexibility of WeavingHooks, particularly in conjunction with the latest OSGi updates for classpath scanning. Drawing on real-world examples from the Apache Aries project Tim will use real-world examples and Equinox 3.7 to prove that first-class weaving support is now available in OSGi

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,613
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
58
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

OSGi-friendly bytecode weaving – enhance your classes, not your dependency graph!

  1. 1. <ul>OSGi-friendly bytecode weaving – enhance your classes, not your dependency graph! </ul><ul>Tim Ward </ul>
  2. 2. <ul>What we're going to cover </ul><ul><li>What weaving is
  3. 3. How weaving is done in Java SE (10,000 foot view)
  4. 4. Why OSGi needs a different approach
  5. 5. How to use OSGi weaving hooks
  6. 6. How to do classpath scanning in OSGi
  7. 7. A real world demo using classpath scanning and weaving </li></ul>
  8. 8. <ul>What is bytecode weaving? </ul><ul><li>Simply put, bytecode weaving involves generating or editing code by directly modifying existing .class files
  9. 9. This can occur at different points in the application lifecycle </li><ul><li>At compile time
  10. 10. At packaging time
  11. 11. At class load time
  12. 12. After a class has been loaded </li></ul><li>Weaving later is more flexible (to a point) </li></ul>Outside a VM Inside a VM
  13. 13. <ul>How do we normally weave bytecode? </ul><ul><li>In Java 1.4 you had to create your own ClassLoader and mess about with the class bytes before defineClass()
  14. 14. Java 5 introduced Java Agents and a defined transformation API (java.lang.instrument) </li><ul><li>ClassFileTransformers registered on a VM wide basis </li></ul><li>Java 6 supports re-transformation (post class load) and addition of Java Agents to a running VM
  15. 15. In all cases we get an opportunity to read/change the bytes of the class file before they are loaded into the VM </li></ul>
  16. 16. <ul>Why do we need a Weaving Hook? </ul><ul><li>Clearly the base VM support exists already but...
  17. 17. OSGi is modular </li><ul><li>Java Agent Class transformation is VM wide </li></ul><li>OSGi has a ClassLoader graph, not a flat classpath </li><ul><li>Weaving often adds new dependencies to a class
  18. 18. New dependencies cannot be loaded without being imported by the bundle hosting the woven class </li></ul></ul>
  19. 19. <ul>How to use a Weaving Hook </ul><ul><li>This is very simple!
  20. 20. Register your implementation as a service using the org.osgi.framework.hooks.weaving.WeavingHook interface </li></ul><ul><li>The framework will call your hook for every class loaded by any bundle </li><ul><li>If security is on then you need AdminPermission WEAVE to make any changes for a given bundle </li></ul></ul>Bundle WeavingHook
  21. 21. <ul>How to use a Weaving Hook (2) </ul><ul><li>You only implement a single method </li><ul><li>public void weave(WovenClass classInfo) </li></ul><li>The WovenClass provides access to: </li><ul><li>The Bundle defining the class
  22. 22. Read/Replace the class bytes that will be used
  23. 23. The BundleWiring and Bundle ClassLoader
  24. 24. A List of dynamic package imports to add, this allows us to add new dependencies based on what we weave! </li></ul></ul>
  25. 25. <ul>Working out what to weave </ul><ul><li>Most weavers use XML and annotations to configure which classes should be woven and what to do to them </li><ul><li>Annotations mean we need access to the class
  26. 26. We don't know the name of the class </li></ul><li>In Java SE we tend to rely on searching the file system to find class files to load on the classpath </li><ul><li>OSGi does not (in general) let you do this </li></ul></ul>
  27. 27. <ul>Scanning the bundle Classpath </ul><ul><li>The BundleWiring API adds a new method </li><ul><li>public Collection<String> listResources( String path, String filePattern, int options ) </li></ul><li>This method lets us scan for resources on the classpath </li><ul><li>path can be used to specify a package folder
  28. 28. filePattern lets you specify a type (e.g. *.class)
  29. 29. options lets you specify whether to recurse into sub-packages, and whether to include resources that are imported from other bundles </li></ul></ul>
  30. 30. <ul>Combining the two specifications </ul><ul><li>There are two options for scanning </li><ul><li>Up front
  31. 31. When the WeavingHook is called </li></ul><li>The BundleWiring is accessible from the Bundle object using the adapt method </li><ul><li>bundle.adapt(BundleWiring.class); </li></ul><li>The BundleWiring is also available from the WovenClass </li><ul><li>wovenClass.getBundleWiring(); </li></ul></ul>
  32. 32. <ul>Live demo! </ul><ul><li>The Apache Aries JPA container uses both scanning and a Weaving Hook to provide load-time entity enhancement </li><ul><li>Well it will once OSGi 4.3 is in Maven Central and I've finished the tests :) </li></ul><li>Using a debugger we can see the various pieces in action! </li></ul>
  33. 33. <ul>Any Questions? </ul><ul><li>Apache Aries:
  34. 34. http://aries.apache.org/
  35. 35. Tim Ward:
  36. 36. @TimothyWard, [email_address] </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×