1 package org.apache.turbine.services.session; 2 3 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Hashtable ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import javax.servlet.http.HttpSession ; 25 26 import org.apache.turbine.om.security.User; 27 import org.apache.turbine.services.TurbineBaseService; 28 29 51 public class TurbineSessionService 52 extends TurbineBaseService 53 implements SessionService 54 { 55 56 private Map activeSessions; 57 58 63 public Collection getActiveSessions() 64 { 65 synchronized (activeSessions) 68 { 69 return new ArrayList (activeSessions.values()); 70 } 71 } 72 73 79 public void addSession(HttpSession session) 80 { 81 activeSessions.put(session.getId(), session); 82 } 83 84 90 public void removeSession(HttpSession session) 91 { 92 activeSessions.remove(session.getId()); 93 } 94 95 106 public boolean isUserLoggedIn(User user) 107 { 108 return getActiveUsers().contains(user); 109 } 110 111 118 public Collection getActiveUsers() 119 { 120 Collection users; 121 synchronized (activeSessions) 122 { 123 users = new ArrayList ((int) (activeSessions.size() * 0.7)); 126 for (Iterator i = activeSessions.values().iterator(); i.hasNext();) 127 { 128 User u = getUserFromSession((HttpSession ) i.next()); 129 if (u != null && u.hasLoggedIn()) 130 { 131 users.add(u); 132 } 133 } 134 } 135 136 return users; 137 } 138 139 145 public User getUserFromSession(HttpSession session) 146 { 147 return (User) session.getAttribute(User.SESSION_KEY); 148 } 149 150 156 public HttpSession getSession(String sessionId) 157 { 158 return (HttpSession ) this.activeSessions.get(sessionId); 159 } 160 161 168 public Collection getSessionsForUser(User user) 169 { 170 Collection sessions = new ArrayList (); 171 synchronized (activeSessions) 172 { 173 for (Iterator i = activeSessions.values().iterator(); i.hasNext();) 174 { 175 HttpSession session = (HttpSession ) i.next(); 176 User u = this.getUserFromSession(session); 177 if (user.equals(u)) 178 { 179 sessions.add(session); 180 } 181 } 182 } 183 184 return sessions; 185 } 186 187 188 190 193 public void init() 194 { 195 this.activeSessions = new Hashtable (); 196 197 setInit(true); 198 } 199 200 203 public void shutdown() 204 { 205 this.activeSessions = null; 206 207 setInit(false); 208 } 209 210 } 211 | Popular Tags |