You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Gunnar Eketrapp <gu...@gmail.com> on 2009/09/30 17:24:13 UTC

How to implement a user tracker in T5 ....

Hi !

I am porting a huge jsp/spring kludge to T5. (And love it ...)

I have now come across a UserTracker implementation that tracks currently
logged in users.

How is this best done in T5 anyone?

I could perhaps stick with the old servlet but I am not sure of how T5
creaets/destoys sessions and
adds/removes attributes.

You know now I have a state ASO where the user object is stored within.

Thanks in advance,
Gunnar Eketrapp,
Stockholm, Sweden




========================================================================
Current User tracker implementaon using HttpSessionListener +
HttpSessionAttributeListener
========================================================================

public class UserTracker implements HttpSessionListener,
HttpSessionAttributeListener {
    private static final Logger logger =
Logger.getLogger(UserTracker.class.getName());
    private static UserTracker instance;

    public static UserTracker getInstance() {
        return instance;
    }

    public UserTracker() {
        instance = this;
    }

    private SortedSet<UserTrackerEntry> current = new
TreeSet<UserTrackerEntry>();

    public SortedSet<UserTrackerEntry> getCurrent() {
        SortedSet<UserTrackerEntry> result = new
TreeSet<UserTrackerEntry>();
        synchronized (current) {
            result.addAll(current);
        }
        return result;
    }

    public SortedSet<String> getCurrentUsernames() {
        SortedSet<String> result = new TreeSet<String>();
        synchronized (current) {
            for( UserTrackerEntry en : current ) {
                result.add(en.getUsername());
            }
        }
        return result;
    }

    public void attributeAdded(HttpSessionBindingEvent e) {
        if( "user".equals(e.getName()) ) {
            User user = (User) e.getValue();
            synchronized (current) {
                String sessionId = e.getSession().getId();
                current.add(new
UserTrackerEntry(user.getUsername(),user.getFullName(),sessionId,new
Date()));
            }
        }
    }

    public void attributeRemoved(HttpSessionBindingEvent e) {
        if( "user".equals(e.getName()) ) {
            User user = (User) e.getValue();
            synchronized (current) {
                String sessionId = e.getSession().getId();
                current.remove(new
UserTrackerEntry(user.getUsername(),user.getFullName(),sessionId,new
Date()));
            }
        }
    }

    public void attributeReplaced(HttpSessionBindingEvent arg0) {
        // ignore
    }

    public void sessionCreated(HttpSessionEvent e) {
        User user = (User) e.getSession().getAttribute("user");
        if( user != null ) {
            synchronized (current) {
                String sessionId = e.getSession().getId();
                current.add(new
UserTrackerEntry(user.getUsername(),user.getFullName(),sessionId,new
Date()));
            }
        }
    }

    public void sessionDestroyed(HttpSessionEvent e) {
        User user = (User) e.getSession().getAttribute("user");
        if( user != null ) {
            synchronized (current) {
                String sessionId = e.getSession().getId();
                current.remove(new
UserTrackerEntry(user.getUsername(),user.getFullName(),sessionId,new
Date()));
            }
        }
    }

}

Re: How to implement a user tracker in T5 ....

Posted by Olle Hallin <ol...@hit.se>.
Also good to know is that @SessionStateObjects (was:
@ApplicationStateObject) are stored in the HTTP session with the key "sso:"
+ MySessionStateObject.class.getName() (Tapestry 5.1)

Olle Hallin
Senior Java Developer and Architect
olle.hallin@crisp.se
www.crisp.se




2009/9/30 cordenier christophe <ch...@gmail.com>

> If you choose to use a servlet session listener, keep in mind that the
> Tapestry Registry can be obtained from the application context under this
> key "TapestryFilter.REGISTRY_CONTEXT_NAME".
>
> Then you can have access to all Tapestry 5 services including the
> ApplicationStateManager.
>
> 2009/9/30 Gunnar Eketrapp <gu...@gmail.com>
>
> > Hi !
> >
> > I am porting a huge jsp/spring kludge to T5. (And love it ...)
> >
> > I have now come across a UserTracker implementation that tracks currently
> > logged in users.
> >
> > How is this best done in T5 anyone?
> >
> > I could perhaps stick with the old servlet but I am not sure of how T5
> > creaets/destoys sessions and
> > adds/removes attributes.
> >
> > You know now I have a state ASO where the user object is stored within.
> >
> > Thanks in advance,
> > Gunnar Eketrapp,
> > Stockholm, Sweden
> >
> >
> >
> >
> > ========================================================================
> > Current User tracker implementaon using HttpSessionListener +
> > HttpSessionAttributeListener
> > ========================================================================
> >
> > public class UserTracker implements HttpSessionListener,
> > HttpSessionAttributeListener {
> >    private static final Logger logger =
> > Logger.getLogger(UserTracker.class.getName());
> >    private static UserTracker instance;
> >
> >    public static UserTracker getInstance() {
> >        return instance;
> >    }
> >
> >    public UserTracker() {
> >        instance = this;
> >    }
> >
> >    private SortedSet<UserTrackerEntry> current = new
> > TreeSet<UserTrackerEntry>();
> >
> >    public SortedSet<UserTrackerEntry> getCurrent() {
> >        SortedSet<UserTrackerEntry> result = new
> > TreeSet<UserTrackerEntry>();
> >        synchronized (current) {
> >            result.addAll(current);
> >        }
> >        return result;
> >    }
> >
> >    public SortedSet<String> getCurrentUsernames() {
> >        SortedSet<String> result = new TreeSet<String>();
> >        synchronized (current) {
> >            for( UserTrackerEntry en : current ) {
> >                result.add(en.getUsername());
> >            }
> >        }
> >        return result;
> >    }
> >
> >    public void attributeAdded(HttpSessionBindingEvent e) {
> >        if( "user".equals(e.getName()) ) {
> >            User user = (User) e.getValue();
> >            synchronized (current) {
> >                String sessionId = e.getSession().getId();
> >                current.add(new
> > UserTrackerEntry(user.getUsername(),user.getFullName(),sessionId,new
> > Date()));
> >            }
> >        }
> >    }
> >
> >    public void attributeRemoved(HttpSessionBindingEvent e) {
> >        if( "user".equals(e.getName()) ) {
> >            User user = (User) e.getValue();
> >            synchronized (current) {
> >                String sessionId = e.getSession().getId();
> >                current.remove(new
> > UserTrackerEntry(user.getUsername(),user.getFullName(),sessionId,new
> > Date()));
> >            }
> >        }
> >    }
> >
> >    public void attributeReplaced(HttpSessionBindingEvent arg0) {
> >        // ignore
> >    }
> >
> >    public void sessionCreated(HttpSessionEvent e) {
> >        User user = (User) e.getSession().getAttribute("user");
> >        if( user != null ) {
> >            synchronized (current) {
> >                String sessionId = e.getSession().getId();
> >                current.add(new
> > UserTrackerEntry(user.getUsername(),user.getFullName(),sessionId,new
> > Date()));
> >            }
> >        }
> >    }
> >
> >    public void sessionDestroyed(HttpSessionEvent e) {
> >        User user = (User) e.getSession().getAttribute("user");
> >        if( user != null ) {
> >            synchronized (current) {
> >                String sessionId = e.getSession().getId();
> >                current.remove(new
> > UserTrackerEntry(user.getUsername(),user.getFullName(),sessionId,new
> > Date()));
> >            }
> >        }
> >    }
> >
> > }
> >
>

