1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
99
100
101
102
103
104
105 public Builder setPerUserDefaultAutoCommitProviderClass(
106 Class<? extends Provider<Map<String, Boolean>>> perUserDefaultAutoCommitProviderClass) {
107 this.perUserDefaultAutoCommitProviderClass = perUserDefaultAutoCommitProviderClass;
108 return this;
109 }
110
111
112
113
114
115
116
117
118
119 public Builder setPerUserDefaultReadOnlyProviderClass(
120 Class<? extends Provider<Map<String, Boolean>>> perUserDefaultReadOnlyProviderClass) {
121 this.perUserDefaultReadOnlyProviderClass = perUserDefaultReadOnlyProviderClass;
122 return this;
123 }
124
125
126
127
128
129
130
131
132
133 public Builder setPerUserDefaultTransactionIsolationProviderClass(
134 Class<? extends Provider<Map<String, Integer>>> perUserDefaultTransactionIsolationProviderClass) {
135 this.perUserDefaultTransactionIsolationProviderClass = perUserDefaultTransactionIsolationProviderClass;
136 return this;
137 }
138
139
140
141
142
143
144
145
146
147 public Builder setPerUserMaxTotalProviderClass(
148 Class<? extends Provider<Map<String, Integer>>> perUserMaxActiveProviderClass) {
149 this.perUserMaxActiveProviderClass = perUserMaxActiveProviderClass;
150 return this;
151 }
152
153
154
155
156
157
158
159
160
161 public Builder setPerUserMaxIdleProviderClass(
162 Class<? extends Provider<Map<String, Integer>>> perUserMaxIdleProviderClass) {
163 this.perUserMaxIdleProviderClass = perUserMaxIdleProviderClass;
164 return this;
165 }
166
167
168
169
170
171
172
173
174
175 public Builder setPerUserMaxWaitMillisProviderClass(
176 Class<? extends Provider<Map<String, Long>>> perUserMaxWaitProviderClass) {
177 this.perUserMaxWaitProviderClass = perUserMaxWaitProviderClass;
178 return this;
179 }
180
181
182
183
184
185
186 public PerUserPoolDataSourceModule create() {
187 return new PerUserPoolDataSourceModule(perUserDefaultAutoCommitProviderClass, perUserDefaultReadOnlyProviderClass,
188 perUserDefaultTransactionIsolationProviderClass, perUserMaxActiveProviderClass, perUserMaxIdleProviderClass,
189 perUserMaxWaitProviderClass);
190 }
191
192 }
193
194 }