1 package net.sourceforge.jdbclogger; 2 18 19 import net.sourceforge.jdbclogger.core.AbstractWrapperDriver; 20 import net.sourceforge.jdbclogger.core.ConnectionWrapper; 21 import net.sourceforge.jdbclogger.core.config.Environment; 22 import net.sourceforge.jdbclogger.core.config.JdbcLoggerConstants; 23 import net.sourceforge.jdbclogger.core.util.ConfigHelper; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.net.URL ; 30 import java.sql.Connection ; 31 import java.sql.Driver ; 32 import java.sql.DriverManager ; 33 import java.sql.SQLException ; 34 import java.util.Enumeration ; 35 import java.util.Properties ; 36 37 38 42 public class JdbcLoggerDriver extends AbstractWrapperDriver { 43 44 private static Log log = LogFactory.getLog(JdbcLoggerDriver.class); 45 46 static 47 { 48 String userDriverClassName = Environment.getProperties().getProperty(JdbcLoggerConstants.USER_DRIVER_PROPERTY_NAME); 50 51 if (userDriverClassName == null){ 53 log.fatal("Missing jdbclogger.properties in classpath with content:\n" + 54 JdbcLoggerConstants.USER_DRIVER_PROPERTY_NAME+"=<your driver class name>"); 55 } 56 else { 57 try { 58 Driver userDriver = findRegisteredUserDriver(userDriverClassName); 60 61 if (userDriver == null) { 62 try { 64 Class.forName(userDriverClassName); 65 userDriver = findRegisteredUserDriver(userDriverClassName); 67 } 68 catch (ClassNotFoundException exc) { 69 } 71 } 72 73 if (userDriver != null) { 74 AbstractWrapperDriver wrapperDriver = getWrapperDriver(userDriverClassName); 75 wrapperDriver.setDriver(userDriver); 76 77 DriverManager.deregisterDriver(userDriver); 78 DriverManager.registerDriver(wrapperDriver); 79 } 80 else { 81 log.fatal("Could not find user driver '"+userDriverClassName+"' in the DriverManager."); 82 } 83 } 84 catch(Exception exc) { 85 log.error("Exception while registering driver.", exc); 86 } 87 } 88 } 89 90 93 public JdbcLoggerDriver() { 94 super(); 95 } 96 97 101 public static Driver findRegisteredUserDriver(String userDriverClassName) { 102 Enumeration en = DriverManager.getDrivers(); 103 while (en.hasMoreElements()){ 104 Driver aDriver = (Driver) en.nextElement(); 105 if (aDriver.getClass().getName().equals(userDriverClassName)){ 106 return aDriver; 107 } 108 } 109 110 return null; 111 } 112 113 117 public static AbstractWrapperDriver getWrapperDriver(String userDriverClassName){ 118 Enumeration driverList = ConfigHelper.getResources("META-INF/" + JdbcLoggerConstants.WRAPPER_DRIVER_CONFIG_FILE_NAME); 119 while (driverList.hasMoreElements()) { 120 Properties driverProperties = new Properties (); 121 URL resUrl = (URL ) driverList.nextElement(); 122 InputStream res = null; 123 try { 124 res = resUrl.openStream(); 125 driverProperties.load(res); 126 } 127 catch (IOException e) { 128 log.error("Error reading "+JdbcLoggerConstants.WRAPPER_DRIVER_CONFIG_FILE_NAME); 129 } 130 finally { 131 if (res != null) 132 try { 133 res.close(); 134 } 135 catch (IOException e) { 136 log.error("Closing input stream", e); 137 } 138 } 139 140 if (userDriverClassName.equals(driverProperties.getProperty(JdbcLoggerConstants.TARGET_DRIVER_PROPERTY_NAME))){ 141 String wrapperDriver = driverProperties.getProperty(JdbcLoggerConstants.WRAPPER_DRIVER_PROPERTY_NAME); 142 try { 143 return (AbstractWrapperDriver) Class.forName(wrapperDriver).newInstance(); 144 } 145 catch (Exception exc) { 146 log.error("Unable to instantiate wrapper class: " + exc); 147 } 148 } 149 } 150 return new JdbcLoggerDriver(); 151 } 152 153 public Connection connect(String url, Properties properties) throws SQLException { 154 if (getDriver() == null) 155 return null; 156 157 return new ConnectionWrapper(getDriver().connect(url, properties), formatters); 158 } 159 } 160 | Popular Tags |