Skip Headers
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g (10.1.3.5.0)

Part Number E13981-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

Configuring an Around Invoke Interceptor Method on an Interceptor Class of an EJB 3.0 MDB

You can specify one nonbusiness method as the interceptor method for an EJB 3.0 message-driven bean. Each time the onMessage method is invoked, OC4J intercepts the invocation and invokes the interceptor method. The onMessage invocation proceeds only if the interceptor method returns InvocationContext.proceed().

You can specify this method on an interceptor class that you associate with an EJB 3.0 MDB or on the EJB 3.0 MDB class itself (see "Configuring an Around Invoke Interceptor Method on an EJB 3.0 MDB").

To configure an interceptor method on an interceptor class, you must do the following:

  1. Create an interceptor class.

    This can be any POJO class.

  2. Implement the interceptor method.

    An interceptor method has the following signature:

    Object <METHOD>(InvocationContext) throws Exception
    

    An interceptor method may have public, private, protected, or package level access, but must not be declared as final or static.

  3. Designate the method as the interceptor method (see "Using Annotations").

  4. Associate the interceptor class with your EJB 3.0 MDB (see "Configuring an Interceptor Class for an EJB 3.0 MDB").

For more information, see "Understanding EJB 3.0 Interceptors".

Using Annotations

Example 10-12 shows how to specify interceptor class method myInterceptor as the interceptor method of an EJB 3.0 MDB using the @AroundInvoke annotation. After you associate this interceptor class with an MDB ("Configuring an Interceptor Class for an EJB 3.0 MDB"), each time the onMessage method is invoked, OC4J intercepts the invocation and invokes the interceptor method myInterceptor. The onMessage method invocation proceeds only if the interceptor method returns InvocationContext.proceed().

Example 10-12 Interceptor Class

public class MyInterceptor {
    ...
    @AroundInvoke
    protected Object myInterceptor(InvocationContext ctx) throws Exception {
        Principal p = ctx.getEJBContext().getCallerPrincipal;
        if (!userIsValid(p)) {
            throw new SecurityException(
                "Caller: '" + p.getName() +
                "' does not have permissions for method " + ctx.getMethod()
            );
        }
        return ctx.proceed();
    }

    @PreDestroy
    public void myPreDestroyMethod (InvocationContext ctx) {
        ...
        ctx.proceed();
        ...
    }
}