FileDocCategorySizeDatePackage
DeltaRequest.javaAPI DocApache Tomcat 6.0.1413952Fri Jul 20 04:20:32 BST 2007org.apache.catalina.ha.session

DeltaRequest

public class DeltaRequest extends Object implements Externalizable
This class is used to track the series of actions that happens when a request is executed. These actions will then translate into invokations of methods on the actual session. This class is NOT thread safe. One DeltaRequest per session
author
Filip Hanik
version
1.0

Fields Summary
public static org.apache.juli.logging.Log
log
protected static org.apache.catalina.util.StringManager
sm
The string manager for this package.
public static final int
TYPE_ATTRIBUTE
public static final int
TYPE_PRINCIPAL
public static final int
TYPE_ISNEW
public static final int
TYPE_MAXINTERVAL
public static final int
ACTION_SET
public static final int
ACTION_REMOVE
public static final String
NAME_PRINCIPAL
public static final String
NAME_MAXINTERVAL
public static final String
NAME_ISNEW
private String
sessionId
private LinkedList
actions
private LinkedList
actionPool
private boolean
recordAllActions
Constructors Summary
public DeltaRequest()


      
        
    
public DeltaRequest(String sessionId, boolean recordAllActions)

        this.recordAllActions=recordAllActions;
        if(sessionId != null)
            setSessionId(sessionId);
    
Methods Summary
protected synchronized voidaddAction(int type, int action, java.lang.String name, java.lang.Object value)

        AttributeInfo info = null;
        if ( this.actionPool.size() > 0 ) {
            try {
                info = (AttributeInfo) actionPool.removeFirst();
            }catch ( Exception x ) {
                log.error("Unable to remove element:",x);
                info = new AttributeInfo(type, action, name, value);
            }
            info.init(type,action,name,value);
        } else {
            info = new AttributeInfo(type, action, name, value);
        }
        //if we have already done something to this attribute, make sure
        //we don't send multiple actions across the wire
        if ( !recordAllActions) {
            try {
                actions.remove(info);
            } catch (java.util.NoSuchElementException x) {
                //do nothing, we wanted to remove it anyway
            }
        }
        //add the action
        actions.addLast(info);
    
public synchronized voidclear()

        actions.clear();
        actionPool.clear();
    
public synchronized voidexecute(DeltaSession session, boolean notifyListeners)

        if ( !this.sessionId.equals( session.getId() ) )
            throw new java.lang.IllegalArgumentException("Session id mismatch, not executing the delta request");
        session.access();
        for ( int i=0; i<actions.size(); i++ ) {
            AttributeInfo info = (AttributeInfo)actions.get(i);
            switch ( info.getType() ) {
                case TYPE_ATTRIBUTE: {
                    if ( info.getAction() == ACTION_SET ) {
                        if ( log.isTraceEnabled() ) log.trace("Session.setAttribute('"+info.getName()+"', '"+info.getValue()+"')");
                        session.setAttribute(info.getName(), info.getValue(),notifyListeners,false);
                    }  else {
                        if ( log.isTraceEnabled() ) log.trace("Session.removeAttribute('"+info.getName()+"')");
                        session.removeAttribute(info.getName(),notifyListeners,false);
                    }
                        
                    break;
                }//case
                case TYPE_ISNEW: {
                if ( log.isTraceEnabled() ) log.trace("Session.setNew('"+info.getValue()+"')");
                    session.setNew(((Boolean)info.getValue()).booleanValue(),false);
                    break;
                }//case
                case TYPE_MAXINTERVAL: {
                    if ( log.isTraceEnabled() ) log.trace("Session.setMaxInactiveInterval('"+info.getValue()+"')");
                    session.setMaxInactiveInterval(((Integer)info.getValue()).intValue(),false);
                    break;
                }//case
                case TYPE_PRINCIPAL: {
                    Principal p = null;
                    if ( info.getAction() == ACTION_SET ) {
                        SerializablePrincipal sp = (SerializablePrincipal)info.getValue();
                        p = (Principal)sp.getPrincipal(session.getManager().getContainer().getRealm());
                    }
                    session.setPrincipal(p,false);
                    break;
                }//case
                default : throw new java.lang.IllegalArgumentException("Invalid attribute info type="+info);
            }//switch
        }//for
        session.endAccess();
        reset();
    
