Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g (10.1.3.1.0) Part Number B28221-02 |
|
|
View PDF |
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".
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(); } }