KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > listener > UserCounterListener


1 package com.blandware.atleap.webapp.listener;
2
3 import com.blandware.atleap.common.Constants;
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6
7 import javax.servlet.ServletContext JavaDoc;
8 import javax.servlet.ServletContextEvent JavaDoc;
9 import javax.servlet.ServletContextListener JavaDoc;
10 import javax.servlet.http.HttpSession JavaDoc;
11 import javax.servlet.http.HttpSessionAttributeListener JavaDoc;
12 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
13 import java.util.HashMap JavaDoc;
14
15
16 /**
17  * UserCounterListener class used to count the current number
18  * of active users for the applications. Does this by counting
19  * how many user objects are stuffed into the session. It also grabs
20  * these users and exposes them in the servlet context.
21  *
22  * @author Matt Raible <a HREF="mailto:matt@raibledesigns.com">&lt;matt@raibledesigns.com&gt;</a>
23  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
24  * @web.listener
25  */

26 public class UserCounterListener implements ServletContextListener JavaDoc, HttpSessionAttributeListener JavaDoc {
27
28     /**
29      * Key to store number of logined users in servletContext.
30      * It contains Integer
31      */

32     public static final String JavaDoc COUNT_KEY = "userCounter";
33
34     /**
35      * Key to store logined users in servletContext.
36      * It contains HashMap with key String with sessionId and value User
37      */

38     public static final String JavaDoc USERS_KEY = "userNames";
39
40     protected transient final Log log = LogFactory.getLog(UserCounterListener.class);
41     protected transient ServletContext JavaDoc servletContext;
42
43     public synchronized void contextInitialized(ServletContextEvent JavaDoc sce) {
44         servletContext = sce.getServletContext();
45         servletContext.setAttribute(COUNT_KEY, new Integer JavaDoc(0));
46         servletContext.setAttribute(USERS_KEY, new HashMap JavaDoc());
47     }
48
49     public synchronized void contextDestroyed(ServletContextEvent JavaDoc event) {
50         servletContext = null;
51     }
52
53     /**
54      * Adds a user: if he is new, he's added to list of logged in users and
55      * the counter is incremented. Otherwise nothing happens.
56      *
57      * @param session The HTTP session to identify user by
58      * @param user User to be added
59      */

60     synchronized void addUser(HttpSession JavaDoc session, Object JavaDoc user) {
61         HashMap JavaDoc users = (HashMap JavaDoc) servletContext.getAttribute(USERS_KEY);
62
63         Integer JavaDoc sessionHashCode = new Integer JavaDoc(session.hashCode());
64
65         if ( !users.containsKey(sessionHashCode) ) {
66             users.put(sessionHashCode, user);
67
68             servletContext.setAttribute(COUNT_KEY, new Integer JavaDoc(users.size()));
69             servletContext.setAttribute(USERS_KEY, users);
70         }
71     }
72
73     /**
74      * Removes a user: if he is in list of logged in users, he's removed from
75      * it and the counter is decremented. Otherwise nothing happens.
76      *
77      * @param session The HTTP session to identify user by
78      * @param user User to be removed
79      */

80     synchronized void removeUser(HttpSession JavaDoc session, Object JavaDoc user) {
81         HashMap JavaDoc users = (HashMap JavaDoc) servletContext.getAttribute(USERS_KEY);
82
83         Integer JavaDoc sessionHashCode = new Integer JavaDoc(session.hashCode());
84
85         if ( users.containsKey(sessionHashCode) ) {
86             users.remove(sessionHashCode);
87
88             servletContext.setAttribute(COUNT_KEY, new Integer JavaDoc(users.size()));
89             servletContext.setAttribute(USERS_KEY, users);
90         }
91     }
92
93     /**
94      * This method is designed to catch when users login and record their names
95      *
96      * @see javax.servlet.http.HttpSessionAttributeListener#attributeAdded(javax.servlet.http.HttpSessionBindingEvent)
97      */

98     public void attributeAdded(HttpSessionBindingEvent JavaDoc event) {
99         if ( event.getName().equals(Constants.USER_KEY) ) {
100             addUser(event.getSession(), event.getValue());
101         }
102     }
103
104     /**
105      * When users logout, remove their names from the hashMap
106      *
107      * @see javax.servlet.http.HttpSessionAttributeListener#attributeRemoved(javax.servlet.http.HttpSessionBindingEvent)
108      */

109     public void attributeRemoved(HttpSessionBindingEvent JavaDoc event) {
110         if ( event.getName().equals(Constants.USER_KEY) ) {
111             removeUser(event.getSession(), event.getValue());
112         }
113     }
114
115     /**
116      * @see javax.servlet.http.HttpSessionAttributeListener#attributeReplaced(javax.servlet.http.HttpSessionBindingEvent)
117      */

118     public void attributeReplaced(HttpSessionBindingEvent JavaDoc event) {
119         if ( event.getName().equals(Constants.USER_KEY) ) {
120             addUser(event.getSession(), event.getValue());
121         }
122     }
123
124 }
125
Popular Tags