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()
.
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:
Create an interceptor class.
This can be any POJO class.
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.
Designate the method as the interceptor method (see "Using Annotations").
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".
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(); ... } }