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.dbcp;
17  
18  import jakarta.inject.Inject;
19  import jakarta.inject.Named;
20  import jakarta.inject.Provider;
21  
22  import java.util.Map.Entry;
23  import java.util.Properties;
24  
25  import javax.sql.DataSource;
26  
27  import org.apache.commons.dbcp2.BasicDataSource;
28  
29  /**
30   * Provides the Apache commons-dbcp {@code BasicDataSource}.
31   */
32  public final class BasicDataSourceProvider implements Provider<DataSource> {
33  
34    /**
35     * The BasicDataSource reference.
36     */
37    private final BasicDataSource dataSource = new BasicDataSource();
38  
39    /**
40     * Creates a new BasicDataSource using the needed parameter.
41     *
42     * @param driver
43     *          The JDBC driver class.
44     * @param url
45     *          the database URL of the form <code>jdbc:subprotocol:subname</code>.
46     * @param driverClassLoader
47     *          ClassLoader to use to load JDBC driver class.
48     */
49    @Inject
50    public BasicDataSourceProvider(@Named("JDBC.driver") final String driver, @Named("JDBC.url") final String url,
51        @Named("JDBC.driverClassLoader") final ClassLoader driverClassLoader) {
52      dataSource.setDriverClassLoader(driverClassLoader);
53      dataSource.setDriverClassName(driver);
54      dataSource.setUrl(url);
55    }
56  
57    /**
58     * Sets the user.
59     *
60     * @param username
61     *          the new user
62     *
63     * @since 3.3
64     */
65    @com.google.inject.Inject(optional = true)
66    public void setUser(@Named("JDBC.username") final String username) {
67      dataSource.setUsername(username);
68    }
69  
70    /**
71     * Sets the password.
72     *
73     * @param password
74     *          the new password
75     *
76     * @since 3.3
77     */
78    @com.google.inject.Inject(optional = true)
79    public void setPassword(@Named("JDBC.password") final String password) {
80      dataSource.setPassword(password);
81    }
82  
83    /**
84     * Sets the auto commit.
85     *
86     * @param autoCommit
87     *          the new auto commit
88     */
89    @com.google.inject.Inject(optional = true)
90    public void setAutoCommit(@Named("JDBC.autoCommit") final boolean autoCommit) {
91      dataSource.setDefaultAutoCommit(autoCommit);
92    }
93  
94    /**
95     * Sets the driver properties.
96     *
97     * @param driverProperties
98     *          the new driver properties
99     */
100   @com.google.inject.Inject(optional = true)
101   public void setDriverProperties(@Named("JDBC.driverProperties") final Properties driverProperties) {
102     for (Entry<Object, Object> property : driverProperties.entrySet()) {
103       String name = property.getKey().toString();
104       String value = property.getValue().toString();
105       dataSource.addConnectionProperty(name, value);
106     }
107   }
108 
109   /**
110    * Sets the access to underlying connection allowed.
111    *
112    * @param allow
113    *          the new access to underlying connection allowed
114    */
115   @com.google.inject.Inject(optional = true)
116   public void setAccessToUnderlyingConnectionAllowed(
117       @Named("DBCP.accessToUnderlyingConnectionAllowed") final boolean allow) {
118     dataSource.setAccessToUnderlyingConnectionAllowed(allow);
119   }
120 
121   /**
122    * Sets the default catalog.
123    *
124    * @param defaultCatalog
125    *          the new default catalog
126    */
127   @com.google.inject.Inject(optional = true)
128   public void setDefaultCatalog(@Named("DBCP.defaultCatalog") final String defaultCatalog) {
129     dataSource.setDefaultCatalog(defaultCatalog);
130   }
131 
132   /**
133    * Sets the default read only.
134    *
135    * @param defaultReadOnly
136    *          the new default read only
137    */
138   @com.google.inject.Inject(optional = true)
139   public void setDefaultReadOnly(@Named("DBCP.defaultReadOnly") final boolean defaultReadOnly) {
140     dataSource.setDefaultReadOnly(defaultReadOnly);
141   }
142 
143   /**
144    * Sets the default transaction isolation.
145    *
146    * @param defaultTransactionIsolation
147    *          the new default transaction isolation
148    */
149   @com.google.inject.Inject(optional = true)
150   public void setDefaultTransactionIsolation(
151       @Named("DBCP.defaultTransactionIsolation") final int defaultTransactionIsolation) {
152     dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
153   }
154 
155   /**
156    * Sets the initial size.
157    *
158    * @param initialSize
159    *          the new initial size
160    */
161   @com.google.inject.Inject(optional = true)
162   public void setInitialSize(@Named("DBCP.initialSize") final int initialSize) {
163     dataSource.setInitialSize(initialSize);
164   }
165 
166   /**
167    * Sets the max total.
168    *
169    * @param maxTotal
170    *          the new max total
171    */
172   @com.google.inject.Inject(optional = true)
173   public void setMaxTotal(@Named("DBCP.maxTotal") final int maxTotal) {
174     dataSource.setMaxTotal(maxTotal);
175   }
176 
177   /**
178    * Sets the max idle.
179    *
180    * @param maxIdle
181    *          the new max idle
182    */
183   @com.google.inject.Inject(optional = true)
184   public void setMaxIdle(@Named("DBCP.maxIdle") final int maxIdle) {
185     dataSource.setMaxIdle(maxIdle);
186   }
187 
188   /**
189    * Sets the max open prepared statements.
190    *
191    * @param maxOpenPreparedStatements
192    *          the new max open prepared statements
193    */
194   @com.google.inject.Inject(optional = true)
195   public void setMaxOpenPreparedStatements(
196       @Named("DBCP.maxOpenPreparedStatements") final int maxOpenPreparedStatements) {
197     dataSource.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
198   }
199 
200   /**
201    * Sets the max wait in milliseconds.
202    *
203    * @param maxWaitMillis
204    *          the new max wait in milliseconds
205    */
206   @com.google.inject.Inject(optional = true)
207   public void setMaxWaitMillis(@Named("DBCP.maxWaitMillis") final long maxWaitMillis) {
208     dataSource.setMaxWaitMillis(maxWaitMillis);
209   }
210 
211   /**
212    * Sets the min evictable idle time millis.
213    *
214    * @param minEvictableIdleTimeMillis
215    *          the new min evictable idle time millis
216    */
217   @com.google.inject.Inject(optional = true)
218   public void setMinEvictableIdleTimeMillis(
219       @Named("DBCP.minEvictableIdleTimeMillis") final long minEvictableIdleTimeMillis) {
220     dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
221   }
222 
223   /**
224    * Sets the min idle.
225    *
226    * @param minIdle
227    *          the new min idle
228    */
229   @com.google.inject.Inject(optional = true)
230   public void setMinIdle(@Named("DBCP.minIdle") final int minIdle) {
231     dataSource.setMinIdle(minIdle);
232   }
233 
234   /**
235    * Sets the num tests per eviction run.
236    *
237    * @param numTestsPerEvictionRun
238    *          the new num tests per eviction run
239    */
240   @com.google.inject.Inject(optional = true)
241   public void setNumTestsPerEvictionRun(@Named("DBCP.numTestsPerEvictionRun") final int numTestsPerEvictionRun) {
242     dataSource.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
243   }
244 
245   /**
246    * Sets the pool prepared statements.
247    *
248    * @param poolPreparedStatements
249    *          the new pool prepared statements
250    */
251   @com.google.inject.Inject(optional = true)
252   public void setPoolPreparedStatements(@Named("DBCP.poolPreparedStatements") final boolean poolPreparedStatements) {
253     dataSource.setPoolPreparedStatements(poolPreparedStatements);
254   }
255 
256   /**
257    * Sets the test on borrow.
258    *
259    * @param testOnBorrow
260    *          the new test on borrow
261    */
262   @com.google.inject.Inject(optional = true)
263   public void setTestOnBorrow(@Named("DBCP.testOnBorrow") final boolean testOnBorrow) {
264     dataSource.setTestOnBorrow(testOnBorrow);
265   }
266 
267   /**
268    * Sets the test on return.
269    *
270    * @param testOnReturn
271    *          the new test on return
272    */
273   @com.google.inject.Inject(optional = true)
274   public void setTestOnReturn(@Named("DBCP.testOnReturn") final boolean testOnReturn) {
275     dataSource.setTestOnReturn(testOnReturn);
276   }
277 
278   /**
279    * Sets the test while idle.
280    *
281    * @param testWhileIdle
282    *          the new test while idle
283    */
284   @com.google.inject.Inject(optional = true)
285   public void setTestWhileIdle(@Named("DBCP.testWhileIdle") final boolean testWhileIdle) {
286     dataSource.setTestWhileIdle(testWhileIdle);
287   }
288 
289   /**
290    * Sets the time between eviction runs millis.
291    *
292    * @param timeBetweenEvictionRunsMillis
293    *          the new time between eviction runs millis
294    */
295   @com.google.inject.Inject(optional = true)
296   public void setTimeBetweenEvictionRunsMillis(
297       @Named("DBCP.timeBetweenEvictionRunsMillis") int timeBetweenEvictionRunsMillis) {
298     dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
299   }
300 
301   /**
302    * Sets the validation query.
303    *
304    * @param validationQuery
305    *          the new validation query
306    */
307   @com.google.inject.Inject(optional = true)
308   public void setValidationQuery(@Named("DBCP.validationQuery") final String validationQuery) {
309     dataSource.setValidationQuery(validationQuery);
310   }
311 
312   @Override
313   public DataSource get() {
314     return dataSource;
315   }
316 
317 }