-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDataSourceConfig.java
More file actions
970 lines (845 loc) · 24.7 KB
/
DataSourceConfig.java
File metadata and controls
970 lines (845 loc) · 24.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
package io.ebean.datasource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Driver;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* Configuration information for a DataSource.
*
* <pre>{@code
*
* DataSourcePool pool = new DataSourceConfig()
* .setName("test")
* .setUrl("jdbc:h2:mem:tests")
* .setUsername("sa")
* .setPassword("")
* .build();
*
* }</pre>
*/
@SuppressWarnings("removal")
public class DataSourceConfig implements DataSourceBuilder.Settings {
private static final String POSTGRES = "postgres";
private String name = "";
private String readOnlyUrl;
private String url;
private String username;
private String password;
private String password2;
private String schema;
private String catalog;
private Driver driver;
private Class<? extends Driver> driverClass;
private String driverClassName;
private DataSource dataSource;
private InitDatabase initDatabase;
/**
* The name of the database platform (for use with ownerUsername and InitDatabase).
*/
private String platform;
/**
* The optional database owner username (for running InitDatabase).
*/
private String ownerUsername;
/**
* The optional database owner password (for running InitDatabase).
*/
private String ownerPassword;
private int minConnections = 2;
private int maxConnections = 200;
private int isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
private boolean autoCommit;
private boolean readOnly;
private String heartbeatSql;
private int heartbeatFreqSecs = 30;
private int heartbeatTimeoutSeconds = 30;
private int heartbeatMaxPoolExhaustedCount = 10;
private boolean captureStackTrace;
private int maxStackTraceSize = 5;
private int leakTimeMinutes = 30;
private int maxInactiveTimeSecs = 300;
private int maxAgeMinutes = 0;
private int trimPoolFreqSecs = 59;
private int pstmtCacheSize = 50;
private int cstmtCacheSize = 20;
private int waitTimeoutMillis = 1000;
private String poolListener;
private boolean offline;
private boolean failOnStart = true;
private Map<String, String> customProperties;
private List<String> initSql;
private DataSourceAlert alert;
private DataSourcePoolListener listener;
private Properties clientInfo;
private String applicationName;
private boolean shutdownOnJvmExit;
private boolean validateOnHeartbeat = !System.getenv().containsKey("LAMBDA_TASK_ROOT");
private boolean enforceCleanClose;
private int affinitySize = 257;
private Supplier<Object> affinityProvider;
@Override
public Settings settings() {
return this;
}
@Override
public DataSourceBuilder apply(Consumer<Settings> apply) {
apply.accept(this);
return this;
}
@Override
public DataSourceConfig copy() {
DataSourceConfig copy = new DataSourceConfig();
copy.initDatabase = initDatabase;
copy.url = url;
copy.readOnlyUrl = readOnlyUrl;
copy.username = username;
copy.password = password;
copy.password2 = password2;
copy.schema = schema;
copy.catalog = catalog;
copy.platform = platform;
copy.ownerUsername = ownerUsername;
copy.ownerPassword = ownerPassword;
copy.driver = driver;
copy.driverClass = driverClass;
copy.driverClassName = driverClassName;
copy.applicationName = applicationName;
copy.minConnections = minConnections;
copy.maxConnections = maxConnections;
copy.isolationLevel = isolationLevel;
copy.autoCommit = autoCommit;
copy.readOnly = readOnly;
copy.heartbeatSql = heartbeatSql;
copy.heartbeatFreqSecs = heartbeatFreqSecs;
copy.heartbeatTimeoutSeconds = heartbeatTimeoutSeconds;
copy.captureStackTrace = captureStackTrace;
copy.maxStackTraceSize = maxStackTraceSize;
copy.leakTimeMinutes = leakTimeMinutes;
copy.maxInactiveTimeSecs = maxInactiveTimeSecs;
copy.maxAgeMinutes = maxAgeMinutes;
copy.trimPoolFreqSecs = trimPoolFreqSecs;
copy.pstmtCacheSize = pstmtCacheSize;
copy.cstmtCacheSize = cstmtCacheSize;
copy.waitTimeoutMillis = waitTimeoutMillis;
copy.poolListener = poolListener;
copy.offline = offline;
copy.failOnStart = failOnStart;
copy.shutdownOnJvmExit = shutdownOnJvmExit;
copy.validateOnHeartbeat = validateOnHeartbeat;
if (customProperties != null) {
copy.customProperties = new LinkedHashMap<>(customProperties);
}
if (clientInfo != null) {
copy.clientInfo = new Properties();
copy.clientInfo.putAll(clientInfo);
}
copy.initSql = initSql;
copy.alert = alert;
copy.listener = listener;
copy.enforceCleanClose = enforceCleanClose;
copy.affinitySize = affinitySize;
copy.affinityProvider = affinityProvider;
return copy;
}
@Override
public DataSourceConfig setDefaults(DataSourceBuilder builder) {
Settings other = builder.settings();
if (driver == null) {
driver = other.driver();
}
if (driverClass == null) {
driverClass = other.driverClass();
}
if (driverClassName == null) {
driverClassName = other.getDriver();
}
if (url == null) {
url = other.getUrl();
}
if (username == null) {
username = other.getUsername();
}
if (password == null) {
password = other.getPassword();
}
if (password2 == null) {
password2 = other.getPassword2();
}
if (schema == null) {
schema = other.getSchema();
}
if (catalog == null) {
catalog = other.catalog();
}
if (minConnections == 2 && other.getMinConnections() < 2) {
minConnections = other.getMinConnections();
}
if (maxConnections == 200 && other.getMaxConnections() != 200) {
maxConnections = other.getMaxConnections();
}
if (!shutdownOnJvmExit && other.isShutdownOnJvmExit()) {
shutdownOnJvmExit = true;
}
if (validateOnHeartbeat && !other.isValidateOnHeartbeat()) {
validateOnHeartbeat = false;
}
if (customProperties == null) {
var otherCustomProps = other.getCustomProperties();
if (otherCustomProps != null) {
customProperties = new LinkedHashMap<>(otherCustomProps);
}
}
return this;
}
@Override
public boolean isEmpty() {
return url == null
&& driverClassName == null
&& username == null
&& password == null;
}
@Override
public DataSourcePool build() {
return DataSourceFactory.create(name, this);
}
@Override
public DataSourceConfig setName(String name) {
this.name = name;
return this;
}
@Override
public DataSource dataSource() {
return dataSource;
}
@Override
public DataSourceConfig dataSource(DataSource dataSource) {
this.dataSource = dataSource;
return this;
}
@Override
public String getApplicationName() {
return applicationName;
}
@Override
public DataSourceConfig setApplicationName(String applicationName) {
this.applicationName = applicationName;
return this;
}
@Override
public Properties getClientInfo() {
return clientInfo;
}
@Override
public DataSourceConfig setClientInfo(Properties clientInfo) {
this.clientInfo = clientInfo;
return this;
}
@Override
public String getReadOnlyUrl() {
return readOnlyUrl;
}
@Override
public DataSourceConfig setReadOnlyUrl(String readOnlyUrl) {
this.readOnlyUrl = readOnlyUrl;
return this;
}
@Override
public String getUrl() {
return url;
}
@Override
public DataSourceConfig setUrl(String url) {
this.url = url;
return this;
}
@Override
public String getUsername() {
return username;
}
@Override
public DataSourceConfig setUsername(String username) {
this.username = username;
return this;
}
@Override
public String getPassword() {
return password;
}
@Override
public DataSourceConfig setPassword(String password) {
this.password = password;
return this;
}
@Override
public String getPassword2() {
return password2;
}
@Override
public DataSourceConfig setPassword2(String password2) {
this.password2 = password2;
return this;
}
@Override
public String getSchema() {
return schema;
}
@Override
public DataSourceConfig setSchema(String schema) {
this.schema = schema;
return this;
}
@Override
public String catalog() {
return catalog;
}
@Override
public DataSourceConfig catalog(String catalog) {
this.catalog = catalog;
return this;
}
@Override
public String getDriver() {
return driverClassName;
}
@Override
public DataSourceConfig setDriver(String driver) {
this.driverClassName = driver;
return this;
}
@Override
public DataSourceBuilder driver(Class<? extends Driver> driverClass) {
this.driverClass = driverClass;
return this;
}
@Override
public DataSourceBuilder driver(Driver driver) {
this.driver = driver;
return this;
}
@Override
public Class<? extends Driver> driverClass() {
return driverClass;
}
@Override
public Driver driver() {
return driver;
}
@Override
public int getIsolationLevel() {
return isolationLevel;
}
@Override
public DataSourceConfig setIsolationLevel(int isolationLevel) {
this.isolationLevel = isolationLevel;
return this;
}
@Override
public boolean isAutoCommit() {
return autoCommit;
}
@Override
public DataSourceConfig setAutoCommit(boolean autoCommit) {
this.autoCommit = autoCommit;
return this;
}
@Override
public boolean isReadOnly() {
return readOnly;
}
@Override
public DataSourceConfig setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
return this;
}
@Override
public int getMinConnections() {
return minConnections;
}
@Override
public DataSourceConfig setMinConnections(int minConnections) {
this.minConnections = minConnections;
return this;
}
@Override
public int getMaxConnections() {
return maxConnections;
}
@Override
public DataSourceConfig setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections;
return this;
}
@Override
public DataSourceAlert getAlert() {
return alert;
}
@Override
public DataSourceConfig setAlert(DataSourceAlert alert) {
this.alert = alert;
return this;
}
@Override
public DataSourcePoolListener getListener() {
return listener;
}
@Override
public DataSourceConfig setListener(DataSourcePoolListener listener) {
this.listener = listener;
return this;
}
@Override
public String getHeartbeatSql() {
return heartbeatSql;
}
@Override
public DataSourceConfig setHeartbeatSql(String heartbeatSql) {
this.heartbeatSql = heartbeatSql;
return this;
}
@Override
public int getHeartbeatFreqSecs() {
return heartbeatFreqSecs;
}
@Override
public DataSourceConfig setHeartbeatFreqSecs(int heartbeatFreqSecs) {
this.heartbeatFreqSecs = heartbeatFreqSecs;
return this;
}
@Override
public int getHeartbeatTimeoutSeconds() {
return heartbeatTimeoutSeconds;
}
@Override
public DataSourceConfig setHeartbeatTimeoutSeconds(int heartbeatTimeoutSeconds) {
this.heartbeatTimeoutSeconds = heartbeatTimeoutSeconds;
return this;
}
@Override
public int getHeartbeatMaxPoolExhaustedCount() {
return this.heartbeatMaxPoolExhaustedCount;
}
@Override
public DataSourceBuilder heartbeatMaxPoolExhaustedCount(int count) {
this.heartbeatMaxPoolExhaustedCount = count;
return this;
}
@Override
public boolean isCaptureStackTrace() {
return captureStackTrace;
}
@Override
public DataSourceConfig setCaptureStackTrace(boolean captureStackTrace) {
this.captureStackTrace = captureStackTrace;
return this;
}
@Override
public int getMaxStackTraceSize() {
return maxStackTraceSize;
}
@Override
public DataSourceConfig setMaxStackTraceSize(int maxStackTraceSize) {
this.maxStackTraceSize = maxStackTraceSize;
return this;
}
@Override
public int getLeakTimeMinutes() {
return leakTimeMinutes;
}
@Override
public DataSourceConfig setLeakTimeMinutes(int leakTimeMinutes) {
this.leakTimeMinutes = leakTimeMinutes;
return this;
}
@Override
public int getPstmtCacheSize() {
return pstmtCacheSize;
}
@Override
public DataSourceConfig setPstmtCacheSize(int pstmtCacheSize) {
this.pstmtCacheSize = pstmtCacheSize;
return this;
}
@Override
public int getCstmtCacheSize() {
return cstmtCacheSize;
}
@Override
public DataSourceConfig setCstmtCacheSize(int cstmtCacheSize) {
this.cstmtCacheSize = cstmtCacheSize;
return this;
}
@Override
public int getWaitTimeoutMillis() {
return waitTimeoutMillis;
}
@Override
public DataSourceConfig setWaitTimeoutMillis(int waitTimeoutMillis) {
this.waitTimeoutMillis = waitTimeoutMillis;
return this;
}
@Override
public int getMaxInactiveTimeSecs() {
return maxInactiveTimeSecs;
}
@Override
public int getMaxAgeMinutes() {
return maxAgeMinutes;
}
@Override
public DataSourceConfig setMaxAgeMinutes(int maxAgeMinutes) {
this.maxAgeMinutes = maxAgeMinutes;
return this;
}
@Override
public DataSourceConfig setMaxInactiveTimeSecs(int maxInactiveTimeSecs) {
this.maxInactiveTimeSecs = maxInactiveTimeSecs;
return this;
}
@Override
public int getTrimPoolFreqSecs() {
return trimPoolFreqSecs;
}
@Override
public DataSourceConfig setTrimPoolFreqSecs(int trimPoolFreqSecs) {
this.trimPoolFreqSecs = trimPoolFreqSecs;
return this;
}
@Override
public String getPoolListener() {
return poolListener;
}
@Override
public DataSourceConfig setPoolListener(String poolListener) {
this.poolListener = poolListener;
return this;
}
@Override
public boolean isOffline() {
return offline;
}
@Override
public boolean isFailOnStart() {
return failOnStart;
}
@Override
public DataSourceConfig setFailOnStart(boolean failOnStart) {
this.failOnStart = failOnStart;
return this;
}
@Override
public DataSourceConfig setOffline(boolean offline) {
this.offline = offline;
return this;
}
@Override
public Map<String, String> getCustomProperties() {
return customProperties;
}
@Override
public List<String> getInitSql() {
return initSql;
}
@Override
public DataSourceConfig setInitSql(List<String> initSql) {
this.initSql = initSql;
return this;
}
@Override
public DataSourceConfig setCustomProperties(Map<String, String> customProperties) {
this.customProperties = customProperties;
return this;
}
@Override
public DataSourceConfig addProperty(String key, String value) {
if (customProperties == null) {
customProperties = new LinkedHashMap<>();
}
customProperties.put(key, value);
return this;
}
@Override
public DataSourceConfig addProperty(String key, boolean value) {
return addProperty(key, Boolean.toString(value));
}
@Override
public DataSourceConfig addProperty(String key, int value) {
return addProperty(key, Integer.toString(value));
}
@Override
public DataSourceBuilder affinitySize(int affinitySize) {
this.affinitySize = affinitySize;
return this;
}
@Override
public int getAffinitySize() {
return affinitySize;
}
@Override
public DataSourceBuilder affinityProvider(Supplier<Object> affinityProvider) {
this.affinityProvider = affinityProvider;
return this;
}
@Override
public Supplier<Object> getAffinityProvider() {
return affinityProvider;
}
@Override
public String getOwnerUsername() {
return ownerUsername;
}
@Override
public DataSourceConfig setOwnerUsername(String ownerUsername) {
this.ownerUsername = ownerUsername;
return this;
}
@Override
public String getOwnerPassword() {
return ownerPassword;
}
@Override
public DataSourceConfig setOwnerPassword(String ownerPassword) {
this.ownerPassword = ownerPassword;
return this;
}
@Override
public String getPlatform() {
return platform;
}
@Override
public DataSourceConfig setPlatform(String platform) {
this.platform = platform;
if (initDatabase != null) {
setInitDatabaseForPlatform(platform);
}
return this;
}
@Override
public InitDatabase getInitDatabase() {
return initDatabase;
}
@Override
public DataSourceConfig setInitDatabase(InitDatabase initDatabase) {
this.initDatabase = initDatabase;
return this;
}
@Override
public DataSourceConfig setInitDatabaseForPlatform(String platform) {
if (platform != null) {
if (POSTGRES.equalsIgnoreCase(platform)) {
initDatabase = new PostgresInitDatabase();
}
}
return this;
}
@Override
public boolean useInitDatabase() {
if (ownerUsername != null && ownerPassword != null) {
if (initDatabase == null) {
// default to postgres
initDatabase = new PostgresInitDatabase();
}
return true;
}
return false;
}
@Override
public boolean isShutdownOnJvmExit() {
return shutdownOnJvmExit;
}
@Override
public DataSourceConfig shutdownOnJvmExit(boolean shutdownOnJvmExit) {
this.shutdownOnJvmExit = shutdownOnJvmExit;
return this;
}
@Override
public boolean isValidateOnHeartbeat() {
return validateOnHeartbeat;
}
@Override
public DataSourceConfig validateOnHeartbeat(boolean validateOnHeartbeat) {
this.validateOnHeartbeat = validateOnHeartbeat;
return this;
}
@Override
public DataSourceConfig load(Properties properties) {
return load(properties, null);
}
@Override
public DataSourceConfig load(Properties properties, String prefix) {
loadSettings(new ConfigPropertiesHelper(prefix, null, properties));
return this;
}
@Override
public DataSourceConfig loadSettings(Properties properties, String poolName) {
loadSettings(new ConfigPropertiesHelper("datasource", poolName, properties));
return this;
}
/**
* Load the settings from the PropertiesWrapper.
*/
private void loadSettings(ConfigPropertiesHelper properties) {
username = properties.get("username", username);
password = properties.get("password", password);
password2 = properties.get("password2", password2);
schema = properties.get("schema", schema);
catalog = properties.get("catalog", catalog);
platform = properties.get("platform", platform);
ownerUsername = properties.get("ownerUsername", ownerUsername);
ownerPassword = properties.get("ownerPassword", ownerPassword);
if (initDatabase == null && platform != null) {
setInitDatabaseForPlatform(platform);
}
applicationName = properties.get("applicationName", applicationName);
driverClassName = properties.get("driver", properties.get("databaseDriver", driverClassName));
readOnlyUrl = properties.get("readOnlyUrl", readOnlyUrl);
url = properties.get("url", properties.get("databaseUrl", url));
autoCommit = properties.getBoolean("autoCommit", autoCommit);
readOnly = properties.getBoolean("readOnly", readOnly);
captureStackTrace = properties.getBoolean("captureStackTrace", captureStackTrace);
maxStackTraceSize = properties.getInt("maxStackTraceSize", maxStackTraceSize);
leakTimeMinutes = properties.getInt("leakTimeMinutes", leakTimeMinutes);
maxInactiveTimeSecs = properties.getInt("maxInactiveTimeSecs", maxInactiveTimeSecs);
trimPoolFreqSecs = properties.getInt("trimPoolFreqSecs", trimPoolFreqSecs);
maxAgeMinutes = properties.getInt("maxAgeMinutes", maxAgeMinutes);
minConnections = properties.getInt("minConnections", minConnections);
maxConnections = properties.getInt("maxConnections", maxConnections);
pstmtCacheSize = properties.getInt("pstmtCacheSize", pstmtCacheSize);
cstmtCacheSize = properties.getInt("cstmtCacheSize", cstmtCacheSize);
waitTimeoutMillis = properties.getInt("waitTimeout", waitTimeoutMillis);
heartbeatSql = properties.get("heartbeatSql", heartbeatSql);
heartbeatTimeoutSeconds = properties.getInt("heartbeatTimeoutSeconds", heartbeatTimeoutSeconds);
poolListener = properties.get("poolListener", poolListener);
offline = properties.getBoolean("offline", offline);
shutdownOnJvmExit = properties.getBoolean("shutdownOnJvmExit", shutdownOnJvmExit);
validateOnHeartbeat = properties.getBoolean("validateOnHeartbeat", validateOnHeartbeat);
enforceCleanClose = properties.getBoolean("enforceCleanClose", enforceCleanClose);
affinitySize = properties.getInt("affinityCacheSize", affinitySize);
String isoLevel = properties.get("isolationLevel", _isolationLevel(isolationLevel));
this.isolationLevel = _isolationLevel(isoLevel);
this.initSql = parseSql(properties.get("initSql", null));
this.failOnStart = properties.getBoolean("failOnStart", failOnStart);
String customProperties = properties.get("customProperties", null);
if (customProperties != null && !customProperties.isEmpty()) {
this.customProperties = parseCustom(customProperties);
}
String infoProperties = properties.get("clientInfo", null);
if (infoProperties != null && !infoProperties.isEmpty()) {
Map<String, String> pairs = parseCustom(infoProperties);
if (!pairs.isEmpty()) {
this.clientInfo = new Properties();
for (Map.Entry<String, String> entry : pairs.entrySet()) {
this.clientInfo.setProperty(entry.getKey(), entry.getValue());
}
}
}
}
private List<String> parseSql(String sql) {
List<String> ret = new ArrayList<>();
if (sql != null) {
String[] queries = sql.split(";");
for (String query : queries) {
query = query.trim();
if (!query.isEmpty()) {
ret.add(query);
}
}
}
return ret;
}
Map<String, String> parseCustom(String customProperties) {
Map<String, String> propertyMap = new LinkedHashMap<>();
String[] pairs = customProperties.split(";");
for (String pair : pairs) {
String[] split = pair.split("=");
if (split.length == 2) {
propertyMap.put(split[0], split[1]);
}
}
return propertyMap;
}
@Override
public DataSourceBuilder enforceCleanClose(boolean enforceCleanClose) {
this.enforceCleanClose = enforceCleanClose;
return this;
}
@Override
public boolean enforceCleanClose() {
return enforceCleanClose;
}
/**
* Return the isolation level description from the associated Connection int value.
*/
private String _isolationLevel(int level) {
switch (level) {
case Connection.TRANSACTION_NONE:
return "NONE";
case Connection.TRANSACTION_READ_COMMITTED:
return "READ_COMMITTED";
case Connection.TRANSACTION_READ_UNCOMMITTED:
return "READ_UNCOMMITTED";
case Connection.TRANSACTION_REPEATABLE_READ:
return "REPEATABLE_READ";
case Connection.TRANSACTION_SERIALIZABLE:
return "SERIALIZABLE";
default:
throw new RuntimeException("Transaction Isolation level [" + level + "] is not known.");
}
}
/**
* Return the isolation level for a given string description.
*/
private int _isolationLevel(String level) {
level = level.toUpperCase();
if (level.startsWith("TRANSACTION")) {
level = level.substring("TRANSACTION".length());
}
level = level.replace("_", "");
if ("NONE".equalsIgnoreCase(level)) {
return Connection.TRANSACTION_NONE;
}
if ("READCOMMITTED".equalsIgnoreCase(level)) {
return Connection.TRANSACTION_READ_COMMITTED;
}
if ("READUNCOMMITTED".equalsIgnoreCase(level)) {
return Connection.TRANSACTION_READ_UNCOMMITTED;
}
if ("REPEATABLEREAD".equalsIgnoreCase(level)) {
return Connection.TRANSACTION_REPEATABLE_READ;
}
if ("SERIALIZABLE".equalsIgnoreCase(level)) {
return Connection.TRANSACTION_SERIALIZABLE;
}
throw new RuntimeException("Transaction Isolation level [" + level + "] is not known.");
}
@Override
public Properties connectionProperties() {
if (username == null) {
throw new DataSourceConfigurationException("DataSource user is not set?");
}
if (password == null) {
throw new DataSourceConfigurationException("DataSource password is null?");
}
Properties connectionProps = new Properties();
connectionProps.setProperty("user", username);
connectionProps.setProperty("password", password);
if (customProperties != null) {
for (Map.Entry<String, String> entry : customProperties.entrySet()) {
connectionProps.setProperty(entry.getKey(), entry.getValue());
}
}
return connectionProps;
}
public long validateStaleMillis() {
if (validateOnHeartbeat) {
return 0L;
} else {
return (maxInactiveTimeSecs + trimPoolFreqSecs) * 1_000L;
}
}
}