001package org.andromda.maven.plugin.andromdapp;
002
003import java.lang.reflect.Method;
004import java.sql.Connection;
005import java.sql.Driver;
006import java.sql.DriverPropertyInfo;
007import java.sql.SQLException;
008import java.sql.SQLFeatureNotSupportedException;
009import java.util.Properties;
010import java.util.logging.Logger;
011
012/**
013 * This just wraps a regular JDBC driver so that we can successfully load
014 * a JDBC driver without having to use the System class loader.
015 *
016 * @author Chad Brandon
017 */
018public class JdbcDriverWrapper
019    implements Driver
020{
021    private Driver driver;
022
023    /**
024     * @param driver
025     */
026    public JdbcDriverWrapper(Driver driver)
027    {
028        this.driver = driver;
029    }
030
031    /**
032     * @see java.sql.Driver#acceptsURL(String)
033     */
034    public boolean acceptsURL(String url)
035        throws SQLException
036    {
037        return this.driver.acceptsURL(url);
038    }
039
040    /**
041     * @see java.sql.Driver#connect(String, java.util.Properties)
042     */
043    public Connection connect(
044        String url,
045        Properties properties)
046        throws SQLException
047    {
048        return this.driver.connect(
049            url,
050            properties);
051    }
052
053    /**
054     * @see java.sql.Driver#getMajorVersion()
055     */
056    public int getMajorVersion()
057    {
058        return this.driver.getMajorVersion();
059    }
060
061    /**
062     * @see java.sql.Driver#getMinorVersion()
063     */
064    public int getMinorVersion()
065    {
066        return this.driver.getMinorVersion();
067    }
068
069    /**
070     * @see java.sql.Driver#getPropertyInfo(String, java.util.Properties)
071     */
072    public DriverPropertyInfo[] getPropertyInfo(
073        String url,
074        Properties properties)
075        throws SQLException
076    {
077        return this.driver.getPropertyInfo(
078            url,
079            properties);
080    }
081
082    /**
083     * @see java.sql.Driver#jdbcCompliant()
084     */
085    public boolean jdbcCompliant()
086    {
087        return this.driver.jdbcCompliant();
088    }
089
090    /**
091     * @see java.sql.Driver#getParentLogger()
092     */
093    // See http://stackoverflow.com/questions/8503338/new-method-added-in-javax-sql-commondatasource-in-1-7
094    // and https://forums.oracle.com/message/10089368
095    //@Override // This means it must be compiled with JDK7
096    public Logger getParentLogger() throws SQLFeatureNotSupportedException
097    {
098        Logger logger = null;
099        try
100        {
101            // Throws a runtime exception is this method is not in the runtime JDBC driver
102            // Use reflection to determine if the method exists at runtime
103            //logger = this.driver.getParentLogger(); // When JDK6 is no longer supported
104            Method method = this.driver.getClass().getMethod("getParentLogger");
105            logger = (Logger)method.invoke(this.driver);
106        }
107        catch (Exception e)
108        {
109            // Not using JDK7, method does not exist, ignore or return UnsupportedOperation.
110            // A better approach would be to use something other than java.util.Logger
111        }
112        return logger;
113    }
114}