/**
* High Availability Service (HA) for JOnAS
*
* Copyright (C) 2006 Distributed Systems Lab.
* Universidad Polit??cnica de Madrid (Spain)
* Contact: http://lsd.ls.fi.upm.es/lsd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* --------------------------------------------------------------------------
* $Id$
* --------------------------------------------------------------------------
*/

package org.objectweb.jonas.ha.interceptor;

import java.util.Iterator;
import java.util.Stack;

import org.objectweb.carol.cmi.ha.Constants;
import org.objectweb.carol.cmi.ha.RequestId;
import org.objectweb.carol.cmi.ha.ViewIdWrapper;
import org.objectweb.carol.rmi.jrmp.interceptor.JServiceContext;

/**
 * This class holds the required information to pass between client/server
 * through HA interceptors
 * @author Francisco Perez-Sorrosal (fpsorrosal@no-spam@fi.upm.es)
 * @author Alberto Paz-Jimenez (apaz@no-spam@fi.upm.es)
 */
public class HAContext  implements JServiceContext {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /**
     * Holds request id stack
     */
    private Stack requests = new Stack();

    /**
     *
     */
    private boolean onFailover = false;

    /**
     * ViewIdWrapper
     */
    private ViewIdWrapper info = null;

    /**
     * Constructor
     */
    public HAContext() {
    }

    /**
     * Returns the HA context identifier
     * @return the HA context id
     */
    public int getContextId() {
        return Constants.HA_CTX_ID;
    }

    /**
     * Sets Requests to pass between client and server
     * @param reqs thre requests
     */
    public void setRequests(Stack reqs) {
        requests = reqs;
    }

    /**
     * Returns the requests
     * @return the requests
     */
    public Stack getRequests() {
        return requests;
    }

    /**
     * @return Returns the onFailover.
     */
    public boolean isOnFailover() {
        return onFailover;
    }

    /**
     * @param onFailover The onFailover to set.
     */
    public void setOnFailover(boolean onFailover) {
        this.onFailover = onFailover;
    }

    /**
     * Returns a string representation of the object
     * @return the string representing the HAContext
     */
    public String toString() {
        String ret = "\nHAContext: " + Constants.HA_CTX_ID + "\n";

        ret += "\tRequests:\n";
        Iterator it = requests.iterator();
        while(it.hasNext()) {
            String request = ((RequestId) it.next()).toString();
            ret += "\t\t" + request + "\n";
        }
        ret += "\tOnFailover:" + onFailover + "\n";

        if (info != null) {
            ret += "\tViewIdWrapper: k=" + info.getKey() + ", v=" + info.getViewId() + "\n";
        } else {
            ret += "\tViewIdWrapper: null" + "\n";
        }

        return ret;

    }

    public ViewIdWrapper getViewIdWrapper() {
        return info;
    }


    public void setViewIdWrapper(ViewIdWrapper info) {
        this.info = info;
    }
}
