src/share/classes/java/sql/DriverManager.java

Print this page




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.sql;
  27 
  28 import java.util.Iterator;
  29 import java.util.ServiceLoader;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 import java.util.concurrent.CopyOnWriteArrayList;

  33 
  34 
  35 /**
  36  * <P>The basic service for managing a set of JDBC drivers.<br>
  37  * <B>NOTE:</B> The {@link <code>DataSource</code>} interface, new in the
  38  * JDBC 2.0 API, provides another way to connect to a data source.
  39  * The use of a <code>DataSource</code> object is the preferred means of
  40  * connecting to a data source.
  41  *
  42  * <P>As part of its initialization, the <code>DriverManager</code> class will
  43  * attempt to load the driver classes referenced in the "jdbc.drivers"
  44  * system property. This allows a user to customize the JDBC Drivers
  45  * used by their applications. For example in your
  46  * ~/.hotjava/properties file you might specify:
  47  * <pre>
  48  * <CODE>jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver</CODE>
  49  * </pre>
  50  *<P> The <code>DriverManager</code> methods <code>getConnection</code> and
  51  * <code>getDrivers</code> have been enhanced to support the Java Standard Edition
  52  * <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">Service Provider</a> mechanism. JDBC 4.0 Drivers must


 445     }
 446 
 447     /**
 448      * Prints a message to the current JDBC log stream.
 449      *
 450      * @param message a log or tracing message
 451      */
 452     public static void println(String message) {
 453         synchronized (logSync) {
 454             if (logWriter != null) {
 455                 logWriter.println(message);
 456 
 457                 // automatic flushing is never enabled, so we must do it ourselves
 458                 logWriter.flush();
 459             }
 460         }
 461     }
 462 
 463     //------------------------------------------------------------------------
 464 









 465     // Indicates whether the class object that would be created if the code calling
 466     // DriverManager is accessible.
 467     private static boolean isDriverAllowed(Driver driver, ClassLoader classLoader) {
 468         boolean result = false;
 469         if(driver != null) {
 470             Class<?> aClass = null;
 471             try {
 472                 aClass =  Class.forName(driver.getClass().getName(), true, classLoader);
 473             } catch (Exception ex) {
 474                 result = false;
 475             }
 476 
 477              result = ( aClass == driver.getClass() ) ? true : false;
 478         }
 479 
 480         return result;
 481     }
 482 
 483     private static void loadInitialDrivers() {
 484         String drivers;


 587                         reason = ex;
 588                     }
 589                 }
 590 
 591             } else {
 592                 println("    skipping: " + aDriver.getClass().getName());
 593             }
 594 
 595         }
 596 
 597         // if we got here nobody could connect.
 598         if (reason != null)    {
 599             println("getConnection failed: " + reason);
 600             throw reason;
 601         }
 602 
 603         println("getConnection: no suitable driver found for "+ url);
 604         throw new SQLException("No suitable driver found for "+ url, "08001");
 605     }
 606 
 607     /* Returns the caller's class loader, or null if none */
 608     private static native ClassLoader getCallerClassLoader();
 609 
 610 }
 611 
 612 /*
 613  * Wrapper class for registered Drivers in order to not expose Driver.equals()
 614  * to avoid the capture of the Driver it being compared to as it might not
 615  * normally have access.
 616  */
 617 class DriverInfo {
 618 
 619     final Driver driver;
 620     DriverInfo(Driver driver) {
 621         this.driver = driver;
 622     }
 623 
 624     @Override
 625     public boolean equals(Object other) {
 626         return (other instanceof DriverInfo)
 627                 && this.driver == ((DriverInfo) other).driver;
 628     }


  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.sql;
  27 
  28 import java.util.Iterator;
  29 import java.util.ServiceLoader;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 import java.util.concurrent.CopyOnWriteArrayList;
  33 import sun.reflect.Reflection;
  34 
  35 
  36 /**
  37  * <P>The basic service for managing a set of JDBC drivers.<br>
  38  * <B>NOTE:</B> The {@link <code>DataSource</code>} interface, new in the
  39  * JDBC 2.0 API, provides another way to connect to a data source.
  40  * The use of a <code>DataSource</code> object is the preferred means of
  41  * connecting to a data source.
  42  *
  43  * <P>As part of its initialization, the <code>DriverManager</code> class will
  44  * attempt to load the driver classes referenced in the "jdbc.drivers"
  45  * system property. This allows a user to customize the JDBC Drivers
  46  * used by their applications. For example in your
  47  * ~/.hotjava/properties file you might specify:
  48  * <pre>
  49  * <CODE>jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver</CODE>
  50  * </pre>
  51  *<P> The <code>DriverManager</code> methods <code>getConnection</code> and
  52  * <code>getDrivers</code> have been enhanced to support the Java Standard Edition
  53  * <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">Service Provider</a> mechanism. JDBC 4.0 Drivers must


 446     }
 447 
 448     /**
 449      * Prints a message to the current JDBC log stream.
 450      *
 451      * @param message a log or tracing message
 452      */
 453     public static void println(String message) {
 454         synchronized (logSync) {
 455             if (logWriter != null) {
 456                 logWriter.println(message);
 457 
 458                 // automatic flushing is never enabled, so we must do it ourselves
 459                 logWriter.flush();
 460             }
 461         }
 462     }
 463 
 464     //------------------------------------------------------------------------
 465     
 466     // Internal method used to get the caller's class loader.
 467     // Replaces the call to the native method
 468     private static ClassLoader getCallerClassLoader() {
 469         Class<?> cc = Reflection.getCallerClass(3);
 470         ClassLoader cl = cc != null ? cc.getClassLoader() : null;
 471         return cl;
 472     }
 473 
 474 
 475     // Indicates whether the class object that would be created if the code calling
 476     // DriverManager is accessible.
 477     private static boolean isDriverAllowed(Driver driver, ClassLoader classLoader) {
 478         boolean result = false;
 479         if(driver != null) {
 480             Class<?> aClass = null;
 481             try {
 482                 aClass =  Class.forName(driver.getClass().getName(), true, classLoader);
 483             } catch (Exception ex) {
 484                 result = false;
 485             }
 486 
 487              result = ( aClass == driver.getClass() ) ? true : false;
 488         }
 489 
 490         return result;
 491     }
 492 
 493     private static void loadInitialDrivers() {
 494         String drivers;


 597                         reason = ex;
 598                     }
 599                 }
 600 
 601             } else {
 602                 println("    skipping: " + aDriver.getClass().getName());
 603             }
 604 
 605         }
 606 
 607         // if we got here nobody could connect.
 608         if (reason != null)    {
 609             println("getConnection failed: " + reason);
 610             throw reason;
 611         }
 612 
 613         println("getConnection: no suitable driver found for "+ url);
 614         throw new SQLException("No suitable driver found for "+ url, "08001");
 615     }
 616 


 617 
 618 }
 619 
 620 /*
 621  * Wrapper class for registered Drivers in order to not expose Driver.equals()
 622  * to avoid the capture of the Driver it being compared to as it might not
 623  * normally have access.
 624  */
 625 class DriverInfo {
 626 
 627     final Driver driver;
 628     DriverInfo(Driver driver) {
 629         this.driver = driver;
 630     }
 631 
 632     @Override
 633     public boolean equals(Object other) {
 634         return (other instanceof DriverInfo)
 635                 && this.driver == ((DriverInfo) other).driver;
 636     }