View Javadoc
1   /*
2    *    Copyright 2009-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.guice.datasource.builtin;
17  
18  import jakarta.inject.Inject;
19  import jakarta.inject.Named;
20  import jakarta.inject.Provider;
21  
22  import java.util.Properties;
23  
24  import javax.sql.DataSource;
25  
26  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
27  
28  /**
29   * Provides the myBatis built-in UnpooledDataSource.
30   */
31  public final class UnpooledDataSourceProvider implements Provider<DataSource> {
32  
33    /**
34     * The UnpooledDataSource reference.
35     */
36    private final UnpooledDataSource unpooledDataSource;
37  
38    /**
39     * Creates a new UnpooledDataSource using the needed parameter.
40     *
41     * @param driver
42     *          The JDBC driver class.
43     * @param url
44     *          the database URL of the form <code>jdbc:subprotocol:subname</code>.
45     * @param driverClassLoader
46     *          ClassLoader to use to load JDBC driver class.
47     */
48    @Inject
49    public UnpooledDataSourceProvider(@Named("JDBC.driver") final String driver, @Named("JDBC.url") final String url,
50        @Named("JDBC.driverClassLoader") final ClassLoader driverClassLoader) {
51      unpooledDataSource = new UnpooledDataSource(driverClassLoader, driver, url, null, null);
52    }
53  
54    /**
55     * Sets the user.
56     *
57     * @param username
58     *          the new user
59     *
60     * @since 3.3
61     */
62    @com.google.inject.Inject(optional = true)
63    public void setUser(@Named("JDBC.username") final String username) {
64      unpooledDataSource.setUsername(username);
65    }
66  
67    /**
68     * Sets the password.
69     *
70     * @param password
71     *          the new password
72     *
73     * @since 3.3
74     */
75    @com.google.inject.Inject(optional = true)
76    public void setPassword(@Named("JDBC.password") final String password) {
77      unpooledDataSource.setPassword(password);
78    }
79  
80    /**
81     * Sets the auto commit.
82     *
83     * @param autoCommit
84     *          the new auto commit
85     */
86    @com.google.inject.Inject(optional = true)
87    public void setAutoCommit(@Named("JDBC.autoCommit") final boolean autoCommit) {
88      unpooledDataSource.setAutoCommit(autoCommit);
89    }
90  
91    /**
92     * Sets the login timeout.
93     *
94     * @param loginTimeout
95     *          the new login timeout
96     */
97    @com.google.inject.Inject(optional = true)
98    public void setLoginTimeout(@Named("JDBC.loginTimeout") final int loginTimeout) {
99      unpooledDataSource.setLoginTimeout(loginTimeout);
100   }
101 
102   @com.google.inject.Inject(optional = true)
103   public void setDriverProperties(@Named("JDBC.driverProperties") final Properties driverProperties) {
104     unpooledDataSource.setDriverProperties(driverProperties);
105   }
106 
107   @Override
108   public DataSource get() {
109     return unpooledDataSource;
110   }
111 
112 }