1 23 24 package org.infoglue.cms.util; 25 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.Date ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import javax.servlet.http.HttpSession ; 35 import javax.servlet.http.HttpSessionEvent ; 36 import javax.servlet.http.HttpSessionListener ; 37 38 import org.infoglue.cms.applications.databeans.SessionInfoBean; 39 import org.infoglue.cms.security.InfoGlueAuthenticationFilter; 40 import org.infoglue.cms.security.InfoGluePrincipal; 41 42 48 49 public class CmsSessionContextListener implements HttpSessionListener 50 { 51 public static final Map sessions = Collections.synchronizedMap(new HashMap ()); 52 53 private static int activeSessions = 0; 54 55 public void sessionCreated(HttpSessionEvent se) 56 { 57 activeSessions++; 59 synchronized (sessions) 60 { 61 sessions.put(se.getSession().getId(), se.getSession()); 62 } 63 } 64 65 public void sessionDestroyed(HttpSessionEvent se) 66 { 67 if(activeSessions > 0) 69 activeSessions--; 70 71 synchronized(sessions) 72 { 73 sessions.remove(se.getSession().getId()); 74 } 75 } 76 77 public static int getActiveSessions() 78 { 79 return activeSessions; 80 } 81 82 static public List getSessionInfoBeanList() 83 { 84 List stiList = new ArrayList (); 85 86 synchronized(sessions) 88 { 89 Iterator iter = sessions.keySet().iterator(); 90 while (iter.hasNext()) 91 { 92 String s = (String ) iter.next(); 93 HttpSession sess = (HttpSession ) sessions.get(s); 94 95 SessionInfoBean sib = new SessionInfoBean(); 96 97 InfoGluePrincipal principal = (InfoGluePrincipal)sess.getAttribute(InfoGlueAuthenticationFilter.INFOGLUE_FILTER_USER); 98 if(principal == null) 99 principal = (InfoGluePrincipal)sess.getAttribute("infogluePrincipal"); 100 101 if(principal != null) 102 { 103 sib.setPrincipal(principal); 104 sib.setLastAccessedDate(new Date (sess.getLastAccessedTime())); 105 106 stiList.add(sib); 107 } 108 } 109 } 110 111 return stiList; 112 } 113 114 } | Popular Tags |