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

    Injection Failure

    robshep


      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()
      








        • 1. Re: Injection Failure
          robshep

          Many thanks for any pointers

          • 2. Re: Injection Failure
            jeanluc

            Terms like injection failure and errors in the EL are vague instead of stacktraces, but some pointers:



            • IIRC, to use EL expressions in a query you need a Seam-managed PC, not a container-managed PC. That is, @In private EntityManager em; not @PersistenceContext private EntityManager em;

            • check your components.xml for the security definitions

            • you're duplicating functionality already built into Seam (JpaIdentityStore). Checks the docs, there's already code that does it for you.