1 17 package org.apache.servicemix.jdbc; 18 19 import java.sql.Connection ; 20 import java.sql.SQLException ; 21 22 import org.apache.activeio.util.FactoryFinder; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.apache.servicemix.jdbc.adapter.DefaultJDBCAdapter; 26 27 public class JDBCAdapterFactory { 28 29 private static final Log log = LogFactory.getLog(JDBCAdapterFactory.class); 30 private static FactoryFinder factoryFinder = new FactoryFinder("META-INF/services/org/apache/servicemix/jdbc/"); 31 32 public static JDBCAdapter getAdapter(Connection connection) { 33 JDBCAdapter adapter = null; 34 try { 35 36 String driverName = connection.getMetaData().getDriverName(); 38 driverName = driverName.replaceAll("[^a-zA-Z0-9\\-]", "_").toLowerCase(); 39 40 try { 41 adapter = (JDBCAdapter) factoryFinder.newInstance(driverName); 42 log.info("Database driver recognized: [" + driverName + "]"); 43 } catch (Throwable e) { 44 log.warn("Database driver NOT recognized: [" + driverName 45 + "]. Will use default JDBC implementation."); 46 } 47 48 } catch (SQLException e) { 49 log.warn("JDBC error occurred while trying to detect database type. Will use default JDBC implementation: " 50 + e.getMessage()); 51 log("Failure details: ", e); 52 } 53 54 if (adapter == null) { 57 adapter = new DefaultJDBCAdapter(); 58 } 59 60 return adapter; 61 } 62 63 public static void log(String msg, SQLException e) { 64 if (log.isDebugEnabled()) { 65 if (log.isDebugEnabled()) { 66 String s = msg + e.getMessage(); 67 while (e.getNextException() != null) { 68 e = e.getNextException(); 69 s += ", due to: " + e.getMessage(); 70 } 71 log.debug(s, e); 72 } 73 } 74 } 75 } 76 | Popular Tags |