1 7 package org.jboss.web.tomcat.tc5.session; 8 9 import org.apache.catalina.Manager; 10 import org.apache.catalina.Context; 11 import org.apache.catalina.Session; 12 13 import java.io.IOException ; 14 import java.io.Serializable ; 15 import java.util.Enumeration ; 16 import java.util.Map ; 17 18 import org.apache.catalina.util.StringManager; 19 import org.apache.catalina.util.Enumerator; 20 import org.apache.catalina.session.StandardSession; 21 import org.jboss.logging.Logger; 22 23 import javax.servlet.http.*; 24 25 32 abstract class ClusteredSession 33 extends StandardSession 34 { 35 private static final long serialVersionUID = -758573655613558722L; 36 protected static Logger log = Logger.getLogger(ClusteredSession.class); 37 38 42 protected static final String info = "ClusteredSession/1.0"; 43 44 protected int invalidationPolicy; 45 46 49 protected transient boolean isOutdated; 50 51 55 protected int version; 56 57 60 protected static StringManager sm = 61 StringManager.getManager("org.jboss.web.tomcat.session"); 62 63 public ClusteredSession(AbstractJBossManager manager) 64 { 65 super(manager); 66 invalidationPolicy = ((AbstractJBossManager) this.manager).getInvalidateSessionPolicy(); 67 isOutdated = false; 68 version = 0; 69 } 70 71 76 public boolean isOutdated() 77 { 78 return isOutdated; 79 } 80 81 public void setIsOutdated(boolean outdated) 82 { 83 isOutdated = outdated; 84 } 85 86 92 public boolean isNewData(int version) 93 { 94 return (this.version < version) ? true: false; 95 } 96 97 public int getVersion() 98 { 99 return version; 100 } 101 102 107 public int incrementVersion() 108 { 109 return version++; 110 } 111 112 117 public abstract void initAfterLoad(AbstractJBossManager manager); 118 119 124 public void setManager(Manager manager) 125 { 126 127 super.setManager(manager); 128 129 this.manager = manager; 130 } 131 132 135 public abstract void processSessionRepl(); 136 137 140 public abstract void removeMyself(); 141 142 145 public abstract void removeMyselfLocal(); 146 147 150 public Object getAttribute(String name) 151 { 152 153 if (!isValid()) 154 throw new IllegalStateException 155 (sm.getString("clusteredSession.getAttribute.ise")); 156 157 return getAttributeInternal(name); 158 } 159 160 public Enumeration getAttributeNames() 161 { 162 if (!isValid()) 163 throw new IllegalStateException 164 (sm.getString("clusteredSession.getAttributeNames.ise")); 165 166 return (new Enumerator(getAttributesInternal().keySet(), true)); 167 } 168 169 public void setAttribute(String name, Object value) 170 { 171 if (name == null) 173 throw new IllegalArgumentException 174 (sm.getString("clusteredSession.setAttribute.namenull")); 175 176 if (value == null) 178 { 179 removeAttribute(name); 180 return; 181 } 182 183 if (!isValid()) 185 throw new IllegalStateException 186 (sm.getString("clusteredSession.setAttribute.ise")); 187 if ((manager != null) && manager.getDistributable() && 188 !(value instanceof Serializable )) 189 throw new IllegalArgumentException 190 (sm.getString("clusteredSession.setAttribute.iae")); 191 192 HttpSessionBindingEvent event = null; 194 195 if (value instanceof HttpSessionBindingListener) 197 { 198 event = new HttpSessionBindingEvent(getSession(), name, value); 199 try 200 { 201 ((HttpSessionBindingListener) value).valueBound(event); 202 } 203 catch (Throwable t) 204 { 205 manager.getContainer().getLogger().error(sm.getString("standardSession.bindingEvent"), t); 206 } 207 } 208 209 Object unbound = setInternalAttribute(name, value); 211 212 if ((unbound != null) && 214 (unbound instanceof HttpSessionBindingListener)) 215 { 216 try 217 { 218 ((HttpSessionBindingListener) unbound).valueUnbound 219 (new HttpSessionBindingEvent(getSession(), name)); 220 } 221 catch (Throwable t) 222 { 223 manager.getContainer().getLogger().error(sm.getString("standardSession.bindingEvent"), t); 224 } 225 } 226 227 Context context = (Context) manager.getContainer(); 229 Object listeners[] = context.getApplicationEventListeners(); 230 if (listeners == null) 231 return; 232 for (int i = 0; i < listeners.length; i++) 233 { 234 if (!(listeners[i] instanceof HttpSessionAttributeListener)) 235 continue; 236 HttpSessionAttributeListener listener = 237 (HttpSessionAttributeListener) listeners[i]; 238 try 239 { 240 if (unbound != null) 241 { 242 fireContainerEvent(context, 243 "beforeSessionAttributeReplaced", 244 listener); 245 if (event == null) 246 { 247 event = new HttpSessionBindingEvent 248 (getSession(), name, unbound); 249 } 250 listener.attributeReplaced(event); 251 fireContainerEvent(context, 252 "afterSessionAttributeReplaced", 253 listener); 254 } 255 else 256 { 257 fireContainerEvent(context, 258 "beforeSessionAttributeAdded", 259 listener); 260 if (event == null) 261 { 262 event = new HttpSessionBindingEvent 263 (getSession(), name, value); 264 } 265 listener.attributeAdded(event); 266 fireContainerEvent(context, 267 "afterSessionAttributeAdded", 268 listener); 269 } 270 } 271 catch (Throwable t) 272 { 273 try 274 { 275 if (unbound != null) 276 { 277 fireContainerEvent(context, 278 "afterSessionAttributeReplaced", 279 listener); 280 } 281 else 282 { 283 fireContainerEvent(context, 284 "afterSessionAttributeAdded", 285 listener); 286 } 287 } 288 catch (Exception e) 289 { 290 ; 291 } 292 manager.getContainer().getLogger().error(sm.getString("standardSession.attributeEvent"), t); 293 } 294 } 295 296 } 297 298 299 306 public void invalidate() 307 { 308 if (!isValid()) 309 throw new IllegalStateException 310 (sm.getString("clusteredSession.invalidate.ise")); 311 312 boolean notify = true; 314 boolean expireLocally = false; 315 expire(notify, expireLocally); 316 } 317 318 324 public void expire(boolean notify) 325 { 326 boolean expireLocally = true; 327 expire(notify, expireLocally); 328 } 329 330 protected void expire(boolean notify, boolean expireLocally) 331 { 332 if (log.isDebugEnabled()) 333 { 334 log.debug("The session has expired with id: " + id + " is it local? " +expireLocally); 335 } 336 if (expiring) 338 return; 339 340 synchronized (this) 341 { 342 343 if (manager == null) 344 return; 345 346 expiring = true; 347 348 Context context = (Context) manager.getContainer(); 351 Object listeners[] = context.getApplicationLifecycleListeners(); 352 if (notify && (listeners != null)) 353 { 354 HttpSessionEvent event = 355 new HttpSessionEvent(getSession()); 356 for (int i = 0; i < listeners.length; i++) 357 { 358 int j = (listeners.length - 1) - i; 359 if (!(listeners[j] instanceof HttpSessionListener)) 360 continue; 361 HttpSessionListener listener = 362 (HttpSessionListener) listeners[j]; 363 try 364 { 365 fireContainerEvent(context, 366 "beforeSessionDestroyed", 367 listener); 368 listener.sessionDestroyed(event); 369 fireContainerEvent(context, 370 "afterSessionDestroyed", 371 listener); 372 } 373 catch (Throwable t) 374 { 375 try 376 { 377 fireContainerEvent(context, 378 "afterSessionDestroyed", 379 listener); 380 } 381 catch (Exception e) 382 { 383 ; 384 } 385 manager.getContainer().getLogger().error(sm.getString("standardSession.sessionEvent"), t); 386 } 387 } 388 } 389 390 if (notify) 392 { 393 fireSessionEvent(Session.SESSION_DESTROYED_EVENT, null); 394 } 395 396 String keys[] = keys(); 399 for (int i = 0; i < keys.length; i++) 400 removeAttributeInternal(keys[i], notify); 401 402 if (manager != null) 404 { 405 if(expireLocally) 406 { 407 ((AbstractJBossManager) manager).removeLocal(this); 408 } else 409 { 410 ((AbstractJBossManager) manager).remove(this); 411 } 412 } 413 414 accessCount = 0; 416 setValid(false); 417 expiring = false; 418 419 } 420 } 421 422 public void passivate() 423 { 424 } 426 427 public void activate() 428 { 429 } 431 432 434 437 protected String [] keys() 438 { 439 return ((String []) getAttributesInternal().keySet().toArray(EMPTY_ARRAY)); 440 } 441 442 protected void removeAttributeInternal(String name, boolean notify) 443 { 444 445 Object value = removeJBossInternalAttribute(name); 447 448 if (!notify || (value == null)) 450 { 451 return; 452 } 453 454 HttpSessionBindingEvent event = null; 456 if (value instanceof HttpSessionBindingListener) 457 { 458 event = new HttpSessionBindingEvent(getSession(), name, value); 459 ((HttpSessionBindingListener) value).valueUnbound(event); 460 } 461 462 Context context = (Context) manager.getContainer(); 464 Object listeners[] = context.getApplicationEventListeners(); 465 if (listeners == null) 466 return; 467 for (int i = 0; i < listeners.length; i++) 468 { 469 if (!(listeners[i] instanceof HttpSessionAttributeListener)) 470 continue; 471 HttpSessionAttributeListener listener = 472 (HttpSessionAttributeListener) listeners[i]; 473 try 474 { 475 fireContainerEvent(context, 476 "beforeSessionAttributeRemoved", 477 listener); 478 if (event == null) 479 { 480 event = new HttpSessionBindingEvent 481 (getSession(), name, value); 482 } 483 listener.attributeRemoved(event); 484 fireContainerEvent(context, 485 "afterSessionAttributeRemoved", 486 listener); 487 } 488 catch (Throwable t) 489 { 490 try 491 { 492 fireContainerEvent(context, 493 "afterSessionAttributeRemoved", 494 listener); 495 } 496 catch (Exception e) 497 { 498 ; 499 } 500 manager.getContainer().getLogger().error(sm.getString("standardSession.attributeEvent"), t); 501 } 502 } 503 504 } 505 506 protected Object getAttributeInternal(String name) 507 { 508 return getJBossInternalAttribute(name); 509 } 510 511 protected Map getAttributesInternal() 512 { 513 return getJBossInternalAttributes(); 514 } 515 516 protected Object setInternalAttribute(String name, Object value) 517 { 518 return setJBossInternalAttribute(name, value); 519 } 520 521 protected abstract Object getJBossInternalAttribute(String name); 523 524 protected abstract Object removeJBossInternalAttribute(String name); 525 526 protected abstract Map getJBossInternalAttributes(); 527 528 protected abstract Object setJBossInternalAttribute(String name, Object value); 529 530 protected void writeObject(java.io.ObjectOutputStream out) 532 throws IOException 533 { 534 synchronized (attributes) 535 { 536 out.defaultWriteObject(); 537 } 538 } 539 540 protected void readObject(java.io.ObjectInputStream in) 541 throws IOException , ClassNotFoundException 542 { 543 in.defaultReadObject(); 544 } 545 546 } 547 | Popular Tags |