1 16 package org.apache.cocoon.components.flow; 17 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.apache.avalon.framework.logger.AbstractLogEnabled; 26 import org.apache.commons.collections.iterators.IteratorEnumeration; 27 import org.apache.commons.lang.StringUtils; 28 29 45 public class WebContinuation extends AbstractLogEnabled 46 implements Comparable { 47 48 51 protected Object continuation; 52 53 63 protected WebContinuation parentContinuation; 64 65 70 protected List children = new ArrayList (); 71 72 75 protected String id; 76 77 80 protected String interpreterId; 81 82 88 protected Object userObject; 89 90 95 protected long lastAccessTime; 96 97 102 protected int timeToLive; 103 104 108 protected ContinuationsDisposer disposer; 109 110 113 private Map attributes; 114 115 127 WebContinuation(String id, 128 Object continuation, 129 WebContinuation parentContinuation, 130 int timeToLive, 131 String interpreterId, 132 ContinuationsDisposer disposer) { 133 this.id = id; 134 this.continuation = continuation; 135 this.parentContinuation = parentContinuation; 136 this.updateLastAccessTime(); 137 this.timeToLive = timeToLive; 138 this.interpreterId = interpreterId; 139 this.disposer = disposer; 140 141 if (parentContinuation != null) { 142 this.parentContinuation.children.add(this); 143 } 144 } 145 146 151 public Object getAttribute(String name) { 152 if (this.attributes == null) 153 return null; 154 155 return this.attributes.get(name); 156 } 157 158 164 public void setAttribute(String name, Object value) { 165 if (this.attributes == null) { 166 this.attributes = Collections.synchronizedMap(new HashMap ()); 167 } 168 169 this.attributes.put(name, value); 170 } 171 172 177 public void removeAttribute(String name) { 178 if (this.attributes == null) 179 return; 180 181 this.attributes.remove(name); 182 } 183 184 189 public Enumeration getAttributeNames() { 190 if (this.attributes == null) 191 return new IteratorEnumeration(); 192 193 ArrayList keys = new ArrayList (this.attributes.keySet()); 194 return new IteratorEnumeration(keys.iterator()); 195 } 196 197 202 public Object getContinuation() { 203 updateLastAccessTime(); 204 return continuation; 205 } 206 207 218 public WebContinuation getContinuation(int level) { 219 if (level <= 0) { 220 updateLastAccessTime(); 221 return this; 222 } else if (parentContinuation == null) { 223 return this; 224 } else { 225 return parentContinuation.getContinuation(level - 1); 226 } 227 } 228 229 235 public WebContinuation getParentContinuation() { 236 return parentContinuation; 237 } 238 239 246 public List getChildren() { 247 return children; 248 } 249 250 256 public String getId() { 257 return id; 258 } 259 260 266 public String getInterpreterId() { 267 return interpreterId; 268 } 269 270 276 public long getLastAccessTime() { 277 return lastAccessTime; 278 } 279 280 286 public long getTimeToLive() { 287 return this.timeToLive; 288 } 289 290 295 public void setUserObject(Object obj) { 296 this.userObject = obj; 297 } 298 299 304 public Object getUserObject() { 305 return userObject; 306 } 307 308 315 ContinuationsDisposer getDisposer() { 316 return this.disposer; 317 } 318 319 324 public int hashCode() { 325 return id.hashCode(); 326 } 327 328 334 public boolean equals(Object another) { 335 if (another instanceof WebContinuation) { 336 return id.equals(((WebContinuation) another).id); 337 } 338 return false; 339 } 340 341 352 public int compareTo(Object other) { 353 WebContinuation wk = (WebContinuation) other; 354 return (int) ((lastAccessTime + timeToLive) 355 - (wk.lastAccessTime + wk.timeToLive)); 356 } 357 358 364 public void display() { 365 getLogger().debug("\nWK: Tree" + display(0)); 366 } 367 368 377 protected String display(int depth) { 378 StringBuffer tree = new StringBuffer ("\n"); 379 for (int i = 0; i < depth; i++) { 380 tree.append(" "); 381 } 382 383 tree.append("WK: WebContinuation ") 384 .append(id) 385 .append(" ExpireTime ["); 386 387 if ((lastAccessTime + timeToLive) < System.currentTimeMillis()) { 388 tree.append("Expired"); 389 } else { 390 tree.append(lastAccessTime + timeToLive); 391 } 392 393 tree.append("]"); 394 395 398 int size = children.size(); 399 depth++; 400 401 for (int i = 0; i < size; i++) { 402 tree.append(((WebContinuation) children.get(i)).display(depth)); 403 } 404 405 return tree.toString(); 406 } 407 408 411 protected void updateLastAccessTime() { 412 lastAccessTime = System.currentTimeMillis(); 413 } 414 415 420 public boolean hasExpired() { 421 long currentTime = System.currentTimeMillis(); 422 long expireTime = this.getLastAccessTime() + this.timeToLive; 423 424 return (currentTime > expireTime); 425 } 426 427 430 public void dispose() { 431 if (this.disposer != null) { 433 this.disposer.disposeContinuation(this); 434 } 435 this.continuation = null; 437 } 438 439 442 public boolean disposed() { 443 return this.continuation == null; 444 } 445 446 public boolean interpreterMatches( String interpreterId ) { 447 return StringUtils.equals( this.interpreterId, interpreterId ); 448 } 449 450 public void detachFromParent() { 451 if (getParentContinuation() != null) 452 getParentContinuation().getChildren().remove(this); 453 } 454 } 455 | Popular Tags |