package org.jboss.web.tomcat.tc5.session;
import org.jboss.metadata.WebMetaData;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.security.Principal;
import java.util.*;
class AttributeBasedClusteredSession
extends ClusteredSession implements Serializable
{
static final long serialVersionUID = -5625209785550936713L;
protected static final String info = "AttributeBasedClusteredSession/1.0";
private transient boolean isSessionModifiedSinceLastSave_;
private transient JBossCacheService proxy_;
private transient Map attrModifiedMap_;
private transient Map attrRemovedMap_;
private static int REMOVE = 0; private static int MODIFY = 1;
private transient Map attributes_;
public AttributeBasedClusteredSession(AbstractJBossManager manager)
{
super(manager);
initAfterLoad(manager);
}
public void initAfterLoad(AbstractJBossManager manager)
{
if (this.proxy_ == null)
{
setManager(manager);
listeners = new ArrayList();
notes = new HashMap();
support = new PropertyChangeSupport(this);
expiring = false;
attributes_ = Collections.synchronizedMap(new HashMap());
attrModifiedMap_ = new HashMap();
attrRemovedMap_ = new HashMap();
isOutdated = false;
proxy_ = ((JBossCacheManager) manager).getCacheService();
if (proxy_ == null)
{
throw new RuntimeException("SessionBasedClusteredSession: Cache service is null.");
}
this.activate();
}
populateAttributes();
}
protected void populateAttributes()
{
Map map = proxy_.getAttributes(id);
if (map.size() != 0) attributes_ = map;
}
public void setCreationTime(long time)
{
super.setCreationTime(time);
sessionIsDirty();
}
public void setPrincipal(Principal principal)
{
Principal oldPrincipal = this.principal;
this.principal = principal;
support.firePropertyChange("principal", oldPrincipal, this.principal);
if ((oldPrincipal != null && !oldPrincipal.equals(principal)) ||
(oldPrincipal == null && principal != null))
sessionIsDirty();
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append("AttributeBasedClusteredSession[");
sb.append(id);
sb.append("]");
return (sb.toString());
}
public synchronized void processSessionRepl()
{
if (!isSessionDirty())
{
if (log.isDebugEnabled())
{
log.debug("processSessionRepl(): session is not dirty. No need to replicate.");
}
return;
}
if (log.isDebugEnabled())
{
log.debug("processSessionRepl(): session is dirty. Will increment version from: " +
getVersion() + " and replicate.");
}
this.incrementVersion();
proxy_.putSession(id, this);
{
Set set = attrModifiedMap_.keySet();
Iterator it = set.iterator();
while (it.hasNext())
{
Object key = it.next();
proxy_.putAttribute(id, (String) key, attrModifiedMap_.get(key));
}
}
{
Set set = attrRemovedMap_.keySet();
Iterator it = set.iterator();
while (it.hasNext())
{
Object key = it.next();
proxy_.removeAttribute(id, (String) key);
}
}
clearAttrChangedMap();
isSessionModifiedSinceLastSave_ = false;
}
public void removeMyself()
{
proxy_.removeSession(id);
if (attributes_ != null)
attributes_.clear();
}
public void removeMyselfLocal()
{
proxy_.removeAttributeLocal(id);
proxy_.removeSessionLocal(id);
if (attributes_ != null)
attributes_.clear();
}
public void access()
{
super.access();
if (invalidationPolicy == WebMetaData.SESSION_INVALIDATE_ACCESS)
{
this.sessionIsDirty();
}
}
protected Object getJBossInternalAttribute(String name)
{
Object result = null;
result = attributes_.get(name);
if (result != null)
{
int invalidationPolicy = ((AbstractJBossManager) this.manager).getInvalidateSessionPolicy();
if (invalidationPolicy == WebMetaData.SESSION_INVALIDATE_SET_AND_GET)
{
attributeChanged(name, result, MODIFY);
}
else if (invalidationPolicy == WebMetaData.SESSION_INVALIDATE_SET_AND_NON_PRIMITIVE_GET)
{
if (!(result instanceof String ||
result instanceof Integer ||
result instanceof Long ||
result instanceof Byte ||
result instanceof Short ||
result instanceof Float ||
result instanceof Double ||
result instanceof Character ||
result instanceof Boolean)
)
{
attributeChanged(name, result, MODIFY);
}
}
}
return result;
}
protected Object removeJBossInternalAttribute(String name)
{
Object result = attributes_.remove(name);
attributeChanged(name, result, REMOVE);
return result;
}
protected Map getJBossInternalAttributes()
{
return attributes_;
}
protected Set getJBossInternalKeys()
{
return attributes_.keySet();
}
protected String[] keys()
{
return ((String[]) getJBossInternalKeys().toArray(EMPTY_ARRAY));
}
protected Object setJBossInternalAttribute(String key, Object value)
{
attributes_.put(key, value);
attributeChanged(key, value, MODIFY);
return value;
}
protected void sessionIsDirty()
{
isSessionModifiedSinceLastSave_ = true;
}
public boolean isSessionDirty()
{
return isSessionModifiedSinceLastSave_;
}
protected synchronized void attributeChanged(Object key, Object value, int op)
{
if (op == MODIFY)
{
if (attrRemovedMap_.containsKey(key))
{
attrRemovedMap_.remove(key);
}
attrModifiedMap_.put(key, value);
}
else if (op == REMOVE)
{
if (attrModifiedMap_.containsKey(key))
{
attrModifiedMap_.remove(key);
}
attrRemovedMap_.put(key, value);
}
sessionIsDirty();
}
protected synchronized void clearAttrChangedMap()
{
attrRemovedMap_.clear();
attrModifiedMap_.clear();
}
}