View Javadoc
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  
19  import java.sql.Driver;
20  import java.sql.SQLException;
21  import java.util.Properties;
22  
23  /*
24   * Creates {@link ConnectionFactory} instances.
25   *
26   * @since 2.7.0
27   */
28  final class ConnectionFactoryFactory {
29  
30      /**
31       * Creates a new {@link DriverConnectionFactory} allowing for an override through
32       * {@link BasicDataSource#getDriverClassName()}.
33       *
34       * @param basicDataSource Configures creation.
35       * @param driver          The JDBC driver.
36       * @return a new {@link DriverConnectionFactory} allowing for a {@link BasicDataSource#getDriverClassName()}
37       *         override.
38       * @throws SQLException Thrown when instantiation fails.
39       */
40      static ConnectionFactory createConnectionFactory(final BasicDataSource basicDataSource, final Driver driver)
41              throws SQLException {
42          final Properties connectionProperties = basicDataSource.getConnectionProperties();
43          final String url = basicDataSource.getUrl();
44          // Set up the driver connection factory we will use
45          final String user = basicDataSource.getUserName();
46          if (user != null) {
47              connectionProperties.put(Constants.KEY_USER, user);
48          } else {
49              basicDataSource.log(String.format("DBCP DataSource configured without a '%s'", Constants.KEY_USER));
50          }
51  
52          final String pwd = basicDataSource.getPassword();
53          if (pwd != null) {
54              connectionProperties.put(Constants.KEY_PASSWORD, pwd);
55          } else {
56              basicDataSource.log(String.format("DBCP DataSource configured without a '%s'", Constants.KEY_PASSWORD));
57          }
58          final String connectionFactoryClassName = basicDataSource.getConnectionFactoryClassName();
59          if (connectionFactoryClassName != null) {
60              try {
61                  final Class<?> connectionFactoryFromCCL = Class.forName(connectionFactoryClassName);
62                  return (ConnectionFactory) connectionFactoryFromCCL
63                          .getConstructor(Driver.class, String.class, Properties.class)
64                          .newInstance(driver, url, connectionProperties);
65              } catch (final Exception t) {
66                  final String message = "Cannot load ConnectionFactory implementation '" + connectionFactoryClassName
67                          + "'";
68                  basicDataSource.log(message, t);
69                  throw new SQLException(message, t);
70              }
71          }
72          // Defaults to DriverConnectionFactory
73          return new DriverConnectionFactory(driver, url, connectionProperties);
74      }
75  
76  }