1 56 package org.objectstyle.cayenne.dba; 57 58 import java.sql.Connection ; 59 import java.sql.DatabaseMetaData ; 60 import java.sql.SQLException ; 61 import java.util.ArrayList ; 62 import java.util.List ; 63 64 import javax.sql.DataSource ; 65 66 import org.apache.log4j.Logger; 67 import org.objectstyle.cayenne.dba.db2.DB2Sniffer; 68 import org.objectstyle.cayenne.dba.hsqldb.HSQLDBSniffer; 69 import org.objectstyle.cayenne.dba.mysql.MySQLSniffer; 70 import org.objectstyle.cayenne.dba.openbase.OpenBaseSniffer; 71 import org.objectstyle.cayenne.dba.oracle.OracleSniffer; 72 import org.objectstyle.cayenne.dba.postgres.PostgresSniffer; 73 import org.objectstyle.cayenne.dba.sqlserver.SQLServerSniffer; 74 import org.objectstyle.cayenne.dba.sybase.SybaseSniffer; 75 76 85 public class DbAdapterDetector { 88 89 private static Logger logObj = Logger.getLogger(DbAdapterDetector.class); 90 91 protected List factories; 92 93 public DbAdapterDetector() { 94 this.factories = new ArrayList (); 95 96 addFactory(new MySQLSniffer()); 98 addFactory(new PostgresSniffer()); 99 addFactory(new OracleSniffer()); 100 addFactory(new SQLServerSniffer()); 101 addFactory(new HSQLDBSniffer()); 102 addFactory(new DB2Sniffer()); 103 addFactory(new SybaseSniffer()); 104 addFactory(new OpenBaseSniffer()); 105 } 106 107 110 public void clearFactories() { 111 this.factories.clear(); 112 } 113 114 public void addFactory(DbAdapterFactory factory) { 115 this.factories.add(factory); 116 } 117 118 public DbAdapter adapterForDataSource(DataSource ds) throws SQLException { 119 120 Connection c = ds.getConnection(); 121 122 long t0 = System.currentTimeMillis(); 124 125 try { 126 return adapterForMetaData(c.getMetaData()); 127 } 128 finally { 129 try { 130 c.close(); 131 } 132 catch (SQLException e) { 133 } 135 136 if (logObj.isInfoEnabled()) { 137 long t1 = System.currentTimeMillis(); 138 logObj.info("auto-detection took " + (t1 - t0) + " ms."); 139 } 140 } 141 } 142 143 146 protected DbAdapter createDefaultAdapter() { 147 return new JdbcAdapter(); 148 } 149 150 154 protected DbAdapter adapterForMetaData(DatabaseMetaData md) throws SQLException { 155 156 if (logObj.isInfoEnabled()) { 157 logObj.info("DB name: " + md.getDatabaseProductName()); 158 } 159 160 162 for (int i = factories.size() - 1; i >= 0; i--) { 165 DbAdapterFactory factory = (DbAdapterFactory) factories.get(i); 166 DbAdapter adapter = factory.canHandleDatabase(md); 167 168 if (adapter != null) { 169 return adapter; 170 } 171 } 172 173 logObj.info("Unrecognized database '" 174 + md.getDatabaseProductName() 175 + "', using default adapter."); 176 177 return createDefaultAdapter(); 178 } 179 } 180 | Popular Tags |