4 Replies Latest reply on Sep 9, 2010 6:57 AM by Jonas Winkler

    Interception and @EJB injection.

    Jonas Winkler Newbie

      Hello,


      I just started to use interception in my project. I got an EJB ('UserBean'), a cdi managed bean ('UserViewBean', annotated with @Named) and some JSF pages that use the UserViewBean. The UserViewBean uses a @EJB injection to get a reference for a UserBean instance. So this is a pretty generic setup.


      My first step was to implement an interceptor. This is what it looks like:


      @Inherited
      @InterceptorBinding
      @Target({ElementType.METHOD, ElementType.TYPE})
      @Retention(RetentionPolicy.RUNTIME)
      public @interface Test {}
      



      @Test
      @Interceptor
      public class TestInterceptor implements Serializable {
      
          @AroundInvoke
          public Object list(InvocationContext ctx) throws Exception {
              System.out.println("Start method " + ctx.getMethod());
              Object result = ctx.proceed();
              System.out.println("End method " + ctx.getMethod());
              return result;
          }
      
      }
      



      Annotating methods in UserBean (the EJB) works as expected, method calls are written on the console. As I read in the Weld documentation, those interceptors are supposed to work in any managed bean. As I annotated some methods the UserViewBean (the cdi managed bean), things got strange. Interception still works, but any call to the UserBean from within the UserViewBean results in a NullPointerException. Calling a method that uses the UserBean will still cause Start method ... to be written on the console, but not the End method ... one.


      I'm using glassfish 3.0.1 to run this project. Any help is appreciated.

        • 1. Re: Interception and @EJB injection.
          Marius Bogoevici Newbie

          Hi Jonas,


          This looks like a bug, but I can't say at this point whether it's the Glassfish integration of Weld or Weld proper that's at fault.


          Would you mind attaching the NPE stacktrace as well?


          Thanks,
          Marius

          • 2. Re: Interception and @EJB injection.
            Jonas Winkler Newbie

            Here it is:


            SEVERE: Error Rendering View[/admin/list_users.xhtml]
            javax.el.ELException: /admin/list_users.xhtml @27,76 value="#{UserViewBean.allUsers}": java.lang.NullPointerException
                    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:107)
                    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190)
                    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:178)
                    at javax.faces.component.UIData.getValue(UIData.java:554)
                    at javax.faces.component.UIData.getDataModel(UIData.java:1248)
                    at javax.faces.component.UIData.setRowIndex(UIData.java:447)
                    at org.primefaces.component.datatable.DataTableRenderer.encodeTable(DataTableRenderer.java:516)
                    at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:441)
                    at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:239)
                    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:878)
                    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1620)
                    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
                    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:848)
                    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
                    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
                    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
                    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:380)
                    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
                    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:273)
                    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
                    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
                    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
                    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
                    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
                    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
                    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
                    at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:341)
                    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
                    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
                    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
                    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
                    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
                    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
                    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
                    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
                    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
                    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
                    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
                    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
                    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
                    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
                    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
                    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
                    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
                    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
                    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
                    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
                    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
                    at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
                    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
                    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
                    at java.lang.Thread.run(Thread.java:619)
            Caused by: java.lang.NullPointerException
                    at gpm.jsf.bean.admin.UserViewBean.getAllUsers(UserViewBean.java:105)
                    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                    at java.lang.reflect.Method.invoke(Method.java:597)
                    at org.jboss.interceptor.proxy.InterceptionChain.invokeNext(InterceptionChain.java:77)
                    at org.jboss.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:113)
                    at org.jboss.interceptor.proxy.InterceptorMethodHandler.doInvoke(InterceptorMethodHandler.java:76)
                    at org.jboss.interceptor.util.proxy.TargetInstanceProxyMethodHandler.invoke(TargetInstanceProxyMethodHandler.java:60)
                    at org.jboss.weld.util.CleanableMethodHandler.invoke(CleanableMethodHandler.java:43)
                    at gpm.jsf.bean.admin.UserViewBean_$$_javassist_82.getAllUsers(UserViewBean_$$_javassist_82.java)
                    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                    at java.lang.reflect.Method.invoke(Method.java:597)
                    at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:304)
                    at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:54)
                    at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:163)
                    at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:298)
                    at org.jboss.weld.bean.proxy.ClientProxyMethodHandler.invoke(ClientProxyMethodHandler.java:113)
                    at org.jboss.weld.util.CleanableMethodHandler.invoke(CleanableMethodHandler.java:43)
                    at gpm.jsf.bean.admin.UserViewBean_$$_javassist_81.getAllUsers(UserViewBean_$$_javassist_81.java)
                    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                    at java.lang.reflect.Method.invoke(Method.java:597)
                    at javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
                    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)
                    at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
                    at com.sun.el.parser.AstValue.getValue(AstValue.java:116)
                    at com.sun.el.parser.AstValue.getValue(AstValue.java:163)
                    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
                    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:71)
                    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:102)
                    ... 51 more
            
            WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
            java.lang.NullPointerException
                    at gpm.jsf.bean.admin.UserViewBean.getAllUsers(UserViewBean.java:105)
                    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                    at java.lang.reflect.Method.invoke(Method.java:597)
                    at org.jboss.interceptor.proxy.InterceptionChain.invokeNext(InterceptionChain.java:77)
                    at org.jboss.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:113)
                    at org.jboss.interceptor.proxy.InterceptorMethodHandler.doInvoke(InterceptorMethodHandler.java:76)
                    at org.jboss.interceptor.util.proxy.TargetInstanceProxyMethodHandler.invoke(TargetInstanceProxyMethodHandler.java:60)
                    at org.jboss.weld.util.CleanableMethodHandler.invoke(CleanableMethodHandler.java:43)
                    at gpm.jsf.bean.admin.UserViewBean_$$_javassist_82.getAllUsers(UserViewBean_$$_javassist_82.java)
                    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                    at java.lang.reflect.Method.invoke(Method.java:597)
                    at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:304)
                    at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:54)
                    at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:163)
                    at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:298)
                    at org.jboss.weld.bean.proxy.ClientProxyMethodHandler.invoke(ClientProxyMethodHandler.java:113)
                    at org.jboss.weld.util.CleanableMethodHandler.invoke(CleanableMethodHandler.java:43)
                    at gpm.jsf.bean.admin.UserViewBean_$$_javassist_81.getAllUsers(UserViewBean_$$_javassist_81.java)
                    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                    at java.lang.reflect.Method.invoke(Method.java:597)
                    at javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
                    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)
                    at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
                    at com.sun.el.parser.AstValue.getValue(AstValue.java:116)
                    at com.sun.el.parser.AstValue.getValue(AstValue.java:163)
                    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
                    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:71)
                    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:102)
                    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190)
                    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:178)
                    at javax.faces.component.UIData.getValue(UIData.java:554)
                    at javax.faces.component.UIData.getDataModel(UIData.java:1248)
                    at javax.faces.component.UIData.setRowIndex(UIData.java:447)
                    at org.primefaces.component.datatable.DataTableRenderer.encodeTable(DataTableRenderer.java:516)
                    at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:441)
                    at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:239)
                    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:878)
                    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1620)
                    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
                    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:848)
                    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
                    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
                    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
                    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:380)
                    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
                    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:273)
                    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
                    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
                    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
                    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
                    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
                    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
                    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
                    at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:341)
                    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
                    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
                    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
                    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
                    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
                    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
                    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
                    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
                    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
                    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
                    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
                    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
                    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
                    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
                    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
                    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
                    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
                    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
                    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
                    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
                    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
                    at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
                    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
                    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
                    at java.lang.Thread.run(Thread.java:619)
            



            And line 105 in my UserViewBean looks like this:


            return userBean.findAll();
            



            where userBean is a EJB and properly annotated with @EJB.


            Furthermore, I added a @PostConstruct lifecycle method to print the ejb's reference to console:


                @PostConstruct
                public void init() {
                    System.out.println(userBean);
                }
            



            This prints null, if either the managed bean or any method in it is annotated with @Test.

            • 3. Re: Interception and @EJB injection.
              Andy Gibson Novice

              Just out of curiosity, is there a particular reason for using the @EJB injection method rather than @Inject? (i.e. it's a remote bean)


              Cheers,


              Andy Gibson

              • 4. Re: Interception and @EJB injection.
                Jonas Winkler Newbie

                Well, there is no reason. Using @Inject resolved my problem, thanks. I was used to use @EJB cause its the way how it worked in Java EE 5.


                By the way, I was able to reduces this problem to a bare minimun. Just created a new project, EJB, managed bean, interceptor and a jsf page (as described above) and got the exact same error.


                Cheers,
                Jonas Winkler