There is no standard facility for this. One way is to do this
independent of Wicket by configuring a session listener:
http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/http/HttpSessionListener.html

A Wicket specific way for this would be to use a custom session store.
This is an example:

/**
 * Session store that keeps attributes in memory instead of putting them in the
 * [EMAIL PROTECTED] HttpSession}.
 *
 * @author hillenius
 */
public final class Ts4SessionStore extends AbstractHttpSessionStore
implements Serializable {

        private static final class ANONYMOUS extends User {
                public ANONYMOUS() {
                        setUsername("[ANONYMOUS]");
                }
        }

        /** Log. */
        private static final Log log = LogFactory.getLog(Ts4SessionStore.class);

        /** Hibernate session factory. */
        @SpringBean
        private SessionFactory sessionFactory;

        /**
         * Map of session ids to store objects.
         */
        private final HashMap<String, Map<String, Serializable>>
sessionIdToStore = new HashMap<String, Map<String, Serializable>>();

        /**
         * Construct.
         */
        public Ts4SessionStore() {

        }

        /**
         * @see wicket.session.ISessionStore#createPageMap(java.lang.String,
         *      wicket.Session)
         */
        public IPageMap createPageMap(String name, Session session) {
                return new AccessStackPageMap(name, session);
        }

        /**
         * @see ISessionStore#getAttribute(Request, String)
         */
        public Object getAttribute(Request request, String name) {
                Map<String, Serializable> store = getStore(request);
                return store.get(name);
        }

        /**
         * @see ISessionStore#getAttributeNames(Request)
         */
        public List<String> getAttributeNames(Request request) {
                Map<String, Serializable> store = getStore(request);
                return new ArrayList<String>(store.keySet());
        }

        /**
         * @return The number of sessions.
         */
        public int getNumberOfSessions() {
                return sessionIdToStore.size();
        }

        /**
         * Gets the internal store (for integration purposes).
         *
         * @return The internal store
         */
        public Map<String, Map<String, Serializable>> getSessionIdToStore() {
                return sessionIdToStore;
        }

        /**
         * @see 
wicket.protocol.http.AbstractHttpSessionStore#lookup(wicket.Request)
         */
        @Override
        public Session lookup(Request request) {
                Map<String, Serializable> store = getStoreUnsafe(request);
                if (store != null) {
                        return (Session) 
store.get(Session.SESSION_ATTRIBUTE_NAME);
                }
                return null;
        }

        /**
         * @see ISessionStore#removeAttribute(Request, String)
         */
        public void removeAttribute(Request request, String name) {
                Map<String, Serializable> store = getStore(request);
                store.remove(name);
        }

        @SuppressWarnings("unchecked")
        public Iterator<Ts4Session> sessions() {

                final Iterator<Map<String, Serializable>> outer = 
((HashMap<String,
Map<String, Serializable>>) sessionIdToStore
                                .clone()).values().iterator();
                return new Iterator<Ts4Session>() {

                        public boolean hasNext() {
                                return outer.hasNext();
                        }

                        public Ts4Session next() {
                                Map<String, Serializable> store = outer.next();
                                return (Ts4Session) 
store.get(Session.SESSION_ATTRIBUTE_NAME);
                        }

                        public void remove() {
                                outer.remove();
                        }
                };
        }

        /**
         * @see ISessionStore#setAttribute(Request, String, Object)
         */
        public void setAttribute(Request request, String name, Object value) {
                Map<String, Serializable> store = getStore(request);
                store.put(name, (Serializable) value);
        }

        /**
         * Gets all users for the current sessions; ANONYMOUS for every user 
that is
         * not logged on. Use with JMX.
         *
         * @return List of current users
         */
        public String users() {

                if (sessionFactory == null) {
                        SpringInjector.injectDependencies(this);
                }

                StringBuilder b = new StringBuilder();

                if (!sessionIdToStore.isEmpty()) {
                        org.hibernate.Session hibernateSession = null;
                        try {
                                hibernateSession = sessionFactory.openSession();
                                for (Iterator<String> i = 
sessionIdToStore.keySet().iterator();
i.hasNext();) {
                                        String sid = i.next();
                                        Map<String, Serializable> store = 
sessionIdToStore.get(sid);
                                        Ts4Session session = (Ts4Session)
store.get(Session.SESSION_ATTRIBUTE_NAME);
                                        Long userId = session.getUserId();
                                        if (userId != null) {
                                                User user = (User) 
hibernateSession.load(User.class, userId);
                                                b.append(user.getUsername());
                                                b.append(": ");
                                                b.append(user.getFirstName());
                                                b.append(" ");
                                                b.append(user.getLastName());
                                        } else {
                                                b.append("ANONYMOUS");
                                        }
                                        b.append(", session ");
                                        b.append(sid);
                                        if (i.hasNext()) {
                                                b.append("\n");
                                        }
                                }
                        } catch (Exception e) {
                                e.printStackTrace();
                                b.delete(0, b.length());
                                b.append("unable to make a list of currently 
logged in users:");
                                for (StackTraceElement el : e.getStackTrace()) {
                                        b.append("\n\t");
                                        b.append(el.toString());
                                }
                        } finally {
                                if (hibernateSession != null) {
                                        hibernateSession.close();
                                }
                        }
                }

                return b.toString();
        }

        /**
         * Gets the store for the session of the provided request, returning 
null
         * and log a warning when the store was not found.
         *
         * @param request
         *
         * @return The store
         */
        private final Map<String, Serializable> getStore(Request request) {
                String sessionId = getSessionId(request, true);
                Map<String, Serializable> store = 
sessionIdToStore.get(sessionId);
                if (store == null) {
                        log.warn("no store found for session with id " + 
sessionId + "
(request=" + request + ")");
                        // return a dummy
                        return new HashMap<String, Serializable>();
                }
                return store;
        }

        /**
         * Gets the store for the session of the provided request, returning 
null
         * when the store was not found.
         *
         * @param request
         *
         * @return The store
         */
        private final Map<String, Serializable> getStoreUnsafe(Request request) 
{
                String sessionId = getSessionId(request, true);
                return sessionIdToStore.get(sessionId);
        }

        /**
         * @see AbstractHttpSessionStore#onBind(Request, Session)
         */
        @Override
        protected void onBind(Request request, Session newSession) {
                String sessionId = getSessionId(request, true);
                sessionIdToStore.put(sessionId, new HashMap<String, 
Serializable>());
                log.info("new session " + sessionId + " bound to session 
store");
        }

        /**
         * @see AbstractHttpSessionStore#onUnbind(String)
         */
        @Override
        protected void onUnbind(String sessionId) {
                Map<String, Serializable> store = 
sessionIdToStore.remove(sessionId);
                log.info("session " + sessionId + " unbound from session store;
cleaning up " + store.size() + " entries");
        }
}


Hope that helps,

Eelco

On 2/6/07, Peter Neubauer <[EMAIL PROTECTED]> wrote:
> Hi there,
> how do I get the logged in users as a list or something from the API
> in Wicket 2.0 SNAPSHOT?
>
>
> Thanks for any hints
>
> /peter
>
> -------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job easier.
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> _______________________________________________
> Wicket-user mailing list
> Wicket-user@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/wicket-user
>

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user

Reply via email to