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