1 package info.ejava.examples.ejb.interceptor.interceptors; 2 3 import info.ejava.examples.ejb.interceptor.bo.Contact; 4 import info.ejava.examples.ejb.interceptor.bo.PhoneInfo; 5 import info.ejava.examples.ejb.interceptor.bo.PostalInfo; 6 import info.ejava.examples.ejb.interceptor.normalizer.ContactNormalizer; 7 8 import javax.annotation.PostConstruct; 9 import javax.annotation.PreDestroy; 10 import javax.interceptor.AroundInvoke; 11 import javax.interceptor.Interceptor; 12 import javax.interceptor.InvocationContext; 13 14 import org.slf4j.Logger; 15 import org.slf4j.LoggerFactory; 16 17 @Validation 18 @Interceptor 19 public class ContactsNormalizerInterceptor { 20 private static final Logger logger = LoggerFactory.getLogger(ContactsNormalizerInterceptor.class); 21 private ContactNormalizer contactNormalizer=new ContactNormalizer(); 22 23 @PostConstruct 24 public void init() { 25 logger.debug("{}:init({})", getClass().getSimpleName(), super.hashCode()); 26 } 27 @PreDestroy 28 public void destroy() { 29 logger.debug("{}:destroy({})", getClass().getSimpleName(), super.hashCode()); 30 } 31 32 @AroundInvoke 33 public Object invoke(InvocationContext ctx) throws Exception { 34 logger.debug("intercepting: {}", ctx.getMethod()); 35 try { 36 for (Object param: ctx.getParameters()) { 37 if (param instanceof Contact) { 38 logger.debug("normalizing: {}", param); 39 contactNormalizer.normalize((Contact) param); 40 logger.debug("normalized: {}", param); 41 } else if (param instanceof PostalInfo) { 42 logger.debug("normalizing: {}", param); 43 contactNormalizer.normalize((PostalInfo) param); 44 logger.debug("normalized: {}", param); 45 } else if (param instanceof PhoneInfo) { 46 logger.debug("normalizing: {}", param); 47 contactNormalizer.normalize((PhoneInfo) param); 48 logger.debug("normalized: {}", param); 49 } 50 } 51 return ctx.proceed(); 52 } finally { 53 54 } 55 } 56 }