Friday, April 3, 2009

AspectJ for Java logging: Serviceability without the clutter - Instrumenting a 3rd party library - part 4

Another common serviceability problem while assembling libraries from multiple sources is that sometimes a reused library may use a difference serviceability framework, or even worse, not have any serviceability at all.

In the previous entries I covered techniques to instrument your own source code and source code from a different contributor. Now it is time to cover a technique to cover 3rd party libraries.

With AspectJ, this is a simple task accomplished with the AspectJ compiler: ajc. The compiler parameters are covered in the ajc page of the AspectJ developer's guide, but I suspect the majority of the people building Java code will resort to an Ant script. AspectJ has an Ant task called iajc, from where you can execute the same commands.

----

Compiling your aspects

The first step is, of course, to compile the serviceability aspects covered in the previous entries.

<iajc destdir="${dir.build.classes}" classpathref="class.path" deprecation="${javac.deprecation}">
<sourceroots>
<pathelement location="${dir.src}"/>
<pathelement location="${aspects.path}"/>
</sourceroots>
</iajc>




where "dir.src" is the directory where the project source code is located and "aspects.path" is the source directory where the aspects are located





----





Instrumenting a 3rd party library





The Ant taks has to be slightly modified to reflect a JAR file as the type of input and output, like this:








<iajc outJar="${dir.build.lib}/3rdparty.jar" classpathref="class.path" deprecation="${javac.deprecation}">
  <injars>
<pathelement location="${3rdparth.installdir}/libs/3rdparty.jar"/>
</injars>
<sourceroots>
<pathelement location="${aspects.path}"/>
</sourceroots>
</iajc>


----





And you are done! If both the JSR-47 and the Log4J aspects are in place, not only your new "3rdparty.jar" file will be instrumented with serviceability trace statements, it will also replace all extraneous Log4J calls with calls to their equivalent in JSR-47.





That is a very good result, considering that the resulting application assembled from multiple sources and libraries will benefit from these desirable serviceability traits:





  1. Consistent tracing, with trace entries for all the method entry and exit points and for all catch blocks


  2. Consistent trace control, with a single JSR-47 logging.properties file for all components


  3. Consistent output, with all modules directing their tracing output to the same file.


No comments:

Post a Comment