应用程序启动时,调用StartupListener初始化ServletCOntext环境变量、Spring环境变量和取得角色信息后,接着执行UserCounterListener监听器。
★ UserCounterListener功能:计算使用应用的用户个数和存储用户对象。应用启动使初始化用户数变量counter,并存入ServletContext,利用HttpSessionAttributeListener监听器每当Session中的属性值有变化时,修改ServletContext中的用户个数和,用户对象。
提供了一种全局跟踪用户信息的方法。
UserCounterListener监听器实现比较简单,主要时理解ServletContextListener和HttpSessionAttributeListener监听器的使用。
★ web.xml
<listener>
<listener-class>org.dudu.webapp.listener.UserCounterListener</listener-class>
</listener>
★ UserCounterListener监听器程序:
UserCounterListener出来继承了ServletContextListener监听器接口外,还继承了
HttpSessionAttributeListener监听器接口。ServletContextListener监听器接口的作用已经在“初始化”前一部分作了介绍。
★ HttpSessionAttributeListener监听器介绍:HttpSessionAttributeListener监听
HttpSession中的属性的操作。当在Session增加一个属性时,激发
attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发
attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激
发attributeReplaced(HttpSessionBindingEvent se) 方法。
public class UserCounterListener implements ServletContextListener,
HttpSessionAttributeListener {
public static final String COUNT_KEY = "userCounter";
public static final String USERS_KEY = "userNames";
private final transient Log log = LogFactory.getLog(UserCounterListener.class);
private transient ServletContext servletContext;
private int counter;
private Set users;
public synchronized void contextInitialized(ServletContextEvent sce) {
servletContext = sce.getServletContext();
servletContext.setAttribute((COUNT_KEY), Integer.toString(counter));
}
public synchronized void contextDestroyed(ServletContextEvent event) {
servletContext = null;
users = null;
counter = 0;
}
synchronized void incrementUserCounter() {
counter =
Integer.parseInt((String) servletContext.getAttribute(COUNT_KEY));
counter++;
servletContext.setAttribute(COUNT_KEY, Integer.toString(counter));
if (log.isDebugEnabled()) {
log.debug("User Count: " + counter);
}
}
synchronized void decrementUserCounter() {
int counter =
Integer.parseInt((String) servletContext.getAttribute(COUNT_KEY));
counter--;
if (counter < 0) {
counter = 0;
}
servletContext.setAttribute(COUNT_KEY, Integer.toString(counter));
if (log.isDebugEnabled()) {
log.debug("User Count: " + counter);
}
}
synchronized void addUsername(Object user) {
users = (Set) servletContext.getAttribute(USERS_KEY);
if (users == null) {
users = new HashSet();
}
if (log.isDebugEnabled()) {
if (users.contains(user)) {
log.debug("User already logged in, adding anyway...");
}
}
users.add(user);
servletContext.setAttribute(USERS_KEY, users);
incrementUserCounter();
}
synchronized void removeUsername(Object user) {
users = (Set) servletContext.getAttribute(USERS_KEY);
if (users != null) {
users.remove(user);
}
servletContext.setAttribute(USERS_KEY, users);
decrementUserCounter();
}
public void attributeAdded(HttpSessionBindingEvent event) {
if (event.getName().equals(Constants.USER_KEY)) {
addUsername(event.getValue());
}
}
public void attributeRemoved(HttpSessionBindingEvent event) {
if (event.getName().equals(Constants.USER_KEY)) {
removeUsername(event.getValue());
}
}
public void attributeReplaced(HttpSessionBindingEvent event) {
// I don't really care if the user changes their information
}
}