1 package de.webman.template.jsp; 2 3 4 import java.io.IOException ; 5 import java.io.ObjectInputStream ; 6 import java.io.ObjectOutputStream ; 7 import java.io.Serializable ; 8 import java.util.Enumeration ; 9 import java.util.Hashtable ; 10 import java.util.Vector ; 11 import javax.servlet.ServletException ; 12 import javax.servlet.ServletContext ; 13 import javax.servlet.http.HttpSession ; 14 import javax.servlet.http.HttpSessionBindingEvent ; 15 import javax.servlet.http.HttpSessionBindingListener ; 16 import javax.servlet.http.HttpSessionContext ; 17 18 19 26 final class WebManSession 27 implements HttpSession , Serializable { 28 29 private ServletContext context; 30 31 36 WebManSession(ServletContext _context) 37 { 38 context = _context; 39 } 40 41 44 public ServletContext getServletContext() 45 { 46 return context; 47 } 48 49 52 private Hashtable attributes = new Hashtable (); 53 54 55 59 private long creationTime; 60 61 62 65 private String id = null; 66 67 68 71 private static final String INFO = "StandardSession/1.0"; 72 73 74 77 private long lastAccessedTime = creationTime; 78 79 80 85 private int maxInactiveInterval = -1; 86 87 88 91 private boolean isNew = true; 92 93 94 97 private boolean isValid = false; 98 99 100 103 private static HttpSessionContext sessionContext = null; 104 105 106 109 private long thisAccessedTime = creationTime; 110 111 112 114 115 121 public void setCreationTime(long time) { 122 123 this.creationTime = time; 124 this.lastAccessedTime = time; 125 this.thisAccessedTime = time; 126 127 } 128 129 130 133 public String getId() { 134 135 return (this.id); 136 137 } 138 139 140 145 public void setId(String id) { 146 147 this.id = id; 148 149 } 150 151 152 157 public String getInfo() 158 { 159 return INFO; 160 } 161 162 163 169 public long getLastAccessedTime() { 170 171 return (this.lastAccessedTime); 172 173 } 174 175 176 177 185 public int getMaxInactiveInterval() { 186 187 188 return (this.maxInactiveInterval); 189 190 } 191 192 193 200 public void setMaxInactiveInterval(int interval) { 201 202 this.maxInactiveInterval = interval; 203 204 } 205 206 207 211 public HttpSession getSession() { 212 213 return ((HttpSession ) this); 214 215 } 216 217 218 220 221 226 public void access() { 227 228 this.lastAccessedTime = this.thisAccessedTime; 229 this.thisAccessedTime = System.currentTimeMillis(); 230 this.isNew=false; 231 } 232 233 234 238 public void expire() { 239 240 241 Vector results = new Vector (); 243 Enumeration attrs = getAttributeNames(); 244 while (attrs.hasMoreElements()) { 245 String attr = (String ) attrs.nextElement(); 246 results.addElement(attr); 247 } 248 Enumeration names = results.elements(); 249 while (names.hasMoreElements()) { 250 String name = (String ) names.nextElement(); 251 removeAttribute(name); 252 } 253 254 setValid(false); 256 257 } 258 259 260 264 public void recycle() 265 { 266 267 attributes.clear(); 269 creationTime = 0; 270 id = null; 271 lastAccessedTime = 0; 272 maxInactiveInterval = -1; 273 isNew = true; 274 isValid = false; 275 276 } 277 278 279 281 282 285 boolean isValid() { 286 287 return (this.isValid); 288 289 } 290 291 292 297 void setNew(boolean isNew) { 298 299 this.isNew = isNew; 300 301 } 302 303 304 309 void setValid(boolean isValid) { 310 311 this.isValid = isValid; 312 } 313 314 315 317 318 325 public long getCreationTime() { 326 327 return (this.creationTime); 328 329 } 330 331 332 339 public HttpSessionContext getSessionContext() { 340 341 if (sessionContext == null) 342 sessionContext = new SessionContextImpl(); 343 return (sessionContext); 344 345 } 346 347 348 350 351 360 public Object getAttribute(String name) { 361 362 return (attributes.get(name)); 363 364 } 365 366 367 374 public Enumeration getAttributeNames() 375 { 376 return (attributes.keys()); 377 } 378 379 380 392 public Object getValue(String name) { 393 394 return (getAttribute(name)); 395 396 } 397 398 399 409 public String [] getValueNames() { 410 411 412 Vector results = new Vector (); 413 Enumeration attrs = getAttributeNames(); 414 while (attrs.hasMoreElements()) { 415 String attr = (String ) attrs.nextElement(); 416 results.addElement(attr); 417 } 418 String names[] = new String [results.size()]; 419 for (int i = 0; i < names.length; i++) 420 names[i] = (String ) results.elementAt(i); 421 return (names); 422 423 } 424 425 426 432 public void invalidate() { 433 434 expire(); 436 437 } 438 439 440 450 public boolean isNew() { 451 return (this.isNew); 452 453 } 454 455 456 474 public void putValue(String name, Object value) { 475 476 setAttribute(name, value); 477 478 } 479 480 481 495 public void removeAttribute(String name) { 496 497 498 synchronized (attributes) { 499 Object object = attributes.get(name); 500 if (object == null) 501 return; 502 attributes.remove(name); 503 if (object instanceof HttpSessionBindingListener ) { 504 ((HttpSessionBindingListener ) object).valueUnbound 505 (new HttpSessionBindingEvent ((HttpSession ) this, name)); 506 } 507 } 508 509 } 510 511 512 529 public void removeValue(String name) { 530 531 removeAttribute(name); 532 533 } 534 535 536 553 public void setAttribute(String name, Object value) 554 { 555 synchronized (attributes) { 557 removeAttribute(name); 558 attributes.put(name, value); 559 if (value instanceof HttpSessionBindingListener ) 560 ((HttpSessionBindingListener ) value).valueBound 561 (new HttpSessionBindingEvent ((HttpSession ) this, name)); 562 } 563 564 } 565 566 567 569 570 582 private void readObject(ObjectInputStream stream) 583 throws ClassNotFoundException , IOException { 584 585 creationTime = ((Long ) stream.readObject()).longValue(); 587 id = (String ) stream.readObject(); 588 lastAccessedTime = ((Long ) stream.readObject()).longValue(); 589 thisAccessedTime = ((Long ) stream.readObject()).longValue(); 590 maxInactiveInterval = ((Integer ) stream.readObject()).intValue(); 591 isNew = ((Boolean ) stream.readObject()).booleanValue(); 592 isValid = ((Boolean ) stream.readObject()).booleanValue(); 593 594 attributes = (Hashtable ) stream.readObject(); 595 } 596 597 598 622 private void writeObject(ObjectOutputStream stream) throws IOException { 623 624 stream.writeObject(new Long (creationTime)); 626 stream.writeObject(id); 627 stream.writeObject(new Long (lastAccessedTime)); 628 stream.writeObject(new Long (thisAccessedTime)); 629 stream.writeObject(new Integer (maxInactiveInterval)); 630 stream.writeObject(new Boolean (isNew)); 631 stream.writeObject(new Boolean (isValid)); 632 633 if (attributes.size() > 0) { 634 Hashtable results = new Hashtable (attributes.size()); 636 637 for (Enumeration e = attributes.keys(); e.hasMoreElements() ; ) { 638 String key = (String ) e.nextElement(); 639 Object value = attributes.get(key); 640 if (value instanceof Serializable ) { 641 results.put(key, value); 642 } 643 else if (value instanceof HttpSessionBindingListener ) { 649 try { 650 ((HttpSessionBindingListener )value) 651 .valueUnbound(new HttpSessionBindingEvent (this, key)); 652 } catch (Exception f) 653 { 654 f.hashCode(); 656 } 658 } 659 } 660 stream.writeObject(results); 661 } else { 662 stream.writeObject(new Hashtable ()); 663 } 664 } 665 } 666 667 class SessionContextImpl implements HttpSessionContext { 668 669 673 674 public HttpSession getSession(String sessionId) { 675 return null; 676 } 677 678 682 683 public Enumeration getIds() { 684 Vector v = new Vector (); 686 687 return v.elements(); 688 } 689 } 690 | Popular Tags |