/*
 * Created on December 12, 2003
 *
 * ConnectionFactoryImpl.java is used to test the J2EE Connector
 * as implemented by JOnAS. 
 * 
 */
package ersatz.resourceadapter;

import javax.naming.Reference;
import javax.naming.NamingException;
import javax.resource.ResourceException;
import javax.resource.NotSupportedException;
import javax.resource.spi.ConnectionManager;
//  interfaces implemented in this class
import javax.resource.cci.ConnectionFactory;
import javax.resource.cci.Connection;
import javax.resource.cci.ConnectionSpec;  // j2ee 1.5
import javax.resource.cci.RecordFactory;
import javax.resource.cci.ResourceAdapterMetaData;
import java.io.Serializable;
import javax.resource.Referenceable;
/**
 * @author Bob Kruse
 *
 * used to test the J2EE Connector as implemented by JOnAS.
 *
 */
public class ConnectionFactoryImpl implements ConnectionFactory,
    Referenceable, Serializable

{
	Reference reference;
    private ConnectionManager cm;
    private ManagedConnectionFactoryImpl mcf; // used in cm.allocateConnection
    private ConnectionSpecImpl cs;  // ConnectionSpec
    protected boolean managed = true;
    private String userName = "";
    private String passWord = ""; 
    String cName = "ConnectionFactoryImpl";
    
    //
    //  ConnectionFactory instance created during lookup() by Application Server
    // 
    public ConnectionFactoryImpl() {
    }
    public ConnectionFactoryImpl(ManagedConnectionFactoryImpl mcf, ConnectionManager cm) {
        this.mcf=mcf;
        this.cm=cm;
        
    }
    //
    // Container Managed Sign-on calls getConnection() from the Application Component
    //
    // Container-managed sign-on when deployment file contains line below
    //
    //  <res-auth>Container</res-auth>
    //
    public Connection getConnection()
		throws ResourceException
	{
        Utility.log(cName+".getConnection"+" (Container Managed Sign-on)");
		ConnectionImpl conn = null;
        try {
            conn = (ConnectionImpl)getConnection(null);
            return conn;
        } catch (ResourceException ex) {
            throw ex;
        }
	}
    //
    // Component Managed Sign-on calls getConnection(cs) directly from the Application Component
    //
    // Component-managed sign-on when file "connector.xml / secured.jar" contains line below
    //
    //  <res-auth>Application</res-auth>
    //
	public Connection getConnection(ConnectionSpec connectionspec)
		throws ResourceException
	{
        ConnectionImpl conn = null;
        ConnectionSpecImpl cs = null;
        ConnectionRequestInfoImpl jcri=null; // 

        if (connectionspec==null) {
            mcf.setRes_Auth("Container");
            Utility.log(cName+".getConnection detected res-auth='"+mcf.getRes_Auth()+"'");
            // TODO what now? anything?
            // now pass null to cm.allocateConnection(mcf, null);
        } else {
            mcf.setRes_Auth("Application");
            Utility.log(cName+".getConnection detected res-auth='"+mcf.getRes_Auth()+"'");
            cs = (ConnectionSpecImpl)connectionspec;
            // load user and password into ConnectionRequestInfo instance
            jcri = new ConnectionRequestInfoImpl();
            jcri.setUserName(cs.getUserName());
            jcri.setPassword(cs.getPassword());
            
        }
        Utility.log(cName+".getConnection calling cm.allocateConnection");
        try {
            conn = (ConnectionImpl)cm.allocateConnection(mcf, jcri);
            if (conn==null) {
                Utility.log(cName+". getConnection, cm.allocateConnection"
                      +" error: Null connection object returned");
                throw new ResourceException("Null connection object returned by allocateConnection");
            }
            conn.crii=jcri;
            return conn;
        } catch (IllegalStateException is) {
            Utility.log(cName+".getConnection IllegalStateException"+is);
            throw is;
        } catch (ResourceException re) {
            Utility.log(cName+".getConnection ResourceException="+re);
            throw re; 
        }
	}

	public RecordFactory getRecordFactory()
		throws ResourceException
	{
		NotSupportedException nse = new NotSupportedException(
			"RecordFactory is not currently supported");
        Utility.log(cName+".getRecordFactory "+nse);
		throw nse;
	}

	public ResourceAdapterMetaData getMetaData()
		throws ResourceException
	{
        Utility.log(cName+".getMetaData");
		ResourceAdapterMetaData rd = null; // TODO  do something
		return rd;
	}
    public ConnectionManager getCM() {
        return cm;
    }
    public ManagedConnectionFactoryImpl getMcf() {
        return mcf;
    }
    //
    // *****************
    //    Reference
    // *****************
    //
	/** Required by the referencable attribute
	 *  @param  ref Reference object
	**/
	public void setReference(javax.naming.Reference ref) 
	{
	  this.reference = ref;
	}

	/** Required by the referencable attribute
	 *  @return  Reference object
	**/
	public Reference getReference() 
		throws NamingException
	{
	  return reference;
	}
}
