Your SlideShare is downloading. ×
OSGi-friendly bytecode weaving – enhance your classes, not your dependency graph!
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

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

3,469
views

Published on

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

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
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,469
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
57
Comments
0
Likes
4
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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