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 javax.sql.ConnectionPoolDataSource;
23  
24  import org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS;
25  
26  /**
27   * Provides the Apache commons-dbcp {@code DriverAdapterCPDS}.
28   */
29  public final class DriverAdapterCPDSProvider implements Provider<ConnectionPoolDataSource> {
30  
31    private final DriverAdapterCPDS adapter = new DriverAdapterCPDS();
32  
33    /**
34     * Instantiates a new driver adapter CPDS provider.
35     *
36     * @param driver
37     *          the driver
38     * @param url
39     *          the url
40     */
41    @Inject
42    public DriverAdapterCPDSProvider(@Named("JDBC.driver") final String driver, @Named("JDBC.url") final String url) {
43      try {
44        adapter.setDriver(driver);
45      } catch (ClassNotFoundException e) {
46        throw new RuntimeException("Driver '" + driver + "' not found in the classpath", e);
47      }
48      adapter.setUrl(url);
49    }
50  
51    /**
52     * Sets the user.
53     *
54     * @param username
55     *          the new user
56     *
57     * @since 3.3
58     */
59    @com.google.inject.Inject(optional = true)
60    public void setUser(@Named("JDBC.username") final String username) {
61      adapter.setUser(username);
62    }
63  
64    /**
65     * Sets the password.
66     *
67     * @param password
68     *          the new password
69     *
70     * @since 3.3
71     */
72    @com.google.inject.Inject(optional = true)
73    public void setPassword(@Named("JDBC.password") final String password) {
74      adapter.setPassword(password);
75    }
76  
77    /**
78     * Sets the description.
79     *
80     * @param description
81     *          the new description
82     */
83    @com.google.inject.Inject(optional = true)
84    public void setDescription(@Named("DBCP.description") String description) {
85      adapter.setDescription(description);
86    }
87  
88    @com.google.inject.Inject(optional = true)
89    public void setLoginTimeout(@Named("JDBC.loginTimeout") int seconds) {
90      adapter.setLoginTimeout(seconds);
91    }
92  
93    @com.google.inject.Inject(optional = true)
94    public void setMaxIdle(@Named("DBCP.maxIdle") int maxIdle) {
95      adapter.setMaxIdle(maxIdle);
96    }
97  
98    @com.google.inject.Inject(optional = true)
99    public void setMaxPreparedStatements(@Named("DBCP.maxOpenPreparedStatements") int maxPreparedStatements) {
100     adapter.setMaxPreparedStatements(maxPreparedStatements);
101   }
102 
103   @com.google.inject.Inject(optional = true)
104   public void setMinEvictableIdleTimeMillis(@Named("DBCP.minEvictableIdleTimeMillis") int minEvictableIdleTimeMillis) {
105     adapter.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
106   }
107 
108   @com.google.inject.Inject(optional = true)
109   public void setNumTestsPerEvictionRun(@Named("DBCP.numTestsPerEvictionRun") int numTestsPerEvictionRun) {
110     adapter.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
111   }
112 
113   @com.google.inject.Inject(optional = true)
114   public void setPoolPreparedStatements(@Named("DBCP.poolPreparedStatements") boolean poolPreparedStatements) {
115     adapter.setPoolPreparedStatements(poolPreparedStatements);
116   }
117 
118   @com.google.inject.Inject(optional = true)
119   public void setTimeBetweenEvictionRunsMillis(
120       @Named("DBCP.timeBetweenEvictionRunsMillis") int timeBetweenEvictionRunsMillis) {
121     adapter.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
122   }
123 
124   @Override
125   public ConnectionPoolDataSource get() {
126     return adapter;
127   }
128 
129 }