Java Reference
In-Depth Information
The following illustrates a simple interceptor class.
package com.ensode.sessionbeanintro.ejb;
import java.lang.reflect.Method;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class LoggingInterceptor {
@AroundInvoke
public Object logMethodCall(
InvocationContext invocationContext)
throws Exception {
Object interceptedObject =
invocationContext.getTarget();
Method interceptedMethod =
invocationContext.getMethod();
System.out.println("Entering " +
interceptedObject.getClass().getName() + "." +
interceptedMethod.getName() + "()");
Object o = invocationContext.proceed();
System.out.println("Leaving " +
interceptedObject.getClass().getName() + "." +
interceptedMethod.getName() + "()");
return o;
}
}
The above example logs a message to the application server log just before and just
after an intercepted method is executed. The purpose of implementing something
like this would be to aid in debugging applications.
For simplicity, the above example simply uses System.out.println
to output messages to the application server log. A real application more
than likely would use a logging API such as the Java Logging API
or Log4j.
 
Search WWH ::




Custom Search