1 package org.appfuse.webapp.listener; 2 3 import java.util.HashSet ; 4 import java.util.Set ; 5 6 import javax.servlet.ServletContext ; 7 import javax.servlet.ServletContextEvent ; 8 import javax.servlet.ServletContextListener ; 9 import javax.servlet.http.HttpSessionAttributeListener ; 10 import javax.servlet.http.HttpSessionBindingEvent ; 11 12 import org.acegisecurity.context.HttpSessionContextIntegrationFilter; 13 import org.acegisecurity.context.SecurityContext; 14 import org.acegisecurity.context.SecurityContextHolder; 15 import org.acegisecurity.Authentication; 16 import org.acegisecurity.AuthenticationTrustResolver; 17 import org.acegisecurity.AuthenticationTrustResolverImpl; 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.appfuse.model.User; 21 22 23 31 public class UserCounterListener implements ServletContextListener , HttpSessionAttributeListener { 32 public static final String COUNT_KEY = "userCounter"; 33 public static final String USERS_KEY = "userNames"; 34 public static final String EVENT_KEY = HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY; 35 private final transient Log log = LogFactory.getLog(UserCounterListener.class); 36 private transient ServletContext servletContext; 37 private int counter; 38 private Set users; 39 40 public synchronized void contextInitialized(ServletContextEvent sce) { 41 servletContext = sce.getServletContext(); 42 servletContext.setAttribute((COUNT_KEY), Integer.toString(counter)); 43 } 44 45 public synchronized void contextDestroyed(ServletContextEvent event) { 46 servletContext = null; 47 users = null; 48 counter = 0; 49 } 50 51 synchronized void incrementUserCounter() { 52 counter = 53 Integer.parseInt((String ) servletContext.getAttribute(COUNT_KEY)); 54 counter++; 55 servletContext.setAttribute(COUNT_KEY, Integer.toString(counter)); 56 57 if (log.isDebugEnabled()) { 58 log.debug("User Count: " + counter); 59 } 60 } 61 62 synchronized void decrementUserCounter() { 63 int counter = 64 Integer.parseInt((String ) servletContext.getAttribute(COUNT_KEY)); 65 counter--; 66 67 if (counter < 0) { 68 counter = 0; 69 } 70 71 servletContext.setAttribute(COUNT_KEY, Integer.toString(counter)); 72 73 if (log.isDebugEnabled()) { 74 log.debug("User Count: " + counter); 75 } 76 } 77 78 synchronized void addUsername(Object user) { 79 users = (Set ) servletContext.getAttribute(USERS_KEY); 80 81 if (users == null) { 82 users = new HashSet (); 83 } 84 85 if (log.isDebugEnabled()) { 86 if (users.contains(user)) { 87 log.debug("User already logged in, adding anyway..."); 88 } 89 } 90 91 users.add(user); 92 servletContext.setAttribute(USERS_KEY, users); 93 incrementUserCounter(); 94 } 95 96 synchronized void removeUsername(Object user) { 97 users = (Set ) servletContext.getAttribute(USERS_KEY); 98 99 if (users != null) { 100 users.remove(user); 101 } 102 103 servletContext.setAttribute(USERS_KEY, users); 104 decrementUserCounter(); 105 } 106 107 111 public void attributeAdded(HttpSessionBindingEvent event) { 112 log.debug("event.name: " + event.getName()); 113 if (event.getName().equals(EVENT_KEY) && !isAnonymous()) { 114 SecurityContext securityContext = (SecurityContext) event.getValue(); 115 User user = (User) securityContext.getAuthentication().getPrincipal(); 116 addUsername(user); 117 } 118 } 119 120 private boolean isAnonymous() { 121 AuthenticationTrustResolver resolver = new AuthenticationTrustResolverImpl(); 122 SecurityContext ctx = SecurityContextHolder.getContext(); 123 if (ctx != null) { 124 Authentication auth = ctx.getAuthentication(); 125 return resolver.isAnonymous(auth); 126 } 127 return true; 128 } 129 130 134 public void attributeRemoved(HttpSessionBindingEvent event) { 135 if (event.getName().equals(EVENT_KEY) && !isAnonymous()) { 136 SecurityContext securityContext = (SecurityContext) event.getValue(); 137 Authentication auth = securityContext.getAuthentication(); 138 if (auth != null && (auth.getPrincipal() instanceof User)) { 139 User user = (User) auth.getPrincipal(); 140 removeUsername(user); 141 } 142 } 143 } 144 145 150 public void attributeReplaced(HttpSessionBindingEvent event) { 151 if (event.getName().equals(EVENT_KEY) && !isAnonymous()) { 152 SecurityContext securityContext = (SecurityContext) event.getValue(); 153 if (securityContext.getAuthentication() != null) { 154 User user = (User) securityContext.getAuthentication().getPrincipal(); 155 addUsername(user); 156 } 157 } 158 } 159 } 160 | Popular Tags |