/*
 * 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$
 * --------------------------------------------------------------------------
 */

// SimpleCommon.java

package org.objectweb.jonas.jtests.beans.transacted;

import java.rmi.RemoteException;
import javax.ejb.EJBObject;
import javax.transaction.Status;
import javax.transaction.UserTransaction;
import org.objectweb.jonas.jtm.TransactionService;
import org.objectweb.jonas.service.ServiceManager;

/**
 * Common part to different implementations of Simple
 */
public class SimpleCommon {


    /**
     * Utility method that returns true if the thread is associated
     * to a transaction
     */
    protected boolean isAssociated() {
	int ret;
	try {
	    // Get UserTransaction. Not the standard way, but OK for test.
	    TransactionService ts =
		(TransactionService) ServiceManager.getInstance().getTransactionService();
	    UserTransaction ut = ts.getUserTransaction();
	    ret = ut.getStatus();
	} catch (Exception e) {
	    System.err.println("isAssociated: "+e);
	    return false;
	}
	if (ret == Status.STATUS_UNKNOWN || ret == Status.STATUS_NO_TRANSACTION) {
	    return false;
	} else {
	    return true;
	}
    }

    /**
     * This method return true if there is an association of a transaction with 
     * this thread
     */
    public boolean  opwith_notsupported() {
	return isAssociated();
    }

    /**
     * This method return true if there is an association of a transaction with 
     * this thread
     */
    public boolean opwith_supports() {
	return isAssociated();
    }
   
    /**
     * This method return true if there is an association of a transaction with 
     * this thread
     */
    public boolean  opwith_required() {
	return isAssociated();
    }

    /**
     * This method return true if there is an association of a transaction with 
     * this thread
     */
    public boolean opwith_requires_new() {
	return isAssociated();
    }

    /**
     * This method return true if there is an association of a transaction with 
     * this thread
     */
    public boolean opwith_mandatory() {
	return isAssociated();
    }
  
    /**
     * This method return true if there is an association of a transaction with 
     * this thread
     */
    public boolean opwith_never() {
	return isAssociated();
    }
    
 

}
