01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.test.web.servlets;
23:
24: import java.util.HashMap;
25: import java.util.Enumeration;
26: import javax.servlet.http.HttpSessionListener;
27: import javax.servlet.http.HttpSessionEvent;
28: import javax.servlet.http.HttpSession;
29:
30: import org.jboss.logging.Logger;
31:
32: /** A test HttpSessionListener that tracks which sessions have seen
33: * create/destory events.
34: *
35: * @author Scott.Stark@jboss.org
36: * @version $Revision: 57211 $
37: */
38: public class TestSessionListener implements HttpSessionListener {
39: static Logger log = Logger.getLogger(TestSessionListener.class);
40: static HashMap eventMap = new HashMap();
41:
42: static boolean wasCreated(String sessionID) {
43: boolean[] events = (boolean[]) eventMap.get(sessionID);
44: if (events == null)
45: return false;
46: return events[0];
47: }
48:
49: static boolean wasDestroyed(String sessionID) {
50: boolean[] events = (boolean[]) eventMap.get(sessionID);
51: if (events == null)
52: return false;
53: return events[1];
54: }
55:
56: public void sessionCreated(HttpSessionEvent event) {
57: String sessionID = event.getSession().getId();
58: log.info("sessionCreated, sessionID=" + sessionID);
59: boolean[] events = { true, false };
60: eventMap.put(sessionID, events);
61: }
62:
63: public void sessionDestroyed(HttpSessionEvent event) {
64: String sessionID = event.getSession().getId();
65: log.info("sessionDestroyed, sessionID=" + sessionID);
66: HttpSession session = event.getSession();
67: Enumeration names = session.getAttributeNames();
68: while (names.hasMoreElements()) {
69: String name = (String) names.nextElement();
70: log.info(name + "=" + session.getAttribute(name));
71: }
72: boolean[] events = (boolean[]) eventMap.get(sessionID);
73: events[1] = true;
74: }
75: }
|