1 22 package org.jboss.web.tomcat.tc6.session; 23 24 import org.jboss.aspects.patterns.observable.Observer; 25 import org.jboss.aspects.patterns.observable.Subject; 26 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 import javax.servlet.http.HttpSessionActivationListener ; 36 37 71 class FieldBasedClusteredSession 72 extends JBossCacheClusteredSession implements Observer 73 { 74 75 private static final long serialVersionUID = 8347544395334247623L; 76 77 80 protected static final String info = "FieldBasedClusteredSession/1.0"; 81 82 protected transient Map attributes_ = Collections.synchronizedMap(new HashMap ()); 83 84 public FieldBasedClusteredSession(JBossCacheManager manager) 85 { 86 super(manager); 87 } 88 89 91 92 100 public void recycle() 101 { 102 super.recycle(); 103 104 attributes_.clear(); 105 } 106 107 110 public String toString() 111 { 112 113 StringBuffer sb = new StringBuffer (); 114 sb.append("FieldBasedClusteredSession["); 115 sb.append(super.toString()); 116 sb.append("]"); 117 return (sb.toString()); 118 119 } 120 121 129 public void removeMyself() 130 { 131 proxy_.removeSession(realId); 135 } 136 137 public void removeMyselfLocal() 138 { 139 proxy_.removePojosLocal(realId); 147 proxy_.removeSessionLocal(realId); 148 } 149 150 152 158 protected void populateAttributes() 159 { 160 Map excluded = removeExcludedAttributes(attributes_); 162 163 Set keys = proxy_.getPojoKeys(realId); 164 Set oldKeys = new HashSet (attributes_.keySet()); 165 166 boolean hasListener = false; 169 170 if (keys != null) 171 { 172 oldKeys.removeAll(keys); 174 for (Iterator it = keys.iterator(); it.hasNext(); ) 175 { 176 String name = (String ) it.next(); 177 178 Object oldAttrib = null; 179 Object newAttrib = proxy_.getPojo(realId, name); 180 if (newAttrib != null) 181 { 182 oldAttrib = attributes_.put(name, newAttrib); 183 184 if (oldAttrib != newAttrib) 185 { 186 proxy_.addObserver(this, newAttrib); 189 190 proxy_.removeObserver(this, oldAttrib); } 193 194 if (newAttrib instanceof HttpSessionActivationListener ) 196 hasListener = true; 197 } 198 else 199 { 200 202 oldAttrib = attributes_.remove(name); 203 proxy_.removeObserver(this, oldAttrib); 206 } 207 } 208 } 209 210 hasActivationListener = hasListener ? Boolean.TRUE : Boolean.FALSE; 211 212 for (Iterator it = oldKeys.iterator(); it.hasNext(); ) 215 { 216 Object oldAttrib = attributes_.remove(it.next()); 217 proxy_.removeObserver(this, oldAttrib); 218 } 219 220 if (excluded != null) 222 attributes.putAll(excluded); 223 } 224 225 protected Object getJBossInternalAttribute(String name) 226 { 227 Object result = attributes_.get(name); 229 230 234 if (isGetDirty(result)) 237 { 238 sessionAttributesDirty(); 239 } 240 241 return result; 242 } 243 244 249 protected boolean isMutable(Object attribute) 250 { 251 boolean pojo = (attribute instanceof Subject); 252 boolean mutable = (!pojo && super.isMutable(attribute)); 253 return mutable; 254 } 255 256 protected Object removeJBossInternalAttribute(String name, boolean localCall, boolean localOnly) 257 { 258 if (localCall && !replicationExcludes.contains(name)) 260 { 261 if (localOnly) 262 proxy_.removePojoLocal(realId, name); 263 else 264 proxy_.removePojo(realId, name); 265 266 sessionAttributesDirty(); 267 } 268 Object result = attributes_.remove(name); 269 if(result == null) 270 { 271 log.warn("removeJBossInternalAttribute(): null value to remove with key: "+ name); 272 return null; 273 } 274 proxy_.removeObserver(this, result); 275 276 return result; 277 } 278 279 protected Map getJBossInternalAttributes() 280 { 281 return attributes_; 282 } 283 284 protected Set getJBossInternalKeys() 285 { 286 return attributes_.keySet(); 287 } 288 289 292 protected String [] keys() 293 { 294 return ((String []) getJBossInternalKeys().toArray(EMPTY_ARRAY)); 295 } 296 297 301 protected boolean canAttributeBeReplicated(Object attribute) 302 { 303 return (attribute == null || Util.checkPojoType(attribute)); 304 } 305 306 314 protected Object setJBossInternalAttribute(String key, Object value) 315 { 316 Object oldVal = null; 317 if (!replicationExcludes.contains(key)) 318 { 319 oldVal = proxy_.setPojo(realId, key, value); 320 if(oldVal != null) 321 { proxy_.removeObserver(this, oldVal); 323 } 324 325 if(value != null) 326 { 327 if( value instanceof Map || value instanceof Collection ) 329 { 330 value = proxy_.getPojo(realId, key); 332 } 333 334 proxy_.addObserver(this, value); 336 } 337 338 sessionAttributesDirty(); 340 } 341 342 oldVal = attributes_.put(key, value); 344 345 return oldVal; 346 } 347 348 354 public void fireChange(Subject subject) 355 { 356 if(log.isTraceEnabled()) 358 { 359 log.trace("fireChange(): subject has changed: " +subject); 360 } 361 sessionAttributesDirty(); 362 } 363 } 364 | Popular Tags |