/*
 * The contents of this file are subject to the terms 
 * of the Common Development and Distribution License 
 * (the License).  You may not use this file except in
 * compliance with the License.
 * 
 * You can obtain a copy of the license at 
 * https://glassfish.dev.java.net/public/CDDLv1.0.html or
 * glassfish/bootstrap/legal/CDDLv1.0.txt.
 * See the License for the specific language governing 
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL 
 * Header Notice in each file and include the License file 
 * at glassfish/bootstrap/legal/CDDLv1.0.txt.  
 * If applicable, add the following below the CDDL Header, 
 * with the fields enclosed by brackets [] replaced by
 * you own identifying information: 
 * "Portions Copyrighted [year] [name of copyright owner]"
 * 
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 */

/*
 * Copyright 2004-2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

package com.sun.jts.jta;

import java.util.*;
import javax.transaction.xa.*;
import javax.transaction.*;
import org.omg.CosTransactions.*;
import com.sun.jts.jta.NativeXAResource;
import com.sun.jts.jtsxa.XID;
import com.sun.jts.jtsxa.OTSResource;
import com.sun.jts.jtsxa.OTSResourceImpl;
import javax.transaction.SystemException;
import javax.transaction.RollbackException;
import javax.transaction.Synchronization;
import com.sun.jts.jtsxa.Utility;
import com.sun.jts.CosTransactions.Configuration;
import com.sun.jts.CosTransactions.ControlImpl;
import com.sun.jts.CosTransactions.TopCoordinator;
import org.omg.CORBA.TRANSACTION_ROLLEDBACK;
import com.sun.jts.CosTransactions.GlobalTID;


import java.util.logging.Logger;
import java.util.logging.Level;
import com.sun.logging.LogDomains;
import com.sun.jts.utils.LogFormatter;
/**
 * keep track of per-transaction state
 *
 * @author Tony Ng
 */
public class TransactionState {

    /**
     * various association states for an XAResource
     */
    private static final int NOT_EXIST = -1;
    private static final int ASSOCIATED = 0;
    private static final int NOT_ASSOCIATED = 1;
    private static final int ASSOCIATION_SUSPENDED = 2;
    private static final int FAILED = 3;
    private static final int ROLLING_BACK = 4;

    private static final Integer NOT_EXIST_INTEGER = new Integer(NOT_EXIST);
    private static final Integer ASSOCIATED_INTEGER = new Integer(ASSOCIATED);
    private static final Integer NOT_ASSOCIATED_INTEGER = new Integer(NOT_ASSOCIATED);
    private static final Integer ASSOCIATION_SUSPENDED_INTEGER = new Integer(ASSOCIATION_SUSPENDED);
    private static final Integer FAILED_INTEGER = new Integer(FAILED);
    private static final Integer ROLLING_BACK_INTEGER = new Integer(ROLLING_BACK);

    /**
     * a mapping of XAResource -> Integer (state)
     * possible states are listed above
     */
    private Map resourceStates;

    /**
     * Map: XAResource -> Xid
     */
    private Map resourceList;

    /**
     * a set of Xid branches on which xa_start() has been called
     */
    private Set seenXids;

    /**
     * a list of unique resource factory (represented by XAResource)
     * Vector of XAResource objects
     */
    private List factories;

    // the OTS synchronization object for this transaction
    private SynchronizationImpl syncImpl;

    // count of XAResources that are still in active state
    // private int activeResources = 0;

    private GlobalTID gtid;
    private TransactionImpl tran;
	/*
		Logger to log transaction messages
	*/  
    
