2 Replies Latest reply on Jun 23, 2009 6:51 PM by Jean Luc

    Injection Failure

    Rob Shepherd Newbie


      import static org.jboss.seam.ScopeType.SESSION;
      
      import java.util.List;
      
      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Out;
      
      @Stateless
      @Name("authenticator")
      public class AuthenticatorAction implements Authenticator
      {
         @PersistenceContext 
         private EntityManager em;
         
         @SuppressWarnings("unused")
         @Out(required=false, scope = SESSION)
         private User user;
         
         public boolean authenticate()
         {
            List results = em.createQuery("select u from User u where u.username=#{identity.username} and u.password=#{identity.password}")
                  .getResultList();
            
            if ( results.size()==0 )
            {
               return false;
            }
            else
            {
               user = (User) results.get(0);
               return true;
            }
         }
      
      }



      Please could anybody tell my why the above code suffers from an injection failure.


      In the above example, it is failing with errors in the EL, but If I change it to.......




      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      
      import net.datacymru.sw.netstat.model.User;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Out;
      import org.jboss.seam.security.Identity;
      
      @Stateless
      @Name("authenticator")
      public class AuthenticatorAction implements Authenticator
      {
         @PersistenceContext 
         private EntityManager em;
         
         @SuppressWarnings("unused")
         @Out(required=false, scope = SESSION)
         private User user;
         
         @In
         Identity identity;
         
         public boolean authenticate()
         {
             String username = identity.getUsername();
            
             List results = em.createQuery("select u from User u where u.username=" + username)
                  .getResultList();
            
            if ( results.size()==0 )
            {
               return false;
            }
            else
            {
               user = (User) results.get(0);
               return true;
            }
         }
      
      }



      .... then the above example fails with a NullPointerException at the line:




      identity.getUsername()