December 15th, 2013

Load Time Weaving in Fuse ESB (Apache ServiceMix) with Equinox Aspects

How to enable AOP in Fuse ESB / ServiceMix

— Matthew Fellows —

Aspect oriented programming in Java has some really awesome benefits (and pitfalls), but it’s a bit different when used in OSGi environments due to the way class loading is performed. There is a weaving hook specification but support is still limited at the time of writing.

Here is how you can take advantage of Equinox Aspects in Fuse ESB, enabling you to do things like centralised security, monitoring/auditing, logging, caching and so on. It’s cool, you should use it and it will make your code much cleaner.

Install JARs

Download Equinox Aspects from http://eclipse.org/equinox/incubator/aspects/equinox-aspects-downloads.php. Unzip the archive and copy the JARs to <fuse home>/system/. Additionally, copy the weaving hook lib into <fuse home>/lib. Lastly, copy the springweaver JAR file from http://martinlippert.blogspot.com.au/2009/04/load-time-weaving-for-spring-dm.html and drop that into <fuse home>/system also.

At the end of it all, your dir structure should look something like this:


./lib/org.eclipse.equinox.weaving.hook_1.0.0.200807082136.jar
 ./system/org.eclipse.equinox.weaving.aspectj_1.0.0.200807082136.jar
 ./system/org.eclipse.equinox.weaving.caching.j9_1.0.0.200807082136.jar
 ./system/org.eclipse.equinox.weaving.caching_1.0.0.200807082136.jar
 ./system/org.eclipse.equinox.weaving.hook_1.0.0.200807082136.jar
 ./system/org.eclipse.equinox.weaving.springweaver_0.1.1.jar

Edit Configuration Files

vim etc/config.properties, add in the equinox weaving jar (line 54)

#
# Framework config properties.
#
org.osgi.framework.system.packages=org.osgi.framework; version=1.5.0, \
org.osgi.framework.launch; version=1.0.0, \
org.osgi.framework.hooks.service; version=1.0.0, \
org.osgi.service.packageadmin; version=1.2.0, \
org.osgi.service.startlevel; version=1.1.0, \
org.osgi.service.url; version=1.0.0, \
org.osgi.util.tracker; version=1.4.0, \
org.apache.karaf.jaas.boot; version=2.2.0.fuse-00-43, \
org.apache.karaf.version; version=2.2.0.fuse-00-43, \
org.eclipse.equinox.service.weaving, \
${jre-${java.specification.version}}

Add the following to the bottom of etc/startup.properties

#
# Equinox aspects
#
org.eclipse.equinox.weaving.hook_1.0.0.200807082136.jar=31
org.eclipse.equinox.weaving.caching.j9_1.0.0.200807082136.jar=31
org.eclipse.equinox.weaving.caching_1.0.0.200807082136.jar=31
org.eclipse.equinox.weaving.springweaver_0.1.1.jar=31

Add the following to the bottom of  etc/system.properties

# Equinox Aspects
osgi.framework.extensions=org.eclipse.equinox.weaving.hook
aj.weaving.verbose=true
org.aspectj.weaver.showWeaveInfo=true
org.aspectj.osgi.verbose=true

OSGi bundle imports

You’ll need the following added to your Manifest file:


Import-Bundle: PATH-TO.aspectj.rt;visibility:=reexport

...

org.aspectj.runtime.reflect;version="[1.7.0,1.8.0)",\
 org.aspectj.util;version="[1.7.0,1.8.0)",\
 org.aspectj.weaver;version="[1.7.0,1.8.0)",\
 org.aspectj.weaver.ast;version="[1.7.0,1.8.0)",\
 org.aspectj.weaver.bcel;version="[1.7.0,1.8.0)",\
 org.aspectj.weaver.bcel.asm;version="[1.7.0,1.8.0)",\
 org.aspectj.weaver.internal.tools;version="[1.7.0,1.8.0)",\
 org.aspectj.weaver.loadtime;version="[1.7.0,1.8.0)",\
 org.aspectj.weaver.loadtime.definition;version="[1.7.0,1.8.0)",\
 org.aspectj.weaver.ltw;version="[1.7.0,1.8.0)",\
 org.aspectj.weaver.model;version="[1.7.0,1.8.0)",\
 org.aspectj.weaver.patterns;version="[1.7.0,1.8.0)",\
 org.aspectj.weaver.reflect;version="[1.7.0,1.8.0)",\
 org.aspectj.weaver.tools;version="[1.7.0,1.8.0)",\
 org.springframework.instrument.classloading;version="3.0.5.RELEASE",\
 org.springframework.aop;version=3.0.5.RELEASE,\

Use it!

To use the Weaver in a spring configuration file, you can use the context extensions like so:


<context:annotation-config />
<context:load-time-weaver weaver-class="org.eclipse.equinox.weaving.springweaver.EquinoxAspectsLoadTimeWeaver" />
<context:component-scan base-package="au.com.substantiate.security" />