6 Replies Latest reply on Oct 15, 2006 3:56 PM by alrubinger

    @EJB not working?

    nomike

       

      /**
       * @author michael
       * @version 0.1.0.0 05.10.2006
       */
      package at.tac.ra.ng.ejb.bl.login;
      
      import at.tac.ra.ng.ejb.beans.user.User;
      import at.tac.ra.ng.ejb.entities.UserEntity;
      import at.tac.ra.ng.ejb.entities.LanguageEntity;
      import at.tac.ra.ng.common.HashGen;
      import at.tac.ra.ng.common.SysConfig;
      
      import javax.ejb.Stateless;
      import javax.ejb.Remote;
      import javax.ejb.EJB;
      import javax.persistence.PersistenceException;
      import javax.persistence.PersistenceContext;
      import javax.persistence.EntityManager;
      import java.util.Calendar;
      import java.security.NoSuchAlgorithmException;
      
      @Stateless(name = "LoginEJB")
      @Remote(Login.class)
      public class LoginBean implements Login {
       @PersistenceContext
       EntityManager em;
       @EJB
       User userBean;
       public loginStatus authenticateUser(String userLogin, char[] password) {
       if (userBean.findByUserLogin(userLogin).size() <= 0) {
       return loginStatus.USERLOGIN_DOES_NOT_EXIST;
       }
       UserEntity user;
       try {
       user = userBean.findByUserLoginEnabled(userLogin);
       } catch (PersistenceException e) {
       return loginStatus.NO_ENABLED_USER_FOUND_WITH_THIS_USERLOGIN;
       }
       Calendar c = Calendar.getInstance();
       if ((user.getReactivationTime() != null) && (c.getTime().getTime() < user.getReactivationTime().getTime())) {
       //Account temporary locked
       return loginStatus.ACCOUNT_TEMPORARY_LOCKED;
       } else {
       // Account not temporary locked
       try {
       if (user.getPassword().equals(HashGen.do_checksum(String.valueOf(password), "SHA-1"))) {
       // Password is correct
       user.setLoginTrials(0);
       user.setReactivationTime(null);
       em.flush();
       return loginStatus.SUCCESS;
       } else {
       // Password is wrong
       user.setLoginTrials(user.getLoginTrials() + 1);
       if (user.getLoginTrials() > SysConfig.getAllowedLoginTrials()) {
       Calendar c2 = Calendar.getInstance();
       c.add(Calendar.MINUTE, SysConfig.getLoginLockTime());
       user.setReactivationTime(c.getTime());
       }
       em.flush();
       return loginStatus.WRONG_PASSWORD;
       }
       } catch (NoSuchAlgorithmException e) {
       e.printStackTrace();
       return loginStatus.UNKNOWN_ERROR;
       }
       }
       }
      
      
       public UserEntity getUser(String userLogin) {
       return userBean.findByUserLoginEnabled(userLogin);
       }
      
       public LanguageEntity getLanguage(UserEntity user, Integer languageID) {
       if (languageID == null) {
       em.persist(user);
       return user.getLanguage();
       } else {
       return em.find(LanguageEntity.class, languageID);
       }
       }
      }
      

      The above code produces a NullPointerException on "If (userBean.findByUserLogin(userLogin).size() <= 0)" because userBean is null.
      Shouldn't a reference to UserEJB be injected there or am I missing something?

      nomike aka Michael Postmann

        • 1. Re: @EJB not working?
          anders.hedstrom

          You don't show your code for the User SessionBean, but my guess from reading the code that you've posted is that you have given your User SB another name than the default one - then you need to specify that name in your @EJB annotation.

          @EJB(beanName="<your-name-on-your-bean>")
          User userBean;


          • 2. Re: @EJB not working?
            bill.burke

            what version? You should be getting a deployment error.

            • 3. Re: @EJB not working?
              nomike

               

              "bill.burke@jboss.com" wrote:
              what version? You should be getting a deployment error.


              I downloaded JBoss-4.0.4-GA Patchrelease 1 from here.
              I'm using it within IntelliJ 6.0. Should deployment fail, or is this just a warning message?

              nomike aka Michael Postmann


              • 4. Re: @EJB not working?
                andydale

                I also cannot get @EJB injection to work, using JBoss.4.0.4.GA with whatever version of EJB3 that comes bundled with it.

                I have experimented with in a few ways such as, specifiing a @LocalBinding, @EJB(mappedname=) and so on, but i the injection always results in null :-(. The current setup i have is that i am trying to look up the EJB in an MBean packaged within the same .ear file, could deployment order (ie .sar before .jar) possibly be causing the problem ? i have verified that it is bound in two ways, InitialContext.lookup() works and in the JNDI view it is bound,

                Any help is appreciated.

                Andy

                • 5. Re: @EJB not working?
                  marcinnowak

                  Hi,

                  I found the answer:
                  Problem is with

                  import javax.ejb.EJB;

                  it should be
                  import javax.annotation.EJB;

                  It is working for me at 4.0.4GA

                  Martin.

                  • 6. Re: @EJB not working?
                    alrubinger

                    Depends on the version of EJB3 you're using. Post EJB3 RC8 inclusive, the change was made from javax.annotation to javax.ejb.

                    http://wiki.jboss.org/wiki/Wiki.jsp?page=FromRC7RC8

                    I believe it's RC7 that ships w/ the 4.0.4-GA installer, but am not sure.

                    S,
                    ALR