[jboss-user] [EJB 3.0] - @EJB not working?

nomike do-not-reply at jboss.com
Thu Oct 5 09:07:51 EDT 2006


/**
  |  * @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

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3976304#3976304

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3976304



More information about the jboss-user mailing list