/* Copyright (c) 2002, Oracle Corporation. All Rights Reserved. */ 
package oracle.portal.sample.portalTools.devguide.jcasample;

import javax.resource.cci.ConnectionFactory;
import javax.resource.cci.ConnectionSpec;
import javax.resource.cci.Connection;
import javax.resource.cci.RecordFactory;
import javax.resource.cci.ResourceAdapterMetaData;
import javax.resource.ResourceException;
import javax.resource.NotSupportedException;

import javax.naming.Reference;

/**
 * This class implements the connection factory for the sample adapter. It takes
 * connection properties to create the connection. 
 * 
 * This class creates a <code>Connection</code> object for the sample adapter.
 * For this simple implementation, no other methods except those creating 
 * connections are implemented.
 *
 * In the provider definition, this should be defined as follows:
 * <pre>
 * &ltconnectionHandler class="oracle.webdb.reformlet.data.jca.ConnectionHandler">
 *   &ltconnectionFactory class="oracle.portal.sample.portalTools.devguide.jcasample.SampleConnectionFactory"/>
 * &lt/connectionHandler>
 * </pre>
 *
 * @see oracle.webdb.reformlet.data.jca.sample.SampleConnection
 */
public class SampleConnectionFactory implements ConnectionFactory 
{
  // To store the referencce set by JNDI naming. We do not use it, but will 
  // simply store it to return when asked.  
  private javax.naming.Reference mRef = null;

  /**
   * Create a connection with the properties.
   *
   * @param properties Connection properties.
   * @return Created connection object of the adapter.
   * @throws ResourceException If failed to create connection. 
   */
  public Connection getConnection(ConnectionSpec properties) 
    throws ResourceException  
  {
    // The connection spec contains the user name and password. We will only 
    // accept user name as "scott" and password as "tiger". This is only to show 
    // that in real life, getConnection can throw exceptions after verifying the 
    // connection or the connection properties.

    String uName = ((SampleConnectionSpec)properties).getUserName();
    String password = ((SampleConnectionSpec)properties).getPassword();

    if (((null == uName) || !(uName.equals("scott"))) ||
        ((null == password) && (password.equals("tiger"))))
      throw new ResourceException("Failed to authenticate.");
  
    return new SampleConnection(
      ((SampleConnectionSpec)properties).getDataLocation());
  }

  /**
   * This way of creating connection object is not supported for this sample
   * adapter. So it will always throw exception if called.
   *
   * @throws ResourceException always, because the adapter needs some vlaue 
   * to create the connection. 
   */
  public Connection getConnection() throws ResourceException  
  {
    throw new ResourceException("Missing properties to create connection.");
  }

  /** 
   * The sample adapter does not support this. 
   */
  public RecordFactory getRecordFactory() throws ResourceException
  {
    throw new NotSupportedException("Not supported by the Sample Adapter"); 
  }

  /** 
   * The sample adapter does not support this. 
   */
  public ResourceAdapterMetaData getMetaData() throws ResourceException
  {
    throw new NotSupportedException("Not supported by the Sample Adapter"); 
  }

  /** 
   * Set the JNDI name reference.
   *
   * This is an implementation of ConnectionFactory interface method for 
   * JNDI support. 
   */
  public void setReference(Reference reference) 
  {
    mRef = reference;
  }

  /** 
   * Get the JNDI name reference.
   *
   * This is an implementation of ConnectionFactory interface method for 
   * JNDI support. 
   */
  public Reference getReference() 
  {
    return mRef;
  }
      
}
