Пример #1
0
  /**
   * Removes the specified driver from the {@code DriverManager}'s list of registered drivers.
   *
   * <p>If a {@code null} value is specified for the driver to be removed, then no action is taken.
   *
   * <p>If a security manager exists and its {@code checkPermission} denies permission, then a
   * {@code SecurityException} will be thrown.
   *
   * <p>If the specified driver is not found in the list of registered drivers, then no action is
   * taken. If the driver was found, it will be removed from the list of registered drivers.
   *
   * <p>If a {@code DriverAction} instance was specified when the JDBC driver was registered, its
   * deregister method will be called prior to the driver being removed from the list of registered
   * drivers.
   *
   * @param driver the JDBC Driver to remove
   * @exception SQLException if a database access error occurs
   * @throws SecurityException if a security manager exists and its {@code checkPermission} method
   *     denies permission to deregister a driver.
   * @see SecurityManager#checkPermission
   */
  @CallerSensitive
  public static synchronized void deregisterDriver(Driver driver) throws SQLException {
    if (driver == null) {
      return;
    }

    SecurityManager sec = System.getSecurityManager();
    if (sec != null) {
      sec.checkPermission(DEREGISTER_DRIVER_PERMISSION);
    }

    println("DriverManager.deregisterDriver: " + driver);

    DriverInfo aDriver = new DriverInfo(driver, null);
    if (registeredDrivers.contains(aDriver)) {
      if (isDriverAllowed(driver, Reflection.getCallerClass())) {
        DriverInfo di = registeredDrivers.get(registeredDrivers.indexOf(aDriver));
        // If a DriverAction was specified, Call it to notify the
        // driver that it has been deregistered
        if (di.action() != null) {
          di.action().deregister();
        }
        registeredDrivers.remove(aDriver);
      } else {
        // If the caller does not have permission to load the driver then
        // throw a SecurityException.
        throw new SecurityException();
      }
    } else {
      println("    couldn't find driver to unload");
    }
  }
Пример #2
0
  /**
   * Attempts to locate a driver that understands the given URL. The <code>DriverManager</code>
   * attempts to select an appropriate driver from the set of registered JDBC drivers.
   *
   * @param url a database URL of the form <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>
   * @return a <code>Driver</code> object representing a driver that can connect to the given URL
   * @exception SQLException if a database access error occurs
   */
  @CallerSensitive
  public static Driver getDriver(String url) throws SQLException {

    println("DriverManager.getDriver(\"" + url + "\")");

    Class<?> callerClass = Reflection.getCallerClass();

    // Walk through the loaded registeredDrivers attempting to locate someone
    // who understands the given URL.
    for (DriverInfo aDriver : registeredDrivers) {
      // If the caller does not have permission to load the driver then
      // skip it.
      if (isDriverAllowed(aDriver.driver, callerClass)) {
        try {
          if (aDriver.driver.acceptsURL(url)) {
            // Success!
            println("getDriver returning " + aDriver.driver.getClass().getName());
            return (aDriver.driver);
          }

        } catch (SQLException sqe) {
          // Drop through and try the next driver.
        }
      } else {
        println("    skipping: " + aDriver.driver.getClass().getName());
      }
    }

    println("getDriver: no suitable driver");
    throw new SQLException("No suitable driver", "08001");
  }
Пример #3
0
  static {
    String title = ING_TRACE_NAME + "-Driver";

    /*
     ** Initialize Ingres driver tracing.
     */
    IngConfig.setDriverVers(driverMajorVersion, driverMinorVersion, driverPatchVersion);
    TraceLog traceLog = IngTrace.getTraceLog();

    /*
     ** Create IngresDriver object and register with DriverManager.
     */
    try {
      DriverManager.registerDriver(new IngresDriver());
      DriverManager.println(
          ING_DRIVER_NAME
              + ": Version "
              + driverMajorVersion
              + "."
              + driverMinorVersion
              + "."
              + driverPatchVersion
              + " ("
              + driverJdbcVersion
              + ")");
      DriverManager.println(driverVendor);
      traceLog.write(title + ": registered");
    } catch (Exception ex) {
      DriverManager.println(ING_DRIVER_NAME + ": registration failed!");
      traceLog.write(title + ": registration failed!");
    }
  } // <class initializer>
Пример #4
0
 /**
  * Print a diagnostic message to the output stream provided by the DataSource or the
  * DriverManager.
  *
  * @param message the diagnostic message to print
  */
 public static void println(String message) {
   if (log != null) {
     log.println(message);
   } else {
     DriverManager.println(message);
   }
 }
Пример #5
0
  /**
   * Registers the given driver with the {@code DriverManager}. A newly-loaded driver class should
   * call the method {@code registerDriver} to make itself known to the {@code DriverManager}. If
   * the driver is currently registered, no action is taken.
   *
   * @param driver the new JDBC Driver that is to be registered with the {@code DriverManager}
   * @param da the {@code DriverAction} implementation to be used when {@code
   *     DriverManager#deregisterDriver} is called
   * @exception SQLException if a database access error occurs
   * @exception NullPointerException if {@code driver} is null
   * @since 1.8
   */
  public static synchronized void registerDriver(java.sql.Driver driver, DriverAction da)
      throws SQLException {

    /* Register the driver if it has not already been added to our list */
    if (driver != null) {
      registeredDrivers.addIfAbsent(new DriverInfo(driver, da));
    } else {
      // This is for compatibility with the original DriverManager
      throw new NullPointerException();
    }

    println("registerDriver: " + driver);
  }
