/*
 * 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_tm;

import java.net.InetAddress;
import java.util.Vector;
import java.rmi.RemoteException;
import javax.transaction.xa.Xid;
import org.objectweb.common.RemoteObject;

/**
 * Implements the OTS like TransactionFactory
 * @author Philippe Durieux
 */
public class TransactionFactoryImpl extends RemoteObject implements TransactionFactory {

    /**
     * @serial
     */
    int timeoutMax = 3600;	// 1 hour

    private Vector coordinatorList = new Vector();

    /**
     * Constructor of the Transaction Factory
     */
    public TransactionFactoryImpl() throws RemoteException {
	TraceTm.debugTm("TransactionFactory constructor");
    }
    
    /**
     * Create a new Control implementation on JTM.
     *
     * @return		The Control object for the transaction
     */
    public synchronized Control create(int timeout) throws RemoteException {
	TraceTm.debugTm("TransactionFactoryImpl.create("+timeout+")");

	ControlImpl ctrl = null;

	// checks timeout value
	if (timeout == 0 || timeout > timeoutMax)
	    timeout = timeoutMax;

	// makes a new xid
	// - should pass servername + ip addr. (LATER)
	XidImpl xid = new XidImpl("TMServer", 0);

	// Creates a new ControlImpl
	try {
	    ctrl = new ControlImpl(timeout, xid, null);
	} catch(Exception e) {
	    TraceTm.error("Cannot create ControlImpl", e);
	}
	return ctrl;
    }


    /**
     * Recreate locally a Control object for an existing transaction. It is
     * possible to call recreate for a transaction already known. In this
     * case, recreate simply returns the existing Control object.
     *
     * @return		The Control object for the transaction
     */
    public synchronized Control recreate(TransactionContext ctx) throws RemoteException {

	TraceTm.debugTm("recreate");

	// Check if Control already exists
	ControlImpl ctrl = null;

	synchronized (coordinatorList) {
	    for (int i = 0; i < coordinatorList.size(); i++) {
		Coordinator coord = (Coordinator) coordinatorList.elementAt(i);
		if (coord.equals(ctx.getCoordinator())) {
		    TraceTm.debugTm("recreate: Control already in the list");
		    ctrl = (ControlImpl) coord;
		    break;
		}
	    }
	}
	if (ctrl != null) {
	    TraceTm.debugTm("recreate twice");
	    return ctrl;
	}

	// TODO: Build an xid with same gtrid and a new bqual
	Xid xid = ctx.getXid();

	// Creates a new ControlImpl and register it to the sup-coord.
	// coordinator may be a JOnAS Coordinator or a org.omg.CosTransactions.Coordinator
	try {
	    ctrl = new ControlImpl(ctx.getTimeout(), xid, ctx.getCoordinator());
	} catch(Exception e) {
	    TraceTm.error("Cannot create ControlImpl", e);
	}
	return ctrl;
    }

    /**
     * management method
     * @return the port number
     */ 
    public int getPortNumber()throws RemoteException {
	return getPort();
    }

    /**
     * management method
     * @return the local host name
     */
    public String getHostName() throws RemoteException {
	try {
	    return InetAddress.getLocalHost().getHostName();
	} catch (Exception e) {
	    throw new RemoteException(""+e);
	} 
    }
}
