/*
 * Created on March 3, 2004
 *
 * DefaultConnectionManager.java is used to test the J2EE Connector
 * as implemented by JOnAS. 
 * 
*/
package ersatz.resourceadapter;

import java.io.Serializable;
import javax.resource.ResourceException;
import javax.resource.spi.ConnectionManager;
import javax.resource.cci.ConnectionFactory;
import javax.resource.spi.ConnectionRequestInfo;
import javax.resource.spi.ManagedConnectionFactory;
import javax.resource.spi.ResourceAdapterInternalException;

/**
 * @author Bob Kruse
 *
 * used to test the J2EE Connector as implemented by JOnAS.
 *
 */
public class DefaultConnectionManager 
       implements ConnectionManager, Serializable 
{

  String cName = "DefaultConnectionManager";

  /** <p>This method gets called by the connection factory instance. 
   *  </p>
   *  
   *  @param   ManagedConnectionFactory
   *                       used by application server to delegate
   *                       connection matching/creation
   *  @param   ConnectionRequestInfo     
   *                       connection request Information
   *
   *  @return  connection handle to the appropriate GCOS system
   * 
   *
   *  @throws  ResourceException     Generic exception
   *  @throws  ResourceAllocationException
   *                                 Failed to allocate system resources for
   *                                 connection request
   *  @throws  ResourceAdapterInternalException
   *                                 Resource adapter related error condition
  **/
    public Object allocateConnection(ManagedConnectionFactory MCF,
      			            ConnectionRequestInfo info)
             throws ResourceException 
    {
        Object obj;

        ManagedConnectionFactoryImpl mcf = (ManagedConnectionFactoryImpl) MCF;
        try {
            ManagedConnectionImpl mc = 
                (ManagedConnectionImpl) mcf.createManagedConnection(null, info);
	        obj = mc.getConnection(null, info);
	        Utility.log(cName+".allocateConnection   mc.getConnection(null, info) obj="+obj);
	        return obj;
        }
          catch (ResourceAdapterInternalException raie)
        {
	        throw raie;
        }
          catch (Exception ex)
        {
            ResourceException re = new ResourceException(ex.getMessage());
	        throw re;
        }
    }

}
