package org.jboss.web.tomcat.tc5.session;
import org.apache.catalina.Manager;
import org.apache.catalina.Context;
import org.apache.catalina.Session;
import java.io.IOException;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Map;
import org.apache.catalina.util.StringManager;
import org.apache.catalina.util.Enumerator;
import org.apache.catalina.session.StandardSession;
import org.jboss.logging.Logger;
import javax.servlet.http.*;
abstract class ClusteredSession
extends StandardSession
{
private static final long serialVersionUID = -758573655613558722L;
protected static Logger log = Logger.getLogger(ClusteredSession.class);
protected static final String info = "ClusteredSession/1.0";
protected int invalidationPolicy;
protected transient boolean isOutdated;
protected int version;
protected static StringManager sm =
StringManager.getManager("org.jboss.web.tomcat.session");
public ClusteredSession(AbstractJBossManager manager)
{
super(manager);
invalidationPolicy = ((AbstractJBossManager) this.manager).getInvalidateSessionPolicy();
isOutdated = false;
version = 0;
}
public boolean isOutdated()
{
return isOutdated;
}
public void setIsOutdated(boolean outdated)
{
isOutdated = outdated;
}
public boolean isNewData(int version)
{
return (this.version < version) ? true: false;
}
public int getVersion()
{
return version;
}
public int incrementVersion()
{
return version++;
}
public abstract void initAfterLoad(AbstractJBossManager manager);
public void setManager(Manager manager)
{
super.setManager(manager);
this.manager = manager;
}
public abstract void processSessionRepl();
public abstract void removeMyself();
public abstract void removeMyselfLocal();
public Object getAttribute(String name)
{
if (!isValid())
throw new IllegalStateException
(sm.getString("clusteredSession.getAttribute.ise"));
return getAttributeInternal(name);
}
public Enumeration getAttributeNames()
{
if (!isValid())
throw new IllegalStateException
(sm.getString("clusteredSession.getAttributeNames.ise"));
return (new Enumerator(getAttributesInternal().keySet(), true));
}
public void setAttribute(String name, Object value)
{
if (name == null)
throw new IllegalArgumentException
(sm.getString("clusteredSession.setAttribute.namenull"));
if (value == null)
{
removeAttribute(name);
return;
}
if (!isValid())
throw new IllegalStateException
(sm.getString("clusteredSession.setAttribute.ise"));
if ((manager != null) && manager.getDistributable() &&
!(value instanceof Serializable))
throw new IllegalArgumentException
(sm.getString("clusteredSession.setAttribute.iae"));
HttpSessionBindingEvent event = null;
if (value instanceof HttpSessionBindingListener)
{
event = new HttpSessionBindingEvent(getSession(), name, value);
try
{
((HttpSessionBindingListener) value).valueBound(event);
}
catch (Throwable t)
{
manager.getContainer().getLogger().error(sm.getString("standardSession.bindingEvent"), t);
}
}
Object unbound = setInternalAttribute(name, value);
if ((unbound != null) &&
(unbound instanceof HttpSessionBindingListener))
{
try
{
((HttpSessionBindingListener) unbound).valueUnbound
(new HttpSessionBindingEvent(getSession(), name));
}
catch (Throwable t)
{
manager.getContainer().getLogger().error(sm.getString("standardSession.bindingEvent"), t);
}
}
Context context = (Context) manager.getContainer();
Object listeners[] = context.getApplicationEventListeners();
if (listeners == null)
return;
for (int i = 0; i < listeners.length; i++)
{
if (!(listeners[i] instanceof HttpSessionAttributeListener))
continue;
HttpSessionAttributeListener listener =
(HttpSessionAttributeListener) listeners[i];
try
{
if (unbound != null)
{
fireContainerEvent(context,
"beforeSessionAttributeReplaced",
listener);
if (event == null)
{
event = new HttpSessionBindingEvent
(getSession(), name, unbound);
}
listener.attributeReplaced(event);
fireContainerEvent(context,
"afterSessionAttributeReplaced",
listener);
}
else
{
fireContainerEvent(context,
"beforeSessionAttributeAdded",
listener);
if (event == null)
{
event = new HttpSessionBindingEvent
(getSession(), name, value);
}
listener.attributeAdded(event);
fireContainerEvent(context,
"afterSessionAttributeAdded",
listener);
}
}
catch (Throwable t)
{
try
{
if (unbound != null)
{
fireContainerEvent(context,
"afterSessionAttributeReplaced",
listener);
}
else
{
fireContainerEvent(context,
"afterSessionAttributeAdded",
listener);
}
}
catch (Exception e)
{
;
}
manager.getContainer().getLogger().error(sm.getString("standardSession.attributeEvent"), t);
}
}
}
public void invalidate()
{
if (!isValid())
throw new IllegalStateException
(sm.getString("clusteredSession.invalidate.ise"));
boolean notify = true;
boolean expireLocally = false;
expire(notify, expireLocally);
}
public void expire(boolean notify)
{
boolean expireLocally = true;
expire(notify, expireLocally);
}
protected void expire(boolean notify, boolean expireLocally)
{
if (log.isDebugEnabled())
{
log.debug("The session has expired with id: " + id + " is it local? " +expireLocally);
}
if (expiring)
return;
synchronized (this)
{
if (manager == null)
return;
expiring = true;
Context context = (Context) manager.getContainer();
Object listeners[] = context.getApplicationLifecycleListeners();
if (notify && (listeners != null))
{
HttpSessionEvent event =
new HttpSessionEvent(getSession());
for (int i = 0; i < listeners.length; i++)
{
int j = (listeners.length - 1) - i;
if (!(listeners[j] instanceof HttpSessionListener))
continue;
HttpSessionListener listener =
(HttpSessionListener) listeners[j];
try
{
fireContainerEvent(context,
"beforeSessionDestroyed",
listener);
listener.sessionDestroyed(event);
fireContainerEvent(context,
"afterSessionDestroyed",
listener);
}
catch (Throwable t)
{
try
{
fireContainerEvent(context,
"afterSessionDestroyed",
listener);
}
catch (Exception e)
{
;
}
manager.getContainer().getLogger().error(sm.getString("standardSession.sessionEvent"), t);
}
}
}
if (notify)
{
fireSessionEvent(Session.SESSION_DESTROYED_EVENT, null);
}
String keys[] = keys();
for (int i = 0; i < keys.length; i++)
removeAttributeInternal(keys[i], notify);
if (manager != null)
{
if(expireLocally)
{
((AbstractJBossManager) manager).removeLocal(this);
} else
{
((AbstractJBossManager) manager).remove(this);
}
}
accessCount = 0;
setValid(false);
expiring = false;
}
}
public void passivate()
{
}
public void activate()
{
}
protected String[] keys()
{
return ((String[]) getAttributesInternal().keySet().toArray(EMPTY_ARRAY));
}
protected void removeAttributeInternal(String name, boolean notify)
{
Object value = removeJBossInternalAttribute(name);
if (!notify || (value == null))
{
return;
}
HttpSessionBindingEvent event = null;
if (value instanceof HttpSessionBindingListener)
{
event = new HttpSessionBindingEvent(getSession(), name, value);
((HttpSessionBindingListener) value).valueUnbound(event);
}
Context context = (Context) manager.getContainer();
Object listeners[] = context.getApplicationEventListeners();
if (listeners == null)
return;
for (int i = 0; i < listeners.length; i++)
{
if (!(listeners[i] instanceof HttpSessionAttributeListener))
continue;
HttpSessionAttributeListener listener =
(HttpSessionAttributeListener) listeners[i];
try
{
fireContainerEvent(context,
"beforeSessionAttributeRemoved",
listener);
if (event == null)
{
event = new HttpSessionBindingEvent
(getSession(), name, value);
}
listener.attributeRemoved(event);
fireContainerEvent(context,
"afterSessionAttributeRemoved",
listener);
}
catch (Throwable t)
{
try
{
fireContainerEvent(context,
"afterSessionAttributeRemoved",
listener);
}
catch (Exception e)
{
;
}
manager.getContainer().getLogger().error(sm.getString("standardSession.attributeEvent"), t);
}
}
}
protected Object getAttributeInternal(String name)
{
return getJBossInternalAttribute(name);
}
protected Map getAttributesInternal()
{
return getJBossInternalAttributes();
}
protected Object setInternalAttribute(String name, Object value)
{
return setJBossInternalAttribute(name, value);
}
protected abstract Object getJBossInternalAttribute(String name);
protected abstract Object removeJBossInternalAttribute(String name);
protected abstract Map getJBossInternalAttributes();
protected abstract Object setJBossInternalAttribute(String name, Object value);
protected void writeObject(java.io.ObjectOutputStream out)
throws IOException
{
synchronized (attributes)
{
out.defaultWriteObject();
}
}
protected void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException
{
in.defaultReadObject();
}
}