author | alanb |
Tue, 15 May 2018 09:33:02 +0100 | |
changeset 50109 | ea54197f4fe4 |
parent 47216 | 71c04702a3d5 |
child 51972 | 67619141f027 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
45885
562fed91cd84
8184311: Update java.sql and java.sql.rowset API docs for accessibility
jjg
parents:
44256
diff
changeset
|
2 |
* Copyright (c) 1996, 2017, 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 |
||
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
28 |
import java.util.ArrayList; |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
29 |
import java.util.Collections; |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
30 |
import java.util.Enumeration; |
2 | 31 |
import java.util.Iterator; |
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
32 |
import java.util.List; |
2 | 33 |
import java.util.ServiceLoader; |
34 |
import java.security.AccessController; |
|
35 |
import java.security.PrivilegedAction; |
|
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
36 |
import java.util.concurrent.CopyOnWriteArrayList; |
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
37 |
import java.util.stream.Stream; |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
38 |
|
37363
329dba26ffd2
8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents:
35302
diff
changeset
|
39 |
import jdk.internal.reflect.CallerSensitive; |
329dba26ffd2
8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents:
35302
diff
changeset
|
40 |
import jdk.internal.reflect.Reflection; |
2 | 41 |
|
42 |
||
43 |
/** |
|
42700 | 44 |
* The basic service for managing a set of JDBC drivers. |
45 |
* <p> |
|
46 |
* <strong>NOTE:</strong> The {@link javax.sql.DataSource} interface, provides |
|
47 |
* another way to connect to a data source. |
|
48 |
* The use of a {@code DataSource} object is the preferred means of |
|
2 | 49 |
* connecting to a data source. |
42700 | 50 |
* <P> |
51 |
* As part of its initialization, the {@code DriverManager} class will |
|
52 |
* attempt to load available JDBC drivers by using: |
|
53 |
* <ul> |
|
54 |
* <li>The {@code jdbc.drivers} system property which contains a |
|
55 |
* colon separated list of fully qualified class names of JDBC drivers. Each |
|
56 |
* driver is loaded using the {@linkplain ClassLoader#getSystemClassLoader |
|
57 |
* system class loader}: |
|
58 |
* <ul> |
|
59 |
* <li>{@code jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver} |
|
60 |
* </ul> |
|
2 | 61 |
* |
42700 | 62 |
* <li>Service providers of the {@code java.sql.Driver} class, that are loaded |
63 |
* via the {@linkplain ServiceLoader#load service-provider loading} mechanism. |
|
64 |
*</ul> |
|
2 | 65 |
* |
42995 | 66 |
* @implNote |
42700 | 67 |
* {@code DriverManager} initialization is done lazily and looks up service |
68 |
* providers using the thread context class loader. The drivers loaded and |
|
69 |
* available to an application will depend on the thread context class loader of |
|
70 |
* the thread that triggers driver initialization by {@code DriverManager}. |
|
2 | 71 |
* |
42700 | 72 |
* <P>When the method {@code getConnection} is called, |
73 |
* the {@code DriverManager} will attempt to |
|
2 | 74 |
* locate a suitable driver from amongst those loaded at |
42700 | 75 |
* initialization and those loaded explicitly using the same class loader |
76 |
* as the current application. |
|
2 | 77 |
* |
78 |
* @see Driver |
|
79 |
* @see Connection |
|
44256 | 80 |
* @since 1.1 |
2 | 81 |
*/ |
82 |
public class DriverManager { |
|
83 |
||
84 |
||
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
85 |
// List of registered JDBC drivers |
11129
f9ad1aadf3fa
7116445: Miscellaneous warnings in the JDBC/RowSet classes
lancea
parents:
9243
diff
changeset
|
86 |
private final static CopyOnWriteArrayList<DriverInfo> registeredDrivers = new CopyOnWriteArrayList<>(); |
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
87 |
private static volatile int loginTimeout = 0; |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
88 |
private static volatile java.io.PrintWriter logWriter = null; |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
89 |
private static volatile java.io.PrintStream logStream = null; |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
90 |
// Used in println() to synchronize logWriter |
27801
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
91 |
private final static Object logSync = new Object(); |
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
92 |
// Used in ensureDriversInitialized() to synchronize driversInitialized |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
93 |
private final static Object lockForInitDrivers = new Object(); |
27801
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
94 |
private static volatile boolean driversInitialized; |
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
95 |
private static final String JDBC_DRIVERS_PROPERTY = "jdbc.drivers"; |
8797
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 |
/* Prevent the DriverManager class from being instantiated. */ |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
98 |
private DriverManager(){} |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
99 |
|
2 | 100 |
/** |
101 |
* The <code>SQLPermission</code> constant that allows the |
|
102 |
* setting of the logging stream. |
|
103 |
* @since 1.3 |
|
104 |
*/ |
|
105 |
final static SQLPermission SET_LOG_PERMISSION = |
|
106 |
new SQLPermission("setLog"); |
|
107 |
||
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
108 |
/** |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
109 |
* The {@code SQLPermission} constant that allows the |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
110 |
* un-register a registered JDBC driver. |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
111 |
* @since 1.8 |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
112 |
*/ |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
113 |
final static SQLPermission DEREGISTER_DRIVER_PERMISSION = |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
114 |
new SQLPermission("deregisterDriver"); |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
115 |
|
2 | 116 |
//--------------------------JDBC 2.0----------------------------- |
117 |
||
118 |
/** |
|
119 |
* Retrieves the log writer. |
|
120 |
* |
|
121 |
* The <code>getLogWriter</code> and <code>setLogWriter</code> |
|
122 |
* methods should be used instead |
|
123 |
* of the <code>get/setlogStream</code> methods, which are deprecated. |
|
124 |
* @return a <code>java.io.PrintWriter</code> object |
|
125 |
* @see #setLogWriter |
|
126 |
* @since 1.2 |
|
127 |
*/ |
|
128 |
public static java.io.PrintWriter getLogWriter() { |
|
129 |
return logWriter; |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* Sets the logging/tracing <code>PrintWriter</code> object |
|
134 |
* that is used by the <code>DriverManager</code> and all drivers. |
|
135 |
*<P> |
|
42700 | 136 |
* If a security manager exists, its {@code checkPermission} |
137 |
* method is first called with a {@code SQLPermission("setLog")} |
|
138 |
* permission to check that the caller is allowed to call {@code setLogWriter}. |
|
2 | 139 |
* |
140 |
* @param out the new logging/tracing <code>PrintStream</code> object; |
|
141 |
* <code>null</code> to disable logging and tracing |
|
42700 | 142 |
* @throws SecurityException if a security manager exists and its |
143 |
* {@code checkPermission} method denies permission to set the log writer. |
|
2 | 144 |
* @see SecurityManager#checkPermission |
145 |
* @see #getLogWriter |
|
146 |
* @since 1.2 |
|
147 |
*/ |
|
148 |
public static void setLogWriter(java.io.PrintWriter out) { |
|
149 |
||
150 |
SecurityManager sec = System.getSecurityManager(); |
|
151 |
if (sec != null) { |
|
152 |
sec.checkPermission(SET_LOG_PERMISSION); |
|
153 |
} |
|
154 |
logStream = null; |
|
155 |
logWriter = out; |
|
156 |
} |
|
157 |
||
158 |
||
159 |
//--------------------------------------------------------------- |
|
160 |
||
161 |
/** |
|
162 |
* Attempts to establish a connection to the given database URL. |
|
163 |
* The <code>DriverManager</code> attempts to select an appropriate driver from |
|
164 |
* the set of registered JDBC drivers. |
|
15278 | 165 |
*<p> |
166 |
* <B>Note:</B> If a property is specified as part of the {@code url} and |
|
167 |
* is also specified in the {@code Properties} object, it is |
|
168 |
* implementation-defined as to which value will take precedence. |
|
169 |
* For maximum portability, an application should only specify a |
|
170 |
* property once. |
|
2 | 171 |
* |
172 |
* @param url a database url of the form |
|
173 |
* <code> jdbc:<em>subprotocol</em>:<em>subname</em></code> |
|
174 |
* @param info a list of arbitrary string tag/value pairs as |
|
175 |
* connection arguments; normally at least a "user" and |
|
176 |
* "password" property should be included |
|
177 |
* @return a Connection to the URL |
|
15278 | 178 |
* @exception SQLException if a database access error occurs or the url is |
179 |
* {@code null} |
|
180 |
* @throws SQLTimeoutException when the driver has determined that the |
|
181 |
* timeout value specified by the {@code setLoginTimeout} method |
|
182 |
* has been exceeded and has at least tried to cancel the |
|
183 |
* current database connection attempt |
|
2 | 184 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16887
diff
changeset
|
185 |
@CallerSensitive |
2 | 186 |
public static Connection getConnection(String url, |
187 |
java.util.Properties info) throws SQLException { |
|
188 |
||
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16887
diff
changeset
|
189 |
return (getConnection(url, info, Reflection.getCallerClass())); |
2 | 190 |
} |
191 |
||
192 |
/** |
|
193 |
* Attempts to establish a connection to the given database URL. |
|
194 |
* The <code>DriverManager</code> attempts to select an appropriate driver from |
|
195 |
* the set of registered JDBC drivers. |
|
15278 | 196 |
*<p> |
21950 | 197 |
* <B>Note:</B> If the {@code user} or {@code password} property are |
198 |
* also specified as part of the {@code url}, it is |
|
15278 | 199 |
* implementation-defined as to which value will take precedence. |
200 |
* For maximum portability, an application should only specify a |
|
201 |
* property once. |
|
2 | 202 |
* |
203 |
* @param url a database url of the form |
|
204 |
* <code>jdbc:<em>subprotocol</em>:<em>subname</em></code> |
|
205 |
* @param user the database user on whose behalf the connection is being |
|
206 |
* made |
|
207 |
* @param password the user's password |
|
208 |
* @return a connection to the URL |
|
15278 | 209 |
* @exception SQLException if a database access error occurs or the url is |
210 |
* {@code null} |
|
211 |
* @throws SQLTimeoutException when the driver has determined that the |
|
212 |
* timeout value specified by the {@code setLoginTimeout} method |
|
213 |
* has been exceeded and has at least tried to cancel the |
|
214 |
* current database connection attempt |
|
2 | 215 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16887
diff
changeset
|
216 |
@CallerSensitive |
2 | 217 |
public static Connection getConnection(String url, |
218 |
String user, String password) throws SQLException { |
|
219 |
java.util.Properties info = new java.util.Properties(); |
|
220 |
||
221 |
if (user != null) { |
|
222 |
info.put("user", user); |
|
223 |
} |
|
224 |
if (password != null) { |
|
225 |
info.put("password", password); |
|
226 |
} |
|
227 |
||
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16887
diff
changeset
|
228 |
return (getConnection(url, info, Reflection.getCallerClass())); |
2 | 229 |
} |
230 |
||
231 |
/** |
|
232 |
* Attempts to establish a connection to the given database URL. |
|
233 |
* The <code>DriverManager</code> attempts to select an appropriate driver from |
|
234 |
* the set of registered JDBC drivers. |
|
235 |
* |
|
236 |
* @param url a database url of the form |
|
237 |
* <code> jdbc:<em>subprotocol</em>:<em>subname</em></code> |
|
238 |
* @return a connection to the URL |
|
15278 | 239 |
* @exception SQLException if a database access error occurs or the url is |
240 |
* {@code null} |
|
241 |
* @throws SQLTimeoutException when the driver has determined that the |
|
242 |
* timeout value specified by the {@code setLoginTimeout} method |
|
243 |
* has been exceeded and has at least tried to cancel the |
|
244 |
* current database connection attempt |
|
2 | 245 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16887
diff
changeset
|
246 |
@CallerSensitive |
2 | 247 |
public static Connection getConnection(String url) |
248 |
throws SQLException { |
|
249 |
||
250 |
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
|
251 |
return (getConnection(url, info, Reflection.getCallerClass())); |
2 | 252 |
} |
253 |
||
254 |
/** |
|
255 |
* Attempts to locate a driver that understands the given URL. |
|
256 |
* The <code>DriverManager</code> attempts to select an appropriate driver from |
|
257 |
* the set of registered JDBC drivers. |
|
258 |
* |
|
259 |
* @param url a database URL of the form |
|
260 |
* <code>jdbc:<em>subprotocol</em>:<em>subname</em></code> |
|
261 |
* @return a <code>Driver</code> object representing a driver |
|
262 |
* that can connect to the given URL |
|
263 |
* @exception SQLException if a database access error occurs |
|
264 |
*/ |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16887
diff
changeset
|
265 |
@CallerSensitive |
2 | 266 |
public static Driver getDriver(String url) |
267 |
throws SQLException { |
|
268 |
||
269 |
println("DriverManager.getDriver(\"" + url + "\")"); |
|
270 |
||
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
271 |
ensureDriversInitialized(); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
272 |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16887
diff
changeset
|
273 |
Class<?> callerClass = Reflection.getCallerClass(); |
2 | 274 |
|
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
275 |
// Walk through the loaded registeredDrivers attempting to locate someone |
2 | 276 |
// who understands the given URL. |
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
277 |
for (DriverInfo aDriver : registeredDrivers) { |
2 | 278 |
// If the caller does not have permission to load the driver then |
279 |
// skip it. |
|
27801
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
280 |
if (isDriverAllowed(aDriver.driver, callerClass)) { |
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
281 |
try { |
27801
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
282 |
if (aDriver.driver.acceptsURL(url)) { |
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
283 |
// Success! |
9239 | 284 |
println("getDriver returning " + aDriver.driver.getClass().getName()); |
285 |
return (aDriver.driver); |
|
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
286 |
} |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
287 |
|
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
288 |
} catch(SQLException sqe) { |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
289 |
// Drop through and try the next driver. |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
290 |
} |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
291 |
} else { |
9239 | 292 |
println(" skipping: " + aDriver.driver.getClass().getName()); |
2 | 293 |
} |
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
294 |
|
2 | 295 |
} |
296 |
||
297 |
println("getDriver: no suitable driver"); |
|
298 |
throw new SQLException("No suitable driver", "08001"); |
|
299 |
} |
|
300 |
||
301 |
||
302 |
/** |
|
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
303 |
* Registers the given driver with the {@code DriverManager}. |
2 | 304 |
* A newly-loaded driver class should call |
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
305 |
* the method {@code registerDriver} to make itself |
21950 | 306 |
* known to the {@code DriverManager}. If the driver is currently |
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
307 |
* registered, no action is taken. |
2 | 308 |
* |
309 |
* @param driver the new JDBC Driver that is to be registered with the |
|
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
310 |
* {@code DriverManager} |
2 | 311 |
* @exception SQLException if a database access error occurs |
19858
7c37f6ac403b
8014967: EBehavior of DriverManager.registerDriver(dr) is unspecified if driver is null
lancea
parents:
18156
diff
changeset
|
312 |
* @exception NullPointerException if {@code driver} is null |
2 | 313 |
*/ |
27801
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
314 |
public static void registerDriver(java.sql.Driver driver) |
2 | 315 |
throws SQLException { |
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
316 |
|
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
317 |
registerDriver(driver, null); |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
318 |
} |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
319 |
|
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
320 |
/** |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
321 |
* Registers the given driver with the {@code DriverManager}. |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
322 |
* A newly-loaded driver class should call |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
323 |
* the method {@code registerDriver} to make itself |
21950 | 324 |
* known to the {@code DriverManager}. If the driver is currently |
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
325 |
* registered, no action is taken. |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
326 |
* |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
327 |
* @param driver the new JDBC Driver that is to be registered with the |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
328 |
* {@code DriverManager} |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
329 |
* @param da the {@code DriverAction} implementation to be used when |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
330 |
* {@code DriverManager#deregisterDriver} is called |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
331 |
* @exception SQLException if a database access error occurs |
19858
7c37f6ac403b
8014967: EBehavior of DriverManager.registerDriver(dr) is unspecified if driver is null
lancea
parents:
18156
diff
changeset
|
332 |
* @exception NullPointerException if {@code driver} is null |
21950 | 333 |
* @since 1.8 |
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
334 |
*/ |
27801
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
335 |
public static void registerDriver(java.sql.Driver driver, |
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
336 |
DriverAction da) |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
337 |
throws SQLException { |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
338 |
|
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
339 |
/* Register the driver if it has not already been added to our list */ |
27801
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
340 |
if (driver != null) { |
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
341 |
registeredDrivers.addIfAbsent(new DriverInfo(driver, da)); |
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
342 |
} else { |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
343 |
// This is for compatibility with the original DriverManager |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
344 |
throw new NullPointerException(); |
2 | 345 |
} |
346 |
||
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
347 |
println("registerDriver: " + driver); |
2 | 348 |
|
349 |
} |
|
350 |
||
351 |
/** |
|
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
352 |
* Removes the specified driver from the {@code DriverManager}'s list of |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
353 |
* registered drivers. |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
354 |
* <p> |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
355 |
* If a {@code null} value is specified for the driver to be removed, then no |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
356 |
* action is taken. |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
357 |
* <p> |
42700 | 358 |
* If a security manager exists, its {@code checkPermission} |
359 |
* method is first called with a {@code SQLPermission("deregisterDriver")} |
|
360 |
* permission to check that the caller is allowed to deregister a JDBC Driver. |
|
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
361 |
* <p> |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
362 |
* If the specified driver is not found in the list of registered drivers, |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
363 |
* then no action is taken. If the driver was found, it will be removed |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
364 |
* from the list of registered drivers. |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
365 |
* <p> |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
366 |
* If a {@code DriverAction} instance was specified when the JDBC driver was |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
367 |
* registered, its deregister method will be called |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
368 |
* prior to the driver being removed from the list of registered drivers. |
2 | 369 |
* |
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
370 |
* @param driver the JDBC Driver to remove |
2 | 371 |
* @exception SQLException if a database access error occurs |
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
372 |
* @throws SecurityException if a security manager exists and its |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
373 |
* {@code checkPermission} method denies permission to deregister a driver. |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
374 |
* |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
375 |
* @see SecurityManager#checkPermission |
2 | 376 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16887
diff
changeset
|
377 |
@CallerSensitive |
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
378 |
public static void deregisterDriver(Driver driver) throws SQLException { |
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
379 |
if (driver == null) { |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
380 |
return; |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
381 |
} |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
382 |
|
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
383 |
SecurityManager sec = System.getSecurityManager(); |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
384 |
if (sec != null) { |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
385 |
sec.checkPermission(DEREGISTER_DRIVER_PERMISSION); |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
386 |
} |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
387 |
|
2 | 388 |
println("DriverManager.deregisterDriver: " + driver); |
389 |
||
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
390 |
DriverInfo aDriver = new DriverInfo(driver, null); |
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
391 |
synchronized (lockForInitDrivers) { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
392 |
if (registeredDrivers.contains(aDriver)) { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
393 |
if (isDriverAllowed(driver, Reflection.getCallerClass())) { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
394 |
DriverInfo di = registeredDrivers.get(registeredDrivers.indexOf(aDriver)); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
395 |
// If a DriverAction was specified, Call it to notify the |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
396 |
// driver that it has been deregistered |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
397 |
if (di.action() != null) { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
398 |
di.action().deregister(); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
399 |
} |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
400 |
registeredDrivers.remove(aDriver); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
401 |
} else { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
402 |
// If the caller does not have permission to load the driver then |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
403 |
// throw a SecurityException. |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
404 |
throw new SecurityException(); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
405 |
} |
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
406 |
} else { |
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
407 |
println(" couldn't find driver to unload"); |
2 | 408 |
} |
409 |
} |
|
410 |
} |
|
411 |
||
412 |
/** |
|
413 |
* Retrieves an Enumeration with all of the currently loaded JDBC drivers |
|
414 |
* to which the current caller has access. |
|
415 |
* |
|
416 |
* <P><B>Note:</B> The classname of a driver can be found using |
|
417 |
* <CODE>d.getClass().getName()</CODE> |
|
418 |
* |
|
419 |
* @return the list of JDBC Drivers loaded by the caller's class loader |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
420 |
* @see #drivers() |
2 | 421 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16887
diff
changeset
|
422 |
@CallerSensitive |
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
423 |
public static Enumeration<Driver> getDrivers() { |
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
424 |
ensureDriversInitialized(); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
425 |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
426 |
return Collections.enumeration(getDrivers(Reflection.getCallerClass())); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
427 |
} |
2 | 428 |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
429 |
/** |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
430 |
* Retrieves a Stream with all of the currently loaded JDBC drivers |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
431 |
* to which the current caller has access. |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
432 |
* |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
433 |
* @return the stream of JDBC Drivers loaded by the caller's class loader |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
31469
diff
changeset
|
434 |
* @since 9 |
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
435 |
*/ |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
436 |
@CallerSensitive |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
437 |
public static Stream<Driver> drivers() { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
438 |
ensureDriversInitialized(); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
439 |
|
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
440 |
return getDrivers(Reflection.getCallerClass()).stream(); |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
441 |
} |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
442 |
|
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
443 |
private static List<Driver> getDrivers(Class<?> callerClass) { |
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
444 |
List<Driver> result = new ArrayList<>(); |
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
445 |
// Walk through the loaded registeredDrivers. |
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
446 |
for (DriverInfo aDriver : registeredDrivers) { |
2 | 447 |
// If the caller does not have permission to load the driver then |
448 |
// skip it. |
|
27801
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
449 |
if (isDriverAllowed(aDriver.driver, callerClass)) { |
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
450 |
result.add(aDriver.driver); |
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
451 |
} else { |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
452 |
println(" skipping: " + aDriver.getClass().getName()); |
2 | 453 |
} |
454 |
} |
|
31469
92cc72d2a11a
8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents:
28117
diff
changeset
|
455 |
return result; |
2 | 456 |
} |
457 |
||
458 |
/** |
|
459 |
* Sets the maximum time in seconds that a driver will wait |
|
15278 | 460 |
* while attempting to connect to a database once the driver has |
461 |
* been identified. |
|
2 | 462 |
* |
463 |
* @param seconds the login time limit in seconds; zero means there is no limit |
|
464 |
* @see #getLoginTimeout |
|
465 |
*/ |
|
466 |
public static void setLoginTimeout(int seconds) { |
|
467 |
loginTimeout = seconds; |
|
468 |
} |
|
469 |
||
470 |
/** |
|
471 |
* Gets the maximum time in seconds that a driver can wait |
|
472 |
* when attempting to log in to a database. |
|
473 |
* |
|
474 |
* @return the driver login time limit in seconds |
|
475 |
* @see #setLoginTimeout |
|
476 |
*/ |
|
477 |
public static int getLoginTimeout() { |
|
478 |
return (loginTimeout); |
|
479 |
} |
|
480 |
||
481 |
/** |
|
482 |
* Sets the logging/tracing PrintStream that is used |
|
483 |
* by the <code>DriverManager</code> |
|
484 |
* and all drivers. |
|
485 |
*<P> |
|
42700 | 486 |
* If a security manager exists, its {@code checkPermission} |
487 |
* method is first called with a {@code SQLPermission("setLog")} |
|
488 |
* permission to check that the caller is allowed to call {@code setLogStream}. |
|
2 | 489 |
* |
490 |
* @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
|
491 |
* @deprecated Use {@code setLogWriter} |
2 | 492 |
* @throws SecurityException if a security manager exists and its |
42700 | 493 |
* {@code checkPermission} method denies permission to set the log stream. |
2 | 494 |
* @see SecurityManager#checkPermission |
495 |
* @see #getLogStream |
|
496 |
*/ |
|
41889
54d1ff9312ce
8169020: Add since element to JDBC deprecated methods
lancea
parents:
37363
diff
changeset
|
497 |
@Deprecated(since="1.2") |
2 | 498 |
public static void setLogStream(java.io.PrintStream out) { |
499 |
||
500 |
SecurityManager sec = System.getSecurityManager(); |
|
501 |
if (sec != null) { |
|
502 |
sec.checkPermission(SET_LOG_PERMISSION); |
|
503 |
} |
|
504 |
||
505 |
logStream = out; |
|
506 |
if ( out != null ) |
|
507 |
logWriter = new java.io.PrintWriter(out); |
|
508 |
else |
|
509 |
logWriter = null; |
|
510 |
} |
|
511 |
||
512 |
/** |
|
513 |
* Retrieves the logging/tracing PrintStream that is used by the <code>DriverManager</code> |
|
514 |
* and all drivers. |
|
515 |
* |
|
516 |
* @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
|
517 |
* @deprecated Use {@code getLogWriter} |
2 | 518 |
* @see #setLogStream |
519 |
*/ |
|
41889
54d1ff9312ce
8169020: Add since element to JDBC deprecated methods
lancea
parents:
37363
diff
changeset
|
520 |
@Deprecated(since="1.2") |
2 | 521 |
public static java.io.PrintStream getLogStream() { |
522 |
return logStream; |
|
523 |
} |
|
524 |
||
525 |
/** |
|
526 |
* Prints a message to the current JDBC log stream. |
|
527 |
* |
|
528 |
* @param message a log or tracing message |
|
529 |
*/ |
|
530 |
public static void println(String message) { |
|
531 |
synchronized (logSync) { |
|
532 |
if (logWriter != null) { |
|
533 |
logWriter.println(message); |
|
534 |
||
535 |
// automatic flushing is never enabled, so we must do it ourselves |
|
536 |
logWriter.flush(); |
|
537 |
} |
|
538 |
} |
|
539 |
} |
|
540 |
||
541 |
//------------------------------------------------------------------------ |
|
542 |
||
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16887
diff
changeset
|
543 |
// 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
|
544 |
// DriverManager is accessible. |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16887
diff
changeset
|
545 |
private static boolean isDriverAllowed(Driver driver, Class<?> caller) { |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16887
diff
changeset
|
546 |
ClassLoader callerCL = caller != null ? caller.getClassLoader() : null; |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16887
diff
changeset
|
547 |
return isDriverAllowed(driver, callerCL); |
12885
30a5ef357cf1
7172551: Remove Native calls from DriverManager for jigsaw
lancea
parents:
11129
diff
changeset
|
548 |
} |
30a5ef357cf1
7172551: Remove Native calls from DriverManager for jigsaw
lancea
parents:
11129
diff
changeset
|
549 |
|
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
550 |
private static boolean isDriverAllowed(Driver driver, ClassLoader classLoader) { |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
551 |
boolean result = false; |
27801
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
552 |
if (driver != null) { |
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
553 |
Class<?> aClass = null; |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
554 |
try { |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
555 |
aClass = Class.forName(driver.getClass().getName(), true, classLoader); |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
556 |
} catch (Exception ex) { |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
557 |
result = false; |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
558 |
} |
2 | 559 |
|
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
560 |
result = ( aClass == driver.getClass() ) ? true : false; |
2 | 561 |
} |
562 |
||
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
563 |
return result; |
2 | 564 |
} |
565 |
||
27801
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
566 |
/* |
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
567 |
* Load the initial JDBC drivers by checking the System property |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
568 |
* jdbc.drivers and then use the {@code ServiceLoader} mechanism |
27801
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
569 |
*/ |
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
570 |
private static void ensureDriversInitialized() { |
27801
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
571 |
if (driversInitialized) { |
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
572 |
return; |
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
573 |
} |
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
574 |
|
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
575 |
synchronized (lockForInitDrivers) { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
576 |
if (driversInitialized) { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
577 |
return; |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
578 |
} |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
579 |
String drivers; |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
580 |
try { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
581 |
drivers = AccessController.doPrivileged(new PrivilegedAction<String>() { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
582 |
public String run() { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
583 |
return System.getProperty(JDBC_DRIVERS_PROPERTY); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
584 |
} |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
585 |
}); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
586 |
} catch (Exception ex) { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
587 |
drivers = null; |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
588 |
} |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
589 |
// If the driver is packaged as a Service Provider, load it. |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
590 |
// Get all the drivers through the classloader |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
591 |
// exposed as a java.sql.Driver.class service. |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
592 |
// ServiceLoader.load() replaces the sun.misc.Providers() |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
593 |
|
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
594 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
595 |
public Void run() { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
596 |
|
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
597 |
ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
598 |
Iterator<Driver> driversIterator = loadedDrivers.iterator(); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
599 |
|
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
600 |
/* Load these drivers, so that they can be instantiated. |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
601 |
* It may be the case that the driver class may not be there |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
602 |
* i.e. there may be a packaged driver with the service class |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
603 |
* as implementation of java.sql.Driver but the actual class |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
604 |
* may be missing. In that case a java.util.ServiceConfigurationError |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
605 |
* will be thrown at runtime by the VM trying to locate |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
606 |
* and load the service. |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
607 |
* |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
608 |
* Adding a try catch block to catch those runtime errors |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
609 |
* if driver not available in classpath but it's |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
610 |
* packaged as service and that service is there in classpath. |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
611 |
*/ |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
612 |
try { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
613 |
while (driversIterator.hasNext()) { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
614 |
driversIterator.next(); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
615 |
} |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
616 |
} catch (Throwable t) { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
617 |
// Do nothing |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
618 |
} |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
619 |
return null; |
2 | 620 |
} |
621 |
}); |
|
622 |
||
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
623 |
println("DriverManager.initialize: jdbc.drivers = " + drivers); |
2 | 624 |
|
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
625 |
if (drivers != null && !drivers.equals("")) { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
626 |
String[] driversList = drivers.split(":"); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
627 |
println("number of Drivers:" + driversList.length); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
628 |
for (String aDriver : driversList) { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
629 |
try { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
630 |
println("DriverManager.Initialize: loading " + aDriver); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
631 |
Class.forName(aDriver, true, |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
632 |
ClassLoader.getSystemClassLoader()); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
633 |
} catch (Exception ex) { |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
634 |
println("DriverManager.Initialize: load failed: " + ex); |
2 | 635 |
} |
636 |
} |
|
637 |
} |
|
638 |
||
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
639 |
driversInitialized = true; |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
640 |
println("JDBC DriverManager initialized"); |
2 | 641 |
} |
642 |
} |
|
643 |
||
644 |
||
645 |
// Worker method called by the public getConnection() methods. |
|
646 |
private static Connection getConnection( |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16887
diff
changeset
|
647 |
String url, java.util.Properties info, Class<?> caller) throws SQLException { |
2 | 648 |
/* |
649 |
* When callerCl is null, we should check the application's |
|
650 |
* (which is invoking this class indirectly) |
|
651 |
* classloader, so that the JDBC driver class outside rt.jar |
|
652 |
* can be loaded from here. |
|
653 |
*/ |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16887
diff
changeset
|
654 |
ClassLoader callerCL = caller != null ? caller.getClassLoader() : null; |
27801
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
655 |
if (callerCL == null) { |
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
656 |
callerCL = Thread.currentThread().getContextClassLoader(); |
2 | 657 |
} |
658 |
||
27801
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
659 |
if (url == null) { |
2 | 660 |
throw new SQLException("The url cannot be null", "08001"); |
661 |
} |
|
662 |
||
663 |
println("DriverManager.getConnection(\"" + url + "\")"); |
|
664 |
||
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
665 |
ensureDriversInitialized(); |
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
666 |
|
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
667 |
// Walk through the loaded registeredDrivers attempting to make a connection. |
2 | 668 |
// Remember the first exception that gets raised so we can reraise it. |
669 |
SQLException reason = null; |
|
670 |
||
28117
8da4af46db02
8067904: Additional DriverManager clean-up from 8060068
lancea
parents:
27801
diff
changeset
|
671 |
for (DriverInfo aDriver : registeredDrivers) { |
2 | 672 |
// If the caller does not have permission to load the driver then |
673 |
// skip it. |
|
27801
2b1b7e235693
8060068: Possible Deadlock scenario with DriverManager.loadInitialDrivers
lancea
parents:
25859
diff
changeset
|
674 |
if (isDriverAllowed(aDriver.driver, callerCL)) { |
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
675 |
try { |
9239 | 676 |
println(" trying " + aDriver.driver.getClass().getName()); |
677 |
Connection con = aDriver.driver.connect(url, info); |
|
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
678 |
if (con != null) { |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
679 |
// Success! |
9239 | 680 |
println("getConnection returning " + aDriver.driver.getClass().getName()); |
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
681 |
return (con); |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
682 |
} |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
683 |
} catch (SQLException ex) { |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
684 |
if (reason == null) { |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
685 |
reason = ex; |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
686 |
} |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
687 |
} |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
688 |
|
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
689 |
} else { |
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
690 |
println(" skipping: " + aDriver.getClass().getName()); |
2 | 691 |
} |
8797
e8507464a69d
7026898: DriverManager to now use CopyOnWriteArrayList
lancea
parents:
7803
diff
changeset
|
692 |
|
2 | 693 |
} |
694 |
||
695 |
// if we got here nobody could connect. |
|
696 |
if (reason != null) { |
|
697 |
println("getConnection failed: " + reason); |
|
698 |
throw reason; |
|
699 |
} |
|
700 |
||
701 |
println("getConnection: no suitable driver found for "+ url); |
|
702 |
throw new SQLException("No suitable driver found for "+ url, "08001"); |
|
703 |
} |
|
704 |
||
705 |
||
706 |
} |
|
9239 | 707 |
|
708 |
/* |
|
709 |
* Wrapper class for registered Drivers in order to not expose Driver.equals() |
|
710 |
* to avoid the capture of the Driver it being compared to as it might not |
|
711 |
* normally have access. |
|
712 |
*/ |
|
713 |
class DriverInfo { |
|
714 |
||
715 |
final Driver driver; |
|
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
716 |
DriverAction da; |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
717 |
DriverInfo(Driver driver, DriverAction action) { |
9239 | 718 |
this.driver = driver; |
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
719 |
da = action; |
9239 | 720 |
} |
721 |
||
11129
f9ad1aadf3fa
7116445: Miscellaneous warnings in the JDBC/RowSet classes
lancea
parents:
9243
diff
changeset
|
722 |
@Override |
9239 | 723 |
public boolean equals(Object other) { |
724 |
return (other instanceof DriverInfo) |
|
725 |
&& this.driver == ((DriverInfo) other).driver; |
|
726 |
} |
|
727 |
||
11129
f9ad1aadf3fa
7116445: Miscellaneous warnings in the JDBC/RowSet classes
lancea
parents:
9243
diff
changeset
|
728 |
@Override |
9239 | 729 |
public int hashCode() { |
730 |
return driver.hashCode(); |
|
731 |
} |
|
732 |
||
11129
f9ad1aadf3fa
7116445: Miscellaneous warnings in the JDBC/RowSet classes
lancea
parents:
9243
diff
changeset
|
733 |
@Override |
9239 | 734 |
public String toString() { |
735 |
return ("driver[className=" + driver + "]"); |
|
736 |
} |
|
17199
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
737 |
|
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
738 |
DriverAction action() { |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
739 |
return da; |
28853592ce2b
8010416: Add a way for java.sql.Driver to be notified when it is deregistered
lancea
parents:
16906
diff
changeset
|
740 |
} |
9239 | 741 |
} |