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.Named;
19  import jakarta.inject.Provider;
20  
21  import java.util.Map;
22  import java.util.Map.Entry;
23  
24  import javax.sql.ConnectionPoolDataSource;
25  import javax.sql.DataSource;
26  
27  import org.apache.commons.dbcp2.datasources.PerUserPoolDataSource;
28  
29  /**
30   * Provides the Apache commons-dbcp {@code PerUserPoolDataSource}.
31   */
32  public final class PerUserPoolDataSourceProvider implements Provider<DataSource> {
33  
34    private final PerUserPoolDataSource dataSource = new PerUserPoolDataSource();
35  
36    @com.google.inject.Inject(optional = true)
37    public void setConnectionPoolDataSource(ConnectionPoolDataSource cpds) {
38      dataSource.setConnectionPoolDataSource(cpds);
39    }
40  
41    @com.google.inject.Inject(optional = true)
42    public void setDataSourceName(@Named("DBCP.name") String name) {
43      dataSource.setDataSourceName(name);
44    }
45  
46    @com.google.inject.Inject(optional = true)
47    public void setDefaultAutoCommit(@Named("JDBC.autoCommit") boolean autoCommit) {
48      dataSource.setDefaultAutoCommit(autoCommit);
49    }
50  
51    @com.google.inject.Inject(optional = true)
52    public void setDefaultReadOnly(@Named("DBCP.defaultReadOnly") boolean defaultReadOnly) {
53      dataSource.setDefaultReadOnly(defaultReadOnly);
54    }
55  
56    @com.google.inject.Inject(optional = true)
57    public void setDefaultTransactionIsolation(
58        @Named("DBCP.defaultTransactionIsolation") int defaultTransactionIsolation) {
59      dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
60    }
61  
62    @com.google.inject.Inject(optional = true)
63    public void setDescription(@Named("DBCP.description") String description) {
64      dataSource.setDescription(description);
65    }
66  
67    @com.google.inject.Inject(optional = true)
68    public void setJndiEnvironment(@Named("DBCP.jndi.key") String key, @Named("DBCP.jndi.value") String value) {
69      dataSource.setJndiEnvironment(key, value);
70    }
71  
72    @com.google.inject.Inject(optional = true)
73    public void setLoginTimeout(@Named("JDBC.loginTimeout") int loginTimeout) {
74      dataSource.setLoginTimeout(loginTimeout);
75    }
76  
77    @com.google.inject.Inject(optional = true)
78    public void setDefaultMinEvictableIdleTimeMillis(
79        @Named("DBCP.defaultMinEvictableIdleTimeMillis") int defaultMinEvictableIdleTimeMillis) {
80      dataSource.setDefaultMinEvictableIdleTimeMillis(defaultMinEvictableIdleTimeMillis);
81    }
82  
83    @com.google.inject.Inject(optional = true)
84    public void setDefaultNumTestsPerEvictionRun(
85        @Named("DBCP.defaultNumTestsPerEvictionRun") int defaultNumTestsPerEvictionRun) {
86      dataSource.setDefaultNumTestsPerEvictionRun(defaultNumTestsPerEvictionRun);
87    }
88  
89    @com.google.inject.Inject(optional = true)
90    public void setRollbackAfterValidation(@Named("DBCP.rollbackAfterValidation") boolean rollbackAfterValidation) {
91      dataSource.setRollbackAfterValidation(rollbackAfterValidation);
92    }
93  
94    @com.google.inject.Inject(optional = true)
95    public void setDefaultTestOnBorrow(@Named("DBCP.defaultTestOnBorrow") boolean defaultTestOnBorrow) {
96      dataSource.setDefaultTestOnBorrow(defaultTestOnBorrow);
97    }
98  
99    @com.google.inject.Inject(optional = true)
100   public void setDefaultTestOnReturn(@Named("DBCP.defaultTestOnReturn") boolean defaultTestOnReturn) {
101     dataSource.setDefaultTestOnReturn(defaultTestOnReturn);
102   }
103 
104   @com.google.inject.Inject(optional = true)
105   public void setDefaultTestWhileIdle(@Named("DBCP.defaultTestWhileIdle") boolean defaultTestWhileIdle) {
106     dataSource.setDefaultTestWhileIdle(defaultTestWhileIdle);
107   }
108 
109   @com.google.inject.Inject(optional = true)
110   public void setDefaultTimeBetweenEvictionRunsMillis(
111       @Named("DBCP.defaultTimeBetweenEvictionRunsMillis") int defaultTimeBetweenEvictionRunsMillis) {
112     dataSource.setDefaultTimeBetweenEvictionRunsMillis(defaultTimeBetweenEvictionRunsMillis);
113   }
114 
115   @com.google.inject.Inject(optional = true)
116   public void setValidationQuery(@Named("DBCP.validationQuery") String validationQuery) {
117     dataSource.setValidationQuery(validationQuery);
118   }
119 
120   @com.google.inject.Inject(optional = true)
121   public void setDefaultMaxTotal(@Named("DBCP.defaultMaxTotal") int defaultMaxTotal) {
122     dataSource.setDefaultMaxTotal(defaultMaxTotal);
123   }
124 
125   @com.google.inject.Inject(optional = true)
126   public void setDefaultMaxIdle(@Named("DBCP.defaultMaxIdle") int defaultMaxIdle) {
127     dataSource.setDefaultMaxIdle(defaultMaxIdle);
128   }
129 
130   @com.google.inject.Inject(optional = true)
131   public void setDefaultMaxWaitMillis(@Named("DBCP.defaultMaxWaitMillis") int defaultMaxWaitMillis) {
132     dataSource.setDefaultMaxWaitMillis(defaultMaxWaitMillis);
133   }
134 
135   /**
136    * Sets the per user default auto commit.
137    *
138    * @param perUserDefaultAutoCommit
139    *          the per user default auto commit
140    */
141   @com.google.inject.Inject(optional = true)
142   public void setPerUserDefaultAutoCommit(@PerUserDefaultAutoCommit Map<String, Boolean> perUserDefaultAutoCommit) {
143     for (Entry<String, Boolean> entry : perUserDefaultAutoCommit.entrySet()) {
144       dataSource.setPerUserDefaultAutoCommit(entry.getKey(), entry.getValue());
145     }
146   }
147 
148   /**
149    * Sets the per user default read only.
150    *
151    * @param perUserDefaultReadOnly
152    *          the per user default read only
153    */
154   @com.google.inject.Inject(optional = true)
155   public void setPerUserDefaultReadOnly(@PerUserDefaultReadOnly Map<String, Boolean> perUserDefaultReadOnly) {
156     for (Entry<String, Boolean> entry : perUserDefaultReadOnly.entrySet()) {
157       dataSource.setPerUserDefaultReadOnly(entry.getKey(), entry.getValue());
158     }
159   }
160 
161   /**
162    * Sets the per user default transaction isolation.
163    *
164    * @param perUserDefaultTransactionIsolation
165    *          the per user default transaction isolation
166    */
167   @com.google.inject.Inject(optional = true)
168   public void setPerUserDefaultTransactionIsolation(
169       @PerUserDefaultTransactionIsolation Map<String, Integer> perUserDefaultTransactionIsolation) {
170     for (Entry<String, Integer> entry : perUserDefaultTransactionIsolation.entrySet()) {
171       dataSource.setPerUserDefaultTransactionIsolation(entry.getKey(), entry.getValue());
172     }
173   }
174 
175   /**
176    * Sets the per user max total.
177    *
178    * @param perUserMaxTotal
179    *          the per user max total
180    */
181   @com.google.inject.Inject(optional = true)
182   public void setPerUserMaxTotal(@PerUserMaxTotal Map<String, Integer> perUserMaxTotal) {
183     for (Entry<String, Integer> entry : perUserMaxTotal.entrySet()) {
184       dataSource.setPerUserMaxTotal(entry.getKey(), entry.getValue());
185     }
186   }
187 
188   /**
189    * Sets the per user max idle.
190    *
191    * @param perUserMaxIdle
192    *          the per user max idle
193    */
194   @com.google.inject.Inject(optional = true)
195   public void setPerUserMaxIdle(@PerUserMaxIdle Map<String, Integer> perUserMaxIdle) {
196     for (Entry<String, Integer> entry : perUserMaxIdle.entrySet()) {
197       dataSource.setPerUserMaxIdle(entry.getKey(), entry.getValue());
198     }
199   }
200 
201   /**
202    * Sets the per user max wait in milliseconds.
203    *
204    * @param perUserMaxWaitMillis
205    *          the per user max wait in milliseconds
206    */
207   @com.google.inject.Inject(optional = true)
208   public void setPerUserMaxWaitMillis(@PerUserMaxWaitMillis Map<String, Long> perUserMaxWaitMillis) {
209     for (Entry<String, Long> entry : perUserMaxWaitMillis.entrySet()) {
210       dataSource.setPerUserMaxWaitMillis(entry.getKey(), entry.getValue());
211     }
212   }
213 
214   @Override
215   public DataSource get() {
216     return dataSource;
217   }
218 
219 }