1 22 23 package org.xquark.jdbc.datasource; 24 25 import java.sql.Driver ; 26 import java.sql.SQLException ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import java.util.Properties ; 30 31 import org.apache.commons.dbcp.SQLNestedException; 32 33 public class JDBCDataSourceFactory { 34 35 36 37 private static final String [] DRIVER_LIST = { 38 "com.mysql.jdbc.Driver", 39 "com.microsoft.jdbc.sqlserver.SQLServerDriver", 40 "com.sybase.jdbc2.jdbc.SybDriver", 41 "oracle.jdbc.driver.OracleDriver" 42 }; 43 44 private ClassLoader classLoader; 45 private List drivers; 46 47 private JDBCDataSourceFactory(ClassLoader cl) { 48 if (cl != null) classLoader = cl; 49 else classLoader = getClass().getClassLoader(); 50 drivers = new ArrayList (); 51 for (int i = 0; i < DRIVER_LIST.length; i++) { 52 try { 53 Class c = Class.forName(DRIVER_LIST[i], true, classLoader); 54 drivers.add(c.newInstance()); 55 } catch (ClassNotFoundException e) { 56 } catch (IllegalAccessException e) { 57 } catch (InstantiationException e) { 58 } 59 } 60 } 61 62 public static JDBCDataSourceFactory newInstance() { 63 return new JDBCDataSourceFactory(null); 64 } 65 66 public static JDBCDataSourceFactory newInstance(ClassLoader loader) { 67 return new JDBCDataSourceFactory(loader); 68 } 69 70 public JDBCDataSource newDataSource(String url, String user, String password) 71 throws SQLException 72 { 73 return newDataSource(null, url, user, password); 74 } 75 76 public JDBCDataSource newDataSource(String driverName, String url, String user, String password) 77 throws SQLException 78 { 79 Properties jdbcProperties = new Properties (); 80 jdbcProperties.put("user", user); 81 jdbcProperties.put("password", password); 82 return createDataSource(driverName, url, jdbcProperties); 83 } 84 85 public JDBCDataSource newDataSource(String driverName, String url, Properties jdbcProperties) 86 throws SQLException 87 { 88 if (jdbcProperties.get("user") == null) 89 throw new SQLException ("Missing user information in JDBC properties"); 90 if (jdbcProperties.get("password") == null) 91 throw new SQLException ("Missing password information in JDBC properties"); 92 return createDataSource(driverName, url, jdbcProperties); 93 } 94 95 private JDBCDataSource createDataSource(String driverName, String url, Properties jdbcProperties) 96 throws SQLException 97 { 98 Driver driver = null; 99 if (driverName != null) { 100 try { 101 Class c = Class.forName(driverName, true, classLoader); 102 Driver d = (Driver ) c.newInstance(); 103 if (d.acceptsURL(url)) { 104 driver = d; 105 } 106 } catch (ClassNotFoundException e) { 107 throw new SQLNestedException("Could not load driver "+driverName, e); 108 } catch (InstantiationException e) { 109 throw new SQLNestedException("Could not create driver "+driverName, e); 110 } catch (IllegalAccessException e) { 111 throw new SQLNestedException("Could not create driver "+driverName, e); 112 } 113 } else { 114 for (int i = 0; driver == null && i < drivers.size(); i++) { 115 Driver d = (Driver ) drivers.get(i); 116 if (d.acceptsURL(url)) { 117 driver = d; 118 } 119 } 120 } 121 if (driver == null) 122 throw new SQLException ("No suitable driver found for "+url); 123 return new JDBCDataSource(driver, url, jdbcProperties); 124 } 125 126 } 127 | Popular Tags |