Wednesday, February 6, 2008

Adding Spring AOP into Glue framework

I have recently tried to use Spring AOP to provide crosscutting logging control to our existing Glue framework; it proved to be lot of pain without much gain. I might try AspectJ to see if it provides more way working with Glue.

The most of the difficulties lies in:

Issue 1: Trying to work with an unsupported product Glue doesn't help. There is no easy way to modify Glue internal binding process to add Spring AOP ProxyFactory way of instantiate new object.

Issue 2: There is literally no documentation out there regarding to how to configure spring to work with Glue and Resin.

The alternative I took for the Issue 1 is to bind to, for example, transcriptrequest proxy, it then create a new TranscriptRequestImpl the Spring AOP way:

public TranscriptRequest() {

ProxyFactory factory = new ProxyFactory(new TranscriptRequestImpl());

impl = (ITranscriptRequest)factory.getProxy();

}

This solution is workable. The disadvantage is that for each Web Service, we need to add a proxy.

Issue 2 turns out to be a harder problem. Spring throws the following exceptions when loading applicationContext:

org.springframework.beans.BeanWrapperImpl] ~ Invoked write method [public void org.springframework.aop.framework.ProxyFactoryBean.setProxyInterfaces(java.lang.String[]) throws org.aopalliance.aop.AspectException,java.lang.ClassNotFoundException] with value [[Ljava.lang.String;@21d23b]

org.springframework.beans.BeanWrapperImpl] ~ Invoked write method [public void org.springframework.aop.support.RegexpMethodPointcut.setPattern(java.lang.String) throws org.apache.oro.text.regex.MalformedPatternException] with value [.*]

Don't know why it's using org.apache.oro.text.regex instead of java.util.regex, maybe it is missing some essential components. It could be the problem with dynamically loaded code, specifically stuff that does a class.forName(...). Since they are dynamic in nature, so can't use any static typing info to figure out what is going on.

To verify the Java code is working, it is just the configuration problem. I programmatically added the advice to the ProxyFacotory, it works fine:

public TranscriptRequest() {

ProxyFactory factory = new ProxyFactory(new TranscriptRequestImpl());

impl = (ITranscriptRequest)factory.getProxy();

factory.addInterface(ITranscriptRequest.class);

factory.addBeforeAdvice(new TracingBeforeAdvice());

factory.addAfterReturningAdvice(new TracingAfterAdvice());

factory.addThrowsAdvice(new LoggingThrowsAdvice());

}

At the level of java code that I directly control, I could see the binding and when it happens.

If we programmatically add the advice to the factory, we could get the logging implemented. But this approach undermines the original purpose of adding AOP.