/*
 * Copyright: (c) TBEE.ORG
 * Version:   $Revision: 1.5 $
 * Modified:  $Date: 2007/07/05 13:09:49 $
 * By:        $Author: toeukpap $
 */
package nl.reinders.jdbc;

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

import org.tbee.util.JdbcUtil;

/**
 * This is for logging SQL statements to log4j.
 * 
 * URL is
 *   jdbc:log4j:[log4j-class|[prefix|]]driverclass#actualURL
 *   
 * for example:
 *   jdbc:log4j:oracle.jdbc.driver.OracleDriver#jdbc:oracle:thin:@localhost:1521:ORCL
 * or:
 *   jdbc:log4j:com.my.class|oracle.jdbc.driver.OracleDriver#jdbc:oracle:thin:@localhost:1521:ORCL
 * or:
 *   jdbc:log4j:com.my.class|myPrefix|oracle.jdbc.driver.OracleDriver#jdbc:oracle:thin:@localhost:1521:ORCL
 *   
 * @version $Revision: 1.5 $
 */
public class ReindersInformixAndLoggingDriver extends org.tbee.util.jdbc.Driver 
implements java.sql.Driver
{
	/** Standard variable for determining version of a class file. */
	static public final String SOURCECODE_VERSION = "$Revision: 1.5 $";
	
    // set up a Log4J category to log in
    static public org.apache.log4j.Logger log4j = org.apache.log4j.Logger.getLogger(org.tbee.util.jdbc.Driver.class);

	// ================================================================================================
    // DriverManager
    
    static
    {
    	register();
    }
    
    /**
     * Register the Driver with DriverManager
     */
    static public void register()
    {
        // Register the Driver with DriverManager
        try
        {
    		if (log4j.isDebugEnabled()) log4j.debug("registering driver with the DriverManager");
            DriverManager.registerDriver(new ReindersInformixAndLoggingDriver());
        }
        catch (SQLException ex)
        {
            throw new RuntimeException("JDBC driver can't be registered: " + ex);
        }
    }
    
	// ================================================================================================
    // Driver
    
	final static public String PREFIX = "jdbc:reinders:";
	
	public Connection connect(String url, Properties info) throws SQLException
	{
		// check URL
		// do not use acceptsURL, because then inheriting this Driver will not work
		if (!url.startsWith(PREFIX))
		{
			if (log4j.isDebugEnabled()) log4j.debug("Prefix does not match, this driver cannot connect: " + url);
			return null;
		}
		else  if (log4j.isDebugEnabled()) log4j.debug("Connect " + url);
		
		// replaced prefix
		String lUrl = org.tbee.util.jdbc.Driver.PREFIX + url.substring(PREFIX.length());

		// connect
		Connection lConnection = super.connect(lUrl, info);
		
		try
		{
			// configure the connection
			JdbcUtil.execute(lConnection, "set lock mode to wait 30");
			JdbcUtil.execute(lConnection, "set isolation to committed read");
			JdbcUtil.execute(lConnection, "set role javaapp1");
		}
		catch (SQLException e)
		{
			// for now we log the error
			log4j.error(e);
		}
		
		// make it not closable
		lConnection = ReindersInformixConnection.wrap(lConnection);
		
		// done
		return lConnection;
	}

	public boolean acceptsURL(String url) throws SQLException
	{
		// must begin with correct string
		if (!url.startsWith(PREFIX))
		{
			if (log4j.isDebugEnabled()) log4j.debug("Prefix does not match, this driver cannot connect: " + url);
			return false;
		}
		else if (log4j.isDebugEnabled()) log4j.debug("acceptsURL: " + url);
		
		// accept
		return true;
	}
}
