1 31 32 package org.opencms.main; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.CmsUser; 36 import org.opencms.util.CmsStringUtil; 37 import org.opencms.util.CmsUUID; 38 39 import java.util.ArrayList ; 40 import java.util.ConcurrentModificationException ; 41 import java.util.HashSet ; 42 import java.util.Hashtable ; 43 import java.util.Iterator ; 44 import java.util.List ; 45 import java.util.Map ; 46 import java.util.Set ; 47 48 import javax.servlet.http.HttpSessionEvent ; 49 50 import org.apache.commons.collections.Buffer; 51 import org.apache.commons.collections.BufferUtils; 52 import org.apache.commons.collections.buffer.BoundedFifoBuffer; 53 import org.apache.commons.logging.Log; 54 55 76 public class CmsSessionManager { 77 78 79 private static final Log LOG = CmsLog.getLog(CmsSessionManager.class); 80 81 82 private int m_sessionCountCurrent; 83 84 85 private int m_sessionCountTotal; 86 87 88 private Map m_sessions; 89 90 93 protected CmsSessionManager() { 94 95 super(); 96 m_sessions = new Hashtable (); 98 } 99 100 107 public Buffer getBroadcastQueue(String sessionId) { 108 109 if (getSessionInfo(sessionId) == null) { 110 return BufferUtils.synchronizedBuffer(new BoundedFifoBuffer(CmsSessionInfo.QUEUE_SIZE)); 112 } 113 return getSessionInfo(sessionId).getBroadcastQueue(); 114 } 115 116 121 public int getSessionCountAuthenticated() { 122 123 return m_sessions.size(); 124 } 125 126 131 public int getSessionCountCurrent() { 132 133 return m_sessionCountCurrent; 134 } 135 136 141 public int getSessionCountTotal() { 142 143 return m_sessionCountTotal; 144 } 145 146 154 public CmsSessionInfo getSessionInfo(String sessionId) { 155 156 return (CmsSessionInfo)m_sessions.get(sessionId); 157 } 158 159 164 public List getSessionInfos() { 165 166 List result = new ArrayList (); 167 synchronized (m_sessions) { 168 Iterator i = getConcurrentSessionIterator(); 169 while (i.hasNext()) { 170 CmsSessionInfo sessionInfo = (CmsSessionInfo)m_sessions.get(i.next()); 171 if (sessionInfo != null) { 172 result.add(sessionInfo); 174 } 175 } 176 } 177 return result; 178 } 179 180 192 public List getSessionInfos(CmsUUID userId) { 193 194 List userSessions = new ArrayList (); 195 synchronized (m_sessions) { 196 Iterator i = getConcurrentSessionIterator(); 197 while (i.hasNext()) { 198 String key = (String )i.next(); 199 CmsSessionInfo sessionInfo = (CmsSessionInfo)m_sessions.get(key); 200 if (userId.equals(sessionInfo.getUser().getId())) { 201 userSessions.add(sessionInfo); 202 } 203 } 204 } 205 return userSessions; 206 } 207 208 215 public void sendBroadcast(CmsObject cms, String message) { 216 217 if (CmsStringUtil.isEmptyOrWhitespaceOnly(message)) { 218 return; 220 } 221 CmsBroadcast broadcast = new CmsBroadcast(cms.getRequestContext().currentUser(), message); 223 synchronized (m_sessions) { 225 Iterator i = getConcurrentSessionIterator(); 226 while (i.hasNext()) { 227 CmsSessionInfo sessionInfo = (CmsSessionInfo)m_sessions.get(i.next()); 228 if (sessionInfo != null) { 229 sessionInfo.getBroadcastQueue().add(broadcast); 231 } 232 } 233 } 234 } 235 236 244 public void sendBroadcast(CmsObject cms, String message, CmsUser user) { 245 246 if (CmsStringUtil.isEmptyOrWhitespaceOnly(message)) { 247 return; 249 } 250 CmsBroadcast broadcast = new CmsBroadcast(cms.getRequestContext().currentUser(), message); 252 List userSessions = getSessionInfos(user.getId()); 253 Iterator i = userSessions.iterator(); 254 synchronized (m_sessions) { 256 while (i.hasNext()) { 257 CmsSessionInfo sessionInfo = (CmsSessionInfo)m_sessions.get(i.next()); 258 if (sessionInfo != null) { 259 sessionInfo.getBroadcastQueue().add(broadcast); 261 } 262 } 263 } 264 } 265 266 274 public void sendBroadcast(CmsObject cms, String message, String sessionId) { 275 276 if (CmsStringUtil.isEmptyOrWhitespaceOnly(message)) { 277 return; 279 } 280 CmsSessionInfo sessionInfo = (CmsSessionInfo)m_sessions.get(sessionId); 282 if (sessionInfo != null) { 283 sessionInfo.getBroadcastQueue().add(new CmsBroadcast(cms.getRequestContext().currentUser(), message)); 285 } 286 } 287 288 291 public String toString() { 292 293 StringBuffer output = new StringBuffer (); 294 synchronized (m_sessions) { 295 Iterator i = getConcurrentSessionIterator(); 296 output.append("[CmsSessions]:\n"); 297 while (i.hasNext()) { 298 String key = (String )i.next(); 299 CmsSessionInfo sessionInfo = (CmsSessionInfo)m_sessions.get(key); 300 output.append(key); 301 output.append(" : "); 302 output.append(sessionInfo.getUser().toString()); 303 output.append('\n'); 304 } 305 } 306 return output.toString(); 307 } 308 309 314 protected void addSessionInfo(CmsSessionInfo sessionInfo) { 315 316 m_sessions.put(sessionInfo.getSessionId(), sessionInfo); 317 } 318 319 327 protected synchronized void sessionCreated(HttpSessionEvent event) { 328 329 m_sessionCountCurrent = (m_sessionCountCurrent <= 0) ? 1 : (m_sessionCountCurrent + 1); 330 m_sessionCountTotal++; 331 if (LOG.isInfoEnabled()) { 332 LOG.info(Messages.get().getBundle().key( 333 Messages.LOG_SESSION_CREATED_2, 334 new Integer (m_sessionCountTotal), 335 new Integer (m_sessionCountCurrent))); 336 } 337 if (LOG.isDebugEnabled()) { 338 LOG.debug(Messages.get().getBundle().key(Messages.LOG_SESSION_CREATED_1, event.getSession().getId())); 339 } 340 } 341 342 350 protected synchronized void sessionDestroyed(HttpSessionEvent event) { 351 352 m_sessionCountCurrent = (m_sessionCountCurrent <= 0) ? 0 : (m_sessionCountCurrent - 1); 353 354 String sessionId = event.getSession().getId(); 355 CmsSessionInfo sessionInfo = (CmsSessionInfo)m_sessions.get(sessionId); 357 CmsUUID userId = CmsUUID.getNullUUID(); 358 if (sessionInfo != null) { 359 userId = sessionInfo.getUser().getId(); 360 } 361 synchronized (m_sessions) { 362 m_sessions.remove(sessionId); 363 } 364 if (!userId.isNullUUID() && getSessionInfos(userId).size() == 0) { 365 OpenCms.getLockManager().removeTempLocks(userId); 367 } 368 369 if (LOG.isInfoEnabled()) { 370 LOG.info(Messages.get().getBundle().key( 371 Messages.LOG_SESSION_DESTROYED_2, 372 new Integer (m_sessionCountTotal), 373 new Integer (m_sessionCountCurrent))); 374 } 375 if (LOG.isDebugEnabled()) { 376 LOG.debug(Messages.get().getBundle().key(Messages.LOG_SESSION_DESTROYED_1, event.getSession().getId())); 377 } 378 } 379 380 384 protected void validateSessionInfos() { 385 386 synchronized (m_sessions) { 387 Iterator i = getConcurrentSessionIterator(); 388 while (i.hasNext()) { 389 String sessionId = (String )i.next(); 390 CmsSessionInfo sessionInfo = (CmsSessionInfo)m_sessions.get(sessionId); 391 if (sessionInfo != null) { 392 if (sessionInfo.isExpired()) { 394 try { 396 m_sessions.remove(sessionId); 397 } catch (ConcurrentModificationException ex) { 398 } 400 } 401 } 402 } 403 } 404 } 405 406 412 private Iterator getConcurrentSessionIterator() { 413 414 Set keySet; 415 int count = 0; 416 do { 417 keySet = new HashSet (m_sessions.size()); 418 try { 419 keySet.addAll(m_sessions.keySet()); 420 } catch (ConcurrentModificationException e) { 421 count++; 423 keySet = null; 424 } 425 } while ((keySet == null) && (count < 5)); 426 if (keySet == null) { 427 keySet = new HashSet (); 429 } 430 return keySet.iterator(); 431 } 432 } | Popular Tags |