Re: How to implement a user tracker in T5 ....

Posted by cordenier christophe <ch...@gmail.com>.
If you choose to use a servlet session listener, keep in mind that the
Tapestry Registry can be obtained from the application context under this
key "TapestryFilter.REGISTRY_CONTEXT_NAME".

Then you can have access to all Tapestry 5 services including the
ApplicationStateManager.

2009/9/30 Gunnar Eketrapp <gu...@gmail.com>

> Hi !
>
> I am porting a huge jsp/spring kludge to T5. (And love it ...)
>
> I have now come across a UserTracker implementation that tracks currently
> logged in users.
>
> How is this best done in T5 anyone?
>
> I could perhaps stick with the old servlet but I am not sure of how T5
> creaets/destoys sessions and
> adds/removes attributes.
>
> You know now I have a state ASO where the user object is stored within.
>
> Thanks in advance,
> Gunnar Eketrapp,
> Stockholm, Sweden
>
>
>
>
> ========================================================================
> Current User tracker implementaon using HttpSessionListener +
> HttpSessionAttributeListener
> ========================================================================
>
> public class UserTracker implements HttpSessionListener,
> HttpSessionAttributeListener {
>    private static final Logger logger =
> Logger.getLogger(UserTracker.class.getName());
>    private static UserTracker instance;
>
>    public static UserTracker getInstance() {
>        return instance;
>    }
>
>    public UserTracker() {
>        instance = this;
>    }
>
>    private SortedSet<UserTrackerEntry> current = new
> TreeSet<UserTrackerEntry>();
>
>    public SortedSet<UserTrackerEntry> getCurrent() {
>        SortedSet<UserTrackerEntry> result = new
> TreeSet<UserTrackerEntry>();
>        synchronized (current) {
>            result.addAll(current);
>        }
>        return result;
>    }
>
>    public SortedSet<String> getCurrentUsernames() {
>        SortedSet<String> result = new TreeSet<String>();
>        synchronized (current) {
>            for( UserTrackerEntry en : current ) {
>                result.add(en.getUsername());
>            }
>        }
>        return result;
>    }
>
>    public void attributeAdded(HttpSessionBindingEvent e) {
>        if( "user".equals(e.getName()) ) {
>            User user = (User) e.getValue();
>            synchronized (current) {
>                String sessionId = e.getSession().getId();
>                current.add(new
> UserTrackerEntry(user.getUsername(),user.getFullName(),sessionId,new
> Date()));
>            }
>        }
>    }
>
>    public void attributeRemoved(HttpSessionBindingEvent e) {
>        if( "user".equals(e.getName()) ) {
>            User user = (User) e.getValue();
>            synchronized (current) {
>                String sessionId = e.getSession().getId();
>                current.remove(new
> UserTrackerEntry(user.getUsername(),user.getFullName(),sessionId,new
> Date()));
>            }
>        }
>    }
>
>    public void attributeReplaced(HttpSessionBindingEvent arg0) {
>        // ignore
>    }
>
>    public void sessionCreated(HttpSessionEvent e) {
>        User user = (User) e.getSession().getAttribute("user");
>        if( user != null ) {
>            synchronized (current) {
>                String sessionId = e.getSession().getId();
>                current.add(new
> UserTrackerEntry(user.getUsername(),user.getFullName(),sessionId,new
> Date()));
>            }
>        }
>    }
>
>    public void sessionDestroyed(HttpSessionEvent e) {
>        User user = (User) e.getSession().getAttribute("user");
>        if( user != null ) {
>            synchronized (current) {
>                String sessionId = e.getSession().getId();
>                current.remove(new
> UserTrackerEntry(user.getUsername(),user.getFullName(),sessionId,new
> Date()));
>            }
>        }
>    }
>
> }
>