I'm building a web app, and am using a stateful session bean to keep
track of logged in users so that I can do DB auditing. My problem is
that whenever I go to lookup the User property from my stateful bean,
its null. I understand you would normally have to keep a handle to
the bean in http session, but what if you use guice singleton or
session scope. Is that supposed to keep the conversational state
active with the stateful bean. What am I doing wrong?
Thanks for any help.
BaseEntity: does auditing by getting the logged in user and saving it
with the record.
<code>
@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class BaseEntity {
private User modifiedBy;
@PrePersist
private void setAddedAudit() {
AuthenticationManager authenticationManager = null;
try {
authenticationManager = (AuthenticationManager) new
InitialContext().lookup("shadows/AuthenticationManagerBean/local");
setModifiedBy(authenticationManager.getUser());
}
catch (NamingException e) {
int i = 0;
}
}
@ManyToOne()
public User getModifiedBy() {
return modifiedBy;
}
public void setModifiedBy(User modifiedBy) {
this.modifiedBy = modifiedBy;
}
}
</code>
AuthenticationManagerBean: stateful bean that stores the logged in
User.
<code>
@Stateful
public class AuthenticationManagerBean extends BaseManagerBean
implements AuthenticationManager {
private User user;
public User login(String userName, String password) throws
NoResultException{
user = (User) entityManager.createNamedQuery
(User.QUERY_FIND_USER_BY_USER_NAME_AND_PASSWORD).setParameter
("userName", userName).setParameter("password", Encryption.encrypt
(password)).getSingleResult();
return user;
}
public void logout() {
user = null;
}
public User getUser() {
return user;
}
}
</code>
AuthenticationManager: interface
<code>
public interface AuthenticationManager {
public User login(String userName, String password) throws
Exception;
public void logout();
public User getUser();
}
</code>
GuiceBindingModule: binds interfaces to beans
<code>
public class GuiceBindingModule extends AbstractModule {
private static String pattern = "shadows/{0}/local";
protected void configure() {
bind(Context.class).to(InitialContext.class);
bindToProvider(AuthenticationManager.class,
"AuthenticationManagerBean").in(ServletScopes.SESSION);
}
private ScopedBindingBuilder bindToProvider(Class clazz, String
beanShortName) {
return bind(clazz).toProvider(JndiIntegration.fromJndi(clazz,
MessageFormat.format(pattern, beanShortName)));
}
}
</code>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---