package applis.ejb.perfs;

import javax.rmi.PortableRemoteObject;
import javax.transaction.UserTransaction;
import javax.naming.InitialContext;
import java.util.Properties;


/**
 * A litlle class that helps clients to lookup UserTransaction. Indeed the InitialContext
 * requires different properties if we are using David or RMI
 */
public class Transaction {

    static private InitialContext ictx = null;

    static public UserTransaction getUserTransaction() throws javax.naming.NamingException {

	if (ictx == null) {
	    String registryPort = System.getProperty("jonas.registryport");

	    if (registryPort != null) {
		// it means we are using David
		Properties prop = new Properties();
		prop.put("java.naming.factory.initial", "com.sun.jndi.rmi.registry.RegistryContextFactory");
		prop.put("java.naming.provider.url", "rmi://localhost:" + registryPort);
		ictx = new InitialContext(prop);
	    }
	    else {
		ictx = new InitialContext(); 
	    }
	}
	 
	return (UserTransaction) PortableRemoteObject.narrow(ictx.lookup("javax.transaction.UserTransaction"), UserTransaction.class);
    }
}
