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 Session Bean

You can specify one nonbusiness method as the interceptor method for a stateless or stateful session bean. Each time a client invokes a session bean business method, OC4J intercepts the invocation and invokes the interceptor method. The client 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 session bean or on the EJB 3.0 session bean class itself (see "Configuring an Around Invoke Interceptor Method on an EJB 3.0 Session Bean").

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 session bean (see "Configuring an Interceptor Class for an EJB 3.0 Session Bean").

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

Using Annotations

Example 5-6 shows how to specify interceptor class method myInterceptor as the interceptor method of an EJB 3.0 session bean using the @AroundInvoke annotation. After you associate this interceptor class with a session bean ("Configuring an Interceptor Class for an EJB 3.0 Session Bean"), each time you invoke a session bean business method, OC4J intercepts the invocation and invokes the myInterceptor method. The client invocation proceeds only if this method returns InvocationContext.proceed().

Example 5-6 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();
        ...
    }
}