public voidexecute(DeltaSession session)

        execute(session,true);
    
public java.lang.StringgetSessionId()

        return sessionId;
    
public intgetSize()

        return actions.size();
    
public synchronized voidreadExternal(java.io.ObjectInput in)

        //sessionId - String
        //recordAll - boolean
        //size - int
        //AttributeInfo - in an array
        reset();
        sessionId = in.readUTF();
        recordAllActions = in.readBoolean();
        int cnt = in.readInt();
        if (actions == null)
            actions = new LinkedList();
        else
            actions.clear();
        for (int i = 0; i < cnt; i++) {
            AttributeInfo info = null;
            if (this.actionPool.size() > 0) {
                try {
                    info = (AttributeInfo) actionPool.removeFirst();
                } catch ( Exception x )  {
                    log.error("Unable to remove element",x);
                    info = new AttributeInfo(-1,-1,null,null);
                }
            }
            else {
                info = new AttributeInfo(-1,-1,null,null);
            }
            info.readExternal(in);
            actions.addLast(info);
        }//for
    
public voidremoveAttribute(java.lang.String name)

        int action = ACTION_REMOVE;
        addAction(TYPE_ATTRIBUTE,action,name,null);
    
public synchronized voidreset()

        while ( actions.size() > 0 ) {
            try {
                AttributeInfo info = (AttributeInfo) actions.removeFirst();
                info.recycle();
                actionPool.addLast(info);
            }catch  ( Exception x ) {
                log.error("Unable to remove element",x);
            }
        }
        actions.clear();
    
protected byte[]serialize()
serialize DeltaRequest

see
DeltaRequest#writeExternal(java.io.ObjectOutput)
param
deltaRequest
return
serialized delta request
throws
IOException

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        writeExternal(oos);
        oos.flush();
        oos.close();
        return bos.toByteArray();
    
public voidsetAttribute(java.lang.String name, java.lang.Object value)

        int action = (value==null)?ACTION_REMOVE:ACTION_SET;
        addAction(TYPE_ATTRIBUTE,action,name,value);
    
public voidsetMaxInactiveInterval(int interval)

        int action = ACTION_SET;
        addAction(TYPE_MAXINTERVAL,action,NAME_MAXINTERVAL,new Integer(interval));
    
public voidsetNew(boolean n)

        int action = ACTION_SET;
        addAction(TYPE_ISNEW,action,NAME_ISNEW,new Boolean(n));
    
public voidsetPrincipal(java.security.Principal p)
convert principal at SerializablePrincipal for backup nodes. Only support principals from type {@link GenericPrincipal GenericPrincipal}

param
p Session principal
see
GenericPrincipal

        int action = (p==null)?ACTION_REMOVE:ACTION_SET;
        SerializablePrincipal sp = null;
        if ( p != null ) {
            if(p instanceof GenericPrincipal) {
                sp = SerializablePrincipal.createPrincipal((GenericPrincipal)p);
                if(log.isDebugEnabled())
                    log.debug(sm.getString("deltaRequest.showPrincipal", p.getName() , getSessionId()));
            } else
                log.error(sm.getString("deltaRequest.wrongPrincipalClass",p.getClass().getName()));
        }
        addAction(TYPE_PRINCIPAL,action,NAME_PRINCIPAL,sp);
    
public voidsetSessionId(java.lang.String sessionId)

        this.sessionId = sessionId;
        if ( sessionId == null ) {
            new Exception("Session Id is null for setSessionId").fillInStackTrace().printStackTrace();
        }
    
public synchronized voidwriteExternal(java.io.ObjectOutput out)

        //sessionId - String
        //recordAll - boolean
        //size - int
        //AttributeInfo - in an array
        out.writeUTF(getSessionId());
        out.writeBoolean(recordAllActions);
        out.writeInt(getSize());
        for ( int i=0; i<getSize(); i++ ) {
            AttributeInfo info = (AttributeInfo)actions.get(i);
            info.writeExternal(out);
        }