1 7 package org.jboss.web.tomcat.tc5.session; 8 9 import org.apache.catalina.Manager; 10 import org.jboss.ha.httpsession.interfaces.SerializableHttpSession; 11 import java.beans.PropertyChangeSupport ; 12 import java.io.IOException ; 13 import java.security.Principal ; 14 import java.util.ArrayList ; 15 import java.util.Enumeration ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import javax.servlet.http.HttpSession ; 19 import javax.servlet.http.HttpSessionContext ; 20 import org.apache.catalina.util.Enumerator; 21 import org.apache.catalina.util.StringManager; 22 import org.apache.catalina.session.StandardSession; 23 import org.jboss.metadata.WebMetaData; 24 import org.jboss.logging.Logger; 25 26 45 class ClusteredSessionCMP 46 extends StandardSession 47 implements SerializableHttpSession 48 { 49 private static final long serialVersionUID = -758573655613558722L; 50 private static Logger log = Logger.getLogger(ClusteredSessionCMP.class); 51 52 54 55 58 protected static final String info = "ClusteredSessionCMP/1.0"; 59 60 61 64 private transient Manager manager = null; 65 66 67 70 private static StringManager sm = 71 StringManager.getManager("org.jboss.web.tomcat.session"); 72 73 protected transient boolean isSessionModifiedSinceLastSave = true; 74 protected transient int replicationType = WebMetaData.REPLICATION_TYPE_SYNC; 75 protected transient boolean replicationTypeAlreadySet = false; 76 77 public ClusteredSessionCMP(JBossManagerCMP manager) 78 { 79 super(manager); 80 this.manager = manager; 81 } 82 83 88 protected void initAfterLoad(JBossManagerCMP manager) 89 { 90 setManager(manager); 91 expiring = false; 92 listeners = new ArrayList (); 93 notes = new HashMap (); 94 support = new PropertyChangeSupport (this); 95 this.replicationType = manager.getReplicationType (); 96 this.replicationTypeAlreadySet = false; 97 98 99 this.activate(); 101 } 102 104 108 public boolean areAttributesModified(SerializableHttpSession previousVersion) 109 { 110 if (this == previousVersion) 112 { 113 if (log.isDebugEnabled()) 114 log.debug ("Are current session " + this.id + " attributes modified? : " + isSessionModifiedSinceLastSave); 115 return isSessionModifiedSinceLastSave; 116 } 117 else 118 { 119 return true; 120 } 121 } 122 123 126 public long getContentCreationTime() 127 { 128 return this.getCreationTime(); 129 } 130 131 134 public long getContentLastAccessTime() 135 { 136 return this.getLastAccessedTime(); 137 } 138 139 public void sessionHasBeenStored () 140 { 141 isSessionModifiedSinceLastSave = false; 142 replicationTypeAlreadySet = false; 143 } 144 145 public int getReplicationTypeForSession () 146 { 147 return this.replicationType; 148 } 149 150 public void setReplicationTypeForSession (int type) 151 { 152 this.replicationType = type; 153 } 154 155 public boolean isReplicationTypeAlreadySet() 156 { 157 return replicationTypeAlreadySet; 158 } 159 161 162 168 public void setCreationTime(long time) 169 { 170 super.setCreationTime(time); 171 sessionIsDirty(); 172 } 173 174 175 183 public void setPrincipal(Principal principal) 184 { 185 186 Principal oldPrincipal = this.principal; 187 this.principal = principal; 188 support.firePropertyChange("principal", oldPrincipal, this.principal); 189 190 if ( (oldPrincipal != null && ! oldPrincipal.equals(principal) ) || 191 (oldPrincipal == null && principal != null) ) 192 sessionIsDirty(); 193 194 } 195 196 201 public void setManager(Manager manager) { 202 203 super.setManager(manager); 204 205 this.manager=manager; 206 } 207 208 210 211 214 public String toString() 215 { 216 217 StringBuffer sb = new StringBuffer (); 218 sb.append("ClusteredSessionCMP["); 219 sb.append(id); 220 sb.append("]"); 221 return (sb.toString()); 222 223 } 224 225 226 228 229 230 232 public void access() 233 { 234 super.access(); 235 236 if (!((JBossManagerCMP)manager).isUseLocalCache()) 239 { 240 this.sessionIsDirty(); 241 } 242 } 243 244 253 public Object getAttribute(String name) 254 { 255 256 if (!isValid) 257 throw new IllegalStateException 258 (sm.getString("clusteredSession.getAttribute.ise")); 259 260 synchronized (attributes) 261 { 262 Object result = attributes.get(name); 263 264 if (result != null) 265 { 266 int invalidationPolicy = ((AbstractJBossManager)manager).getInvalidateSessionPolicy(); 267 268 if (invalidationPolicy == WebMetaData.SESSION_INVALIDATE_SET_AND_GET) 269 { 270 sessionIsDirty(); 271 } 272 else if (invalidationPolicy == WebMetaData.SESSION_INVALIDATE_SET_AND_NON_PRIMITIVE_GET) 273 { 274 if ( ! (result instanceof String || 275 result instanceof Integer || 276 result instanceof Long || 277 result instanceof Byte || 278 result instanceof Short || 279 result instanceof Float || 280 result instanceof Double || 281 result instanceof Character || 282 result instanceof Boolean ) 283 ) 284 { 285 sessionIsDirty(); 286 } 287 } 288 } 289 290 return result; 291 } 292 293 } 294 295 296 310 public void removeAttribute(String name, boolean notify) 311 { 312 boolean found = false; 314 synchronized (attributes) 315 { 316 found = attributes.containsKey(name); 317 if (found) 318 { 319 sessionIsDirty (); 321 } 322 else 323 { 324 return; 325 } 326 } 327 328 super.removeAttribute(name, notify); 329 } 330 331 340 public Enumeration getAttributeNames() 341 { 342 if (!isValid()) 343 { 344 throw new IllegalStateException 345 (sm.getString("standardSession.getAttributeNames.ise")); 346 } 347 348 synchronized (attributes) 349 { 350 HashSet keys = new HashSet (attributes.keySet()); 351 Enumeration iter = new Enumerator(keys); 352 return iter; 353 } 354 } 355 356 373 public void setAttribute(String name, Object value) 374 { 375 376 super.setAttribute(name, value); 377 378 sessionIsDirty (); 380 } 381 382 383 388 protected void log(String message) 389 { 390 log.debug(message); 391 } 392 393 394 400 protected void log(String message, Throwable throwable) 401 { 402 log.error(message, throwable); 403 } 404 405 406 protected void writeObject(java.io.ObjectOutputStream out) 407 throws IOException 408 { 409 synchronized (attributes) 410 { 411 out.defaultWriteObject(); 412 } 413 } 414 protected void readObject(java.io.ObjectInputStream in) 415 throws IOException , ClassNotFoundException 416 { 417 in.defaultReadObject(); 418 } 419 420 protected void sessionIsDirty () 421 { 422 isSessionModifiedSinceLastSave = true; 424 } 425 } 426 427 428 430 431 441 442 final class ClusteredSessionContext implements HttpSessionContext 443 { 444 445 446 private HashMap dummy = new HashMap (); 447 448 456 public Enumeration getIds() 457 { 458 459 return (new Enumerator(dummy)); 460 461 } 462 463 464 474 public HttpSession getSession(String id) 475 { 476 477 return (null); 478 479 } 480 481 482 } 483 | Popular Tags |