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.DriverManager;
21  import java.sql.SQLException;
22  
23  /*
24   * Creates {@link Driver} instances.
25   *
26   * @since 2.7.0
27   */
28  final class DriverFactory {
29  
30      static Driver createDriver(final BasicDataSource basicDataSource) throws SQLException {
31          // Load the JDBC driver class
32          Driver driverToUse = basicDataSource.getDriver();
33          final String driverClassName = basicDataSource.getDriverClassName();
34          final ClassLoader driverClassLoader = basicDataSource.getDriverClassLoader();
35          final String url = basicDataSource.getUrl();
36  
37          if (driverToUse == null) {
38              Class<?> driverFromCCL = null;
39              if (driverClassName != null) {
40                  try {
41                      try {
42                          if (driverClassLoader == null) {
43                              driverFromCCL = Class.forName(driverClassName);
44                          } else {
45                              driverFromCCL = Class.forName(driverClassName, true, driverClassLoader);
46                          }
47                      } catch (final ClassNotFoundException cnfe) {
48                          driverFromCCL = Thread.currentThread().getContextClassLoader().loadClass(driverClassName);
49                      }
50                  } catch (final Exception t) {
51                      final String message = "Cannot load JDBC driver class '" + driverClassName + "'";
52                      basicDataSource.log(message, t);
53                      throw new SQLException(message, t);
54                  }
55              }
56  
57              try {
58                  if (driverFromCCL == null) {
59                      driverToUse = DriverManager.getDriver(url);
60                  } else {
61                      // Usage of DriverManager is not possible, as it does not
62                      // respect the ContextClassLoader
63                      // N.B. This cast may cause ClassCastException which is
64                      // handled below
65                      driverToUse = (Driver) driverFromCCL.getConstructor().newInstance();
66                      if (!driverToUse.acceptsURL(url)) {
67                          throw new SQLException("No suitable driver", "08001");
68                      }
69                  }
70              } catch (final Exception t) {
71                  final String message = "Cannot create JDBC driver of class '"
72                          + (driverClassName != null ? driverClassName : "") + "' for connect URL '" + url + "'";
73                  basicDataSource.log(message, t);
74                  throw new SQLException(message, t);
75              }
76          }
77          return driverToUse;
78      }
79  
80  }