FileDocCategorySizeDatePackage
ReplicatedSession.javaAPI DocApache Tomcat 6.0.149328Fri Jul 20 04:20:36 BST 2007org.apache.catalina.ha.session

ReplicatedSession

public class ReplicatedSession extends org.apache.catalina.session.StandardSession implements org.apache.catalina.ha.ClusterSession
Title: Tomcat Session Replication for Tomcat 4.0
Description: A very simple straight forward implementation of session replication of servers in a cluster.
This session replication is implemented "live". By live I mean, when a session attribute is added into a session on Node A a message is broadcasted to other messages and setAttribute is called on the replicated sessions.
A full description of this implementation can be found under Filip's Tomcat Page
Copyright: See apache license
author
Filip Hanik
version
$Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $ Description:
The ReplicatedSession class is a simple extension of the StandardSession class It overrides a few methods (setAttribute, removeAttribute, expire, access) and has hooks into the InMemoryReplicationManager to broadcast and receive events from the cluster.
This class inherits the readObjectData and writeObject data methods from the StandardSession and does not contain any serializable elements in addition to the inherited ones from the StandardSession

Fields Summary
private transient org.apache.catalina.Manager
mManager
protected boolean
isDirty
private transient long
lastAccessWasDistributed
private boolean
isPrimarySession
Constructors Summary
public ReplicatedSession(org.apache.catalina.Manager manager)

    

       
        super(manager);
        mManager = manager;
    
Methods Summary
public voidexpire()

        SimpleTcpReplicationManager mgr =(SimpleTcpReplicationManager)getManager();
        mgr.sessionInvalidated(getIdInternal());
        setIsDirty(true);
        super.expire();
    
public intgetAccessCount()

        return accessCount.get();
    
public longgetLastAccessWasDistributed()

        return lastAccessWasDistributed;
    
public longgetLastAccessedTime()

        return lastAccessedTime;
    
public longgetThisAccessedTime()

        return thisAccessedTime;
    
public voidinvalidate()

        SimpleTcpReplicationManager mgr =(SimpleTcpReplicationManager)getManager();
        mgr.sessionInvalidated(getIdInternal());
        setIsDirty(true);
        super.invalidate();
    
public booleanisDirty()

        return isDirty;
    
public booleanisPrimarySession()
returns true if this session is the primary session, if that is the case, the manager can expire it upon timeout.

        return isPrimarySession;
    
protected voidlog(java.lang.String message)
Implements a log method to log through the manager


        if ((mManager != null) && (mManager instanceof SimpleTcpReplicationManager)) {
            ((SimpleTcpReplicationManager) mManager).log.debug("ReplicatedSession: " + message);
        } else {
            System.out.println("ReplicatedSession: " + message);
        }

    
protected voidlog(java.lang.String message, java.lang.Throwable x)


        if ((mManager != null) && (mManager instanceof SimpleTcpReplicationManager)) {
            ((SimpleTcpReplicationManager) mManager).log.error("ReplicatedSession: " + message,x);
        } else {
            System.out.println("ReplicatedSession: " + message);
            x.printStackTrace();
        }

    
public voidreadObjectData(java.io.ObjectInputStream stream)
Read a serialized version of the contents of this session object from the specified object input stream, without requiring that the StandardSession itself have been serialized.

param
stream The object input stream to read from
exception
ClassNotFoundException if an unknown class is specified
exception
IOException if an input/output error occurs


        super.readObjectData(stream);

    
public voidremoveAttribute(java.lang.String name)

        setIsDirty(true);
        super.removeAttribute(name);
    
public voidremoveAttribute(java.lang.String name, boolean notify)
see parent description, plus we also notify other nodes in the cluster

        setIsDirty(true);
        super.removeAttribute(name,notify);
    
public voidsetAccessCount(int accessCount)

        this.accessCount.set(accessCount);
    
public voidsetAttribute(java.lang.String name, java.lang.Object value)
Sets an attribute and notifies the other nodes in the cluster

        if ( value == null ) {
          removeAttribute(name);
          return;
        }
        if (!(value instanceof java.io.Serializable))
            throw new java.lang.IllegalArgumentException("Value for attribute "+name+" is not serializable.");
        setIsDirty(true);
        super.setAttribute(name,value);
    
public voidsetId(java.lang.String id, boolean tellNew)


        if ((this.id != null) && (manager != null))
            manager.remove(this);

        this.id = id;

        if (manager != null)
            manager.add(this);
        if (tellNew) tellNew();
    
public voidsetIsDirty(boolean dirty)

        isDirty = dirty;
    
public voidsetLastAccessWasDistributed(long time)

        lastAccessWasDistributed = time;
    
public voidsetLastAccessedTime(long lastAccessedTime)

        this.lastAccessedTime = lastAccessedTime;
    
public voidsetManager(SimpleTcpReplicationManager mgr)
Sets the manager for this session

param
mgr - the servers InMemoryReplicationManager

        mManager = mgr;
        super.setManager(mgr);
    
public voidsetMaxInactiveInterval(int interval)

        setIsDirty(true);
        super.setMaxInactiveInterval(interval);
    
public voidsetPrimarySession(boolean primarySession)
Sets whether this is the primary session or not.

param
primarySession Flag value

        this.isPrimarySession=primarySession;
    
public voidsetPrincipal(java.security.Principal principal)
Set the authenticated Principal that is associated with this Session. This provides an Authenticator with a means to cache a previously authenticated Principal, and avoid potentially expensive Realm.authenticate() calls on every request.

param
principal The new Principal, or null if none

        super.setPrincipal(principal);
        setIsDirty(true);
    
public voidsetThisAccessedTime(long thisAccessedTime)

        this.thisAccessedTime = thisAccessedTime;
    
public java.lang.StringtoString()

        StringBuffer buf = new StringBuffer("ReplicatedSession id=");
        buf.append(getIdInternal()).append(" ref=").append(super.toString()).append("\n");
        java.util.Enumeration e = getAttributeNames();
        while ( e.hasMoreElements() ) {
            String name = (String)e.nextElement();
            Object value = getAttribute(name);
            buf.append("\tname=").append(name).append("; value=").append(value).append("\n");
        }
        buf.append("\tLastAccess=").append(getLastAccessedTime()).append("\n");
        return buf.toString();
    
public voidwriteObjectData(java.io.ObjectOutputStream stream)
Write a serialized version of the contents of this session object to the specified object output stream, without requiring that the StandardSession itself have been serialized.

param
stream The object output stream to write to
exception
IOException if an input/output error occurs


        super.writeObjectData(stream);