Java Reference
In-Depth Information
In effect, this means that the
logMethodEntry()
method will be executed whenever
the
ActionBazaarLogger
interceptor is triggered. As you might gather from this code,
any method designated
@AroundInvoke
must follow this pattern:
Object <METHOD>(InvocationContext) throws Exception
The
InvocationContext
interface passed in as the single parameter to the method
provides a number of features that make the AOP mechanism extremely flexible. The
logMethodEntry()
method uses just two of the methods included in the interface.
The
getMethod().getName()
call returns the name of the method being intercep-
ted—
"addBid"
in this case.
The call to the
proceed()
method is extremely critical to the functioning of the intercept-
or. This tells the container it should proceed to the next interceptor in the execution chain
or call the intercepted business method. On the other hand, not calling the
proceed()
method will bring processing to a halt and prevent the business method (and any other in-
terceptor down the execution chain) from being called.
This feature can be extremely useful for procedures like security validation. For example,
the following interceptor method prevents the intercepted business method from being ex-
ecuted if security validation fails:
@AroundInvoke
public Object validateSecurity(InvocationContext invocationContext)
throws Exception {
if (!validate(...)) {
throw new SecurityException("Security cannot be validated. " +
"The method invocation is being blocked.");
}
return invocationContext.proceed();
}
The InvocationContext interface
The following listing shows that the
javax.interceptor.InvocationContext
interface has a number of other useful methods.
Listing 5.11.
javax.interceptor.InvocationContext
interface
public interface InvocationContext {
public Object getTarget();
public Method getMethod();