Τετάρτη 22 Δεκεμβρίου 2010

Πληρώστε για να πληρώσετε!

Ίσως το πιο άθλιο που έχω δει σε site που πουλάει προϊόντα μέσω Διαδικτύου!ΥΓ: Επίτηδες δεν αναφέρω το site

Possibly the most usefull library for an existing system!

I was always wondering how to intercept method calls. How to execute code before and after calling some method, for logging perposes or whatever! Today it became a real need so I searched and found that there is a solution name "Tie: Java Method Interception" and you can find it at http://sourceforge.net/projects/tie/

Notes:
1) You need to have an interface for each class that you want to intercept
2) You need to implement MethodInterceptor in order to handle method invocations
3) You need a single instance of your MethodInterceptor's implementation, MethodInterceptorStack and ProxyInjector

In my case I created a class named "LoggingManager" which watches several classes
import net.sf.tie.InterceptorStack;
import net.sf.tie.MethodInterceptorStack;
import net.sf.tie.ProxyInjector;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class LoggingManager implements MethodInterceptor {

public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("Method to be called:" + invocation.getMethod().getName());
return invocation.proceed();
}
static ProxyInjector proxyInjector;

static{
LoggingManager log = new LoggingManager();
MethodInterceptorStack stack = InterceptorStack.singleton(log);
proxyInjector= new ProxyInjector(stack);
}

private LoggingManager() {
}

public static AgentIF watch(AgentIF agent) {
agent = proxyInjector.wrapObject(AgentIF.class, agent);
return agent;
}
}

If you need more information leave a comment and I will answer to you as soon as possible!

LinkWithin

Blog Widget by LinkWithin

Mobile edition