1 package guru.mikelue.jdut;
2
3 import java.io.Reader;
4 import java.sql.Connection;
5 import java.sql.SQLException;
6 import java.util.Collections;
7 import java.util.HashMap;
8 import java.util.Map;
9 import java.util.Optional;
10 import java.util.function.Consumer;
11 import java.util.function.Function;
12 import java.util.function.Supplier;
13
14 import org.apache.commons.lang3.StringUtils;
15 import org.apache.commons.lang3.Validate;
16
17 import guru.mikelue.jdut.decorate.DataGrainDecorator;
18 import guru.mikelue.jdut.jdbc.JdbcFunction;
19 import guru.mikelue.jdut.jdbc.SQLExceptionConvert;
20 import guru.mikelue.jdut.operation.DataGrainOperator;
21 import guru.mikelue.jdut.operation.OperatorFactory;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class ConductorConfig {
39
40
41
42 public class Builder {
43 private Builder() {};
44
45
46
47
48
49
50
51
52 public Builder parent(ConductorConfig newParent)
53 {
54 parent = Optional.ofNullable(newParent);
55 return this;
56 }
57
58
59
60
61
62
63
64
65 public Builder resourceLoader(Function<String, Reader> newResourceLoader)
66 {
67 resourceLoader = Optional.ofNullable(newResourceLoader);
68 return this;
69 }
70
71
72
73
74
75
76
77
78
79 public <E extends RuntimeException> Builder sqlExceptionConvert(SQLExceptionConvert<E> newSqlExceptionConvert)
80 {
81 sqlExceptionConvert = Optional.ofNullable(newSqlExceptionConvert);
82 return this;
83 }
84
85
86
87
88
89
90
91
92 public Builder operatorFactory(OperatorFactory newOperatorFactory)
93 {
94 operatorFactory = Optional.ofNullable(newOperatorFactory);
95 return this;
96 }
97
98
99
100
101
102
103
104
105
106 public Builder namedOperator(String name, DataGrainOperator operator)
107 {
108 name = StringUtils.trimToNull(name);
109 Validate.notNull(name, "Need viable name of operator");
110 Validate.notNull(operator, "Need viable operator");
111
112 namedOperators.put(name, operator);
113 return this;
114 }
115
116
117
118
119
120
121
122
123
124 public Builder namedSupplier(String name, Supplier<?> supplier)
125 {
126 name = StringUtils.trimToNull(name);
127 Validate.notNull(name, "Need viable name of supplier");
128 Validate.notNull(supplier, "Need viable supplier");
129
130 namedSuppliers.put(name, supplier);
131 return this;
132 }
133
134
135
136
137
138
139
140
141
142 @SuppressWarnings("unchecked")
143 public Builder namedJdbcFunction(String name, JdbcFunction<? extends Connection, ?> jdbcFunction)
144 {
145 name = StringUtils.trimToNull(name);
146 Validate.notNull(name, "Need viable name of JDBC function");
147 Validate.notNull(jdbcFunction, "Need viable operator");
148
149 namedJdbcFunctions.put(name, (JdbcFunction<Connection, ?>)jdbcFunction);
150 return this;
151 }
152
153
154
155
156
157
158
159
160
161 public Builder namedDecorator(String name, DataGrainDecorator decorator)
162 {
163 name = StringUtils.trimToNull(name);
164 Validate.notNull(name, "Need viable name of JDBC function");
165 Validate.notNull(decorator, "Need viable decorator");
166
167 namedDecorators.put(name, decorator);
168 return this;
169 }
170 }
171
172
173
174
175
176
177
178
179
180
181 public static ConductorConfig build(Consumer<Builder> builderConsumer)
182 {
183 ConductorConfig config = new ConductorConfig();
184 ConductorConfig.Builder newBuilder = config.new Builder();
185
186 builderConsumer.accept(newBuilder);
187
188 return config.clone();
189 }
190
191
192
193
194
195
196
197
198
199
200
201 public static ConductorConfigl#ConductorConfig">ConductorConfig build(Consumer<Builder> builderConsumer, ConductorConfig clonedConfig)
202 {
203 ConductorConfig newConfig = clonedConfig.modifiableClone();
204 ConductorConfig.Builder newBuilder = newConfig.new Builder();
205
206 builderConsumer.accept(newBuilder);
207
208 return newConfig.clone();
209 }
210
211 private Optional<ConductorConfig> parent = Optional.empty();
212
213 private Map<String, DataGrainOperator> namedOperators = new HashMap<>(4);
214 private Map<String, JdbcFunction<Connection, ?>> namedJdbcFunctions = new HashMap<>(4);
215 private Map<String, DataGrainDecorator> namedDecorators = new HashMap<>(4);
216 private Map<String, Supplier<?>> namedSuppliers = new HashMap<>(4);
217 private Optional<Function<String, Reader>> resourceLoader = Optional.empty();
218 private Optional<OperatorFactory> operatorFactory = Optional.empty();
219 private Optional<SQLExceptionConvert<?>> sqlExceptionConvert = Optional.empty();
220
221 private ConductorConfig() {}
222
223
224
225
226
227
228 public Optional<Function<String, Reader>> getResourceLoader()
229 {
230 if (!resourceLoader.isPresent() && parent.isPresent()) {
231 return parent.get().getResourceLoader();
232 }
233
234 return resourceLoader;
235 }
236
237
238
239
240
241
242 public Optional<OperatorFactory> getOperatorFactory()
243 {
244 return operatorFactory;
245 }
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262 public Optional<DataGrainOperator> getOperator(String name)
263 {
264 if (namedOperators.containsKey(name)) {
265 return Optional.of(namedOperators.get(name));
266 }
267
268 if (operatorFactory.isPresent()) {
269 DataGrainOperator operator = operatorFactory.get().get(name);
270 if (operator != null) {
271 return Optional.of(operator);
272 }
273 }
274
275 if (parent.isPresent()) {
276 return parent.get().getOperator(name);
277 }
278
279 return Optional.empty();
280 }
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295 public Optional<DataGrainDecorator> getDecorator(String name)
296 {
297 if (namedDecorators.containsKey(name)) {
298 return Optional.of(namedDecorators.get(name));
299 }
300
301 if (parent.isPresent()) {
302 return parent.get().getDecorator(name);
303 }
304
305 return Optional.empty();
306 }
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321 public Optional<Supplier<?>> getSupplier(String name)
322 {
323 if (namedSuppliers.containsKey(name)) {
324 return Optional.of(namedSuppliers.get(name));
325 }
326
327 if (parent.isPresent()) {
328 return parent.get().getSupplier(name);
329 }
330
331 return Optional.empty();
332 }
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347 public Optional<JdbcFunction<Connection, ?>> getJdbcFunction(String name)
348 {
349 if (namedJdbcFunctions.containsKey(name)) {
350 return Optional.of(namedJdbcFunctions.get(name));
351 }
352
353 if (parent.isPresent()) {
354 return parent.get().getJdbcFunction(name);
355 }
356
357 return Optional.empty();
358 }
359
360
361
362
363
364
365 public Optional<SQLExceptionConvert<?>> getSqlExceptionConvert()
366 {
367 if (sqlExceptionConvert.isPresent()) {
368 return sqlExceptionConvert;
369 }
370
371 if (parent.isPresent()) {
372 return parent.get().getSqlExceptionConvert();
373 }
374
375 return Optional.empty();
376 }
377
378 @Override
379 protected ConductorConfig clone()
380 {
381 ConductorConfig newConfig = commonClone();
382 newConfig.namedOperators = Collections.unmodifiableMap(this.namedOperators);
383 newConfig.namedJdbcFunctions = Collections.unmodifiableMap(this.namedJdbcFunctions);
384 newConfig.namedDecorators = Collections.unmodifiableMap(this.namedDecorators);
385 newConfig.namedSuppliers = Collections.unmodifiableMap(this.namedSuppliers);
386
387 return newConfig;
388 }
389
390 private ConductorConfig modifiableClone()
391 {
392 ConductorConfig newConfig = commonClone();
393 newConfig.namedOperators = new HashMap<>(this.namedOperators);
394 newConfig.namedJdbcFunctions = new HashMap<>(this.namedJdbcFunctions);
395 newConfig.namedDecorators = new HashMap<>(this.namedDecorators);
396 newConfig.namedSuppliers = new HashMap<>(this.namedSuppliers);
397
398 return newConfig;
399 }
400
401 private ConductorConfig commonClone()
402 {
403 ConductorConfig newConfig = new ConductorConfig();
404 newConfig.parent = this.parent;
405 newConfig.resourceLoader = this.resourceLoader;
406 newConfig.operatorFactory = this.operatorFactory;
407 newConfig.sqlExceptionConvert = this.sqlExceptionConvert;
408
409 return newConfig;
410 }
411 }