1 19 20 package org.netbeans.modules.db.explorer; 21 22 import java.sql.Connection ; 23 import java.sql.Driver ; 24 import java.sql.DriverManager ; 25 import java.sql.SQLException ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import java.util.Properties ; 30 import java.util.Set ; 31 import java.util.WeakHashMap ; 32 import org.netbeans.api.db.explorer.JDBCDriver; 33 import org.openide.ErrorManager; 34 35 54 public class DbDriverManager { 55 56 private static final ErrorManager LOGGER = ErrorManager.getDefault().getInstance("org.netbeans.modules.db.explorer.DbDriverManager"); private static final boolean LOG = LOGGER.isLoggable(ErrorManager.INFORMATIONAL); 58 59 private static final DbDriverManager DEFAULT = new DbDriverManager(); 60 61 private Set registeredDrivers; 62 63 66 private Map conn2Driver = new WeakHashMap (); 67 68 71 private Map driver2Loader = new WeakHashMap (); 72 73 private DbDriverManager() { 74 } 75 76 79 public static DbDriverManager getDefault() { 80 return DEFAULT; 81 } 82 83 90 public Connection getConnection(String databaseURL, Properties props, JDBCDriver jdbcDriver) throws SQLException { 91 if (LOG) { 92 LOGGER.log(ErrorManager.INFORMATIONAL, "Attempting to connect to '" + databaseURL + "'"); } 94 95 Driver driver = getDriverInternal(databaseURL, jdbcDriver, false); 98 if (driver != null) { 99 Connection conn = driver.connect(databaseURL, props); 100 if (conn == null) { 101 if (LOG) { 102 LOGGER.log(ErrorManager.INFORMATIONAL, driver.getClass().getName() + ".connect() returned null"); } 104 throw createDriverNotFoundException(); 105 } 106 synchronized (conn2Driver) { 107 conn2Driver.put(conn, driver); 108 } 109 return conn; 110 } 111 112 try { 114 Connection conn = DriverManager.getConnection(databaseURL, props); 115 synchronized (conn2Driver) { 116 conn2Driver.put(conn, driver); 117 } 118 return conn; 119 } catch (SQLException e) { 120 } 122 123 throw createDriverNotFoundException(); 124 } 125 126 129 public Connection getSameDriverConnection(Connection existingConn, String databaseURL, Properties props) throws SQLException { 130 if (existingConn == null) { 131 throw new NullPointerException (); 132 } 133 Driver driver = null; 134 synchronized (conn2Driver) { 135 if (!conn2Driver.containsKey(existingConn)) { 136 throw new IllegalArgumentException ("A connection not obtained through DbDriverManager was passed."); } 138 driver = (Driver )conn2Driver.get(existingConn); 139 } 140 if (driver != null) { 141 Connection newConn = driver.connect(databaseURL, props); 142 if (newConn == null) { 143 throw new SQLException ("Unable to connect using existingConn's original driver", "08001"); } 145 conn2Driver.put(newConn, driver); 146 return newConn; 147 } else { 148 return DriverManager.getConnection(databaseURL, props); 149 } 150 } 151 152 155 public synchronized void registerDriver(Driver driver) { 156 if (registeredDrivers == null) { 157 registeredDrivers = new HashSet (); 158 } 159 registeredDrivers.add(driver); 160 } 161 162 165 public synchronized void deregisterDriver(Driver driver) { 166 if (registeredDrivers == null) { 167 return; 168 } 169 registeredDrivers.remove(driver); 170 } 171 172 178 public Driver getDriver(String databaseURL, JDBCDriver jdbcDriver) throws SQLException { 179 Driver d = getDriverInternal(databaseURL, jdbcDriver, true); 180 if (d == null) { 181 throw createDriverNotFoundException(); 182 } 183 return d; 184 } 185 186 189 private Driver getDriverInternal(String databaseURL, JDBCDriver jdbcDriver, boolean lookInDriverManager) throws SQLException { 190 synchronized (this) { 192 if (registeredDrivers != null) { 193 for (Iterator i = registeredDrivers.iterator(); i.hasNext();) { 194 Driver d = (Driver )i.next(); 195 try { 196 if (d.acceptsURL(databaseURL)) { 197 return d; 198 } 199 } catch (SQLException e) { 200 } 202 } 203 } 204 } 205 206 if (jdbcDriver != null) { 208 ClassLoader l = getClassLoader(jdbcDriver); 209 try { 210 return (Driver )Class.forName(jdbcDriver.getClassName(), true, l).newInstance(); 211 } catch (Exception e) { 212 SQLException sqlex = createDriverNotFoundException(); 213 sqlex.initCause(e); 214 throw sqlex; 215 } 216 } 217 218 if (lookInDriverManager) { 220 try { 221 return DriverManager.getDriver(databaseURL); 222 } catch (SQLException e) { 223 } 225 } 226 227 return null; 228 } 229 230 private ClassLoader getClassLoader(JDBCDriver driver) { 231 ClassLoader loader = null; 232 synchronized (driver2Loader) { 233 loader = (ClassLoader )driver2Loader.get(driver); 234 if (loader == null) { 235 loader = new DbURLClassLoader(driver.getURLs()); 236 if (LOG) { 237 LOGGER.log(ErrorManager.INFORMATIONAL, "Creating " + loader); } 239 driver2Loader.put(driver, loader); 240 } else { 241 if (LOG) { 242 LOGGER.log(ErrorManager.INFORMATIONAL, "Reusing " + loader); } 244 } 245 } 246 return loader; 247 } 248 249 private SQLException createDriverNotFoundException() { 250 return new SQLException ("Unable to find a suitable driver", "08001"); } 252 } 253 | Popular Tags |