	static Logger _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER);

    public static boolean debug = false;
    // private static TransactionManagerImpl tm = TransactionManagerImpl.getTransactionManagerImpl();

    public TransactionState(GlobalTID gtid, TransactionImpl tran) {
        resourceStates = new HashMap();
        resourceList = new HashMap();
        seenXids = new HashSet();
        factories = new ArrayList();
        this.gtid = gtid;
        this.tran  = tran;
    }


    /**
     * this is called via callback of Synchronization
     * right before a transaction commit or rollback
     * to ensure that all XAResources are properly delisted
     */
    synchronized public void beforeCompletion() {

        boolean exceptionThrown = false;
        XAResource res = null;
        Iterator e = resourceStates.keySet().iterator();
        while (e.hasNext()) {
            try {
                res = (XAResource) e.next();
                int XAState = getXAState(res);
                switch (XAState) {
                    case NOT_ASSOCIATED:
                    case FAILED:
                        break;
                    case ASSOCIATION_SUSPENDED:
                    case ASSOCIATED:
                        Xid xid = (Xid) resourceList.get(res);
                        res.end(xid, XAResource.TMSUCCESS);
                        setXAState(res, NOT_ASSOCIATED_INTEGER);
                        break;
                    case ROLLING_BACK:
                    case NOT_EXIST:
                    default:
                        throw new IllegalStateException("Wrong XA State: " + XAState/*#Frozen*/);
                }
            } catch (Exception ex) {
                setXAState(res, FAILED);
                _logger.log(Level.WARNING,"jts.delist_exception",ex);
                exceptionThrown = true;
            }
        }
        if (exceptionThrown) {
            try {
                tran.setRollbackOnly();
            } catch (Exception ex) {}
        }
    }

    /**
     * This is called from OTS to rollback a particular XAResource
     */
    synchronized public void rollback(XAResource res)
        throws IllegalStateException, XAException {

        Xid xid = (Xid) resourceList.get(res);
        assert_prejdk14(xid != null);
        int XAState = getXAState(res);
        switch (XAState) {
        case NOT_ASSOCIATED:
        case FAILED:
            res.rollback(xid);
            break;
        case ASSOCIATION_SUSPENDED:
            res.end(xid, XAResource.TMSUCCESS);
            setXAState(res, NOT_ASSOCIATED_INTEGER);
            res.rollback(xid);
            break;
        case ASSOCIATED:
            res.end(xid, XAResource.TMSUCCESS);
            setXAState(res, NOT_ASSOCIATED_INTEGER);
            res.rollback(xid);
            /**
            // rollback is deferred until delistment
            setXAState(res, ROLLING_BACK);
            activeResources++;
            **/
            break;
        case ROLLING_BACK:
        case NOT_EXIST:
        default:
            throw new IllegalStateException("Wrong XAState: " +
                                            XAState/*#Frozen*/);
        }
    }

    private Xid computeXid(XAResource res, Control control)
        throws Inactive, Unavailable, XAException {

        // one branch id per RM
        int size = factories.size();
        for (int i=0; i<size; i++) {
            XAResource fac = (XAResource) factories.get(i);
            if (res.isSameRM(fac)) {
                // use same branch
                Xid xid = (Xid) resourceList.get(fac);
                return xid;
            }
        }

        // use a different branch
        // XXX ideally should call JTS layer to get the branch id
        XID xid;

        if( Configuration.isLocalFactory()) {
            xid = Utility.getXID(((ControlImpl) control).get_localCoordinator());
        } else {
            xid = Utility.getXID(control.get_coordinator());
        }
        factories.add(res);

	    byte [] branchid = parseSize(size);
	    byte [] sname    = Configuration.getServerNameByteArray();
	    byte [] branch = new byte[sname.length+1+branchid.length];

	    System.arraycopy(sname, 0, branch, 0, sname.length);
	    branch[sname.length] = (byte) ',';
	    System.arraycopy(branchid, 0, branch, sname.length+1, branchid.length);

        xid.setBranchQualifier(branch);

        return xid;
    }


    synchronized public void startAssociation(XAResource res, Control control,
                                              int status)
        throws XAException, SystemException, IllegalStateException,
        RollbackException
    {
        OTSResource ref;

        try {
            // XXX should avoid using XID in JTA layer
            Xid xid = null;
            boolean newResource = false;
            boolean seenXid = false;
            if (resourceList.get(res) == null) {
                // throw RollbackException if try to register
                // a new resource when a transaction is marked rollback
                if (status !=
                    javax.transaction.Status.STATUS_ACTIVE) {
                    throw new RollbackException();
                }

                newResource = true;
                xid = computeXid(res, control);
                seenXid = seenXids.contains(xid);

                // register with OTS
                if (!seenXid) {
                    // new branch
                    // COMMENT(Ram J) no need to activate OTSResource object since its local.
                    ref = new OTSResourceImpl(xid, res, this);
                    if( Configuration.isLocalFactory()) {
                      ((ControlImpl) control).get_localCoordinator().register_resource(ref);
                    }
                    else {
                      control.get_coordinator().register_resource(ref);
                    }
                }
                resourceList.put(res, xid);
            } else {
                // use the previously computed branch id
                xid = (Xid) resourceList.get(res);
                seenXid = seenXids.contains(xid);
            }

            int XAState = getXAState(res);
            if (!seenXid) {
                // first time this branch is enlisted
                seenXids.add(xid);
                res.start(xid, XAResource.TMNOFLAGS);
                setXAState(res, ASSOCIATED_INTEGER);
            } else {
                // have seen this branch before
                switch (XAState) {
                case NOT_ASSOCIATED:
                case NOT_EXIST:
                    res.start(xid, XAResource.TMJOIN);
                    setXAState(res, ASSOCIATED_INTEGER);
                    break;
                case ASSOCIATION_SUSPENDED:
                    res.start(xid, XAResource.TMRESUME);
                    setXAState(res, ASSOCIATED_INTEGER);
                    break;
                case ASSOCIATED:
                case FAILED:
                case ROLLING_BACK:
                default:
                    throw new IllegalStateException("Wrong XAState: " + 
                                                    XAState/*#Frozen*/);
                }
            }

           /**
            // need to do connection enlistment for NativeXAResource
            if (res instanceof NativeXAResource) {
                if (newResource) {
                    ((NativeXAResource) res).enlistConnectionInXA();
                }
            }
           **/
        } catch (XAException ex) {
            setXAState(res, FAILED_INTEGER);
            throw ex;
        } catch (Inactive ex) {
			_logger.log(Level.WARNING,"jts.transaction_inactive",ex);
            throw new SystemException();
        } catch (Unavailable ex) {
			_logger.log(Level.WARNING,"jts.object_unavailable",ex);
            throw new SystemException();
        }
    }

    synchronized
    public void endAssociation(XAResource xares, int flags)
        throws XAException, IllegalStateException
    {
        try {
            Xid xid = (Xid) resourceList.get(xares);
            assert_prejdk14(xid != null);
            int XAState = getXAState(xares);
            switch (XAState) {
            case ASSOCIATED:
                if ((flags & XAResource.TMSUCCESS) != 0) {
                    xares.end(xid, XAResource.TMSUCCESS);
                    setXAState(xares, NOT_ASSOCIATED_INTEGER);
                } else if ((flags & XAResource.TMSUSPEND) != 0) {
                    xares.end(xid, XAResource.TMSUSPEND);
                    setXAState(xares, ASSOCIATION_SUSPENDED_INTEGER);
                } else {
                    xares.end(xid, XAResource.TMFAIL);
                    setXAState(xares, FAILED_INTEGER);
                }
                break;
            case ROLLING_BACK:
                // rollback deferred XAResources
                // activeResources--;
                // cleanupTransactionStateMapping();
                xares.end(xid, XAResource.TMSUCCESS);
                setXAState(xares, NOT_ASSOCIATED_INTEGER);
                xares.rollback(xid);
                
                break;
            case ASSOCIATION_SUSPENDED:
                if ((flags & XAResource.TMSUCCESS) != 0) {
                    xares.end(xid, XAResource.TMSUCCESS);
                    setXAState(xares, NOT_ASSOCIATED_INTEGER);
                } else if ((flags & XAResource.TMSUSPEND) != 0) {
                    throw new IllegalStateException
                        ("Wrong XAState: " + XAState/*#Frozen*/);
                } else {
                    xares.end(xid, XAResource.TMFAIL);
                    setXAState(xares, FAILED_INTEGER);
                }
                break;
            case NOT_ASSOCIATED:
            case NOT_EXIST:
            case FAILED:
            default:
                throw new IllegalStateException("Wrong XAState: " + XAState/*#Frozen*/);
            }
        } catch (XAException ex) {
            setXAState(xares, FAILED_INTEGER);
            throw ex;
        }
    }

   // To be called by SynchronizationImpl, if there are any exception
   // in beforeCompletion() call backs
   void setRollbackOnly()
        throws IllegalStateException, SystemException {
       tran.setRollbackOnly();
   }

   /**
    synchronized void cleanupTransactionStateMapping() {
        if (activeResources == 0) {
            TransactionManagerImpl tm =
                TransactionManagerImpl.getTransactionManagerImpl();
        }
    }
  **/

    synchronized
    public void registerSynchronization(Synchronization sync,
                                        Control control,
                                        boolean interposed) 
        throws RollbackException, IllegalStateException,
        SystemException {

        try {
            // One OTS Synchronization object per transaction
            if (syncImpl == null) {
                // syncImpl = new SynchronizationImpl();
                syncImpl = new SynchronizationImpl(this);

                // COMMENT(Ram J) syncImpl is a local object. No need to
                // activate it.
                if (Configuration.isLocalFactory()) {
                  ((ControlImpl) control).get_localCoordinator().register_synchronization(syncImpl);
                } else {
                  control.get_coordinator().register_synchronization(syncImpl);
                }
            }
            syncImpl.addSynchronization(sync, interposed);
        } catch (TRANSACTION_ROLLEDBACK ex) {
            throw new RollbackException();
        } catch (Unavailable ex) {
            throw new SystemException();
        } catch (Inactive ex) {
            throw new IllegalStateException();
        } catch (Exception ex) {
            throw new SystemException();
        }
    }

    private void setXAState(XAResource res, Integer state) {
        if (debug) {
            int oldValue = getXAState(res);
			_logger.log(Level.FINE,"transaction id : " + gtid);
            _logger.log(Level.FINE,"res: " + res + "," + oldValue + "," + state);
        }
        resourceStates.put(res, state);
    }

    private int getXAState(XAResource res) {
        Integer result = (Integer) resourceStates.get(res);
        if (result == null) return NOT_EXIST;
        return result.intValue();
    }

    /**
     * list all the XAResources that have been enlisted in this
     * transaction.
     */
    public Enumeration listXAResources() {
        return Collections.enumeration(resourceList.keySet());
        // return resourceList.keys();
    }

    /**
     * return true if res has been enlisted in this transaction;
     * false otherwise.
     */
    public boolean containsXAResource(XAResource res) {
        return resourceList.containsKey(res);
    }

    static private void assert_prejdk14(boolean value) {
        if (!value) {
            Exception e = new Exception();
			_logger.log(Level.WARNING,"jts.assert",e);
        }
    }

    private static byte[] parseSize(int size) {

          switch(size) {
            case 0: 
               return new byte[]{0};
            case 1: 
               return new byte[]{1};
            case 2: 
               return new byte[]{2};
            case 3: 
               return new byte[]{3};
            case 4: 
               return new byte[]{4};
            case 5: 
               return new byte[]{5};
            case 6: 
               return new byte[]{6};
            case 7: 
               return new byte[]{7};
            case 8: 
               return new byte[]{8};
            case 9: 
               return new byte[]{9};
        }
        int j = 9;
        byte [] res = new byte[10];
        while (size > 0) {
            res[j--] = (byte) (size % 10);
            size = size / 10;
        }
        int len = 9-j;
        byte [] result = new byte[len];
        System.arraycopy(res, j+1, result, 0, len);
        return result;
    }

}
