1 22 package org.jboss.web.tomcat.tc6.session; 23 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.Map.Entry; 30 31 55 class AttributeBasedClusteredSession 56 extends JBossCacheClusteredSession 57 { 58 static final long serialVersionUID = -5625209785550936713L; 59 62 protected static final String info = "AttributeBasedClusteredSession/1.0"; 63 64 private transient Map attrModifiedMap_ = new HashMap (); 66 private transient Map attrRemovedMap_ = new HashMap (); 69 private static final int REMOVE = 0; private static final int MODIFY = 1; 71 private transient Map attributes_ = Collections.synchronizedMap(new HashMap ()); 73 74 public AttributeBasedClusteredSession(JBossCacheManager manager) 75 { 76 super(manager); 77 } 78 79 81 82 90 public void recycle() 91 { 92 super.recycle(); 93 94 attributes_.clear(); 95 clearAttrChangedMaps(); 96 } 97 98 101 public String toString() 102 { 103 104 StringBuffer sb = new StringBuffer (); 105 sb.append("AttributeBasedClusteredSession["); 106 sb.append(super.toString()); 107 sb.append("]"); 108 return (sb.toString()); 109 110 } 111 112 115 public synchronized void processSessionRepl() 116 { 117 if (log.isTraceEnabled()) 120 { 121 log.trace("processSessionRepl(): session is dirty. Will increment " + 122 "version from: " + getVersion() + " and replicate."); 123 } 124 this.incrementVersion(); 125 proxy_.putSession(realId, this); 126 127 129 if (getSessionAttributesDirty()) 130 { 131 int modCount = attrModifiedMap_.size(); 133 if (modCount == 1) 134 { 135 for (Iterator it = attrModifiedMap_.entrySet().iterator(); it.hasNext(); ) 136 { 137 Map.Entry entry = (Entry) it.next(); 138 proxy_.putAttribute(realId, (String ) entry.getKey(), entry.getValue()); 139 } 140 } 141 else if (modCount > 0) 142 { 143 proxy_.putAttribute(realId, attrModifiedMap_); 146 } 147 148 if (attrRemovedMap_.size() > 0) 150 { 151 for (Iterator it = attrRemovedMap_.keySet().iterator(); it.hasNext(); ) 152 { 153 proxy_.removeAttribute(realId, (String ) it.next()); 154 } 155 } 156 157 clearAttrChangedMaps(); 158 } 159 160 sessionAttributesDirty = false; 161 sessionMetadataDirty = false; 162 163 updateLastReplicated(); 164 } 165 166 public void removeMyself() 167 { 168 proxy_.removeSession(realId); 170 } 171 172 public void removeMyselfLocal() 173 { 174 proxy_.removeAttributesLocal(realId); 180 proxy_.removeSessionLocal(realId); 181 } 182 183 185 188 protected void populateAttributes() 189 { 190 Map map = proxy_.getAttributes(realId); 191 192 Map excluded = removeExcludedAttributes(attributes_); 194 if (excluded != null) 195 map.putAll(excluded); 196 197 attributes_ = Collections.synchronizedMap(map); 198 attrModifiedMap_.clear(); 199 attrRemovedMap_.clear(); 200 } 201 202 protected Object getJBossInternalAttribute(String name) 203 { 204 Object result = attributes_.get(name); 205 206 if (isGetDirty(result) && !replicationExcludes.contains(name)) 209 { 210 attributeChanged(name, result, MODIFY); 211 } 212 213 return result; 214 } 215 216 protected Object removeJBossInternalAttribute(String name, 217 boolean localCall, 218 boolean localOnly) 219 { 220 Object result = attributes_.remove(name); 221 if (localCall && !replicationExcludes.contains(name)) 222 attributeChanged(name, result, REMOVE); 223 return result; 224 } 225 226 protected Map getJBossInternalAttributes() 227 { 228 return attributes_; 229 } 230 231 protected Set getJBossInternalKeys() 232 { 233 return attributes_.keySet(); 234 } 235 236 239 protected String [] keys() 240 { 241 return ((String []) getJBossInternalKeys().toArray(EMPTY_ARRAY)); 242 } 243 244 protected Object setJBossInternalAttribute(String key, Object value) 245 { 246 Object old = attributes_.put(key, value); 247 if (!replicationExcludes.contains(key)) 248 attributeChanged(key, value, MODIFY); 249 return old; 250 } 251 252 protected synchronized void attributeChanged(Object key, Object value, int op) 253 { 254 if (op == MODIFY) 255 { 256 if (attrRemovedMap_.containsKey(key)) 257 { 258 attrRemovedMap_.remove(key); 259 } 260 attrModifiedMap_.put(key, value); 261 } 262 else if (op == REMOVE) 263 { 264 if (attrModifiedMap_.containsKey(key)) 265 { 266 attrModifiedMap_.remove(key); 267 } 268 attrRemovedMap_.put(key, value); 269 } 270 sessionAttributesDirty(); 271 } 272 273 protected synchronized void clearAttrChangedMaps() 274 { 275 attrRemovedMap_.clear(); 276 attrModifiedMap_.clear(); 277 } 278 } 279 | Popular Tags |