Java Reference
In-Depth Information
LISTING 8‐7: Accessing information in the InvocationContext
package com.devchronicles.interceptor;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
@Interceptor
public class SecurityInterceptor {
@AroundInvoke
public Object doSecurityCheck(InvocationContext context) throws Exception{
//Do some security checks!     
Logger.getLogger("SecurityLog").info(context.getMethod()
.getName()+ "is accessed!");
String user = context.getContextData.get("user");
if (user==null){
user=(String)context.getParameters()[0];
context.getContextData.put("user", user)'
}
return context.proceed();
}
}
Interceptor Life Cycle
You can capture the interceptor's life-cycle phases easily with the help of life-cycle annotations.
Unlike extending and overriding, using life-cycle annotations hooks any function to the appropriate
phase. The available life-cycle annotations are @PostConstruct , @PrePassivate , @PostActivate ,
and @PreDestroy . Listing 8‐8 shows how to hook up using interceptor life-cycle methods.
y
LISTING 8‐8: Hooking the interceptor's life-cycle phases
package com.devchronicles.interceptor;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
@Interceptor
public class SecurityInterceptor {
    @AroundInvoke
    public Object doSecurityCheck(InvocationContext context)
throws Exception{
  //Do some security checks!
      Logger.getLogger("SecurityLog").info(context.getMethod()
.getName()+ "is accessed!");
continues
 
Search WWH ::




Custom Search