View Javadoc
1   /*
2    *    Copyright 2006-2023 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.mybatis.generator.internal;
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.api.ConnectionFactory;
27  import org.mybatis.generator.config.JDBCConnectionConfiguration;
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   * This avoids the dreaded UnsatisfiedLinkError library loaded in another
33   * classloader.
34   *
35   * @author Jeff Butler
36   */
37  public class JDBCConnectionFactory implements ConnectionFactory {
38  
39      private String userId;
40      private String password;
41      private String connectionURL;
42      private String driverClass;
43      private Properties otherProperties;
44  
45      /**
46       * This constructor is called when there is a JDBCConnectionConfiguration
47       * specified in the configuration.
48       *
49       * @param config
50       *            the configuration
51       */
52      public JDBCConnectionFactory(JDBCConnectionConfiguration config) {
53          super();
54          userId = config.getUserId();
55          password = config.getPassword();
56          connectionURL = config.getConnectionURL();
57          driverClass = config.getDriverClass();
58          otherProperties = config.getProperties();
59      }
60  
61      /**
62       * This constructor is called when this connection factory is specified
63       * as the type in a ConnectionFactory configuration element.
64       */
65      public JDBCConnectionFactory() {
66          super();
67      }
68  
69      @Override
70      public Connection getConnection() throws SQLException {
71  
72          Properties props = new Properties();
73  
74          if (stringHasValue(userId)) {
75              props.setProperty("user", userId); //$NON-NLS-1$
76          }
77  
78          if (stringHasValue(password)) {
79              props.setProperty("password", password); //$NON-NLS-1$
80          }
81  
82          props.putAll(otherProperties);
83  
84          Driver driver = getDriver();
85          Connection conn = driver.connect(connectionURL, props);
86  
87          if (conn == null) {
88              throw new SQLException(getString("RuntimeError.7")); //$NON-NLS-1$
89          }
90  
91          return conn;
92      }
93  
94      private Driver getDriver() {
95          Driver driver;
96  
97          try {
98              Class<?> clazz = ObjectFactory.externalClassForName(driverClass);
99              driver = (Driver) clazz.getConstructor().newInstance();
100         } catch (Exception e) {
101             throw new RuntimeException(getString("RuntimeError.8"), e); //$NON-NLS-1$
102         }
103 
104         return driver;
105     }
106 
107     @Override
108     public void addConfigurationProperties(Properties properties) {
109         // this should only be called when this connection factory is
110         // specified in a ConnectionFactory configuration
111         userId = properties.getProperty("userId"); //$NON-NLS-1$
112         password = properties.getProperty("password"); //$NON-NLS-1$
113         connectionURL = properties.getProperty("connectionURL"); //$NON-NLS-1$
114         driverClass = properties.getProperty("driverClass"); //$NON-NLS-1$
115 
116         otherProperties = new Properties();
117         otherProperties.putAll(properties);
118 
119         // remove all the properties that we have specific attributes for
120         otherProperties.remove("userId"); //$NON-NLS-1$
121         otherProperties.remove("password"); //$NON-NLS-1$
122         otherProperties.remove("connectionURL"); //$NON-NLS-1$
123         otherProperties.remove("driverClass"); //$NON-NLS-1$
124     }
125 }