JdbcConnectionProvider.java

  1. /*
  2.  *    Copyright 2010-2022 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.migration;

  17. import java.sql.Connection;
  18. import java.sql.Driver;
  19. import java.sql.DriverManager;
  20. import java.sql.SQLException;
  21. import java.util.Enumeration;
  22. import java.util.HashMap;
  23. import java.util.Map;

  24. import org.apache.ibatis.migration.driver.DriverShim;

  25. public class JdbcConnectionProvider implements ConnectionProvider {
  26.   private static final Map<String, Driver> registeredDrivers = registeredDrivers();

  27.   private final String url;
  28.   private final String username;
  29.   private final String password;

  30.   public JdbcConnectionProvider(String driver, String url, String username, String password) {
  31.     this(null, driver, url, username, password);
  32.   }

  33.   public JdbcConnectionProvider(ClassLoader classLoader, String driver, String url, String username, String password) {
  34.     this.url = url;
  35.     this.username = username;
  36.     this.password = password;
  37.     registerDriver(classLoader, driver);
  38.   }

  39.   @Override
  40.   public Connection getConnection() throws SQLException {
  41.     return DriverManager.getConnection(url, username, password);
  42.   }

  43.   private void registerDriver(ClassLoader classLoader, String driver) {
  44.     registeredDrivers.computeIfAbsent(driver, d -> createDriverClass(classLoader, d));
  45.   }

  46.   private Driver createDriverClass(ClassLoader classLoader, String driver) {
  47.     try {
  48.       final Class<?> driverClass = classLoader == null ? Class.forName(driver)
  49.           : Class.forName(driver, true, classLoader);
  50.       final Driver driverInstance = (Driver) driverClass.getDeclaredConstructor().newInstance();
  51.       final DriverShim driverShim = new DriverShim(driverInstance);
  52.       DriverManager.registerDriver(driverShim);
  53.       return driverShim;
  54.     } catch (final Exception e) {
  55.       throw new IllegalStateException("Failed to register driver " + driver, e);
  56.     }
  57.   }

  58.   private static Map<String, Driver> registeredDrivers() {
  59.     final Map<String, Driver> registeredDrivers = new HashMap<>();
  60.     final Enumeration<Driver> drivers = DriverManager.getDrivers();
  61.     while (drivers.hasMoreElements()) {
  62.       final Driver driver = drivers.nextElement();
  63.       registeredDrivers.put(driver.getClass().getName(), driver);
  64.     }
  65.     return registeredDrivers;
  66.   }
  67. }