Skip Headers
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g Release 3 (10.1.3)
B14428-01
  Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
Next
Next
 

Configuring an Interceptor on an EJB 3.0 Session Bean

You can designate one non-business method as the interceptor method for a stateless or stateful session bean (see "Using Annotations"). The method must have a signature of:

public Object <MethodName>(InvocationContext) throws Exception

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

Using Annotations

Example 5-2 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-2 @AroundInvoke in an EJB 3.0 Session Bean

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

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