/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * 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.resource;

import javax.resource.ResourceException;
import javax.transaction.Transaction;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import org.objectweb.transaction.jta.ResourceManagerEvent;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;

/**
 *  This class implements ResourceManagerEvent. An instance of this object is
 *  also an event transmited by the Resource adapter manager to the user
 *  transaction. This event is linked to a ManagedConnection, more exatcly this
 *  implementation is linked to MCInfo instance.
 *
 *@author     sebastien.chassande@inrialpes.fr
 */
class RMEImpl implements ResourceManagerEvent {
    /**
     *  The MCInfo instance which represents the physical connection
     */
    private MCInfo mci = null;
    /**
     *  The Logger instance where messages are written.
     */
    private Logger trace = null;
    /**
     *  Set if the RME is valid to call enlist Resource
     *  Issue with JOTM and one thread calling ConnectionOpened, but a
     *   different thread calling Closed or Error and not enough information
     *   in JOTM to keep information straight.
     */
    protected boolean isValid = false;

    /**
     *  This constructor permits to specify the MCInfp corresponding to the
     *  ManagedConnection (the physical connection).
     *
     * @param mci MCInfo
     * @param trace Logger
     */
    public RMEImpl(MCInfo mci, Logger trace) {
        this.mci = mci;
        this.trace = trace;
    }


    /**
     * Check if equals
     * @param o Object to test equality
     * @return boolean if RMEImpl objets are equal
     */
    public boolean equals(Object o) {
        return (o instanceof RMEImpl) && mci.equals(((RMEImpl) o).mci);
    }


    /**
     *  This method is in charge of the enlisting of the managedConnection linked
     *  to this instance, in the transaction specified in parameter.
     * @param tx TransactionConnection
     * @throws SystemException if an Exception occurs
     */
    public void enlistConnection(Transaction tx) throws SystemException {
        try {
            if (isValid) {
                if (trace.isLoggable(BasicLevel.DEBUG)) {
                    trace.log(BasicLevel.DEBUG, "Enlist the XA Resource " + mci.getXAResource()
                            + " in Tx:" + tx);
                }
                tx.enlistResource(mci.getXAResource());
            }
        } catch (RollbackException rbe) {
            throw new SystemException(rbe.getMessage());
        } catch (ResourceException re) {
            throw new SystemException(re.getMessage());
        }
    }
}
