jdk/src/share/classes/java/sql/DriverManager.java
author lancea
Sat, 04 Sep 2010 15:37:26 -0400
changeset 6531 30b5903e8301
parent 5506 202f599c92aa
child 7803 56bc97d69d93
permissions -rw-r--r--
6982510: Updated SQLException subclasses from the outback for 6861385 so that the copyrights only have 2 years Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1996, 2006, 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.sql.Driver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.ServiceLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.PrivilegedAction;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * The <code>SQLPermission</code> constant that allows the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * setting of the logging stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    final static SQLPermission SET_LOG_PERMISSION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        new SQLPermission("setLog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    //--------------------------JDBC 2.0-----------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Retrieves the log writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * The <code>getLogWriter</code> and <code>setLogWriter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * methods should be used instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * of the <code>get/setlogStream</code> methods, which are deprecated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @return a <code>java.io.PrintWriter</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @see #setLogWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static java.io.PrintWriter getLogWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return logWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Sets the logging/tracing <code>PrintWriter</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * that is used by the <code>DriverManager</code> and all drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * There is a minor versioning problem created by the introduction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * of the method <code>setLogWriter</code>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * method <code>setLogWriter</code> cannot create a <code>PrintStream</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * that will be returned by <code>getLogStream</code>---the Java platform does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * not provide a backward conversion.  As a result, a new application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * that uses <code>setLogWriter</code> and also uses a JDBC 1.0 driver that uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <code>getLogStream</code> will likely not see debugging information written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * by that driver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *<P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Starting with the Java 2 SDK, Standard Edition, version 1.3 release, this method checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * to see that there is an <code>SQLPermission</code> object before setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * the logging stream.  If a <code>SecurityManager</code> exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <code>checkPermission</code> method denies setting the log writer, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * method throws a <code>java.lang.SecurityException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param out the new logging/tracing <code>PrintStream</code> object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *      <code>null</code> to disable logging and tracing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *    if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *    <code>checkPermission</code> method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *    setting the log writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @see #getLogWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public static void setLogWriter(java.io.PrintWriter out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        SecurityManager sec = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (sec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            sec.checkPermission(SET_LOG_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            logStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            logWriter = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    //---------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Attempts to establish a connection to the given database URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * The <code>DriverManager</code> attempts to select an appropriate driver from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * the set of registered JDBC drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param url a database url of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <code> jdbc:<em>subprotocol</em>:<em>subname</em></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param info a list of arbitrary string tag/value pairs as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * connection arguments; normally at least a "user" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * "password" property should be included
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return a Connection to the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @exception SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public static Connection getConnection(String url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        java.util.Properties info) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // Gets the classloader of the code that called this method, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return (getConnection(url, info, callerCL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Attempts to establish a connection to the given database URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * The <code>DriverManager</code> attempts to select an appropriate driver from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * the set of registered JDBC drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param url a database url of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param user the database user on whose behalf the connection is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *   made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param password the user's password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @return a connection to the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @exception SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public static Connection getConnection(String url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        String user, String password) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        java.util.Properties info = new java.util.Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // Gets the classloader of the code that called this method, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (user != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            info.put("user", user);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (password != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            info.put("password", password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return (getConnection(url, info, callerCL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Attempts to establish a connection to the given database URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * The <code>DriverManager</code> attempts to select an appropriate driver from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * the set of registered JDBC drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param url a database url of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *  <code> jdbc:<em>subprotocol</em>:<em>subname</em></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @return a connection to the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @exception SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public static Connection getConnection(String url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        java.util.Properties info = new java.util.Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // Gets the classloader of the code that called this method, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        // be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return (getConnection(url, info, callerCL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Attempts to locate a driver that understands the given URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * The <code>DriverManager</code> attempts to select an appropriate driver from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * the set of registered JDBC drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param url a database URL of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *     <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @return a <code>Driver</code> object representing a driver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * that can connect to the given URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @exception SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public static Driver getDriver(String url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        java.util.Vector drivers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        println("DriverManager.getDriver(\"" + url + "\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        synchronized (DriverManager.class){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            // use the read copy of the drivers vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            drivers = readDrivers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        // Gets the classloader of the code that called this method, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        // be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        // Walk through the loaded drivers attempting to locate someone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        // who understands the given URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        for (int i = 0; i < drivers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            DriverInfo di = (DriverInfo)drivers.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // If the caller does not have permission to load the driver then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            // skip it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if ( getCallerClass(callerCL, di.driverClassName ) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                 di.driverClass ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                println("    skipping: " + di);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                println("    trying " + di);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                if (di.driver.acceptsURL(url)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    // Success!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    println("getDriver returning " + di);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    return (di.driver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            } catch (SQLException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                // Drop through and try the next driver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        println("getDriver: no suitable driver");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        throw new SQLException("No suitable driver", "08001");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Registers the given driver with the <code>DriverManager</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * A newly-loaded driver class should call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * the method <code>registerDriver</code> to make itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * known to the <code>DriverManager</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param driver the new JDBC Driver that is to be registered with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *               <code>DriverManager</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @exception SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public static synchronized void registerDriver(java.sql.Driver driver)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        DriverInfo di = new DriverInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        di.driver = driver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        di.driverClass = driver.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        di.driverClassName = di.driverClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        // Not Required -- drivers.addElement(di);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        writeDrivers.addElement(di);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        println("registerDriver: " + di);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        /* update the read copy of drivers vector */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        readDrivers = (java.util.Vector) writeDrivers.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Drops a driver from the <code>DriverManager</code>'s list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *  Applets can only deregister drivers from their own classloaders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param driver the JDBC Driver to drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @exception SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public static synchronized void deregisterDriver(Driver driver)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // Gets the classloader of the code that called this method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        // may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        println("DriverManager.deregisterDriver: " + driver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        // Walk through the loaded drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        DriverInfo di = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        for (i = 0; i < writeDrivers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            di = (DriverInfo)writeDrivers.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (di.driver == driver) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        // If we can't find the driver just return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (i >= writeDrivers.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            println("    couldn't find driver to unload");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        // If the caller does not have permission to load the driver then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        // throw a security exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (getCallerClass(callerCL, di.driverClassName ) != di.driverClass ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            throw new SecurityException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        // Remove the driver.  Other entries in drivers get shuffled down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        writeDrivers.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        /* update the read copy of drivers vector */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        readDrivers = (java.util.Vector) writeDrivers.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Retrieves an Enumeration with all of the currently loaded JDBC drivers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * to which the current caller has access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <P><B>Note:</B> The classname of a driver can be found using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <CODE>d.getClass().getName()</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @return the list of JDBC Drivers loaded by the caller's class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public static java.util.Enumeration<Driver> getDrivers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        java.util.Vector<Driver> result = new java.util.Vector<Driver>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        java.util.Vector drivers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        synchronized (DriverManager.class){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            // use the readcopy of drivers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            drivers  = readDrivers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        // Gets the classloader of the code that called this method, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        ClassLoader callerCL = DriverManager.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // Walk through the loaded drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        for (int i = 0; i < drivers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            DriverInfo di = (DriverInfo)drivers.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            // If the caller does not have permission to load the driver then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            // skip it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if ( getCallerClass(callerCL, di.driverClassName ) != di.driverClass ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                println("    skipping: " + di);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            result.addElement(di.driver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return (result.elements());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Sets the maximum time in seconds that a driver will wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * while attempting to connect to a database.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @param seconds the login time limit in seconds; zero means there is no limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @see #getLoginTimeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public static void setLoginTimeout(int seconds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        loginTimeout = seconds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * Gets the maximum time in seconds that a driver can wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * when attempting to log in to a database.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @return the driver login time limit in seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @see #setLoginTimeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public static int getLoginTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return (loginTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Sets the logging/tracing PrintStream that is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * by the <code>DriverManager</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * and all drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *<P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * In the Java 2 SDK, Standard Edition, version 1.3 release, this method checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * to see that there is an <code>SQLPermission</code> object before setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * the logging stream.  If a <code>SecurityManager</code> exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * <code>checkPermission</code> method denies setting the log writer, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * method throws a <code>java.lang.SecurityException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @param out the new logging/tracing PrintStream; to disable, set to <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @throws SecurityException if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *    <code>checkPermission</code> method denies setting the log stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @see #getLogStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    public static void setLogStream(java.io.PrintStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        SecurityManager sec = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        if (sec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            sec.checkPermission(SET_LOG_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        logStream = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if ( out != null )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            logWriter = new java.io.PrintWriter(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            logWriter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Retrieves the logging/tracing PrintStream that is used by the <code>DriverManager</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * and all drivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @return the logging/tracing PrintStream; if disabled, is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @see #setLogStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    public static java.io.PrintStream getLogStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return logStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Prints a message to the current JDBC log stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @param message a log or tracing message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    public static void println(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        synchronized (logSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            if (logWriter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                logWriter.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                // automatic flushing is never enabled, so we must do it ourselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                logWriter.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    //------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    // Returns the class object that would be created if the code calling the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    // driver manager had loaded the driver class, or null if the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    // is inaccessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    private static Class getCallerClass(ClassLoader callerClassLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                                        String driverClassName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        Class callerC = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            callerC = Class.forName(driverClassName, true, callerClassLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            callerC = null;           // being very careful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return callerC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    private static void loadInitialDrivers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        String drivers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            drivers = (String)  AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    return System.getProperty("jdbc.drivers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            drivers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        // If the driver is packaged as a Service Provider, load it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        // Get all the drivers through the classloader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        // exposed as a java.sql.Driver.class service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        // ServiceLoader.load() replaces the sun.misc.Providers()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                Iterator driversIterator = loadedDrivers.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                /* Load these drivers, so that they can be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                 * It may be the case that the driver class may not be there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                 * i.e. there may be a packaged driver with the service class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                 * as implementation of java.sql.Driver but the actual class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                 * may be missing. In that case a java.util.ServiceConfigurationError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                 * will be thrown at runtime by the VM trying to locate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                 * and load the service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                 * Adding a try catch block to catch those runtime errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                 * if driver not available in classpath but it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                 * packaged as service and that service is there in classpath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    while(driversIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                        println(" Loading done by the java.util.ServiceLoader :  "+driversIterator.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                } catch(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                // Do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                return null;
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
        println("DriverManager.initialize: jdbc.drivers = " + drivers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (drivers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        while (drivers.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            int x = drivers.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            String driver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            if (x < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                driver = drivers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                drivers = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                driver = drivers.substring(0, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                drivers = drivers.substring(x+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            if (driver.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                println("DriverManager.Initialize: loading " + driver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                Class.forName(driver, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                              ClassLoader.getSystemClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                println("DriverManager.Initialize: load failed: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    //  Worker method called by the public getConnection() methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    private static Connection getConnection(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        String url, java.util.Properties info, ClassLoader callerCL) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        java.util.Vector drivers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
         * When callerCl is null, we should check the application's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
         * (which is invoking this class indirectly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
         * classloader, so that the JDBC driver class outside rt.jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
         * can be loaded from here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        synchronized(DriverManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
          // synchronize loading of the correct classloader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
          if(callerCL == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
              callerCL = Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        if(url == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            throw new SQLException("The url cannot be null", "08001");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        println("DriverManager.getConnection(\"" + url + "\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        synchronized (DriverManager.class){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            // use the readcopy of drivers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            drivers = readDrivers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        // Walk through the loaded drivers attempting to make a connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        // Remember the first exception that gets raised so we can reraise it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        SQLException reason = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        for (int i = 0; i < drivers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            DriverInfo di = (DriverInfo)drivers.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            // If the caller does not have permission to load the driver then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            // skip it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            if ( getCallerClass(callerCL, di.driverClassName ) != di.driverClass ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                println("    skipping: " + di);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                println("    trying " + di);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                Connection result = di.driver.connect(url, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    // Success!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    println("getConnection returning " + di);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    return (result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            } catch (SQLException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                if (reason == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    reason = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        // if we got here nobody could connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        if (reason != null)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            println("getConnection failed: " + reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            throw reason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        println("getConnection: no suitable driver found for "+ url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        throw new SQLException("No suitable driver found for "+ url, "08001");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    // Class initialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    static void initialize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        if (initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        loadInitialDrivers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        println("JDBC DriverManager initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    /* Prevent the DriverManager class from being instantiated. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    private DriverManager(){}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /* write copy of the drivers vector */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    private static java.util.Vector writeDrivers = new java.util.Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    /* write copy of the drivers vector */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    private static java.util.Vector readDrivers = new java.util.Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    private static int loginTimeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    private static java.io.PrintWriter logWriter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    private static java.io.PrintStream logStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    private static boolean initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    private static Object logSync = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    /* Returns the caller's class loader, or null if none */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    private static native ClassLoader getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
// DriverInfo is a package-private support class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
class DriverInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    Driver         driver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    Class          driverClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    String         driverClassName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        return ("driver[className=" + driverClassName + "," + driver + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
}