/**
 * Copyright (c) 2004 Ryan Bloom
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Ryan Bloom. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Ryan Bloom.
 */
package net.rkbloom.jdbcLogDriver;

import org.apache.log4j.Logger;

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

/**
 * LogDriver
 * @version $Rev$
 */
public class LogDriver implements Driver {

    private static Logger log = Logger.getLogger(LogDriver.class);
    
    /**
     * {@inheritDoc}
     */
    public int getMajorVersion() {
        return 1;
    }

    /**
     * {@inheritDoc}
     */
    public int getMinorVersion() {
        return 0;
    }

    /**
     * {@inheritDoc}
     */
    public boolean jdbcCompliant() {
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public boolean acceptsURL(String url) throws SQLException {
        return url.startsWith("jdbc:log");
    }

    /**
     * {@inheritDoc}
     */
    public Connection connect(String url, Properties info) throws SQLException {
        if (!acceptsURL(url)) {
            return null;
        }
        String realUrl = parseUrl(url);
        log.debug("Trying to find: " + realUrl);
        return new LogConnection(DriverManager.getConnection(realUrl, info));
    }

    /**
     * {@inheritDoc}
     */
    public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
        throws SQLException {
        return new DriverPropertyInfo[0];
    }

    private String parseUrl(String url) {
        String newUrl = url.replaceFirst(":log", "");
        return newUrl;
    }
    
    static {
        try {
            DriverManager.registerDriver(new LogDriver());
        }
        catch (SQLException e) {
            log.error("Couldn't register LogDriver.", e);
        }
    }
}

@@ -1439,32 +1439,51 @@
 ll;%0D%0A        %7D%0D%0A
+        try %7B%0D%0A    
         String r
@@ -1511,24 +1511,28 @@
 );%0D%0A        
+  
+  
 log.debug(%22T
@@ -1557,24 +1557,28 @@
  realUrl);%0D%0A
+    
         retu
@@ -1640,24 +1640,225 @@
 l, info));%0D%0A
+        %7D%0D%0A        catch (ClassNotFoundException e) %7B%0D%0A            throw new SQLException(%22Couldn't load class for embedded driver: %22 +%0D%0A                                   e.getMessage());%0D%0A        %7D%0D%0A
     %7D%0D%0A%0D%0A   
@@ -2099,62 +2099,485 @@
 rl) 
-%7B%0D%0A        String newUrl = url.replaceFirst(%22:log%22, %22%22
+throws ClassNotFoundException %7B%0D%0A        // Get the class for the real JDBC driver.%0D%0A        int start = url.indexOf(%22:log:%22) + %22:log:%22.length();%0D%0A        int end = url.indexOf(%22:%22, start);%0D%0A        String realClass = url.substring(start, end);%0D%0A        log.debug(%22Real JDBC driver Class: %22 + realClass);%0D%0A        Class.forName(realClass);%0D%0A        %0D%0A        String newUrl = url.replaceFirst(%22:log:%22 + realClass, %22%22);%0D%0A        log.debug(%22Real JDBC connection string: %22 + newUrl
 );%0D%0A

Embed the real jdbc driver class in the logdriver url. The logdriver now finds that class name and loads it automatically.