1 16 package org.directwebremoting.impl; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 import java.util.SortedSet ; 27 import java.util.TreeSet ; 28 29 import org.directwebremoting.ScriptBuffer; 30 import org.directwebremoting.extend.MarshallException; 31 import org.directwebremoting.extend.RealScriptSession; 32 import org.directwebremoting.extend.ScriptConduit; 33 import org.directwebremoting.util.Logger; 34 35 44 public class DefaultScriptSession implements RealScriptSession 45 { 46 51 protected DefaultScriptSession(String id, DefaultScriptSessionManager manager) 52 { 53 this.id = id; 54 if (id == null) 55 { 56 throw new IllegalArgumentException ("id can not be null"); 57 } 58 59 this.manager = manager; 60 this.creationTime = System.currentTimeMillis(); 61 this.lastAccessedTime = creationTime; 62 } 63 64 67 public Object getAttribute(String name) 68 { 69 checkNotInvalidated(); 70 synchronized (attributes) 71 { 72 return attributes.get(name); 73 } 74 } 75 76 79 public void setAttribute(String name, Object value) 80 { 81 checkNotInvalidated(); 82 synchronized (attributes) 83 { 84 attributes.put(name, value); 85 } 86 } 87 88 91 public void removeAttribute(String name) 92 { 93 checkNotInvalidated(); 94 synchronized (attributes) 95 { 96 attributes.remove(name); 97 } 98 } 99 100 103 public Iterator getAttributeNames() 104 { 105 checkNotInvalidated(); 106 synchronized (attributes) 107 { 108 Set keys = Collections.unmodifiableSet(attributes.keySet()); 109 return keys.iterator(); 110 } 111 } 112 113 116 public void invalidate() 117 { 118 synchronized (invalidLock) 119 { 120 invalidated = true; 121 manager.invalidate(this); 122 } 123 } 124 125 128 public boolean isInvalidated() 129 { 130 synchronized (invalidLock) 131 { 132 return invalidated; 133 } 134 } 135 136 139 public String getId() 140 { 141 return id; 142 } 143 144 147 public long getCreationTime() 148 { 149 checkNotInvalidated(); 150 return creationTime; 151 } 152 153 156 public long getLastAccessedTime() 157 { 158 synchronized (invalidLock) 159 { 160 return lastAccessedTime; 167 } 168 } 169 170 173 public void addScript(ScriptBuffer script) 174 { 175 checkNotInvalidated(); 176 177 if (script == null) 178 { 179 throw new NullPointerException ("null script"); 180 } 181 182 184 synchronized (scriptLock) 186 { 187 if (conduits.size() == 0) 188 { 189 scripts.add(script); 191 } 193 else 194 { 195 boolean written = false; 197 for (Iterator it = conduits.iterator(); !written && it.hasNext();) 198 { 199 ScriptConduit conduit = (ScriptConduit) it.next(); 200 try 201 { 202 written = conduit.addScript(script); 203 } 205 catch (Exception ex) 206 { 207 it.remove(); 208 log.debug("Failed to write to ScriptConduit, removing from list: " + conduit); 209 } 210 } 211 212 if (!written) 213 { 214 scripts.add(script); 215 } 217 } 218 } 219 } 220 221 224 public void addScriptConduit(ScriptConduit conduit) throws IOException 225 { 226 checkNotInvalidated(); 227 228 synchronized (scriptLock) 229 { 230 writeScripts(conduit); 231 conduits.add(conduit); 232 233 } 235 } 236 237 240 public void writeScripts(ScriptConduit conduit) throws IOException 241 { 242 checkNotInvalidated(); 243 244 synchronized (scriptLock) 245 { 246 for (Iterator it = scripts.iterator(); it.hasNext();) 247 { 248 ScriptBuffer script = (ScriptBuffer) it.next(); 249 250 try 251 { 252 if (conduit.addScript(script)) 253 { 254 it.remove(); 256 } 257 else 258 { 259 break; 261 } 262 } 263 catch (MarshallException ex) 264 { 265 log.warn("Failed to convert data. Dropping Javascript: " + script, ex); 266 } 267 } 268 } 269 } 270 271 274 public void removeScriptConduit(ScriptConduit conduit) 275 { 276 checkNotInvalidated(); 277 278 synchronized (scriptLock) 279 { 280 boolean removed = conduits.remove(conduit); 281 if (!removed) 282 { 283 log.debug("Removing unattached ScriptConduit: " + conduit); 284 debug(); 285 } 286 } 287 288 } 290 291 294 public Object getScriptLock() 295 { 296 return scriptLock; 297 } 298 299 302 public boolean hasWaitingScripts() 303 { 304 synchronized (scriptLock) 305 { 306 return !scripts.isEmpty(); 307 } 308 } 309 310 313 protected void updateLastAccessedTime() 314 { 315 synchronized (invalidLock) 316 { 317 lastAccessedTime = System.currentTimeMillis(); 318 } 319 } 320 321 326 protected void checkNotInvalidated() 327 { 328 synchronized (invalidLock) 329 { 330 long now = System.currentTimeMillis(); 331 long age = now - lastAccessedTime; 332 if (age > manager.getScriptSessionTimeout()) 333 { 334 invalidate(); 335 } 336 337 if (invalidated) 338 { 339 log.debug("ScriptSession has been invalidated."); 340 } 341 } 342 } 343 344 347 private void debug() 348 { 349 if (log.isDebugEnabled()) 350 { 351 log.debug("Known ScriptConduits:"); 352 for (Iterator it = conduits.iterator(); it.hasNext();) 353 { 354 ScriptConduit c = (ScriptConduit) it.next(); 355 log.debug("- " + c); 356 } 357 } 358 } 359 360 363 public int hashCode() 364 { 365 return 572 + id.hashCode(); 366 } 367 368 371 public boolean equals(Object obj) 372 { 373 if (obj == null) 374 { 375 return false; 376 } 377 378 if (obj == this) 379 { 380 return true; 381 } 382 383 if (!this.getClass().equals(obj.getClass())) 384 { 385 return false; 386 } 387 388 DefaultScriptSession that = (DefaultScriptSession) obj; 389 390 if (!this.id.equals(that.id)) 391 { 392 return false; 393 } 394 395 return true; 396 } 397 398 401 public String toString() 402 { 403 return "DefaultScriptSession[id=" + id + "]"; 404 } 405 406 410 protected final Map attributes = Collections.synchronizedMap(new HashMap ()); 411 412 416 protected long lastAccessedTime = 0L; 417 418 422 protected boolean invalidated = false; 423 424 428 private final Object invalidLock = new Object (); 429 430 434 protected final SortedSet conduits = new TreeSet (); 435 436 440 protected final List scripts = new ArrayList (); 441 442 446 private final Object scriptLock = new Object (); 447 448 452 protected final String id; 453 454 458 protected final long creationTime; 459 460 464 protected final DefaultScriptSessionManager manager; 465 466 469 private static final Logger log = Logger.getLogger(DefaultScriptSession.class); 470 } 471 | Popular Tags |