[jboss-user] [JBoss Seam] - Re: Begin conversation *before* entering a method

schmatz do-not-reply at jboss.com
Mon Feb 26 07:15:37 EST 2007


Maybe it's easier to roll out the complete scenario:

I have

- a CONVERSATIONAL-scoped User entity
- an Authenticator class which outjects the User entity
- a CONVERSATIONAL-scoped userAction EJB which encapsulates the above mentioned login method (and the corresponding logout method)

The EJB injects the User entity.

Now, I want the User entity to live in a long running conversation context after successfully logging in until the user logs out.

Here's the beginning of my EJB:


  | @Stateful
  | @Scope(ScopeType.CONVERSATION)
  | @Name("userAction")
  | public class UserActionImpl implements UserAction
  | {
  |     @Logger private Log log;
  |     
  |     @In
  |     FacesMessages facesMessages;
  | 
  |     @In(value="entityManager")
  |     private EntityManager em;
  |    
  |     @In(required=false) @Out(required=false)
  |     private User user;
  | 
  | 	@Begin
  | 	public Object login()
  | 	{
  | 		log.info("****** USER 1   : ", user);
  | 		Object object = Identity.instance().login();
  | 		log.info("****** USER 2  : ", user);
  | 		return object;
  | 	}
  | 
  | 	@End
  | 	public Object logout()
  | 	{
  | 		Identity.instance().logout();
  | 		return "success";
  | 	}
  | 
  |     [...]
  | 
BTW: both log.info(...)'s print null to the console.


Here's the beginning of my User entity:


  | @Entity
  | @Scope(ScopeType.CONVERSATION)
  | @Name("user")
  | @Table(name="tzuser")
  | @javax.persistence.SequenceGenerator(
  | 	name="s_tzuser",
  | 	sequenceName="s_tzuser")
  | public class User implements Serializable
  | {
  | 	private static final long serialVersionUID = 3366658268468153076L;
  | 	
  |     [...]
  | 

And finally the Authenticator Bean:


  | @Name("authenticator")
  | public class Authenticator
  | {
  |     @Logger Log log;
  |     
  |     @In
  |     private Identity identity;
  | 
  |     @In(value="entityManager")
  |     private EntityManager entityManager;
  | 
  |     @Out(required=false)
  |     private User user;
  |     
  |     public boolean authenticate()
  |     {
  |         log.info("authenticating #0", identity.getUsername());
  |         
  |         //write your authentication logic here,
  |         //return true if the authentication was
  |         //successful, false otherwise
  | 
  |         try
  |         {
  | 	        user = (User) entityManager.createQuery(
  | 		        "from User where username = :username and password = :password")
  | 		        .setParameter("username", identity.getUsername())
  | 		        .setParameter("password", identity.getPassword())
  | 		        .getSingleResult();
  | 	
  | 	        if( user.getRoles() != null )
  | 	        {
  | 	           for( Role role : user.getRoles() )
  | 	        	   identity.addRole(role.getName());
  | 	        }
  | 	        
  | 	        //FacesMessages.instance().add("Successfully authenticated #0", identity.getUsername());
  | 	        log.info("Successfully authenticated #0", identity.getUsername());
  | 	        return true;
  |         }
  |         catch( NoResultException e )
  |         {
  |         	//FacesMessages.instance().add("Invalid username/password");
  |         	log.info("authentication of #0 failed.", identity.getUsername());
  |         	return false;
  |         }
  |         catch( Exception e )
  |         {
  |         	FacesMessages.instance().add("Unexpected error occurred.");
  |         	log.error("Unexpected error occurred", e);
  |         	return false;
  |         }
  |     }
  | }
  | 

Ok, I hope someone can help me with that.

Thanks i.a.,
Mark


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

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



More information about the jboss-user mailing list