1 23 24 package com.rift.coad.lib.security.user; 26 27 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.HashMap ; 31 32 import org.apache.log4j.Logger; 34 35 import com.rift.coad.lib.configuration.Configuration; 37 import com.rift.coad.lib.configuration.ConfigurationFactory; 38 import com.rift.coad.lib.security.ThreadsPermissionContainer; 39 import com.rift.coad.lib.security.ThreadPermissionSession; 40 import com.rift.coad.lib.security.UserSession; 41 import com.rift.coad.lib.thread.ThreadStateMonitor; 42 43 48 public class UserSessionManager { 49 50 53 public class UserSessionIndex { 54 private Map sessionIdMap = new HashMap (); 56 57 58 61 public UserSessionIndex() { 62 63 } 64 65 66 71 public synchronized void putUser(UserSession userSession) { 72 sessionIdMap.put(userSession.getSessionId(),userSession); 73 } 74 75 76 83 public synchronized UserSession getSessionById(String sessionId) 84 throws UserException { 85 try { 86 if (!sessionIdMap.containsKey(sessionId)) { 87 throw new UserException("No session exists for session id."); 88 } 89 UserSession userSession = 90 (UserSession)sessionIdMap.get(sessionId); 91 if (userSession.isExpired()) { 92 sessionIdMap.remove(userSession.getSessionId()); 93 throw new UserException("No session exists for user."); 94 } 95 userSession.touch(); 96 return userSession; 97 } catch (SecurityException ex) { 98 throw ex; 99 } catch (Exception ex) { 100 throw new UserException("Failed to retrieve the user session : " 101 + ex.getMessage(),ex); 102 } 103 } 104 105 106 111 public synchronized void removeSessionById(String sessionId) { 112 if (!sessionIdMap.containsKey(sessionId)) { 113 return; 114 } 115 UserSession userSession = 116 (UserSession)sessionIdMap.get(sessionId); 117 sessionIdMap.remove(userSession.getSessionId()); 118 } 119 } 120 121 122 127 public class TimeoutThread extends Thread 128 { 129 private final static String DELAY = "session_timeout_delay"; 131 private final static long DELAY_DEFAULT = 60 * 1000; 132 133 private ThreadStateMonitor stateMonitor = null; 135 136 141 public TimeoutThread() throws UserException { 142 try { 143 Configuration config = ConfigurationFactory.getInstance(). 144 getConfig(this.getClass()); 145 long delay = config.getLong(DELAY,DELAY_DEFAULT); 146 stateMonitor = new ThreadStateMonitor(delay); 147 } catch (Exception ex) { 148 log.error("Failed to init the timeout thread : " + 149 ex.getMessage(),ex); 150 throw new UserException("Failed to init the timeout thread : " + 151 ex.getMessage(),ex); 152 } 153 } 154 155 156 159 public void run() { 160 while(!stateMonitor.isTerminated()) { 161 stateMonitor.monitor(); 162 Map sessionMapCopy = new HashMap (); 163 synchronized(userSessionIndex) { 164 sessionMapCopy.putAll(userSessionIndex.sessionIdMap); 165 } 166 for (Iterator iter = sessionMapCopy.keySet().iterator(); 167 iter.hasNext() && !stateMonitor.isTerminated();) { 168 UserSession userSession = (UserSession)sessionMapCopy.get( 169 iter.next()); 170 if (userSession.isExpired()) { 174 synchronized(userSessionIndex) { 175 if (userSession.isExpired()) { 176 userSessionIndex.sessionIdMap.remove( 177 userSession.getSessionId()); 178 } 179 } 180 } 181 } 182 } 183 } 184 185 186 189 public void terminte() { 190 stateMonitor.terminate(true); 191 } 192 } 193 194 private Logger log = 196 Logger.getLogger(UserSessionManager.class.getName()); 197 198 private UserSessionIndex userSessionIndex = new UserSessionIndex(); 200 private TimeoutThread timeoutThread = null; 201 private ThreadsPermissionContainer permissions = null; 202 private UserStoreManager userStoreManager = null; 203 204 210 public UserSessionManager(ThreadsPermissionContainer permissions, 211 UserStoreManager userStoreManager) { 212 this.permissions = permissions; 213 this.userStoreManager = userStoreManager; 214 } 215 216 217 224 public void initSessionForUser(String username) throws UserException { 225 try { 226 UserSession user = userStoreManager.getUserInfo(username); 227 Long threadId = new Long (Thread.currentThread().getId()); 228 permissions.putSession(threadId,new ThreadPermissionSession( 229 threadId,user)); 230 userSessionIndex.putUser(user); 231 } catch (Exception ex) { 232 throw new UserException("Failed to init the session for [" + 233 username + "] : " + ex.getMessage(),ex); 234 } 235 } 236 237 238 245 public void initSessionForUser(UserSession user) throws UserException { 246 try { 247 Long threadId = new Long (Thread.currentThread().getId()); 248 permissions.putSession(threadId,new ThreadPermissionSession( 249 threadId,user)); 250 userSessionIndex.putUser(user); 251 } catch (Exception ex) { 252 throw new UserException("Failed to init the session for [" + 253 user.getName() + "] : " + ex.getMessage(),ex); 254 } 255 } 256 257 258 266 public void addUserSession(UserSession user) throws UserException { 267 try { 268 userSessionIndex.putUser(user); 269 } catch (Exception ex) { 270 throw new UserException("Failed to init the session for [" + 271 user.getName() + "] : " + ex.getMessage(),ex); 272 } 273 } 274 275 276 283 public UserSession getSessionById(String sessionId) throws UserException { 284 return userSessionIndex.getSessionById(sessionId); 285 } 286 287 288 293 public void startCleanup() throws UserException { 294 timeoutThread = new TimeoutThread(); 295 timeoutThread.start(); 296 } 297 298 299 302 public void shutdown() { 303 try { 304 timeoutThread.terminte(); 305 timeoutThread.join(); 306 } catch (Exception ex) { 307 log.error("Failed to wait for the time out thread to terminate " + 308 "because : " + ex.getMessage(),ex); 309 } 310 } 311 } 312 | Popular Tags |