/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.database;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * Get database connections used by Bodington from JNDI.
 * <p>
 * Properties:
 * <table border="1">
 * <tr>
 *  <th>Property</th><th>Description</th><th>Default</th><th>Required</th>
 * </tr>
 * <tr>
 *  <td>sqldatabase.jndi.context</td>
 *  <td>The string to use as the lookup against the <code>java:/comp/env</code> context.</td>
 *  <td>jdbc/bodington</td>
 *  <td>No</td>
 * </tr>
 * </table>
 * 
 * @author buckett
 */
public class JNDIConnectionPool implements ConnectionPool
{
    Context envContext;
    private static final String DEFAULT_CONTEXT = "jdbc/bodington";
    private String context = DEFAULT_CONTEXT;
	private DataSource datasource;

    /**
     * Create a JNDI connection pool.
     * @param properties The configuration information for this pool.
     */
	public JNDIConnectionPool( Properties properties ) throws ConnectionPoolException
	{
		context = properties.getProperty("sqldatabase.jndi.context", DEFAULT_CONTEXT);
		try 
		{
			Context initContext = new InitialContext();
			envContext  = (Context)initContext.lookup("java:/comp/env");
			datasource = (DataSource)envContext.lookup(context);
		}
		catch (NamingException ne)
		{
			throw new ConnectionPoolException("Error getting the initial contexts",ne);
		}
		
	}

    /** 
     * Gets a database connection from a JNDI context.
     */
    public synchronized Connection getConnection(Object user) throws ConnectionPoolException
    {
        try
        {
        return datasource.getConnection();
        }
        catch (SQLException sqle)
        {
            throw new ConnectionPoolException("Error getting connection", sqle);
        }
    }

    /**
     * Free the database connection. In the JNDI world we just close the connection
     * and it goes back into the pool.
     * @param connection The database connection to close.
     */
    public void freeConnection(Connection connection)
        throws ConnectionPoolException
    {
        try
        {
            connection.close();
        }
        catch (SQLException sqle){}
       
    }

}