Пример #6
0
  //  Worker method called by the public getConnection() methods.
  private static Connection getConnection(String url, java.util.Properties info, Class<?> caller)
      throws SQLException {
    /*
     * When callerCl is null, we should check the application's
     * (which is invoking this class indirectly)
     * classloader, so that the JDBC driver class outside rt.jar
     * can be loaded from here.
     */
    ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
    synchronized (DriverManager.class) {
      // synchronize loading of the correct classloader.
      if (callerCL == null) {
        callerCL = Thread.currentThread().getContextClassLoader();
      }
    }

    if (url == null) {
      throw new SQLException("The url cannot be null", "08001");
    }

    println("DriverManager.getConnection(\"" + url + "\")");

    // Walk through the loaded registeredDrivers attempting to make a connection.
    // Remember the first exception that gets raised so we can reraise it.
    SQLException reason = null;

    for (DriverInfo aDriver : registeredDrivers) {
      // If the caller does not have permission to load the driver then
      // skip it.
      if (isDriverAllowed(aDriver.driver, callerCL)) {
        try {
          println("    trying " + aDriver.driver.getClass().getName());
          Connection con = aDriver.driver.connect(url, info);
          if (con != null) {
            // Success!
            println("getConnection returning " + aDriver.driver.getClass().getName());
            return (con);
          }
        } catch (SQLException ex) {
          if (reason == null) {
            reason = ex;
          }
        }

      } else {
        println("    skipping: " + aDriver.getClass().getName());
      }
    }

    // if we got here nobody could connect.
    if (reason != null) {
      println("getConnection failed: " + reason);
      throw reason;
    }

    println("getConnection: no suitable driver found for " + url);
    throw new SQLException("No suitable driver found for " + url, "08001");
  }
Пример #7
0
  /**
   * Retrieves an Enumeration with all of the currently loaded JDBC drivers to which the current
   * caller has access.
   *
   * <p><B>Note:</B> The classname of a driver can be found using <CODE>d.getClass().getName()
   * </CODE>
   *
   * @return the list of JDBC Drivers loaded by the caller's class loader
   */
  @CallerSensitive
  public static java.util.Enumeration<Driver> getDrivers() {
    java.util.Vector<Driver> result = new java.util.Vector<>();

    Class<?> callerClass = Reflection.getCallerClass();

    // Walk through the loaded registeredDrivers.
    for (DriverInfo aDriver : registeredDrivers) {
      // If the caller does not have permission to load the driver then
      // skip it.
      if (isDriverAllowed(aDriver.driver, callerClass)) {
        result.addElement(aDriver.driver);
      } else {
        println("    skipping: " + aDriver.getClass().getName());
      }
    }
    return (result.elements());
  }
Пример #8
0
 /**
  * Load the initial JDBC drivers by checking the System property jdbc.properties and then use the
  * {@code ServiceLoader} mechanism
  */
 static {
   loadInitialDrivers();
   println("JDBC DriverManager initialized");
 }
Пример #9
0
  private static void loadInitialDrivers() {
    String drivers;
    try {
      drivers =
          AccessController.doPrivileged(
              new PrivilegedAction<String>() {
                public String run() {
                  return System.getProperty("jdbc.drivers");
                }
              });
    } catch (Exception ex) {
      drivers = null;
    }
    // If the driver is packaged as a Service Provider, load it.
    // Get all the drivers through the classloader
    // exposed as a java.sql.Driver.class service.
    // ServiceLoader.load() replaces the sun.misc.Providers()

    AccessController.doPrivileged(
        new PrivilegedAction<Void>() {
          public Void run() {

            ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
            Iterator<Driver> driversIterator = loadedDrivers.iterator();

            /* Load these drivers, so that they can be instantiated.
             * It may be the case that the driver class may not be there
             * i.e. there may be a packaged driver with the service class
             * as implementation of java.sql.Driver but the actual class
             * may be missing. In that case a java.util.ServiceConfigurationError
             * will be thrown at runtime by the VM trying to locate
             * and load the service.
             *
             * Adding a try catch block to catch those runtime errors
             * if driver not available in classpath but it's
             * packaged as service and that service is there in classpath.
             */
            try {
              while (driversIterator.hasNext()) {
                driversIterator.next();
              }
            } catch (Throwable t) {
              // Do nothing
            }
            return null;
          }
        });

    println("DriverManager.initialize: jdbc.drivers = " + drivers);

    if (drivers == null || drivers.equals("")) {
      return;
    }
    String[] driversList = drivers.split(":");
    println("number of Drivers:" + driversList.length);
    for (String aDriver : driversList) {
      try {
        println("DriverManager.Initialize: loading " + aDriver);
        Class.forName(aDriver, true, ClassLoader.getSystemClassLoader());
      } catch (Exception ex) {
        println("DriverManager.Initialize: load failed: " + ex);
      }
    }
  }