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 Interceptor Class for an EJB 3.0 Session Bean

An interceptor class is a class, distinct from the bean class itself, whose methods are invoked in response to business method invocations and life cycle events on the bean. You can associate a bean class can with any number of interceptor classes.

You can associate an interceptor class with an EJB 3.0 stateless or stateful session bean.

To configure an EJB 3.0 session bean with an interceptor class, you must do the following:

  1. Create an interceptor class (see "Creating an Interceptor Class").

    This can be any POJO class.

  2. Implement interceptor methods in the interceptor class.

    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.

    You can annotate an interceptor method as a life cycle callback (see "Configuring a Life Cycle Callback Interceptor Method on an Interceptor Class of an EJB 3.0 Session Bean") or as an AroundInvoke method (see "Configuring an Around Invoke Interceptor Method on an Interceptor Class of an EJB 3.0 Session Bean").

  3. Associate the interceptor class with your EJB 3.0 session bean (see "Associating an Interceptor Class With a Session Bean").

  4. Optionally configure the session bean to use singleton interceptors (see "Specifying Singleton Interceptors in a Session Bean").

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

Using Annotations

This section describes the following:

Creating an Interceptor Class

Example 5-7 shows how to specify an AroundInvoke interceptor method and a life cycle callback interceptor method in an interceptor class for an EJB 3.0 session bean. After you associate this interceptor class with a session bean (see Example 5-8), each time you invoke a session bean business method, OC4J invokes the AroundInvoke method myInterceptor. When the appropriate life cycle event occurs, OC4J invokes the corresponding life cycle callback interceptor method such as myPreDestroyMethod.

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

Associating an Interceptor Class With a Session Bean

You can associate an interceptor class with an EJB 3.0 session bean using the @Interceptors annotation. Example 5-8 shows how to associate the interceptor class from Example 5-7 with an EJB 3.0 session bean class.

Note that the life cycle method for @PostConstruct is a method of the EJB 3.0 session bean class itself (for more information, see "Configuring a Life Cycle Callback Interceptor Method on an EJB 3.0 Session Bean"), while the life cycle method for @PreDestroy is a life cycle callback interceptor method on the interceptor class associated with this session bean (see "Configuring a Life Cycle Callback Interceptor Method on an Interceptor Class of an EJB 3.0 Session Bean").

Example 5-8 Associating an Interceptor Class With an EJB 3.0 Session Bean

@Stateful
@Interceptors(MyInterceptor.class)
public class CartBean implements Cart {
    private ArrayList items;

    @PostConstruct
    public void initialize() {
        items = new ArrayList();
    }
    ...
}

Specifying Singleton Interceptors in a Session Bean

As Example 5-9 shows, you can configure OC4J to use singleton interceptor classes by setting the @StatelessDeployment or @StatefulDeployment attribute interceptorType to singleton. All instances of this session bean will share the same instance of MyInterceptor. The MyInterceptor class must be stateless.

For more information about this attribute, see Table A-1. For more information on singleton interceptors, see "Singleton Interceptors".

Example 5-9 Specifying a Singleton Interceptor Class with an EJB 3.0 Stateful Session Bean

@Stateful
@StatefulDeployment(interceptorType="singleton")
@Interceptors(MyInterceptor.class)
public class CartBean implements Cart {
    private ArrayList items;

    @PostConstruct
    public void initialize() {
        items = new ArrayList();
    }
    ...
}