View Javadoc
1   /**
2    *    Copyright 2006-2016 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    *       http://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.mybatis.generator.internal.db;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
19  import static org.mybatis.generator.internal.util.messages.Messages.getString;
20  
21  import java.sql.Connection;
22  import java.sql.Driver;
23  import java.sql.SQLException;
24  import java.util.Properties;
25  
26  import org.mybatis.generator.config.JDBCConnectionConfiguration;
27  import org.mybatis.generator.internal.ObjectFactory;
28  
29  /**
30   * This class assumes that classes are cached elsewhere for performance reasons,
31   * but also to make sure that any native libraries are only loaded one time
32   * (avoids the dreaded UnsatisfiedLinkError library loaded in another
33   * classloader)
34   * 
35   * @author Jeff Butler
36   */
37  public class ConnectionFactory {
38  
39      private static ConnectionFactory instance = new ConnectionFactory();
40  
41      public static ConnectionFactory getInstance() {
42          return instance;
43      }
44  
45      /**
46  	 *  
47  	 */
48      private ConnectionFactory() {
49          super();
50      }
51  
52      public Connection getConnection(JDBCConnectionConfiguration config)
53              throws SQLException {
54          Driver driver = getDriver(config);
55  
56          Properties props = new Properties();
57  
58          if (stringHasValue(config.getUserId())) {
59              props.setProperty("user", config.getUserId()); //$NON-NLS-1$
60          }
61  
62          if (stringHasValue(config.getPassword())) {
63              props.setProperty("password", config.getPassword()); //$NON-NLS-1$
64          }
65  
66          props.putAll(config.getProperties());
67  
68          Connection conn = driver.connect(config.getConnectionURL(), props);
69  
70          if (conn == null) {
71              throw new SQLException(getString("RuntimeError.7")); //$NON-NLS-1$
72          }
73  
74          return conn;
75      }
76  
77      private Driver getDriver(JDBCConnectionConfiguration connectionInformation) {
78          String driverClass = connectionInformation.getDriverClass();
79          Driver driver;
80  
81          try {
82              Class<?> clazz = ObjectFactory.externalClassForName(driverClass);
83              driver = (Driver) clazz.newInstance();
84          } catch (Exception e) {
85              throw new RuntimeException(getString("RuntimeError.8"), e); //$NON-NLS-1$
86          }
87  
88          return driver;
89      }
90  }