You need to collect all registered users in Set<User> in the application area . Just hook on login and logout and add and remove User accordingly. Mostly:
public void login(User user) {
If you save registered users in a session, then you want to add another hook to the destroy session to issue a logout to any registered user. I'm not sure how Grails fits in the picture, but speaking in the Java Servlet API, you want to use HttpSessionListener#sessionDestroyed() .
public void sessionDestroyed(HttpSessionEvent event) { User user = (User) event.getSession().getAttribute("user"); if (user != null) { Set<User> logins = (Set<User>) event.getSession().getServletContext().getAttribute("logins"); logins.remove(user); } }
You can also just let the User model implement the HttpSessionBindingListener . The implemented methods will be called automatically when the User instance is placed in the session or removed from it (which will also happen when the session is destroyed).
public class User implements HttpSessionBindingListener { @Override public void valueBound(HttpSessionBindingEvent event) { Set<User> logins = (Set<User>) event.getSession().getServletContext().getAttribute("logins"); logins.add(this); } @Override public void valueUnbound(HttpSessionBindingEvent event) { Set<User> logins = (Set<User>) event.getSession().getServletContext().getAttribute("logins"); logins.remove(this); }
BalusC Jul 17 '10 at 13:57 2010-07-17 13:57
source share