jdk/src/share/classes/java/sql/DriverManager.java
author lancea
Thu, 17 Mar 2011 11:37:06 -0400
changeset 8797 e8507464a69d
parent 7803 56bc97d69d93
child 9239 f4bedba1a211
permissions -rw-r--r--
7026898: DriverManager to now use CopyOnWriteArrayList Reviewed-by: alanb, briangoetz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.sql;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.ServiceLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PrivilegedAction;
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    32
import java.util.concurrent.CopyOnWriteArrayList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <P>The basic service for managing a set of JDBC drivers.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <B>NOTE:</B> The {@link <code>DataSource</code>} interface, new in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * JDBC 2.0 API, provides another way to connect to a data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The use of a <code>DataSource</code> object is the preferred means of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * connecting to a data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <P>As part of its initialization, the <code>DriverManager</code> class will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * attempt to load the driver classes referenced in the "jdbc.drivers"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * system property. This allows a user to customize the JDBC Drivers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * used by their applications. For example in your
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * ~/.hotjava/properties file you might specify:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <CODE>jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *<P> The <code>DriverManager</code> methods <code>getConnection</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <code>getDrivers</code> have been enhanced to support the Java Standard Edition
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">Service Provider</a> mechanism. JDBC 4.0 Drivers must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * include the file <code>META-INF/services/java.sql.Driver</code>. This file contains the name of the JDBC drivers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * implementation of <code>java.sql.Driver</code>.  For example, to load the <code>my.sql.Driver</code> class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * the <code>META-INF/services/java.sql.Driver</code> file would contain the entry:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <code>my.sql.Driver</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <P>Applications no longer need to explictly load JDBC drivers using <code>Class.forName()</code>. Existing programs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * which currently load JDBC drivers using <code>Class.forName()</code> will continue to work without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <P>When the method <code>getConnection</code> is called,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * the <code>DriverManager</code> will attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * locate a suitable driver from amongst those loaded at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * initialization and those loaded explicitly using the same classloader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * as the current applet or application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * Starting with the Java 2 SDK, Standard Edition, version 1.3, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * logging stream can be set only if the proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * permission has been granted.  Normally this will be done with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * the tool PolicyTool, which can be used to grant <code>permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * java.sql.SQLPermission "setLog"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @see Driver
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @see Connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
public class DriverManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    82
    // List of registered JDBC drivers
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    83
    private final static CopyOnWriteArrayList<Driver> registeredDrivers = new CopyOnWriteArrayList<Driver>();
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    84
    private static volatile int loginTimeout = 0;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    85
    private static volatile java.io.PrintWriter logWriter = null;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    86
    private static volatile java.io.PrintStream logStream = null;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    87
    // Used in println() to synchronize logWriter
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    88
    private final static  Object logSync = new Object();
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    89
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    90
    /* Prevent the DriverManager class from being instantiated. */
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    91
    private DriverManager(){}
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    92
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    93
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    94
    /**
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    95
     * Load the initial JDBC drivers by checking the System property
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    96
     * jdbc.properties and then use the {@code ServiceLoader} mechanism
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    97
     */
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    98
    static {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
    99
        loadInitialDrivers();
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   100
        println("JDBC DriverManager initialized");
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   101
    }
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   102
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * The <code>SQLPermission</code> constant that allows the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * setting of the logging stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    final static SQLPermission SET_LOG_PERMISSION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        new SQLPermission("setLog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    //--------------------------JDBC 2.0-----------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Retrieves the log writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * The <code>getLogWriter</code> and <code>setLogWriter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * methods should be used instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * of the <code>get/setlogStream</code> methods, which are deprecated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @return a <code>java.io.PrintWriter</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @see #setLogWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static java.io.PrintWriter getLogWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return logWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Sets the logging/tracing <code>PrintWriter</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * that is used by the <code>DriverManager</code> and all drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * There is a minor versioning problem created by the introduction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * of the method <code>setLogWriter</code>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * method <code>setLogWriter</code> cannot create a <code>PrintStream</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * that will be returned by <code>getLogStream</code>---the Java platform does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * not provide a backward conversion.  As a result, a new application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * that uses <code>setLogWriter</code> and also uses a JDBC 1.0 driver that uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * <code>getLogStream</code> will likely not see debugging information written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * by that driver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *<P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Starting with the Java 2 SDK, Standard Edition, version 1.3 release, this method checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * to see that there is an <code>SQLPermission</code> object before setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * the logging stream.  If a <code>SecurityManager</code> exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <code>checkPermission</code> method denies setting the log writer, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * method throws a <code>java.lang.SecurityException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param out the new logging/tracing <code>PrintStream</code> object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *      <code>null</code> to disable logging and tracing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *    if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *    <code>checkPermission</code> method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *    setting the log writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @see #getLogWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public static void setLogWriter(java.io.PrintWriter out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        SecurityManager sec = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (sec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            sec.checkPermission(SET_LOG_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            logStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            logWriter = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    //---------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Attempts to establish a connection to the given database URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * The <code>DriverManager</code> attempts to select an appropriate driver from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * the set of registered JDBC drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param url a database url of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <code> jdbc:<em>subprotocol</em>:<em>subname</em></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param info a list of arbitrary string tag/value pairs as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * connection arguments; normally at least a "user" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * "password" property should be included
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @return a Connection to the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @exception SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public static Connection getConnection(String url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        java.util.Properties info) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // Gets the classloader of the code that called this method, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return (getConnection(url, info, callerCL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Attempts to establish a connection to the given database URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * The <code>DriverManager</code> attempts to select an appropriate driver from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * the set of registered JDBC drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param url a database url of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param user the database user on whose behalf the connection is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *   made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param password the user's password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @return a connection to the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @exception SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public static Connection getConnection(String url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        String user, String password) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        java.util.Properties info = new java.util.Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // Gets the classloader of the code that called this method, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (user != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            info.put("user", user);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (password != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            info.put("password", password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return (getConnection(url, info, callerCL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Attempts to establish a connection to the given database URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * The <code>DriverManager</code> attempts to select an appropriate driver from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * the set of registered JDBC drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param url a database url of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *  <code> jdbc:<em>subprotocol</em>:<em>subname</em></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @return a connection to the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @exception SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public static Connection getConnection(String url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        java.util.Properties info = new java.util.Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // Gets the classloader of the code that called this method, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return (getConnection(url, info, callerCL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Attempts to locate a driver that understands the given URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * The <code>DriverManager</code> attempts to select an appropriate driver from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * the set of registered JDBC drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param url a database URL of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *     <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @return a <code>Driver</code> object representing a driver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * that can connect to the given URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @exception SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public static Driver getDriver(String url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        println("DriverManager.getDriver(\"" + url + "\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // Gets the classloader of the code that called this method, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   266
        // Walk through the loaded registeredDrivers attempting to locate someone
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // who understands the given URL.
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   268
        for (Driver aDriver : registeredDrivers) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            // If the caller does not have permission to load the driver then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            // skip it.
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   271
            if(isDriverAllowed(aDriver, callerCL)) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   272
                try {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   273
                    if(aDriver.acceptsURL(url)) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   274
                        // Success!
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   275
                        println("getDriver returning " + aDriver.getClass().getName());
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   276
                    return (aDriver);
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   277
                    }
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   278
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   279
                } catch(SQLException sqe) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   280
                    // Drop through and try the next driver.
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   281
                }
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   282
            } else {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   283
                println("    skipping: " + aDriver.getClass().getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   285
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        println("getDriver: no suitable driver");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        throw new SQLException("No suitable driver", "08001");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Registers the given driver with the <code>DriverManager</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * A newly-loaded driver class should call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * the method <code>registerDriver</code> to make itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * known to the <code>DriverManager</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param driver the new JDBC Driver that is to be registered with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *               <code>DriverManager</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @exception SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public static synchronized void registerDriver(java.sql.Driver driver)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        throws SQLException {
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   305
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   306
        /* Register the driver if it has not already been added to our list */
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   307
        if(driver != null) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   308
            registeredDrivers.addIfAbsent(driver);
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   309
        } else {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   310
            // This is for compatibility with the original DriverManager
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   311
            throw new NullPointerException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   314
        println("registerDriver: " + driver);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Drops a driver from the <code>DriverManager</code>'s list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *  Applets can only deregister drivers from their own classloaders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @param driver the JDBC Driver to drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @exception SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public static synchronized void deregisterDriver(Driver driver)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        throws SQLException {
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   327
        if (driver == null) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   328
            return;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   329
        }
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   330
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // Gets the classloader of the code that called this method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        println("DriverManager.deregisterDriver: " + driver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   336
        if(registeredDrivers.contains(driver)) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   337
            if (isDriverAllowed(driver, callerCL)) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   338
                 registeredDrivers.remove(driver);
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   339
            } else {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   340
                // If the caller does not have permission to load the driver then
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   341
                // throw a SecurityException.
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   342
                throw new SecurityException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   344
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            println("    couldn't find driver to unload");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Retrieves an Enumeration with all of the currently loaded JDBC drivers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * to which the current caller has access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * <P><B>Note:</B> The classname of a driver can be found using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * <CODE>d.getClass().getName()</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @return the list of JDBC Drivers loaded by the caller's class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public static java.util.Enumeration<Driver> getDrivers() {
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   359
        java.util.Vector<Driver> result = new java.util.Vector<Driver>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        // Gets the classloader of the code that called this method, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        // be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   365
        // Walk through the loaded registeredDrivers.
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   366
        for(Driver aDriver : registeredDrivers) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            // If the caller does not have permission to load the driver then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            // skip it.
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   369
            if(isDriverAllowed(aDriver, callerCL)) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   370
                result.addElement(aDriver);
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   371
            } else {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   372
                println("    skipping: " + aDriver.getClass().getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return (result.elements());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Sets the maximum time in seconds that a driver will wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * while attempting to connect to a database.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @param seconds the login time limit in seconds; zero means there is no limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @see #getLoginTimeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public static void setLoginTimeout(int seconds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        loginTimeout = seconds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * Gets the maximum time in seconds that a driver can wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * when attempting to log in to a database.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @return the driver login time limit in seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @see #setLoginTimeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public static int getLoginTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return (loginTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Sets the logging/tracing PrintStream that is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * by the <code>DriverManager</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * and all drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *<P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * In the Java 2 SDK, Standard Edition, version 1.3 release, this method checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * to see that there is an <code>SQLPermission</code> object before setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * the logging stream.  If a <code>SecurityManager</code> exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * <code>checkPermission</code> method denies setting the log writer, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * method throws a <code>java.lang.SecurityException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @param out the new logging/tracing PrintStream; to disable, set to <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @throws SecurityException if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *    <code>checkPermission</code> method denies setting the log stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @see #getLogStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public static void setLogStream(java.io.PrintStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        SecurityManager sec = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (sec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            sec.checkPermission(SET_LOG_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        logStream = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if ( out != null )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            logWriter = new java.io.PrintWriter(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            logWriter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Retrieves the logging/tracing PrintStream that is used by the <code>DriverManager</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * and all drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @return the logging/tracing PrintStream; if disabled, is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @see #setLogStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    public static java.io.PrintStream getLogStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return logStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Prints a message to the current JDBC log stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @param message a log or tracing message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public static void println(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        synchronized (logSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            if (logWriter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                logWriter.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                // automatic flushing is never enabled, so we must do it ourselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                logWriter.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    //------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   464
    // Indicates whether the class object that would be created if the code calling
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   465
    // DriverManager is accessible.
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   466
    private static boolean isDriverAllowed(Driver driver, ClassLoader classLoader) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   467
        boolean result = false;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   468
        if(driver != null) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   469
            Class<?> aClass = null;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   470
            try {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   471
                aClass =  Class.forName(driver.getClass().getName(), true, classLoader);
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   472
            } catch (Exception ex) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   473
                result = false;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   474
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   476
             result = ( aClass == driver.getClass() ) ? true : false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   479
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private static void loadInitialDrivers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        String drivers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            drivers = (String)  AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    return System.getProperty("jdbc.drivers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            drivers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        // If the driver is packaged as a Service Provider, load it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        // Get all the drivers through the classloader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        // exposed as a java.sql.Driver.class service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        // ServiceLoader.load() replaces the sun.misc.Providers()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                Iterator driversIterator = loadedDrivers.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                /* Load these drivers, so that they can be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                 * It may be the case that the driver class may not be there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                 * i.e. there may be a packaged driver with the service class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                 * as implementation of java.sql.Driver but the actual class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                 * may be missing. In that case a java.util.ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                 * will be thrown at runtime by the VM trying to locate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                 * and load the service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                 * Adding a try catch block to catch those runtime errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                 * if driver not available in classpath but it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                 * packaged as service and that service is there in classpath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    while(driversIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        println(" Loading done by the java.util.ServiceLoader :  "+driversIterator.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                } catch(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                // Do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        println("DriverManager.initialize: jdbc.drivers = " + drivers);
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   528
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   529
        if (drivers == null || drivers.equals("")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   532
        String[] driversList = drivers.split(":");
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   533
        println("number of Drivers:" + driversList.length);
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   534
        for (String aDriver : driversList) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            try {
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   536
                println("DriverManager.Initialize: loading " + aDriver);
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   537
                Class.forName(aDriver, true,
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   538
                        ClassLoader.getSystemClassLoader());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                println("DriverManager.Initialize: load failed: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    //  Worker method called by the public getConnection() methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    private static Connection getConnection(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        String url, java.util.Properties info, ClassLoader callerCL) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
         * When callerCl is null, we should check the application's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
         * (which is invoking this class indirectly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
         * classloader, so that the JDBC driver class outside rt.jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
         * can be loaded from here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        synchronized(DriverManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
          // synchronize loading of the correct classloader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
          if(callerCL == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
              callerCL = Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if(url == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            throw new SQLException("The url cannot be null", "08001");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        println("DriverManager.getConnection(\"" + url + "\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   568
        // Walk through the loaded registeredDrivers attempting to make a connection.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        // Remember the first exception that gets raised so we can reraise it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        SQLException reason = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   572
        for(Driver aDriver : registeredDrivers) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            // If the caller does not have permission to load the driver then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            // skip it.
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   575
            if(isDriverAllowed(aDriver, callerCL)) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   576
                try {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   577
                    println("    trying " + aDriver.getClass().getName());
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   578
                    Connection con = aDriver.connect(url, info);
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   579
                    if (con != null) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   580
                        // Success!
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   581
                        println("getConnection returning " + aDriver.getClass().getName());
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   582
                        return (con);
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   583
                    }
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   584
                } catch (SQLException ex) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   585
                    if (reason == null) {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   586
                        reason = ex;
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   587
                    }
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   588
                }
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   589
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   590
            } else {
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   591
                println("    skipping: " + aDriver.getClass().getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
8797
e8507464a69d 7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents: 7803
diff changeset
   593
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        // if we got here nobody could connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        if (reason != null)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            println("getConnection failed: " + reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            throw reason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        println("getConnection: no suitable driver found for "+ url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        throw new SQLException("No suitable driver found for "+ url, "08001");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    /* Returns the caller's class loader, or null if none */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    private static native ClassLoader getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
}