1 16 package org.directwebremoting.impl; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import org.directwebremoting.extend.PageNormalizer; 28 import org.directwebremoting.extend.RealScriptSession; 29 import org.directwebremoting.extend.ScriptSessionManager; 30 import org.directwebremoting.util.Logger; 31 32 42 public class DefaultScriptSessionManager implements ScriptSessionManager 43 { 44 47 public RealScriptSession getScriptSession(String id) 48 { 49 maybeCheckTimeouts(); 50 51 synchronized (sessionLock) 52 { 53 DefaultScriptSession scriptSession = (DefaultScriptSession) sessionMap.get(id); 54 if (scriptSession == null) 55 { 56 scriptSession = new DefaultScriptSession(id, this); 57 sessionMap.put(id, scriptSession); 58 } 59 else 60 { 61 scriptSession.updateLastAccessedTime(); 62 } 63 64 return scriptSession; 65 } 66 } 67 68 71 public void setPageForScriptSession(RealScriptSession scriptSession, String page) 72 { 73 String normalizedPage = pageNormalizer.normalizePage(page); 74 synchronized (sessionLock) 75 { 76 Set pageSessions = (Set ) pageSessionMap.get(normalizedPage); 77 if (pageSessions == null) 78 { 79 pageSessions = new HashSet (); 80 pageSessionMap.put(normalizedPage, pageSessions); 81 } 82 83 pageSessions.add(scriptSession); 84 } 85 } 86 87 90 public Collection getScriptSessionsByPage(String page) 91 { 92 String normalizedPage = pageNormalizer.normalizePage(page); 93 synchronized (sessionLock) 94 { 95 Set pageSessions = (Set ) pageSessionMap.get(normalizedPage); 96 if (pageSessions == null) 97 { 98 pageSessions = new HashSet (); 99 } 100 101 Set reply = new HashSet (); 102 reply.addAll(pageSessions); 103 return reply; 104 } 105 } 106 107 110 public Collection getAllScriptSessions() 111 { 112 synchronized (sessionLock) 113 { 114 Set reply = new HashSet (); 115 reply.addAll(sessionMap.values()); 116 return reply; 117 } 118 } 119 120 125 protected void invalidate(RealScriptSession scriptSession) 126 { 127 synchronized (sessionLock) 130 { 131 RealScriptSession removed = (RealScriptSession) sessionMap.remove(scriptSession.getId()); 132 if (!scriptSession.equals(removed)) 133 { 134 log.error("ScriptSession already removed from manager. scriptSession=" + scriptSession + " removed=" + removed); 135 } 136 137 int removeCount = 0; 138 for (Iterator it = pageSessionMap.values().iterator(); it.hasNext();) 139 { 140 Set pageSessions = (Set ) it.next(); 141 boolean isRemoved = pageSessions.remove(scriptSession); 142 143 if (isRemoved) 144 { 145 removeCount++; 146 } 147 } 148 149 if (removeCount != 1) 150 { 151 log.error("DefaultScriptSessionManager.invalidate(): removeCount=" + removeCount + " when invalidating: " + scriptSession); 152 } 153 } 154 } 155 156 161 protected void maybeCheckTimeouts() 162 { 163 long now = System.currentTimeMillis(); 164 if (now - scriptSessionCheckTime > lastSessionCheckAt) 165 { 166 checkTimeouts(); 167 } 168 } 169 170 174 protected void checkTimeouts() 175 { 176 long now = System.currentTimeMillis(); 177 List timeouts = new ArrayList (); 178 179 synchronized (sessionLock) 180 { 181 for (Iterator it = sessionMap.values().iterator(); it.hasNext();) 182 { 183 DefaultScriptSession session = (DefaultScriptSession) it.next(); 184 185 if (session.isInvalidated()) 186 { 187 continue; 188 } 189 190 long age = now - session.getLastAccessedTime(); 191 if (age > scriptSessionTimeout) 192 { 193 timeouts.add(session); 194 } 195 } 196 197 for (Iterator it = timeouts.iterator(); it.hasNext();) 198 { 199 DefaultScriptSession session = (DefaultScriptSession) it.next(); 200 session.invalidate(); 201 } 202 } 203 } 204 205 208 public long getScriptSessionTimeout() 209 { 210 return scriptSessionTimeout; 211 } 212 213 216 public void setScriptSessionTimeout(long timeout) 217 { 218 this.scriptSessionTimeout = timeout; 219 } 220 221 225 public void setPageNormalizer(PageNormalizer pageNormalizer) 226 { 227 this.pageNormalizer = pageNormalizer; 228 } 229 230 233 public void setScriptSessionCheckTime(long scriptSessionCheckTime) 234 { 235 this.scriptSessionCheckTime = scriptSessionCheckTime; 236 } 237 238 241 protected static final long DEFAULT_SESSION_CHECK_TIME = 30000; 242 243 246 protected PageNormalizer pageNormalizer; 247 248 251 protected long scriptSessionTimeout = DEFAULT_TIMEOUT_MILLIS; 252 253 256 protected long scriptSessionCheckTime = DEFAULT_SESSION_CHECK_TIME; 257 258 262 protected long lastSessionCheckAt = System.currentTimeMillis(); 263 264 268 protected final Object sessionLock = new Object (); 269 270 274 protected Map sessionMap = new HashMap (); 275 276 280 protected Map pageSessionMap = new HashMap (); 281 282 285 private static final Logger log = Logger.getLogger(DefaultScriptSessionManager.class); 286 } 287 | Popular Tags |