KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > CmsSessionContextListener


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23
24 package org.infoglue.cms.util;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.servlet.http.HttpSession JavaDoc;
35 import javax.servlet.http.HttpSessionEvent JavaDoc;
36 import javax.servlet.http.HttpSessionListener JavaDoc;
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 /**
43  * This class keeps track of all sessions created / removed so we can see
44  * how many users a site / cms has right now.
45  *
46  * @author mattias
47  */

48
49 public class CmsSessionContextListener implements HttpSessionListener JavaDoc
50 {
51     public static final Map JavaDoc sessions = Collections.synchronizedMap(new HashMap JavaDoc());
52     
53     private static int activeSessions = 0;
54     
55     public void sessionCreated(HttpSessionEvent JavaDoc se)
56     {
57         //System.out.println("Session created..");
58
activeSessions++;
59         synchronized (sessions)
60         {
61             sessions.put(se.getSession().getId(), se.getSession());
62         }
63     }
64     
65     public void sessionDestroyed(HttpSessionEvent JavaDoc se)
66     {
67         //System.out.println("Session destroyed..");
68
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 JavaDoc getSessionInfoBeanList()
83     {
84         List JavaDoc stiList = new ArrayList JavaDoc();
85
86         //System.out.println("Sessions:" + sessions.size());
87
synchronized(sessions)
88         {
89             Iterator JavaDoc iter = sessions.keySet().iterator();
90             while (iter.hasNext())
91             {
92                 String JavaDoc s = (String JavaDoc) iter.next();
93                 HttpSession JavaDoc sess = (HttpSession JavaDoc) 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 JavaDoc(sess.getLastAccessedTime()));
105                     
106                     stiList.add(sib);
107                 }
108             }
109         }
110     
111         return stiList;
112     }
113
114 }
Popular Tags