/*
 * RisorsaSomma.java
 *
 * Created on 20 luglio 2006, 11.57
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package servizi;

//package x XAResource
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;

//package x kandula
import org.apache.geronimo.transaction.manager.NamedXAResource;

//package x log4j
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author Valerio
 */
public class RisorsaSomma implements NamedXAResource {
    
    private Xid tx;
    private static RisorsaSomma instance = null;
    private int stato;
    private boolean locked;
    
    static Logger logger;
    
    
    /** Creates a new instance of RisorsaSomma */
    protected RisorsaSomma() {
        tx = null;
        stato = 0;
        locked = false;
        
        logger = Logger.getLogger("RisorsaSomma");
        PropertyConfigurator.configure("/home/valerio/apache-tomcat-5.5.17/webapps/axis/WEB-INF/classes/log4j.properties");
        
    }
    
    public void commit(Xid arg0, boolean arg1) throws XAException {
        logger.info("RisorsaSomma commit");
        if (!locked){
            logger.error("risorsa non bloccata!!!");
            throw new XAException();
        }
        logger.info("Valore di cui faccio il commit : " + stato);
        locked = false;
        	
    }
    
    public void end(Xid arg0, int arg1) throws XAException {
        logger.info("RisorsaSomma end");
    }
    
    public void forget(Xid arg0) throws XAException {
        logger.info("RisorsaSomma forget");
    }
    
    public static RisorsaSomma getInstance() {
        if (instance == null) {
        {
            instance = new RisorsaSomma();
        }
        
	return instance;
    }
    
    public String getName() {
        return("RisorsaSomma");
    }
    
    public int getTransactionTimeout() throws XAException {
	logger.info("RisorsaSomma getTransactionTimeout");
        return 0;
    }
    
    public boolean isSameRM(XAResource arg0) throws XAException {
	logger.info("RisorsaSomma isSameRM");
        //stato = 0;
	return arg0 == this;
    }

    public int prepare(Xid arg0) throws XAException {
        logger.info("RisorsaSomma prepare");
        locked = true;
        if (tx.equals(arg0))
            return XAResource.XA_OK;
        //se incontra un problema sblocca la risorsa;
        locked = false;
        logger.error("prepare non riuscito");
	throw new XAException();
    }
    
    public Xid[] recover(int arg0) throws XAException {
        logger.info("RisorsaSomma recover");
        return null;
    }
    public void rollback(Xid arg0) throws XAException {
        logger.info("RisorsaSomma rollback");
        if (tx.equals(arg0))
            locked = false;
    }
    
    public boolean setTransactionTimeout(int arg0) throws XAException {
	logger.info("RisorsaSomma setTransactionTimeout");
        return false;
    }
    
    public int somma(int op1, int op2){
        
        stato = op1 + op2;
        return(op1+op2);
    }
    public void start(Xid arg0, int arg1) throws XAException {
        logger.info("RisorsaSomma start");
        if(locked)
            throw new XAException();
        this.tx = arg0;
		
    }
    
}
