9 Replies Latest reply on Apr 20, 2011 7:04 AM by Michael Feichtegger

    Injection in managed bean fails in JBoss AS 6 Final

    Johan Andersson Newbie

      Running JBoss AS 6 Final.

       

      The following pieces of code is used to reset the session after a timeout.

       

      public class GUIResetPhaseListener implements PhaseListener {

                private static final String SESSION_BEAN = "#{sessionBean}";

       

           public void beforePhase(PhaseEvent phaseEvent) {

                SessionBean sessionBean = (SessionBean) ElUtil.findJSFBean(SESSION_BEAN, SessionBean.class);

                try {

                     if (sessionBean != null) {

                          sessionBean.reset();

                     }

                } catch (Exception e) {}

           }

       

      The injection failure occurs in sessionBean.reset(), called from beforePhase() above:

       

      public class SessionBean {

       

          @EJB(mappedName = "app/SessionManagerBean/remote")

          private SessionManagerRemote sm;

          .

          public void reset() throws Exception {

              //sm is null at runtime, causing an NPE.

              sm.clearSession();

      }

       

      This code is used to find SessionBean in  GUIResetPhaseListener.beforePhase()

       

      public class ElUtil {

           public static Object findJSFBean(String beanConfigName, Class<?> clazz) {

                FacesContext facesContext = FacesContext.getCurrentInstance();

                ValueExpression valueExpression = facesContext.getApplication().getExpressionFactory().createValueExpression(facesContext.getELContext(),

                      beanConfigName, clazz);

                return valueExpression.getValue(facesContext.getELContext());

          }

      }

       

      The app is configured to use Mojarra JSF 1.2 that comes with JBoss 6. It's deployed as an .ear file, containing a .war file and some .jar files.

       

      SessionBean.java, GUIResetPhaseListener.java and ElUtil.java are contained in one .jar file inside the .ear. The injected @Stateless SessionManagerBean in SessionBean.java is contained in another .jar file.

       

      This ran without problems on JBoss 4.2.3.

       

      NOTE: Using javax.naming.InitialContext instead of injection finds the bean.

        • 2. Injection in managed bean fails in JBoss AS 6 Final
          Stan Silvert Master

          I don't understand what you are trying to do.  You mean you want something to happen during a timeout of the HttpSession?  You wouldn't want to do that in a JSF PhaseListener.  You would instead use an HttpSessionBindingListener.

           

          Stan

          • 3. Re: Injection in managed bean fails in JBoss AS 6 Final
            Johan Andersson Newbie

            Thanks, I'll make sure to refactor that part.

             

            However, I get an NPE in the same bean down the stream as well.

             

            Short example from our source:

             

            A page to navigate between "applications".

             

            <h:commandLink action="#{sessionBean.navigateApplication}" >

             

            JSF backing bean with an injected stateless bean.

             

            public class SessionBean {

                @EJB(name = "app/SessionManagerBean/remote")

                private SessionManager sm;

             

                 public String navigateApplication() {

                    try {

                        //sm is null at runtime.

                        sm.checkPrivilege(null, null);

                    } catch (Exception x) {

                        return FAILURE;

                      }

                 }

                 //navigation code left out to keep it brief

            }

             

            I've checked the JMX console and JBoss startup logs. The bean is added to the deployment:

             

            2011-01-19 16:03:52,601 INFO  [org.jboss.ejb3.deployers.JBossASKernel]           persistence.unit:unitName=app.ear/core-ejb.jar#app-ds; Required: Described

            2011-01-19 16:03:52,606 INFO  [org.jboss.ejb3.deployers.JBossASKernel]           jboss.ejb:service=EJBTimerService; Required: Described

            2011-01-19 16:03:52,613 INFO  [org.jboss.ejb3.deployers.JBossASKernel]           jboss-switchboard:appName=app,module=core-ejb,name=SessionManagerBean; Required: Create

            2011-01-19 16:03:52,623 INFO  [org.jboss.ejb3.deployers.JBossASKernel]           jboss-injector:topLevelUnit=app.ear,unit=core-ejb.jar,bean=SessionManagerBean; Required: Described

            2011-01-19 16:03:52,633 INFO  [org.jboss.ejb3.deployers.JBossASKernel]   and supplies:

            2011-01-19 16:03:52,640 INFO  [org.jboss.ejb3.deployers.JBossASKernel]           jndi:app/SessionManagerBean/local

            2011-01-19 16:03:52,645 INFO  [org.jboss.ejb3.deployers.JBossASKernel]           jndi:app/SessionManagerBean/local-com.app.common.session.SessionManagerLocal

            2011-01-19 16:03:52,651 INFO  [org.jboss.ejb3.deployers.JBossASKernel]           Class:com.app.common.session.SessionManagerRemote

            2011-01-19 16:03:52,657 INFO  [org.jboss.ejb3.deployers.JBossASKernel]           jndi:SessionManagerBean

            2011-01-19 16:03:52,663 INFO  [org.jboss.ejb3.deployers.JBossASKernel]           jndi:app/SessionManagerBean/remote

            2011-01-19 16:03:52,668 INFO  [org.jboss.ejb3.deployers.JBossASKernel]           jndi:app/SessionManagerBean/remote-com.app.common.session.SessionManagerRemote

            2011-01-19 16:03:52,674 INFO  [org.jboss.ejb3.deployers.JBossASKernel]           Class:com.app.common.session.SessionManagerLocal

            2011-01-19 16:03:52,684 INFO  [org.jboss.ejb3.deployers.JBossASKernel] Added bean(jboss.j2ee:ear=app.ear,jar=core-ejb.jar,name=SessionManagerBean,service=EJB3) to KernelDeployment of: core-ejb.jar

             

            Is it impossible to use dependency injection to lookup beans located in one jar from JSF backing beans located in another jar if both jars are located in the same ear?

            • 4. Re: Injection in managed bean fails in JBoss AS 6 Final
              valliman Newbie

              Hello,

               

              I have somehow the same problem. Our project looks like the following.

               

              web-app project:

              containing jsf managed beans

               

              connector project:

              contains interfaces of our ejbs and common beans who are needed on server and client side

               

              server project:

              contains the implementations of our EJB interfaces and business logic

               

              We package like the following.

              WAR including web-app project and connector as lib

              EAR including server-project as ejb module and the connector as jar

               

              Our code looks like the one specified below.

               

              @Managed

              @SessionScoped

              public class ManagedSessionBean {

               

              @EJB(lookup = MyService.mappedName)

              MyService service;

               

              }

               

              @Remote

              public interface MyService {

               

              public static final String mappedName = "someName";

               

              }

               

              @Stateless(mappedName = MyService.mappedName)

              public class MyServiceImpl implements MyService {

               

              }

               

              We get an NPE when accesing the EJB in the web-app jsf managed bean. However as mentioned before lookup with InitialContext is working as expected.

               

              I've also tried to specify no mappedName, however injection with the JNDI name provided by JBoss isn't working either.

               

              Is this a JBoss 6 Final bug, or I am just doing something wrong?

              • 5. Re: Injection in managed bean fails in JBoss AS 6 Final
                Michael Feichtegger Newbie

                As far as I understand your problem I think I've a kind of the same issue.

                 

                I also try to inject an EJB reference using the @EJB annotation into a JSF backing bean.

                If I declare the bean using the @ManagedBean annotation the reference is null.

                If I use the CDI compliant @Named annotation the EJB will referenced correctly.

                 

                Unfortunately some other JSF behavior will be lost when I'm using the @Named annotation - the bean properties will not be set as excpected.

                 

                So my question is, what is the state of your problem. Have you found a solution? Have you created a JIRA issue?

                 

                kind regards

                Michael

                • 6. Injection in managed bean fails in JBoss AS 6 Final
                  jaikiran pai Master

                  What else does your ear contain? Are you packaging any jar(s) containing javax.ejb.* classes? Can anyone of you attach a simple application here?

                  • 7. Re: Injection in managed bean fails in JBoss AS 6 Final
                    Michael Feichtegger Newbie

                    I deploy war which contains the JSF application and a few EJB's packaged in a separate jar files.

                     

                    I'm trying to extract a sample out of my project and provide it.

                    • 8. Re: Injection in managed bean fails in JBoss AS 6 Final
                      jaikiran pai Master

                      Michael Feichtegger wrote:

                       

                      I deploy war which contains the JSF application and a few EJB's packaged in a separate jar files.

                       

                       

                      The .war and EJB jars are deployed separately and not part of an .ear?

                       

                       

                      Michael Feichtegger wrote:

                       

                      I'm trying to extract a sample out of my project and provide it.

                      Yeah, that would help.

                      • 9. Re: Injection in managed bean fails in JBoss AS 6 Final
                        Michael Feichtegger Newbie

                        During preparing the example I discovered the problem.

                        I used @LocalBean annotations to declare the EJB without a @Local interface class.

                        In the backing bean I used @EJB to get a reference. When using @ViewScoped JSF serializes the bean and I got an Exception. So I decided to declare the EJB transient which causes a NullPointerException.

                         

                        Finally I declared an interface and annotated it with @Local and used

                         

                        @EJB(name="HelloServiceBean", beanInterface="HelloService")

                        private HelloService service;

                         

                        works as expceted.