1 7 package org.jboss.web.tomcat.tc5.session; 8 9 import org.jboss.metadata.WebMetaData; 10 11 import java.beans.PropertyChangeSupport ; 12 import java.io.Serializable ; 13 import java.security.Principal ; 14 import java.util.*; 15 16 37 class AttributeBasedClusteredSession 38 extends ClusteredSession implements Serializable 39 { 40 static final long serialVersionUID = -5625209785550936713L; 41 44 protected static final String info = "AttributeBasedClusteredSession/1.0"; 45 46 private transient boolean isSessionModifiedSinceLastSave_; 47 private transient JBossCacheService proxy_; 48 private transient Map attrModifiedMap_; 50 private transient Map attrRemovedMap_; 52 private static int REMOVE = 0; private static int MODIFY = 1; 54 private transient Map attributes_; 55 56 public AttributeBasedClusteredSession(AbstractJBossManager manager) 57 { 58 super(manager); 59 initAfterLoad(manager); 60 } 61 62 68 public void initAfterLoad(AbstractJBossManager manager) 69 { 70 if (this.proxy_ == null) 72 { 73 setManager(manager); 74 listeners = new ArrayList(); 75 notes = new HashMap(); 76 support = new PropertyChangeSupport (this); 77 expiring = false; 78 attributes_ = Collections.synchronizedMap(new HashMap()); 79 attrModifiedMap_ = new HashMap(); 80 attrRemovedMap_ = new HashMap(); 81 82 isOutdated = false; 84 85 proxy_ = ((JBossCacheManager) manager).getCacheService(); 86 87 if (proxy_ == null) 89 { 90 throw new RuntimeException ("SessionBasedClusteredSession: Cache service is null."); 91 } 92 93 this.activate(); 95 } 96 populateAttributes(); 98 } 99 100 103 protected void populateAttributes() 104 { 105 Map map = proxy_.getAttributes(id); 106 if (map.size() != 0) attributes_ = map; 107 } 108 109 116 public void setCreationTime(long time) 117 { 118 super.setCreationTime(time); 119 sessionIsDirty(); 120 } 121 122 123 131 public void setPrincipal(Principal principal) 132 { 133 Principal oldPrincipal = this.principal; 134 this.principal = principal; 135 support.firePropertyChange("principal", oldPrincipal, this.principal); 136 137 if ((oldPrincipal != null && !oldPrincipal.equals(principal)) || 138 (oldPrincipal == null && principal != null)) 139 sessionIsDirty(); 140 141 } 142 143 147 public String toString() 148 { 149 150 StringBuffer sb = new StringBuffer (); 151 sb.append("AttributeBasedClusteredSession["); 152 sb.append(id); 153 sb.append("]"); 154 return (sb.toString()); 155 156 } 157 158 161 public synchronized void processSessionRepl() 162 { 163 if (!isSessionDirty()) 164 { 165 if (log.isDebugEnabled()) 166 { 167 log.debug("processSessionRepl(): session is not dirty. No need to replicate."); 168 } 169 return; 170 } 171 if (log.isDebugEnabled()) 174 { 175 log.debug("processSessionRepl(): session is dirty. Will increment version from: " + 176 getVersion() + " and replicate."); 177 } 178 this.incrementVersion(); 179 proxy_.putSession(id, this); 180 181 { 184 Set set = attrModifiedMap_.keySet(); 185 Iterator it = set.iterator(); 186 while (it.hasNext()) 187 { 188 Object key = it.next(); 189 proxy_.putAttribute(id, (String ) key, attrModifiedMap_.get(key)); 190 } 191 } 192 193 { 195 Set set = attrRemovedMap_.keySet(); 196 Iterator it = set.iterator(); 197 while (it.hasNext()) 198 { 199 Object key = it.next(); 200 proxy_.removeAttribute(id, (String ) key); 201 } 202 } 203 204 clearAttrChangedMap(); 205 isSessionModifiedSinceLastSave_ = false; 206 } 207 208 public void removeMyself() 209 { 210 proxy_.removeSession(id); 212 if (attributes_ != null) 213 attributes_.clear(); 214 } 215 216 public void removeMyselfLocal() 217 { 218 proxy_.removeAttributeLocal(id); 220 proxy_.removeSessionLocal(id); 221 if (attributes_ != null) 222 attributes_.clear(); 223 } 224 225 227 public void access() 228 { 229 super.access(); 230 if (invalidationPolicy == WebMetaData.SESSION_INVALIDATE_ACCESS) 233 { 234 this.sessionIsDirty(); 235 } 236 } 237 238 protected Object getJBossInternalAttribute(String name) 240 { 241 Object result = null; 243 result = attributes_.get(name); 245 246 if (result != null) 247 { 248 int invalidationPolicy = ((AbstractJBossManager) this.manager).getInvalidateSessionPolicy(); 249 250 if (invalidationPolicy == WebMetaData.SESSION_INVALIDATE_SET_AND_GET) 251 { 252 attributeChanged(name, result, MODIFY); 253 } 254 else if (invalidationPolicy == WebMetaData.SESSION_INVALIDATE_SET_AND_NON_PRIMITIVE_GET) 255 { 256 if (!(result instanceof String || 257 result instanceof Integer || 258 result instanceof Long || 259 result instanceof Byte || 260 result instanceof Short || 261 result instanceof Float || 262 result instanceof Double || 263 result instanceof Character || 264 result instanceof Boolean ) 265 ) 266 { 267 attributeChanged(name, result, MODIFY); 268 } 269 } 270 } 271 return result; 272 } 273 274 protected Object removeJBossInternalAttribute(String name) 275 { 276 Object result = attributes_.remove(name); 277 attributeChanged(name, result, REMOVE); 278 return result; 279 } 280 281 protected Map getJBossInternalAttributes() 282 { 283 return attributes_; 284 } 285 286 protected Set getJBossInternalKeys() 287 { 288 return attributes_.keySet(); 289 } 290 291 294 protected String [] keys() 295 { 296 return ((String []) getJBossInternalKeys().toArray(EMPTY_ARRAY)); 297 } 298 299 protected Object setJBossInternalAttribute(String key, Object value) 300 { 301 attributes_.put(key, value); 302 attributeChanged(key, value, MODIFY); 303 return value; 304 } 305 306 protected void sessionIsDirty() 307 { 308 isSessionModifiedSinceLastSave_ = true; 310 } 311 312 public boolean isSessionDirty() 313 { 314 return isSessionModifiedSinceLastSave_; 316 } 317 318 protected synchronized void attributeChanged(Object key, Object value, int op) 319 { 320 if (op == MODIFY) 321 { 322 if (attrRemovedMap_.containsKey(key)) 323 { 324 attrRemovedMap_.remove(key); 325 } 326 attrModifiedMap_.put(key, value); 327 } 328 else if (op == REMOVE) 329 { 330 if (attrModifiedMap_.containsKey(key)) 331 { 332 attrModifiedMap_.remove(key); 333 } 334 attrRemovedMap_.put(key, value); 335 } 336 sessionIsDirty(); 337 } 338 339 protected synchronized void clearAttrChangedMap() 340 { 341 attrRemovedMap_.clear(); 342 attrModifiedMap_.clear(); 343 } 344 } 345 | Popular Tags |