1 7 8 package java.sql; 9 10 46 public class DriverManager { 47 48 49 54 final static SQLPermission SET_LOG_PERMISSION = 55 new SQLPermission ("setLog"); 56 57 59 69 public static java.io.PrintWriter getLogWriter() { 70 synchronized (logSync) { 71 return logWriter; 72 } 73 } 74 75 105 public static void setLogWriter(java.io.PrintWriter out) { 106 107 SecurityManager sec = System.getSecurityManager(); 108 if (sec != null) { 109 sec.checkPermission(SET_LOG_PERMISSION); 110 } 111 synchronized (logSync) { 112 logStream = null; 113 logWriter = out; 114 } 115 } 116 117 118 120 133 public static synchronized Connection getConnection(String url, 134 java.util.Properties info) throws SQLException { 135 136 ClassLoader callerCL = DriverManager.getCallerClassLoader(); 139 140 return (getConnection(url, info, callerCL)); 141 } 142 143 156 public static synchronized Connection getConnection(String url, 157 String user, String password) throws SQLException { 158 java.util.Properties info = new java.util.Properties (); 159 160 ClassLoader callerCL = DriverManager.getCallerClassLoader(); 163 164 if (user != null) { 165 info.put("user", user); 166 } 167 if (password != null) { 168 info.put("password", password); 169 } 170 171 return (getConnection(url, info, callerCL)); 172 } 173 174 184 public static synchronized Connection getConnection(String url) 185 throws SQLException { 186 187 java.util.Properties info = new java.util.Properties (); 188 189 ClassLoader callerCL = DriverManager.getCallerClassLoader(); 192 193 return (getConnection(url, info, callerCL)); 194 } 195 196 207 public static synchronized Driver getDriver(String url) 208 throws SQLException { 209 println("DriverManager.getDriver(\"" + url + "\")"); 210 211 if (!initialized) { 212 initialize(); 213 } 214 215 ClassLoader callerCL = DriverManager.getCallerClassLoader(); 218 219 for (int i = 0; i < drivers.size(); i++) { 222 DriverInfo di = (DriverInfo)drivers.elementAt(i); 223 if ( getCallerClass(callerCL, di.driverClassName ) != 226 di.driverClass ) { 227 println(" skipping: " + di); 228 continue; 229 } 230 try { 231 println(" trying " + di); 232 if (di.driver.acceptsURL(url)) { 233 println("getDriver returning " + di); 235 return (di.driver); 236 } 237 } catch (SQLException ex) { 238 } 240 } 241 242 println("getDriver: no suitable driver"); 243 throw new SQLException ("No suitable driver", "08001"); 244 } 245 246 247 257 public static synchronized void registerDriver(java.sql.Driver driver) 258 throws SQLException { 259 if (!initialized) { 260 initialize(); 261 } 262 263 DriverInfo di = new DriverInfo(); 264 di.driver = driver; 265 di.driverClass = driver.getClass(); 266 di.driverClassName = di.driverClass.getName(); 267 drivers.addElement(di); 268 println("registerDriver: " + di); 269 } 270 271 278 public static synchronized void deregisterDriver(Driver driver) 279 throws SQLException { 280 ClassLoader callerCL = DriverManager.getCallerClassLoader(); 283 println("DriverManager.deregisterDriver: " + driver); 284 285 int i; 287 DriverInfo di = null; 288 for (i = 0; i < drivers.size(); i++) { 289 di = (DriverInfo)drivers.elementAt(i); 290 if (di.driver == driver) { 291 break; 292 } 293 } 294 if (i >= drivers.size()) { 296 println(" couldn't find driver to unload"); 297 return; 298 } 299 300 if ( getCallerClass(callerCL, di.driverClassName ) != di.driverClass ) { 303 throw new SecurityException (); 304 } 305 306 drivers.removeElementAt(i); 308 309 } 310 311 320 public static synchronized java.util.Enumeration <Driver > getDrivers() { 321 java.util.Vector result = new java.util.Vector (); 322 323 if (!initialized) { 324 initialize(); 325 } 326 327 ClassLoader callerCL = DriverManager.getCallerClassLoader(); 330 331 for (int i = 0; i < drivers.size(); i++) { 333 DriverInfo di = (DriverInfo)drivers.elementAt(i); 334 if ( getCallerClass(callerCL, di.driverClassName ) != di.driverClass ) { 337 println(" skipping: " + di); 338 continue; 339 } 340 result.addElement(di.driver); 341 } 342 343 return (result.elements()); 344 } 345 346 347 354 public static void setLoginTimeout(int seconds) { 355 loginTimeout = seconds; 356 } 357 358 365 public static int getLoginTimeout() { 366 return (loginTimeout); 367 } 368 369 388 @Deprecated 389 public static synchronized void setLogStream(java.io.PrintStream out) { 390 391 SecurityManager sec = System.getSecurityManager(); 392 if (sec != null) { 393 sec.checkPermission(SET_LOG_PERMISSION); 394 } 395 396 logStream = out; 397 if ( out != null ) 398 logWriter = new java.io.PrintWriter (out); 399 else 400 logWriter = null; 401 } 402 403 411 @Deprecated 412 public static java.io.PrintStream getLogStream() { 413 return logStream; 414 } 415 416 421 public static void println(String message) { 422 synchronized (logSync) { 423 if (logWriter != null) { 424 logWriter.println(message); 425 426 logWriter.flush(); 428 } 429 } 430 } 431 432 434 private static Class getCallerClass(ClassLoader callerClassLoader, 438 String driverClassName) { 439 Class callerC = null; 440 441 try { 442 callerC = Class.forName(driverClassName, true, callerClassLoader); 443 } 444 catch (Exception ex) { 445 callerC = null; } 447 448 return callerC; 449 } 450 451 private static void loadInitialDrivers() { 452 String drivers; 453 try { 454 drivers = (String ) java.security.AccessController.doPrivileged( 455 new sun.security.action.GetPropertyAction("jdbc.drivers")); 456 } catch (Exception ex) { 457 drivers = null; 458 } 459 println("DriverManager.initialize: jdbc.drivers = " + drivers); 460 if (drivers == null) { 461 return; 462 } 463 while (drivers.length() != 0) { 464 int x = drivers.indexOf(':'); 465 String driver; 466 if (x < 0) { 467 driver = drivers; 468 drivers = ""; 469 } else { 470 driver = drivers.substring(0, x); 471 drivers = drivers.substring(x+1); 472 } 473 if (driver.length() == 0) { 474 continue; 475 } 476 try { 477 println("DriverManager.Initialize: loading " + driver); 478 Class.forName(driver, true, 479 ClassLoader.getSystemClassLoader()); 480 } catch (Exception ex) { 481 println("DriverManager.Initialize: load failed: " + ex); 482 } 483 } 484 } 485 486 487 private static synchronized Connection getConnection( 489 String url, java.util.Properties info, ClassLoader callerCL) throws SQLException { 490 491 497 if(callerCL == null) { 498 callerCL = Thread.currentThread().getContextClassLoader(); 499 } 500 501 if(url == null) { 502 throw new SQLException ("The url cannot be null", "08001"); 503 } 504 505 println("DriverManager.getConnection(\"" + url + "\")"); 506 507 if (!initialized) { 508 initialize(); 509 } 510 511 SQLException reason = null; 514 for (int i = 0; i < drivers.size(); i++) { 515 DriverInfo di = (DriverInfo)drivers.elementAt(i); 516 517 if ( getCallerClass(callerCL, di.driverClassName ) != di.driverClass ) { 520 println(" skipping: " + di); 521 continue; 522 } 523 try { 524 println(" trying " + di); 525 Connection result = di.driver.connect(url, info); 526 if (result != null) { 527 println("getConnection returning " + di); 529 return (result); 530 } 531 } catch (SQLException ex) { 532 if (reason == null) { 533 reason = ex; 534 } 535 } 536 } 537 538 if (reason != null) { 540 println("getConnection failed: " + reason); 541 throw reason; 542 } 543 544 println("getConnection: no suitable driver"); 545 throw new SQLException ("No suitable driver", "08001"); 546 } 547 548 549 static void initialize() { 551 if (initialized) { 552 return; 553 } 554 initialized = true; 555 loadInitialDrivers(); 556 println("JDBC DriverManager initialized"); 557 } 558 559 private DriverManager(){} 561 562 private static java.util.Vector drivers = new java.util.Vector (); 563 private static int loginTimeout = 0; 564 private static java.io.PrintWriter logWriter = null; 565 private static java.io.PrintStream logStream = null; 566 private static boolean initialized = false; 567 568 private static Object logSync = new Object (); 569 570 private static native ClassLoader getCallerClassLoader(); 572 573 } 574 575 576 class DriverInfo { 578 Driver driver; 579 Class driverClass; 580 String driverClassName; 581 582 public String toString() { 583 return ("driver[className=" + driverClassName + "," + driver + "]"); 584 } 585 } 586 | Popular Tags |