1 /* |
1 /* |
2 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package java.sql; |
26 package java.sql; |
27 |
27 |
|
28 import java.util.ArrayList; |
|
29 import java.util.Collections; |
|
30 import java.util.Enumeration; |
28 import java.util.Iterator; |
31 import java.util.Iterator; |
|
32 import java.util.List; |
29 import java.util.ServiceLoader; |
33 import java.util.ServiceLoader; |
30 import java.security.AccessController; |
34 import java.security.AccessController; |
31 import java.security.PrivilegedAction; |
35 import java.security.PrivilegedAction; |
32 import java.util.concurrent.CopyOnWriteArrayList; |
36 import java.util.concurrent.CopyOnWriteArrayList; |
|
37 import java.util.stream.Stream; |
|
38 |
33 import sun.reflect.CallerSensitive; |
39 import sun.reflect.CallerSensitive; |
34 import sun.reflect.Reflection; |
40 import sun.reflect.Reflection; |
35 |
41 |
36 |
42 |
37 /** |
43 /** |
427 * |
433 * |
428 * <P><B>Note:</B> The classname of a driver can be found using |
434 * <P><B>Note:</B> The classname of a driver can be found using |
429 * <CODE>d.getClass().getName()</CODE> |
435 * <CODE>d.getClass().getName()</CODE> |
430 * |
436 * |
431 * @return the list of JDBC Drivers loaded by the caller's class loader |
437 * @return the list of JDBC Drivers loaded by the caller's class loader |
|
438 * @see #drivers() |
432 */ |
439 */ |
433 @CallerSensitive |
440 @CallerSensitive |
434 public static java.util.Enumeration<Driver> getDrivers() { |
441 public static Enumeration<Driver> getDrivers() { |
435 java.util.Vector<Driver> result = new java.util.Vector<>(); |
|
436 |
|
437 ensureDriversInitialized(); |
442 ensureDriversInitialized(); |
438 |
443 |
439 Class<?> callerClass = Reflection.getCallerClass(); |
444 return Collections.enumeration(getDrivers(Reflection.getCallerClass())); |
440 |
445 } |
|
446 |
|
447 /** |
|
448 * Retrieves a Stream with all of the currently loaded JDBC drivers |
|
449 * to which the current caller has access. |
|
450 * |
|
451 * @return the stream of JDBC Drivers loaded by the caller's class loader |
|
452 * @since 1.9 |
|
453 */ |
|
454 @CallerSensitive |
|
455 public static Stream<Driver> drivers() { |
|
456 ensureDriversInitialized(); |
|
457 |
|
458 return getDrivers(Reflection.getCallerClass()).stream(); |
|
459 } |
|
460 |
|
461 private static List<Driver> getDrivers(Class<?> callerClass) { |
|
462 List<Driver> result = new ArrayList<>(); |
441 // Walk through the loaded registeredDrivers. |
463 // Walk through the loaded registeredDrivers. |
442 for (DriverInfo aDriver : registeredDrivers) { |
464 for (DriverInfo aDriver : registeredDrivers) { |
443 // If the caller does not have permission to load the driver then |
465 // If the caller does not have permission to load the driver then |
444 // skip it. |
466 // skip it. |
445 if (isDriverAllowed(aDriver.driver, callerClass)) { |
467 if (isDriverAllowed(aDriver.driver, callerClass)) { |
446 result.addElement(aDriver.driver); |
468 result.add(aDriver.driver); |
447 } else { |
469 } else { |
448 println(" skipping: " + aDriver.getClass().getName()); |
470 println(" skipping: " + aDriver.getClass().getName()); |
449 } |
471 } |
450 } |
472 } |
451 return (result.elements()); |
473 return result; |
452 } |
474 } |
453 |
|
454 |
475 |
455 /** |
476 /** |
456 * Sets the maximum time in seconds that a driver will wait |
477 * Sets the maximum time in seconds that a driver will wait |
457 * while attempting to connect to a database once the driver has |
478 * while attempting to connect to a database once the driver has |
458 * been identified. |
479 * been identified. |