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 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().

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 specify this method on the EJB 3.0 session bean class (see "Using Annotations") or on an interceptor class that you associate with an EJB 3.0 session bean (see "Configuring an Around Invoke Interceptor Method on an Interceptor Class of an EJB 3.0 Session Bean").

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

Using Annotations

Example 5-5 shows how to designate a method of a session bean class as an interceptor method using the @AroundInvoke annotation. Each time a client invokes a business method of this stateless session bean, OC4J intercepts the invocation and invokes the interceptor method myInterceptor. The client invocation proceeds only if the interceptor method returns InvocationContext.proceed().

Example 5-5 @AroundInvoke in an EJB 3.0 Session Bean

@Stateless
public class HelloWorldBean implements HelloWorld  {
    public void sayHello()  {
        System.out.println("Hello!");
    }

    @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();
    }
}