Generic interceptor with dependancy injection
rsw Jul 26, 2005 12:50 PMI am trying to create an interceptor class that uses a @EJB tag to inject a site specific implementation of an interface. This is a common use case for our product where each site has specific code that is executed based on a trigger (method invocation). I am simply modifing the EJBTrail example trying to make this work. The following code compiles cleanly, deploys cleanly, yet throw an execption on execution.
/*** Interceptor base class *****/ package trail.interceptor; import javax.annotation.EJB; import javax.ejb.AroundInvoke; import javax.ejb.InvocationContext; public class BasicIntercept { // Inject EJB @EJB CalcTwoNumbers calc2; @AroundInvoke public Object trigger (InvocationContext ctx) throws Exception { return(calc2.doIt(ctx)); } } /**** implementation interface ********/ package trail.interceptor; import javax.ejb.InvocationContext; public interface CalcTwoNumbers { public Object doIt (InvocationContext ctx) throws Exception; } /***** simple example implementation ****/ package trail.interceptor; import javax.annotation.EJB; import javax.ejb.*; @Stateless public class CalcTwoNumbersMulti implements CalcTwoNumbers { public Object doIt (InvocationContext ctx) throws Exception { int x = 3; int y = 2; CalculatorBean cal = (CalculatorBean) ctx.getBean(); String className = ctx.getBean().getClass().getName(); String methodName = ctx.getMethod().getName(); cal.setTrace("Intercepted call to "+ className + "." + methodName + "!\nCalcTwoNumber x=" + x + " y=" + y + " result = " + (x * y) ); try { return ctx.proceed(); } catch(Exception e) { throw e; } finally { System.out.println("CalcTwoNumber x=" + x + " y=" + y + " result = " + (x * y) ); } } } /*** setup interceptor ***/ package trail.interceptor; import javax.ejb.*; import java.util.*; import java.io.Serializable; //TODO: Fix the client @Stateful @Interceptor(BasicIntercept.class) public class CalculatorBean implements Calculator, Serializable { ......
According to the examples this should work and I cannot find anything in the EJB3.0 spec that forbids this type of design. But here is the exception:
javax.ejb.EJBException: null; CausedByException is: null org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:46) org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:70) org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134) org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88) org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:72) org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88) org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63) org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88) org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:93) org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88) org.jboss.ejb3.stateful.StatefulContainer.localInvoke(StatefulContainer.java:122) org.jboss.ejb3.stateful.StatefulLocalProxy.invoke(StatefulLocalProxy.java:86) $Proxy87.setTrace(Unknown Source) org.apache.jsp.services.interceptor.calculator_jsp._jspService(org.apache.jsp.services.interceptor.calculator_jsp:80) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
Any help would be appreciated.
Randy