/**
 *
 * Copyright 2004 Hiram Chirino
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **/
package org.codehaus.activemq.ra;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.resource.ResourceException;
import javax.resource.spi.LocalTransaction;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;

/**
 * Used to provide a LocalTransaction and XAResource to a JMS session.
 */
abstract class TxResourceProxy implements XAResource, LocalTransaction {

    abstract Session getSession();

    abstract XAResource getXAResource();

    public void begin() throws ResourceException {
        try {
            if (getSession() instanceof ActiveMQRASession) {
                ((ActiveMQRASession) getSession()).begin();
            }
        } catch (JMSException e) {
            throw new ResourceException("begin failed.", e);
        }
    }

    public void commit() throws ResourceException {
        try {
            getSession().commit();
        } catch (JMSException e) {
            throw new ResourceException("commit failed.", e);
        }
    }

    public void rollback() throws ResourceException {
        try {
            getSession().rollback();
        } catch (JMSException e) {
            throw new ResourceException("rollback failed.", e);
        }
    }

    public void commit(Xid arg0, boolean arg1) throws XAException {
        getXAResource().commit(arg0, arg1);
    }

    public void end(Xid arg0, int arg1) throws XAException {
        getXAResource().end(arg0, arg1);
    }

    public void forget(Xid arg0) throws XAException {
        getXAResource().forget(arg0);
    }

    public int getTransactionTimeout() throws XAException {
        return getXAResource().getTransactionTimeout();
    }

    public boolean isSameRM(XAResource xaresource) throws XAException {
        if( xaresource == null )
            return false;
        // Do we have to unwrap?
        if( xaresource instanceof TxResourceProxy ) {
            xaresource = ((TxResourceProxy)xaresource).getXAResource();
        }
        return getXAResource().isSameRM(xaresource);
    }

    public int prepare(Xid arg0) throws XAException {
        return getXAResource().prepare(arg0);
    }

    public Xid[] recover(int arg0) throws XAException {
        return getXAResource().recover(arg0);
    }

    public void rollback(Xid arg0) throws XAException {
        getXAResource().rollback(arg0);
    }

    public boolean setTransactionTimeout(int arg0) throws XAException {
        return getXAResource().setTransactionTimeout(arg0);
    }

    public void start(Xid arg0, int arg1) throws XAException {
        getXAResource().start(arg0, arg1);
    }
}