Book Image

EJB 3 Developer Guide

By : Michael Sikora
Book Image

EJB 3 Developer Guide

By: Michael Sikora

Overview of this book

Table of Contents (18 chapters)
EJB 3 Developer Guide
Credits
About the Author
About the Reviewers
Preface
Annotations and Their Corresponding Packages

Interceptor Classes


Interceptors can be defined in a separate class. The class will contain an @AroundInvoke annotated method as described in the previous section. The code below shows an interceptor class, Auditor, containing the audit() method. The audit() method updates the Audit entity with the class and method name of the intercepted business method, together with the current date and time. Of course a real world auditing application will store a lot more information, such as the identity of the user or process invoking the business method, as well as an indication as to whether the business method has executed successfully. The code for the Auditor interceptor class is shown below:

public class Auditor {
@PersistenceContext(unitName="BankService")
private EntityManager em;
@AroundInvoke
public Object audit(InvocationContext invctx)
throws Exception {
String className = invctx.getClass().getName();
String methodName = invctx.getMethod().getName();
Formatter fmt = new Formatter();
Calendar...