/*
 * Copyright (C) 2013, 2014 Brett Wooldridge
 *
 * 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 com.zaxxer.hikari.util;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;

import javax.sql.DataSource;

public final class DriverDataSource implements DataSource
{
    private final Driver driver;
    private final String jdbcUrl;
    private final Properties driverProperties;

    private PrintWriter logWriter;
    private int loginTimeout;

    public DriverDataSource(String jdbcUrl, Properties properties, String username, String password)
    {
        try
        {
            this.jdbcUrl = jdbcUrl;
            this.driverProperties = new Properties(properties);
            if (username != null)
            {
                driverProperties.put("user", driverProperties.getProperty("user", username));
            }
            if (password != null)
            {
                driverProperties.put("password", driverProperties.getProperty("password", password));
            }
            this.driver = DriverManager.getDriver(jdbcUrl);
        }
        catch (SQLException e)
        {
            throw new RuntimeException("Unable to get driver for JDBC URL "+ jdbcUrl);
        }
    }

    @Override
    public Connection getConnection() throws SQLException
    {
        return driver.connect(jdbcUrl, driverProperties);
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException
    {
        return getConnection();
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException
    {
        return logWriter;
    }

    @Override
    public void setLogWriter(PrintWriter logWriter) throws SQLException
    {
        this.logWriter = logWriter;
    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException
    {
        loginTimeout = seconds;
    }

    @Override
    public int getLoginTimeout() throws SQLException
    {
        return loginTimeout;
    }

    public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException
    {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException
    {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException
    {
        return false;
    }

    public void shutdown()
    {
        if (driver.getClass().getClassLoader() == this.getClass().getClassLoader())
        {
            try
            {
                DriverManager.deregisterDriver(driver);
            }
            catch (SQLException e)
            {
                // eat it
            }
        }
    }
}

@@ -692,32 +692,8 @@
 on;%0A
-import java.sql.Driver;%0A
 impo
@@ -715,24 +715,24 @@
 verManager;%0A
+
 import java.
@@ -922,41 +922,8 @@
 e%0A%7B%0A
-    private final Driver driver;%0A
     
@@ -1038,38 +1038,8 @@
 ter;
-%0A    private int loginTimeout;
 %0A%0A  
@@ -1577,32 +1577,33 @@
 ;%0A            %7D%0A
+%0A
             this
@@ -1602,55 +1602,195 @@
     
-this.driver = DriverManager.getDriver(jdbcUrl);
+if (DriverManager.getDriver(jdbcUrl) == null)%0A            %7B%0A                throw new IllegalArgumentException(%22DriverManager was unable to load driver for URL %22 + jdbcUrl);%0A            %7D
 %0A   
@@ -2038,22 +2038,35 @@
 urn 
-d
+D
 river
-.c
+Manager.getC
 onnect
+ion
 (jdb
@@ -2585,17 +2585,34 @@
         
-l
+DriverManager.setL
 oginTime
@@ -2618,18 +2618,17 @@
 eout
- = 
+(
 seconds
+)
 ;%0A  
@@ -2720,17 +2720,34 @@
  return 
-l
+DriverManager.getL
 oginTime
@@ -2749,16 +2749,18 @@
 nTimeout
+()
 ;%0A    %7D%0A
@@ -3207,307 +3207,14 @@
 n()%0A
+
     %7B%0A
-        if (driver.getClass().getClassLoader() == this.getClass().getClassLoader())%0A        %7B%0A            try%0A            %7B%0A                DriverManager.deregisterDriver(driver);%0A            %7D%0A            catch (SQLException e)%0A            %7B%0A                // eat it%0A            %7D%0A        %7D%0A
     

Use the DriverManager to create connections.