12.07.2015 Views

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 8 ■ CALLBACKS AND INTERCEPTORSListing 8-3. CustomerEJB Uses an Interceptor@Statelesspublic class CustomerEJB {@PersistenceContext(unitName = "chapter08PU")private EntityManager em;private Logger logger = Logger.getLogger("com.apress.javaee6");public void createCustomer(Customer customer) {em.persist(customer);}public Customer findCustomerById(Long id) {return em.find(Customer.class, id);}@AroundInvokeprivate Object logMethod(InvocationContext ic) throws Exception {logger.entering(ic.getTarget().toString(), ic.getMethod().getName());try {return ic.proceed();} finally {logger.exiting(ic.getTarget().toString(), ic.getMethod().getName());}}}Despite being annotated <strong>with</strong> @AroundInvoke, the logMethod() has to follow a certain signature:@AroundInvokeObject (InvocationContext ic) throws Exception;The following rules apply to an around-invoke method:• The method can have public, private, protected, or package-level access, butmust not be static or final.• The method must have a javax.interceptor.InvocationContext parameter andmust return Object, which is the result of the invoked target method (if themethod returns void, it returns null).• The method can throw a checked exception.The InvocationContext object allows interceptors to control the behavior of the invocation chain. Ifseveral interceptors are chained, the same InvocationContext instance is passed to each interceptor,which can add contextual data to be processed by other interceptors. The InvocationContext API isdescribed in Table 8-2.245

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!