1 package info.ejava.examples.ejb.interceptor.interceptors; 2 3 import javax.annotation.PostConstruct; 4 import javax.annotation.PreDestroy; 5 import javax.annotation.Resource; 6 import javax.ejb.EJBContext; 7 import javax.interceptor.AroundConstruct; 8 import javax.interceptor.AroundInvoke; 9 import javax.interceptor.AroundTimeout; 10 import javax.interceptor.InvocationContext; 11 12 import org.slf4j.Logger; 13 import org.slf4j.LoggerFactory; 14 15 16 public class LifecycleInterceptor { 17 private static final Logger logger = LoggerFactory.getLogger(LifecycleInterceptor.class); 18 19 @Resource 20 private EJBContext ejbCtx; 21 22 @AroundConstruct 23 public void ctor(InvocationContext ctx) { 24 try { 25 logger.debug("*** Contructor: {}, ejbCtx={}", ctx.getConstructor(), ejbCtx); 26 ctx.proceed(); 27 } catch (Exception ex) { 28 throw new RuntimeException("error calling post construct", ex); 29 } 30 } 31 32 @PostConstruct 33 public void init(InvocationContext ctx) { 34 logger.debug("*** Lifecycle event: {}::INIT", ctx.getTarget()); 35 try { 36 ctx.proceed(); 37 } catch (Exception ex) { 38 throw new RuntimeException("error calling post construct", ex); 39 } 40 } 41 42 @AroundInvoke 43 public Object invoke(InvocationContext ctx) throws Exception { 44 logger.debug("*** Business Method: {}, caller={}", ctx.getMethod(), ejbCtx.getCallerPrincipal().getName()); 45 Object response = ctx.proceed(); 46 logger.debug("*** Response Object: {}", response); 47 return response; 48 } 49 50 @AroundTimeout 51 public Object timeout(InvocationContext ctx) throws Exception { 52 logger.debug("*** Timeout: {}", ctx.getTimer()); 53 Object response = ctx.proceed(); 54 return response; 55 } 56 57 @PreDestroy 58 public void destory(InvocationContext ctx) { 59 logger.debug("*** Lifecycle event: {}::DESTROY", ctx.getTarget()); 60 try { 61 ctx.proceed(); 62 } catch (Exception ex) { 63 throw new RuntimeException("error calling pre destroy", ex); 64 } 65 } 66 }