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 com.google.inject.AbstractModule;
19  import com.google.inject.Scopes;
20  import com.google.inject.TypeLiteral;
21  
22  import jakarta.inject.Provider;
23  
24  import java.lang.annotation.Annotation;
25  import java.util.Map;
26  
27  public final class PerUserPoolDataSourceModule extends AbstractModule {
28  
29    private static final TypeLiteral<Map<String, Boolean>> STRING_BOOLEAN_MAP = new TypeLiteral<Map<String, Boolean>>() {
30    };
31  
32    private static final TypeLiteral<Map<String, Integer>> STRING_INTEGER_MAP = new TypeLiteral<Map<String, Integer>>() {
33    };
34  
35    private static final TypeLiteral<Map<String, Long>> STRING_LONG_MAP = new TypeLiteral<Map<String, Long>>() {
36    };
37  
38    private final Class<? extends Provider<Map<String, Boolean>>> perUserDefaultAutoCommitProviderClass;
39  
40    private final Class<? extends Provider<Map<String, Boolean>>> perUserDefaultReadOnlyProviderClass;
41  
42    private final Class<? extends Provider<Map<String, Integer>>> perUserDefaultTransactionIsolationProviderClass;
43  
44    private final Class<? extends Provider<Map<String, Integer>>> perUserMaxActiveProviderClass;
45  
46    private final Class<? extends Provider<Map<String, Integer>>> perUserMaxIdleProviderClass;
47  
48    private final Class<? extends Provider<Map<String, Long>>> perUserMaxWaitMillisProviderClass;
49  
50    private PerUserPoolDataSourceModule(
51        Class<? extends Provider<Map<String, Boolean>>> perUserDefaultAutoCommitProviderClass,
52        Class<? extends Provider<Map<String, Boolean>>> perUserDefaultReadOnlyProviderClass,
53        Class<? extends Provider<Map<String, Integer>>> perUserDefaultTransactionIsolationProviderClass,
54        Class<? extends Provider<Map<String, Integer>>> perUserMaxActiveProviderClass,
55        Class<? extends Provider<Map<String, Integer>>> perUserMaxIdleProviderClass,
56        Class<? extends Provider<Map<String, Long>>> perUserMaxWaitMillisProviderClass) {
57      this.perUserDefaultAutoCommitProviderClass = perUserDefaultAutoCommitProviderClass;
58      this.perUserDefaultReadOnlyProviderClass = perUserDefaultReadOnlyProviderClass;
59      this.perUserDefaultTransactionIsolationProviderClass = perUserDefaultTransactionIsolationProviderClass;
60      this.perUserMaxActiveProviderClass = perUserMaxActiveProviderClass;
61      this.perUserMaxIdleProviderClass = perUserMaxIdleProviderClass;
62      this.perUserMaxWaitMillisProviderClass = perUserMaxWaitMillisProviderClass;
63    }
64  
65    @Override
66    protected void configure() {
67      doBind(perUserDefaultAutoCommitProviderClass, STRING_BOOLEAN_MAP, PerUserDefaultAutoCommit.class);
68      doBind(perUserDefaultReadOnlyProviderClass, STRING_BOOLEAN_MAP, PerUserDefaultReadOnly.class);
69      doBind(perUserDefaultTransactionIsolationProviderClass, STRING_INTEGER_MAP,
70          PerUserDefaultTransactionIsolation.class);
71      doBind(perUserMaxActiveProviderClass, STRING_INTEGER_MAP, PerUserMaxTotal.class);
72      doBind(perUserMaxIdleProviderClass, STRING_INTEGER_MAP, PerUserMaxIdle.class);
73      doBind(perUserMaxWaitMillisProviderClass, STRING_LONG_MAP, PerUserMaxWaitMillis.class);
74    }
75  
76    private <T> void doBind(Class<? extends Provider<T>> providerClass, TypeLiteral<T> typeLiteral,
77        Class<? extends Annotation> annotation) {
78      if (providerClass != null) {
79        bind(typeLiteral).annotatedWith(annotation).toProvider(providerClass).in(Scopes.SINGLETON);
80      }
81    }
82  
83    public static final class Builder {
84  
85      private Class<? extends Provider<Map<String, Boolean>>> perUserDefaultAutoCommitProviderClass;
86  
87      private Class<? extends Provider<Map<String, Boolean>>> perUserDefaultReadOnlyProviderClass;
88  
89      private Class<? extends Provider<Map<String, Integer>>> perUserDefaultTransactionIsolationProviderClass;
90  
91      private Class<? extends Provider<Map<String, Integer>>> perUserMaxActiveProviderClass;
92  
93      private Class<? extends Provider<Map<String, Integer>>> perUserMaxIdleProviderClass;
94  
95      private Class<? extends Provider<Map<String, Long>>> perUserMaxWaitProviderClass;
96  
97      /**
98       * Sets the per user default auto commit provider class.
99       *
100      * @param perUserDefaultAutoCommitProviderClass
101      *          the perUserDefaultAutoCommitProviderClass to set
102      *
103      * @return the builder
104      */
105     public Builder setPerUserDefaultAutoCommitProviderClass(
106         Class<? extends Provider<Map<String, Boolean>>> perUserDefaultAutoCommitProviderClass) {
107       this.perUserDefaultAutoCommitProviderClass = perUserDefaultAutoCommitProviderClass;
108       return this;
109     }
110 
111     /**
112      * Sets the per user default read only provider class.
113      *
114      * @param perUserDefaultReadOnlyProviderClass
115      *          the perUserDefaultReadOnlyProviderClass to set
116      *
117      * @return the builder
118      */
119     public Builder setPerUserDefaultReadOnlyProviderClass(
120         Class<? extends Provider<Map<String, Boolean>>> perUserDefaultReadOnlyProviderClass) {
121       this.perUserDefaultReadOnlyProviderClass = perUserDefaultReadOnlyProviderClass;
122       return this;
123     }
124 
125     /**
126      * Sets the per user default transaction isolation provider class.
127      *
128      * @param perUserDefaultTransactionIsolationProviderClass
129      *          the perUserDefaultTransactionIsolationProviderClass to set
130      *
131      * @return the builder
132      */
133     public Builder setPerUserDefaultTransactionIsolationProviderClass(
134         Class<? extends Provider<Map<String, Integer>>> perUserDefaultTransactionIsolationProviderClass) {
135       this.perUserDefaultTransactionIsolationProviderClass = perUserDefaultTransactionIsolationProviderClass;
136       return this;
137     }
138 
139     /**
140      * Sets the per user max active provider class.
141      *
142      * @param perUserMaxActiveProviderClass
143      *          the perUserMaxActiveProviderClass to set
144      *
145      * @return the builder
146      */
147     public Builder setPerUserMaxTotalProviderClass(
148         Class<? extends Provider<Map<String, Integer>>> perUserMaxActiveProviderClass) {
149       this.perUserMaxActiveProviderClass = perUserMaxActiveProviderClass;
150       return this;
151     }
152 
153     /**
154      * Sets the per user max idle provider class.
155      *
156      * @param perUserMaxIdleProviderClass
157      *          the perUserMaxIdleProviderClass to set
158      *
159      * @return the builder
160      */
161     public Builder setPerUserMaxIdleProviderClass(
162         Class<? extends Provider<Map<String, Integer>>> perUserMaxIdleProviderClass) {
163       this.perUserMaxIdleProviderClass = perUserMaxIdleProviderClass;
164       return this;
165     }
166 
167     /**
168      * Sets the per user max wait provider class.
169      *
170      * @param perUserMaxWaitProviderClass
171      *          the perUserMaxWaitProviderClass to set
172      *
173      * @return the builder
174      */
175     public Builder setPerUserMaxWaitMillisProviderClass(
176         Class<? extends Provider<Map<String, Long>>> perUserMaxWaitProviderClass) {
177       this.perUserMaxWaitProviderClass = perUserMaxWaitProviderClass;
178       return this;
179     }
180 
181     /**
182      * Creates the pool.
183      *
184      * @return the per user pool data source module
185      */
186     public PerUserPoolDataSourceModule create() {
187       return new PerUserPoolDataSourceModule(perUserDefaultAutoCommitProviderClass, perUserDefaultReadOnlyProviderClass,
188           perUserDefaultTransactionIsolationProviderClass, perUserMaxActiveProviderClass, perUserMaxIdleProviderClass,
189           perUserMaxWaitProviderClass);
190     }
191 
192   }
193 
194 }