ConnectionFactoryFactory.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.dbcp2;

  18. import java.sql.Driver;
  19. import java.sql.SQLException;
  20. import java.util.Properties;

  21. /*
  22.  * Creates {@link ConnectionFactory} instances.
  23.  *
  24.  * @since 2.7.0
  25.  */
  26. final class ConnectionFactoryFactory {

  27.     /**
  28.      * Creates a new {@link DriverConnectionFactory} allowing for an override through
  29.      * {@link BasicDataSource#getDriverClassName()}.
  30.      *
  31.      * @param basicDataSource Configures creation.
  32.      * @param driver          The JDBC driver.
  33.      * @return a new {@link DriverConnectionFactory} allowing for a {@link BasicDataSource#getDriverClassName()}
  34.      *         override.
  35.      * @throws SQLException Thrown when instantiation fails.
  36.      */
  37.     static ConnectionFactory createConnectionFactory(final BasicDataSource basicDataSource, final Driver driver)
  38.             throws SQLException {
  39.         final Properties connectionProperties = basicDataSource.getConnectionProperties();
  40.         final String url = basicDataSource.getUrl();
  41.         // Set up the driver connection factory we will use
  42.         final String user = basicDataSource.getUserName();
  43.         if (user != null) {
  44.             connectionProperties.put(Constants.KEY_USER, user);
  45.         } else {
  46.             basicDataSource.log(String.format("DBCP DataSource configured without a '%s'", Constants.KEY_USER));
  47.         }

  48.         final String pwd = basicDataSource.getPassword();
  49.         if (pwd != null) {
  50.             connectionProperties.put(Constants.KEY_PASSWORD, pwd);
  51.         } else {
  52.             basicDataSource.log(String.format("DBCP DataSource configured without a '%s'", Constants.KEY_PASSWORD));
  53.         }
  54.         final String connectionFactoryClassName = basicDataSource.getConnectionFactoryClassName();
  55.         if (connectionFactoryClassName != null) {
  56.             try {
  57.                 final Class<?> connectionFactoryFromCCL = Class.forName(connectionFactoryClassName);
  58.                 return (ConnectionFactory) connectionFactoryFromCCL
  59.                         .getConstructor(Driver.class, String.class, Properties.class)
  60.                         .newInstance(driver, url, connectionProperties);
  61.             } catch (final Exception t) {
  62.                 final String message = "Cannot load ConnectionFactory implementation '" + connectionFactoryClassName
  63.                         + "'";
  64.                 basicDataSource.log(message, t);
  65.                 throw new SQLException(message, t);
  66.             }
  67.         }
  68.         // Defaults to DriverConnectionFactory
  69.         return new DriverConnectionFactory(driver, url, connectionProperties);
  70.     }

  71. }