12 Replies Latest reply on Dec 7, 2008 7:16 PM by fiorenzino

    JBoss5GA: @EJB in jsf managed bean doesn't work???

    fiorenzino

      Hi,
      i want test jboss 5GA for EJB Injection in jsf application.
      JBoss 5GA default configuration.
      My application code:

      Ejb3JSFTest.ear
      -META-INF
      ---application.xml
      --------|+++++++(ejb-module)Ejb3JSFTest_ejb3
      -------|+++++++(web-module)Ejb3JSFTest.war
      --war
      ----WEB-INF
      ------web.xml
      ------faces-config.xml
      --jar
      --META-INF
      ----persistence.xml


      EJB session bean

      @Stateless
      @Local(TestManager.class)
      public class TestManagerBean implements TestManager {
       @PersistenceContext(unitName = "Manager")
       EntityManager em;
      
       public List<Test> getAllTest() {
       List result = null;
       try {
       result = em.createNamedQuery("Test.getAllTest").getResultList();
       } catch (Exception e) {
       e.printStackTrace();
       }
       return result;
       }
      }
      


      @Entity

      import java.io.Serializable;
      
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.NamedQueries;
      import javax.persistence.NamedQuery;
      
      @Entity
      @NamedQueries( {
       @NamedQuery(name = "Test.getAllTest", query = "SELECT p FROM Test p order by p.nome") })
      public class Test implements Serializable {
      
       private Long id;
       private String nome;
      
       @Id
       @GeneratedValue(strategy = GenerationType.IDENTITY)
       public Long getId() {
       return id;
       }
      
       public void setId(Long id) {
       this.id = id;
       }
      
       public String getNome() {
       return nome;
       }
      
       public void setNome(String nome) {
       this.nome = nome;
       }
      
      }
      


      ManagedBean
      package it.jsftest.web;
      
      import java.util.List;
      
      import it.jsftest.ejb3.TestManager;
      import it.jsftest.par.Test;
      
      import javax.ejb.EJB;
      import javax.faces.model.ArrayDataModel;
      import javax.faces.model.DataModel;
      
      public class TestHandler {
       @EJB(mappedName = "Ejb3JSFTest/TestManagerBean/local")
       TestManager testManager;
       private DataModel model;
      
       public DataModel getModel() {
       if (model == null)
       aggModel();
       return model;
       }
      
       public void aggModel() {
       try {
       System.out.println("update Model");
       model = new ArrayDataModel();
       List<Test> lista = testManager.getAllTest();
       model.setWrappedData(lista.toArray());
       } catch (Exception e) {
       e.printStackTrace();
       }
      
       }
      
      }
      


      index.xhtml
      ....
      <rich:dataTable width="300" id="TestList" rows="5"
       columnClasses="col" value="#{TestHandler.model}" var="test">
       <f:facet name="header">
       <rich:columnGroup>
       <h:column>
       <h:outputText styleClass="headerText" value="id" />
       </h:column>
       <h:column>
       <h:outputText styleClass="headerText" value="Nome" />
       </h:column>
       </rich:columnGroup>
       </f:facet>
       <h:column>
       <h:outputText value="#{test.id}" />
       </h:column>
       <h:column>
       <h:outputText value="#{test.nome}" />
       </h:column>
       <f:facet name="footer">
       <rich:datascroller id="scollerTestList" for="TestList"
       maxPages="10" />
       </f:facet>
       </rich:dataTable>
      


      I'm sad...this solution doesn't work!
      What Mistakes?
      With eclipse-debug i see the null value for TestManager.

      good night
      Fiorenzo

      PS :
      also if i use ejb-local-ref to web.xml - doesn't work!!


      LOG:

      at deploytime:
       Created KernelDeployment for: Ejb3JSFTest_ejb3.jar
      01:30:51,542 INFO [JBossASKernel] installing bean: jboss.j2ee:ear=Ejb3JSFTest.ear,jar=Ejb3JSFTest_ejb3.jar,name=TestManagerBean,service=EJB3
      01:30:51,542 INFO [JBossASKernel] with dependencies:
      01:30:51,546 INFO [JBossASKernel] and demands:
      01:30:51,546 INFO [JBossASKernel] jboss.ejb:service=EJBTimerService
      01:30:51,546 INFO [JBossASKernel] persistence.unit:unitName=Ejb3JSFTest.ear/Ejb3JSFTest_ejb3.jar#SomaroManager
      01:30:51,546 INFO [JBossASKernel] and supplies:
      01:30:51,546 INFO [JBossASKernel] jndi:Ejb3JSFTest/TestManagerBean/local
      01:30:51,547 INFO [JBossASKernel] jndi:Ejb3JSFTest/TestManagerBean/local-it.jsftest.ejb3.TestManager
      01:30:51,547 INFO [JBossASKernel] Class:it.jsftest.ejb3.TestManager
      01:30:51,547 INFO [JBossASKernel] jndi:Ejb3JSFTest/TestManagerBean/remote
      01:30:51,547 INFO [JBossASKernel] Added bean(jboss.j2ee:ear=Ejb3JSFTest.ear,jar=Ejb3JSFTest_ejb3.jar,name=TestManagerBean,service=EJB3) to KernelDeployment of: Ejb3JSFTest_ejb3.jar
      
      ....
      ...
      
      01:30:51,811 INFO [EJBContainer] STARTED EJB: it.jsftest.ejb3.TestManagerBean ejbName: TestManagerBean
      01:30:51,833 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
      
       Ejb3JSFTest/TestManagerBean/local - EJB3.x Default Local Business Interface
       Ejb3JSFTest/TestManagerBean/local-it.jsftest.ejb3.TestManager - EJB3.x Local Business Interface
      


      runtime error:

      01:31:43,867 ERROR [STDERR] java.lang.NullPointerException
      01:31:43,868 ERROR [STDERR] at it.jsftest.web.TestHandler.aggModel(TestHandler.java:27)
      


        • 1. Re: JBoss5GA: @EJB in jsf managed bean doesn't work???
          jaikiran

           

          @EJB(mappedName = "Ejb3JSFTest/TestManagerBean/local")
          TestManager testManager;


          Does it work without the mappedName?

          @EJB
          TestManager testManager;


          And please post the entire console logs when you application is being deployed and also the entire exception stacktrace.


          • 2. Re: JBoss5GA: @EJB in jsf managed bean doesn't work???
            emuckenhuber

             

            "fiorenzino" wrote:
            Hi,
            i want test jboss 5GA for EJB Injection in jsf application.
            JBoss 5GA default configuration.
            My application code:

            Ejb3JSFTest.ear
            -META-INF
            ---application.xml
            --------|+++++++(ejb-module)Ejb3JSFTest_ejb3
            -------|+++++++(web-module)Ejb3JSFTest.war
            --war
            ----WEB-INF
            ------web.xml
            ------faces-config.xml
            --jar
            --META-INF
            ----persistence.xml



            Where exactly is you managed bean packaged in you ear?
            If it's not packaged within your .war you might want to try to put this in your web.xml:

             <context-param>
             <param-name>com.sun.faces.injectionProvider</param-name>
             <param-value>org.jboss.web.jsf.integration.injection.JBossScanningInjectionProvider</param-value>
             </context-param>
            


            • 3. Re: JBoss5GA: @EJB in jsf managed bean doesn't work???
              fiorenzino

              OK!!!

              with this option in web.xml it work!!!

              <context-param>
               <param-name>com.sun.faces.injectionProvider</param-name>
               <param-value>org.jboss.web.jsf.integration.injection.JBossScanningInjectionProvider</param-value>
               </context-param>
              


              I summarize my test:

              1) my managed beans classes are in Ejb3JSFTest .ear/Ejb3JSFTest.war/WEB-INF/classes
              my ejb3 in test.ear/Ejb3JSFTest_ejb3.jar/it/.....

              2) for inject ejb3 in the managed bean it must use mappedname if you want inject a local interface for ejb3:
               @EJB(mappedName="Ejb3JSFTest/TestManagerBean/local")
               TestManager testManager;
              

              3) if you want use @EJB without mappedname the container inject Remote interface for your ejb3

               @EJB
               TestManagerRemote testManager;
              

              4) for use facelets in web application use jsf-facelets-1.1.15.B1.jar

              I am happy like a child who has waited his game for a long time!!

              Fiorenzo

              w jbozz


              • 4. JBoss5GA: @EJB in jsf managed bean: ok!!
                fiorenzino

                My complete eclipse project with libraries:

                ejb3_jsf_facelets_eclipse_project.zip (7,7 MB)

                http://liverockmedia.com/jboss5/ejb3jsf_eclipse_project.zip

                bye bye

                Fiorenzo

                • 5. Re: JBoss5GA: @EJB in jsf managed bean doesn't work???
                  alrubinger

                   

                  "fiorenzino" wrote:
                  2) for inject ejb3 in the managed bean it must use mappedname if you want inject a local interface for ejb3:


                  I'm assuming that "TestManager" is not unique within your deployment. You can get around the non-portable "mappedName" usage by ensuring a deterministic @EJB.beanInterface (which is also read from the type you're injecting into). So the form:

                  @EJB
                  TestManagerLocalBusiness testManager


                  ...should work, assuming only one EJB has a business interface of "TestManagerLocalBusiness". If you have more than one implementation of this interface, add the EJB name to form a unique identity:


                  @EJB(beanName="NameOfTargetEJB")
                  TestManagerLocalBusiness testManager


                  S,
                  ALR

                  • 6. Re: JBoss5GA: @EJB in jsf managed bean doesn't work???
                    fiorenzino

                    Hi Andrew,

                    TestManager is unique within my deployment.
                    If TestManager is declared @Local, @EJB injection produces this error:

                    17:09:41,166 ERROR [viewhandler] Error Rendering View[/test/index.xhtml]
                    com.sun.faces.mgbean.ManagedBeanCreationException: An error occurred performing resource injection on managed bean TestHandler
                     at com.sun.faces.mgbean.BeanBuilder.injectResources(BeanBuilder.java:209)
                     at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:107)
                     at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:368)
                     at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:222)
                     at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:86)
                     at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
                     at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
                     at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:61)
                     at org.apache.el.parser.AstValue.getValue(AstValue.java:107)
                     at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
                     at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
                     at javax.faces.component.UIData.getValue(UIData.java:609)
                     at org.ajax4jsf.component.UIDataAdaptor.getValue(UIDataAdaptor.java:1640)
                     at org.ajax4jsf.component.SequenceDataAdaptor.getDataModel(SequenceDataAdaptor.java:48)
                     at org.ajax4jsf.component.SequenceDataAdaptor.createDataModel(SequenceDataAdaptor.java:42)
                     at org.richfaces.component.UIDataTable.createDataModel(UIDataTable.java:122)
                     at org.ajax4jsf.component.UIDataAdaptor.getExtendedDataModel(UIDataAdaptor.java:621)
                     at org.ajax4jsf.component.UIDataAdaptor.setRowKey(UIDataAdaptor.java:339)
                     at org.richfaces.renderkit.AbstractTableRenderer.encodeTableStructure(AbstractTableRenderer.java:121)
                     at org.richfaces.renderkit.html.DataTableRenderer.doEncodeBegin(DataTableRenderer.java:206)
                     at org.richfaces.renderkit.html.DataTableRenderer.doEncodeBegin(DataTableRenderer.java:194)
                     at org.ajax4jsf.renderkit.RendererBase.encodeBegin(RendererBase.java:101)
                     at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:813)
                     at javax.faces.component.UIData.encodeBegin(UIData.java:962)
                     at org.ajax4jsf.component.UIDataAdaptor.encodeBegin(UIDataAdaptor.java:1220)
                     at javax.faces.component.UIComponent.encodeAll(UIComponent.java:928)
                     at javax.faces.render.Renderer.encodeChildren(Renderer.java:148)
                     at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
                     at org.ajax4jsf.renderkit.RendererBase.renderChild(RendererBase.java:282)
                     at org.ajax4jsf.renderkit.RendererBase.renderChildren(RendererBase.java:262)
                     at org.richfaces.renderkit.html.PanelRenderer.doEncodeChildren(PanelRenderer.java:220)
                     at org.richfaces.renderkit.html.PanelRenderer.doEncodeChildren(PanelRenderer.java:215)
                     at org.ajax4jsf.renderkit.RendererBase.encodeChildren(RendererBase.java:121)
                     at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
                     at javax.faces.component.UIComponent.encodeAll(UIComponent.java:930)
                     at javax.faces.component.UIComponent.encodeAll(UIComponent.java:933)
                     at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:592)
                     at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:108)
                     at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:196)
                     at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:110)
                     at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
                     at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
                     at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                     at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:177)
                     at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:267)
                     at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:380)
                    
                    
                    
                     at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:507)
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                     at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:638)
                     at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:444)
                     at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:382)
                     at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:310)
                     at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:696)
                     at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:667)
                     at org.apache.jsp.index_jsp._jspService(index_jsp.java:54)
                     at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
                     at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
                     at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
                     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
                     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
                     at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                     at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
                     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
                     at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
                     at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
                     at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
                     at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
                     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
                     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                     at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
                     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
                     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
                     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
                     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                     at java.lang.Thread.run(Thread.java:619)
                    Caused by: com.sun.faces.spi.InjectionProviderException: Injection failed on managed bean.
                     at org.jboss.web.jsf.integration.injection.JBossScanningInjectionProvider.inject(JBossScanningInjectionProvider.java:153)
                     at com.sun.faces.mgbean.BeanBuilder.injectResources(BeanBuilder.java:203)
                     ... 83 more
                    Caused by: java.lang.RuntimeException: Unable to inject jndi dependency: env/it.jsftest.web.TestHandler/testManager into property it.jsftest.web.TestHandler.testManager: remote not bound
                     at org.jboss.injection.JndiPropertyInjector.lookup(JndiPropertyInjector.java:82)
                     at org.jboss.injection.JndiPropertyInjector.inject(JndiPropertyInjector.java:99)
                     at org.jboss.web.tomcat.service.TomcatInjectionContainer.processInjectors(TomcatInjectionContainer.java:360)
                     at org.jboss.web.jsf.integration.injection.JBossScanningInjectionProvider.inject(JBossScanningInjectionProvider.java:148)
                     ... 84 more
                    Caused by: javax.naming.NamingException: Could not dereference object [Root exception is javax.naming.NameNotFoundException: remote not bound]
                     at org.jnp.interfaces.NamingContext.resolveLink(NamingContext.java:1339)
                     at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:804)
                     at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:673)
                     at org.jboss.ejb3.JndiUtil.lookup(JndiUtil.java:44)
                     at org.jboss.injection.JndiPropertyInjector.lookup(JndiPropertyInjector.java:75)
                     ... 87 more
                    Caused by: javax.naming.NameNotFoundException: remote not bound
                     at org.jnp.server.NamingServer.getBinding(NamingServer.java:771)
                     at org.jnp.server.NamingServer.getBinding(NamingServer.java:779)
                     at org.jnp.server.NamingServer.getObject(NamingServer.java:785)
                     at org.jnp.server.NamingServer.lookup(NamingServer.java:443)
                     at org.jnp.server.NamingServer.lookup(NamingServer.java:399)
                     at org.jnp.server.NamingServer.lookup(NamingServer.java:399)
                     at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:713)
                     at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:673)
                     at javax.naming.InitialContext.lookup(InitialContext.java:392)
                     at org.jnp.interfaces.NamingContext.resolveLink(NamingContext.java:1333)
                     ... 91 more
                    
                    


                    If i use TestManager in @Remote annotation
                    @Stateless
                    @Remote(TestManager.class)
                    public class TestManagerBean implements TestManager {
                    ...
                    }
                    

                    this work correctly:
                    @EJB
                    TestManager testManager;
                    


                    It's a little bug?

                    Fiorenzo

                    • 7. Re: JBoss5GA: @EJB in jsf managed bean doesn't work???
                      alrubinger

                      Here's the concerning point:

                      org.jboss.web.jsf.integration.injection.JBossScanningInjectionProvider.inject(JBossScanningInjectionProvider.java:148)
                       | ... 84 more
                       | Caused by: javax.naming.NamingException: Could not dereference object [Root exception is javax.naming.NameNotFoundException: remote not bound]


                      Meaning we've resolved to the wrong target, "[beanName]/remote", for a business local interface.

                      If I were to take a quick educated guess, it's that WebEJBRemoteHandler.ejbRefXml() is always consulted for @EJB refs from WebEJBHandler. The following code is not taking into account the interface name in JNDI resolution?

                      108 String mappedName = ref.getMappedName();
                      109 if (mappedName != null && mappedName.equals(""))
                      110 mappedName = null;
                      111 if(mappedName == null && ref.getResolvedJndiName() != null)
                      112 mappedName = ref.getResolvedJndiName();


                      Emanuel should be able to either confirm this or offer a better explanation; I'm not familiar w/ the Tomcat integration's JNDI resolution layer.

                      S,
                      ALR

                      • 8. Re: JBoss5GA: @EJB in jsf managed bean doesn't work???
                        emuckenhuber

                        Hmm wait a sec - if you have your ManagedBean in the .war then it should work without specifying a different injection provider.
                        My suggestion was just if you don't have your managed bean in the .war :)

                        So you should go with that what jaikiran suggested.
                        If you want to use the mappedName you should define a mappedName on your stateless bean and use that one.

                        Furthermore i will add some tests for this different injection provider and try to reproduce that problem.

                        • 9. Re: JBoss5GA: @EJB in jsf managed bean doesn't work???
                          emuckenhuber

                          Fiorenzo,

                          btw. looking at your demo app - i now know why your injection did not work.

                          This is because you used a J2EE 1.4 web.xml descriptor.
                          You need to use a JEE 5 descriptor:

                          <web-app version="2.5"
                           xmlns="http://java.sun.com/xml/ns/javaee"
                           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
                          


                          Otherwise annotations are going to be ignored :)

                          • 10. Re: JBoss5GA: @EJB in jsf managed bean doesn't work???
                            fiorenzino

                            Hi Emanuel,

                            you are the best!!

                            With correct JEE 5 descriptor the injection work in all situation, without mapped name..
                            Local and remote...

                            Thanks a lot for 2 information:
                            1) the choice of correct declaration for web.xml
                            2) optional context-param for injection Provider

                            Fiorenzo

                            PS1: i updated my example of application with your corrections...i hope to be util for someone.
                            PS2: also JMX Singleton injection works fine...

                            • 11. Re: JBoss5GA: @EJB in jsf managed bean doesn't work???
                              ssilvert

                              BTW, this is covered in the wiki here: http://www.jboss.org/community/docs/DOC-10837

                              For all "JSF on JBoss" wiki info, you can go to the easy-to-remember http://jsf.jboss.org


                              Stan

                              • 12. Re: JBoss5GA: @EJB in jsf managed bean doesn't work???
                                fiorenzino

                                thanks Stan and apologize for my post!

                                Fiorenzo