1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.utils;
19
20 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.FileNotFoundException;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.PrintWriter;
36 import java.io.StringWriter;
37 import java.io.UnsupportedEncodingException;
38 import java.net.URLDecoder;
39 import java.nio.charset.StandardCharsets;
40 import java.security.ProtectionDomain;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Enumeration;
44 import java.util.List;
45 import java.util.Properties;
46 import java.util.UUID;
47 import java.util.function.Predicate;
48 import java.util.regex.Pattern;
49 import java.util.stream.Collectors;
50
51
52
53
54
55
56
57 public final class Settings {
58
59
60
61
62 private static final Logger LOGGER = LoggerFactory.getLogger(Settings.class);
63
64
65
66 private static final String PROPERTIES_FILE = "dependencycheck.properties";
67
68
69
70 private static final String ARRAY_SEP = ",";
71
72
73
74 private Properties props = null;
75
76
77
78 private List<Predicate<String>> maskedKeys;
79
80
81
82
83 private File tempDirectory = null;
84
85
86
87
88 private final ObjectMapper objectMapper = new ObjectMapper();
89
90
91
92
93
94
95 @SuppressWarnings("squid:S2068")
96 public static final class KEYS {
97
98
99
100
101 public static final String APPLICATION_NAME = "odc.application.name";
102
103
104
105 public static final String APPLICATION_VERSION = "odc.application.version";
106
107
108
109
110 public static final String ENGINE_VERSION_CHECK_URL = "engine.version.url";
111
112
113
114
115 public static final String AUTO_UPDATE = "odc.autoupdate";
116
117
118
119
120 public static final String DB_DRIVER_NAME = "data.driver_name";
121
122
123
124
125 public static final String DB_DRIVER_PATH = "data.driver_path";
126
127
128
129
130 public static final String DB_CONNECTION_STRING = "data.connection_string";
131
132
133
134 public static final String DB_USER = "data.user";
135
136
137
138 public static final String DB_PASSWORD = "data.password";
139
140
141
142
143 public static final String DATA_DIRECTORY = "data.directory";
144
145
146
147 public static final String H2_DATA_DIRECTORY = "data.h2.directory";
148
149
150
151 public static final String DB_FILE_NAME = "data.file_name";
152
153
154
155 public static final String DB_VERSION = "data.version";
156
157
158
159
160
161
162
163 public static final String CVE_CPE_STARTS_WITH_FILTER = "cve.cpe.startswith.filter";
164
165
166
167 public static final String NVD_API_ENDPOINT = "nvd.api.endpoint";
168
169
170
171 public static final String NVD_API_KEY = "nvd.api.key";
172
173
174
175 public static final String NVD_API_DELAY = "nvd.api.delay";
176
177
178
179
180 public static final String NVD_API_MAX_RETRY_COUNT = "nvd.api.max.retry.count";
181
182
183
184
185 public static final String NVD_API_VALID_FOR_HOURS = "nvd.api.check.validforhours";
186
187
188
189
190 public static final String NVD_API_RESULTS_PER_PAGE = "nvd.api.results.per.page";
191
192
193
194
195 public static final String NVD_API_DATAFEED_VALID_FOR_DAYS = "nvd.api.datafeed.validfordays";
196
197
198
199 public static final String NVD_API_DATAFEED_URL = "nvd.api.datafeed.url";
200
201
202
203 public static final String NVD_API_DATAFEED_USER = "nvd.api.datafeed.user";
204
205
206
207 public static final String NVD_API_DATAFEED_PASSWORD = "nvd.api.datafeed.password";
208
209
210
211 public static final String NVD_API_DATAFEED_START_YEAR = "nvd.api.datafeed.startyear";
212
213
214
215
216 public static final String ANALYZER_NVD_CVE_ENABLED = "analyzer.nvdcve.enabled";
217
218
219
220
221 public static final String CPE_MODIFIED_VALID_FOR_DAYS = "cpe.validfordays";
222
223
224
225 public static final String CPE_URL = "cpe.url";
226
227
228
229
230 public static final String KEV_URL = "kev.url";
231
232
233
234
235
236 public static final String KEV_USER = "kev.user";
237
238
239
240
241
242 public static final String KEV_PASSWORD = "kev.password";
243
244
245
246
247
248 public static final String KEV_CHECK_VALID_FOR_HOURS = "kev.check.validforhours";
249
250
251
252
253
254 public static final String PROXY_DISABLE_SCHEMAS = "proxy.disableSchemas";
255
256
257
258 public static final String PROXY_SERVER = "proxy.server";
259
260
261
262
263 public static final String PROXY_PORT = "proxy.port";
264
265
266
267 public static final String PROXY_USERNAME = "proxy.username";
268
269
270
271 public static final String PROXY_PASSWORD = "proxy.password";
272
273
274
275 public static final String PROXY_NON_PROXY_HOSTS = "proxy.nonproxyhosts";
276
277
278
279 public static final String CONNECTION_TIMEOUT = "connection.timeout";
280
281
282
283 public static final String CONNECTION_READ_TIMEOUT = "connection.read.timeout";
284
285
286
287 public static final String TEMP_DIRECTORY = "temp.directory";
288
289
290
291 public static final String MAX_DOWNLOAD_THREAD_POOL_SIZE = "max.download.threads";
292
293
294
295 public static final String ANALYSIS_TIMEOUT = "odc.analysis.timeout";
296
297
298
299 public static final String SUPPRESSION_FILE = "suppression.file";
300
301
302
303 public static final String SUPPRESSION_FILE_USER = "suppression.file.user";
304
305
306
307 public static final String SUPPRESSION_FILE_PASSWORD = "suppression.file.password";
308
309
310
311
312 public static final String HOSTED_SUPPRESSIONS_ENABLED = "hosted.suppressions.enabled";
313
314
315
316 public static final String HOSTED_SUPPRESSIONS_URL = "hosted.suppressions.url";
317
318
319
320
321
322 public static final String HOSTED_SUPPRESSIONS_USER = "hosted.suppressions.user";
323
324
325
326
327
328 public static final String HOSTED_SUPPRESSIONS_PASSWORD = "hosted.suppressions.password";
329
330
331
332
333
334 public static final String HOSTED_SUPPRESSIONS_FORCEUPDATE = "hosted.suppressions.forceupdate";
335
336
337
338
339
340 public static final String HOSTED_SUPPRESSIONS_VALID_FOR_HOURS = "hosted.suppressions.validforhours";
341
342
343
344
345 public static final String HINTS_FILE = "hints.file";
346
347
348
349
350 public static final String JUNIT_FAIL_ON_CVSS = "junit.fail.on.cvss";
351
352
353
354
355 public static final String ANALYZER_JAR_ENABLED = "analyzer.jar.enabled";
356
357
358
359
360
361 public static final String ANALYZER_KNOWN_EXPLOITED_ENABLED = "analyzer.knownexploited.enabled";
362
363
364
365
366 public static final String ANALYZER_EXPERIMENTAL_ENABLED = "analyzer.experimental.enabled";
367
368
369
370 public static final String ANALYZER_RETIRED_ENABLED = "analyzer.retired.enabled";
371
372
373
374 public static final String ANALYZER_ARCHIVE_ENABLED = "analyzer.archive.enabled";
375
376
377
378
379 public static final String ANALYZER_NODE_PACKAGE_ENABLED = "analyzer.node.package.enabled";
380
381
382
383
384 public static final String ANALYZER_NODE_PACKAGE_SKIPDEV = "analyzer.node.package.skipdev";
385
386
387
388 public static final String ANALYZER_NODE_AUDIT_ENABLED = "analyzer.node.audit.enabled";
389
390
391
392 public static final String ANALYZER_YARN_AUDIT_ENABLED = "analyzer.yarn.audit.enabled";
393
394
395
396 public static final String ANALYZER_PNPM_AUDIT_ENABLED = "analyzer.pnpm.audit.enabled";
397
398
399
400 public static final String ANALYZER_NODE_AUDIT_URL = "analyzer.node.audit.url";
401
402
403
404
405 public static final String ANALYZER_NODE_AUDIT_SKIPDEV = "analyzer.node.audit.skipdev";
406
407
408
409
410 public static final String ANALYZER_NODE_AUDIT_USE_CACHE = "analyzer.node.audit.use.cache";
411
412
413
414 public static final String ANALYZER_RETIREJS_ENABLED = "analyzer.retirejs.enabled";
415
416
417
418
419 public static final String ANALYZER_RETIREJS_FILTERS = "analyzer.retirejs.filters";
420
421
422
423
424 public static final String ANALYZER_RETIREJS_FILTER_NON_VULNERABLE = "analyzer.retirejs.filternonvulnerable";
425
426
427
428 public static final String ANALYZER_RETIREJS_REPO_JS_URL = "analyzer.retirejs.repo.js.url";
429
430
431
432 public static final String ANALYZER_RETIREJS_REPO_JS_USER = "analyzer.retirejs.repo.js.username";
433
434
435
436 public static final String ANALYZER_RETIREJS_REPO_JS_PASSWORD = "analyzer.retirejs.repo.js.password";
437
438
439
440
441 public static final String ANALYZER_RETIREJS_FORCEUPDATE = "analyzer.retirejs.forceupdate";
442
443
444
445
446 public static final String ANALYZER_RETIREJS_REPO_VALID_FOR_HOURS = "analyzer.retirejs.repo.validforhours";
447
448
449
450
451 public static final String ANALYZER_COMPOSER_LOCK_ENABLED = "analyzer.composer.lock.enabled";
452
453
454
455
456 public static final String ANALYZER_COMPOSER_LOCK_SKIP_DEV = "analyzer.composer.lock.skipdev";
457
458
459
460
461 public static final String ANALYZER_CPANFILE_ENABLED = "analyzer.cpanfile.enabled";
462
463
464
465
466 public static final String ANALYZER_PYTHON_DISTRIBUTION_ENABLED = "analyzer.python.distribution.enabled";
467
468
469
470
471 public static final String ANALYZER_PYTHON_PACKAGE_ENABLED = "analyzer.python.package.enabled";
472
473
474
475
476 public static final String ANALYZER_MIX_AUDIT_ENABLED = "analyzer.mix.audit.enabled";
477
478
479
480 public static final String ANALYZER_MIX_AUDIT_PATH = "analyzer.mix.audit.path";
481
482
483
484 public static final String ANALYZER_GOLANG_MOD_ENABLED = "analyzer.golang.mod.enabled";
485
486
487
488 public static final String ANALYZER_GOLANG_PATH = "analyzer.golang.path";
489
490
491
492 public static final String ANALYZER_YARN_PATH = "analyzer.yarn.path";
493
494
495
496 public static final String ANALYZER_PNPM_PATH = "analyzer.pnpm.path";
497
498
499
500 public static final String ANALYZER_GOLANG_DEP_ENABLED = "analyzer.golang.dep.enabled";
501
502
503
504 public static final String ANALYZER_RUBY_GEMSPEC_ENABLED = "analyzer.ruby.gemspec.enabled";
505
506
507
508 public static final String ANALYZER_AUTOCONF_ENABLED = "analyzer.autoconf.enabled";
509
510
511
512
513 public static final String ANALYZER_MAVEN_INSTALL_ENABLED = "analyzer.maveninstall.enabled";
514
515
516
517 public static final String ANALYZER_PIP_ENABLED = "analyzer.pip.enabled";
518
519
520
521 public static final String ANALYZER_PIPFILE_ENABLED = "analyzer.pipfile.enabled";
522
523
524
525 public static final String ANALYZER_POETRY_ENABLED = "analyzer.poetry.enabled";
526
527
528
529 public static final String ANALYZER_CMAKE_ENABLED = "analyzer.cmake.enabled";
530
531
532
533
534 public static final String ANALYZER_BUNDLE_AUDIT_ENABLED = "analyzer.bundle.audit.enabled";
535
536
537
538 public static final String ANALYZER_ASSEMBLY_ENABLED = "analyzer.assembly.enabled";
539
540
541
542 public static final String ANALYZER_NUSPEC_ENABLED = "analyzer.nuspec.enabled";
543
544
545
546
547 public static final String ANALYZER_NUGETCONF_ENABLED = "analyzer.nugetconf.enabled";
548
549
550
551 public static final String ANALYZER_LIBMAN_ENABLED = "analyzer.libman.enabled";
552
553
554
555
556 public static final String ANALYZER_MSBUILD_PROJECT_ENABLED = "analyzer.msbuildproject.enabled";
557
558
559
560 public static final String ANALYZER_NEXUS_ENABLED = "analyzer.nexus.enabled";
561
562
563
564 public static final String ANALYZER_NEXUS_URL = "analyzer.nexus.url";
565
566
567
568 public static final String ANALYZER_NEXUS_USER = "analyzer.nexus.username";
569
570
571
572 public static final String ANALYZER_NEXUS_PASSWORD = "analyzer.nexus.password";
573
574
575
576 public static final String ANALYZER_NEXUS_USES_PROXY = "analyzer.nexus.proxy";
577
578
579
580 public static final String ANALYZER_ARTIFACTORY_ENABLED = "analyzer.artifactory.enabled";
581
582
583
584 public static final String ANALYZER_ARTIFACTORY_URL = "analyzer.artifactory.url";
585
586
587
588 public static final String ANALYZER_ARTIFACTORY_API_USERNAME = "analyzer.artifactory.api.username";
589
590
591
592 public static final String ANALYZER_ARTIFACTORY_API_TOKEN = "analyzer.artifactory.api.token";
593
594
595
596
597
598
599
600
601 public static final String ANALYZER_ARTIFACTORY_BEARER_TOKEN = "analyzer.artifactory.bearer.token";
602
603
604
605 public static final String ANALYZER_ARTIFACTORY_USES_PROXY = "analyzer.artifactory.proxy";
606
607
608
609
610 public static final String ANALYZER_ARTIFACTORY_PARALLEL_ANALYSIS = "analyzer.artifactory.parallel.analysis";
611
612
613
614 public static final String ANALYZER_CENTRAL_ENABLED = "analyzer.central.enabled";
615
616
617
618 public static final String MAVEN_LOCAL_REPO = "odc.maven.local.repo";
619
620
621
622 public static final String CENTRAL_CONTENT_URL = "central.content.url";
623
624
625
626
627 public static final String ANALYZER_CENTRAL_PARALLEL_ANALYSIS = "analyzer.central.parallel.analysis";
628
629
630
631
632 public static final String ANALYZER_CENTRAL_RETRY_COUNT = "analyzer.central.retry.count";
633
634
635
636 public static final String ANALYZER_OPENSSL_ENABLED = "analyzer.openssl.enabled";
637
638
639
640 public static final String ANALYZER_COCOAPODS_ENABLED = "analyzer.cocoapods.enabled";
641
642
643
644 public static final String ANALYZER_CARTHAGE_ENABLED = "analyzer.carthage.enabled";
645
646
647
648
649 public static final String ANALYZER_SWIFT_PACKAGE_MANAGER_ENABLED = "analyzer.swift.package.manager.enabled";
650
651
652
653
654 public static final String ANALYZER_SWIFT_PACKAGE_RESOLVED_ENABLED = "analyzer.swift.package.resolved.enabled";
655
656
657
658 public static final String ANALYZER_CENTRAL_URL = "analyzer.central.url";
659
660
661
662 public static final String ANALYZER_CENTRAL_QUERY = "analyzer.central.query";
663
664
665
666 public static final String ANALYZER_CENTRAL_USE_CACHE = "analyzer.central.use.cache";
667
668
669
670 public static final String ANALYZER_ASSEMBLY_DOTNET_PATH = "analyzer.assembly.dotnet.path";
671
672
673
674 public static final String ANALYZER_BUNDLE_AUDIT_PATH = "analyzer.bundle.audit.path";
675
676
677
678 public static final String ANALYZER_BUNDLE_AUDIT_WORKING_DIRECTORY = "analyzer.bundle.audit.working.directory";
679
680
681
682 public static final String ADDITIONAL_ZIP_EXTENSIONS = "extensions.zip";
683
684
685
686 public static final String VFEED_DATA_FILE = "vfeed.data_file";
687
688
689
690 public static final String VFEED_CONNECTION_STRING = "vfeed.connection_string";
691
692
693
694 public static final String VFEED_DOWNLOAD_URL = "vfeed.download_url";
695
696
697
698 public static final String VFEED_DOWNLOAD_FILE = "vfeed.download_file";
699
700
701
702 public static final String VFEED_UPDATE_STATUS = "vfeed.update_status";
703
704
705
706 public static final String DOWNLOADER_QUICK_QUERY_TIMESTAMP = "downloader.quick.query.timestamp";
707
708
709
710 public static final String DOWNLOADER_TLS_PROTOCOL_LIST = "downloader.tls.protocols";
711
712
713
714 public static final String ANALYZER_CPE_ENABLED = "analyzer.cpe.enabled";
715
716
717
718 public static final String ANALYZER_NPM_CPE_ENABLED = "analyzer.npm.cpe.enabled";
719
720
721
722 public static final String ANALYZER_CPE_SUPPRESSION_ENABLED = "analyzer.cpesuppression.enabled";
723
724
725
726 public static final String ANALYZER_DEPENDENCY_BUNDLING_ENABLED = "analyzer.dependencybundling.enabled";
727
728
729
730 public static final String ANALYZER_DEPENDENCY_MERGING_ENABLED = "analyzer.dependencymerging.enabled";
731
732
733
734 public static final String ANALYZER_FALSE_POSITIVE_ENABLED = "analyzer.falsepositive.enabled";
735
736
737
738 public static final String ANALYZER_FILE_NAME_ENABLED = "analyzer.filename.enabled";
739
740
741
742 public static final String ANALYZER_PE_ENABLED = "analyzer.pe.enabled";
743
744
745
746 public static final String ANALYZER_HINT_ENABLED = "analyzer.hint.enabled";
747
748
749
750 public static final String ANALYZER_VERSION_FILTER_ENABLED = "analyzer.versionfilter.enabled";
751
752
753
754
755 public static final String ANALYZER_VULNERABILITY_SUPPRESSION_ENABLED = "analyzer.vulnerabilitysuppression.enabled";
756
757
758
759 public static final String UPDATE_NVDCVE_ENABLED = "updater.nvdcve.enabled";
760
761
762
763
764 public static final String UPDATE_VERSION_CHECK_ENABLED = "updater.versioncheck.enabled";
765
766
767
768 public static final String ECOSYSTEM_SKIP_CPEANALYZER = "ecosystem.skip.cpeanalyzer";
769
770
771
772 public static final String ENABLE_BATCH_UPDATES = "database.batchinsert.enabled";
773
774
775
776 public static final String MAX_BATCH_SIZE = "database.batchinsert.maxsize";
777
778
779
780
781 public static final String WRITELOCK_SHUTDOWN_HOOK = "data.writelock.shutdownhook";
782
783
784
785
786 public static final String ANALYZER_OSSINDEX_ENABLED = "analyzer.ossindex.enabled";
787
788
789
790
791 public static final String ANALYZER_OSSINDEX_USE_CACHE = "analyzer.ossindex.use.cache";
792
793
794
795 public static final String ANALYZER_OSSINDEX_URL = "analyzer.ossindex.url";
796
797
798
799 public static final String ANALYZER_OSSINDEX_USER = "analyzer.ossindex.user";
800
801
802
803 public static final String ANALYZER_OSSINDEX_PASSWORD = "analyzer.ossindex.password";
804
805
806
807 public static final String ANALYZER_OSSINDEX_BATCH_SIZE = "analyzer.ossindex.batch.size";
808
809
810
811
812
813 public static final String ANALYZER_OSSINDEX_REQUEST_DELAY = "analyzer.ossindex.request.delay";
814
815
816
817
818 public static final String ANALYZER_OSSINDEX_WARN_ONLY_ON_REMOTE_ERRORS = "analyzer.ossindex.remote-error.warn-only";
819
820
821
822
823
824
825
826
827 public static final String ANALYZER_DART_ENABLED = "analyzer.dart.enabled";
828
829
830
831
832 public static final String PRETTY_PRINT = "odc.reports.pretty.print";
833
834
835
836
837 public static final String MASKED_PROPERTIES = "odc.settings.mask";
838
839
840
841
842 public static final String MAX_QUERY_SIZE_DEFAULT = "odc.ecosystem.maxquerylimit.default";
843
844
845
846
847 public static final String MAX_QUERY_SIZE_PREFIX = "odc.ecosystem.maxquerylimit.";
848
849
850
851
852
853 private KEYS() {
854
855 }
856 }
857
858
859
860
861
862 public Settings() {
863 initialize(PROPERTIES_FILE);
864 }
865
866
867
868
869
870
871
872 public Settings(final Properties properties) {
873 props = properties;
874 logProperties("Properties loaded", props);
875 }
876
877
878
879
880
881
882 public Settings(@NotNull final String propertiesFilePath) {
883 initialize(propertiesFilePath);
884 }
885
886
887
888
889
890
891 private void initialize(@NotNull final String propertiesFilePath) {
892 props = new Properties();
893 try (InputStream in = FileUtils.getResourceAsStream(propertiesFilePath)) {
894 props.load(in);
895 } catch (NullPointerException ex) {
896 LOGGER.error("Did not find settings file '{}'.", propertiesFilePath);
897 LOGGER.debug("", ex);
898 } catch (IOException ex) {
899 LOGGER.error("Unable to load settings from '{}'.", propertiesFilePath);
900 LOGGER.debug("", ex);
901 }
902 logProperties("Properties loaded", props);
903 }
904
905
906
907
908 public void cleanup() {
909 cleanup(true);
910 }
911
912
913
914
915
916
917
918 public synchronized void cleanup(boolean deleteTemporary) {
919 if (deleteTemporary && tempDirectory != null && tempDirectory.exists()) {
920 LOGGER.debug("Deleting ALL temporary files from `{}`", tempDirectory.toString());
921 FileUtils.delete(tempDirectory);
922 tempDirectory = null;
923 }
924 }
925
926
927
928
929
930
931
932
933 private boolean isKeyMasked(@NotNull String key) {
934 if (maskedKeys == null || maskedKeys.isEmpty()) {
935 initMaskedKeys();
936 }
937 return maskedKeys.stream().anyMatch(maskExp -> maskExp.test(key));
938 }
939
940
941
942
943
944
945
946
947
948 String getPrintableValue(@NotNull String key, String value) {
949 String printableValue = null;
950 if (value != null) {
951 printableValue = isKeyMasked(key) ? "********" : value;
952 }
953 return printableValue;
954 }
955
956
957
958
959
960
961
962 void initMaskedKeys() {
963 final String[] masked = getArray(Settings.KEYS.MASKED_PROPERTIES);
964 if (masked == null) {
965 maskedKeys = new ArrayList<>();
966 } else {
967 maskedKeys = Arrays.stream(masked)
968 .map(v -> Pattern.compile(v).asPredicate())
969 .collect(Collectors.toList());
970 }
971 }
972
973
974
975
976
977
978
979
980 private void logProperties(@NotNull final String header, @NotNull final Properties properties) {
981 if (LOGGER.isDebugEnabled()) {
982 initMaskedKeys();
983 final StringWriter sw = new StringWriter();
984 try (PrintWriter pw = new PrintWriter(sw)) {
985 pw.format("%s:%n%n", header);
986 final Enumeration<?> e = properties.propertyNames();
987 while (e.hasMoreElements()) {
988 final String key = (String) e.nextElement();
989 final String value = getPrintableValue(key, properties.getProperty(key));
990 if (value != null) {
991 pw.format("%s='%s'%n", key, value);
992 }
993 }
994 pw.flush();
995 LOGGER.debug(sw.toString());
996 }
997 }
998 }
999
1000
1001
1002
1003
1004
1005
1006 public void setString(@NotNull final String key, @NotNull final String value) {
1007 props.setProperty(key, value);
1008 LOGGER.debug("Setting: {}='{}'", key, getPrintableValue(key, value));
1009 }
1010
1011
1012
1013
1014
1015
1016
1017 public void setStringIfNotNull(@NotNull final String key, @Nullable final String value) {
1018 if (null != value) {
1019 setString(key, value);
1020 }
1021 }
1022
1023
1024
1025
1026
1027
1028
1029 public void setStringIfNotEmpty(@NotNull final String key, @Nullable final String value) {
1030 if (null != value && !value.isEmpty()) {
1031 setString(key, value);
1032 }
1033 }
1034
1035
1036
1037
1038
1039
1040
1041 public void setArrayIfNotEmpty(@NotNull final String key, @Nullable final String[] value) {
1042 if (null != value && value.length > 0) {
1043 try {
1044 setString(key, objectMapper.writeValueAsString(value));
1045 } catch (JsonProcessingException e) {
1046 throw new IllegalArgumentException();
1047 }
1048 }
1049 }
1050
1051
1052
1053
1054
1055
1056
1057 public void setArrayIfNotEmpty(@NotNull final String key, @Nullable final List<String> value) {
1058 if (null != value && !value.isEmpty()) {
1059 try {
1060 setString(key, objectMapper.writeValueAsString(value));
1061 } catch (JsonProcessingException e) {
1062 throw new IllegalArgumentException();
1063 }
1064 }
1065 }
1066
1067
1068
1069
1070
1071
1072
1073 public void setBoolean(@NotNull final String key, boolean value) {
1074 setString(key, Boolean.toString(value));
1075 }
1076
1077
1078
1079
1080
1081
1082
1083 public void setBooleanIfNotNull(@NotNull final String key, @Nullable final Boolean value) {
1084 if (null != value) {
1085 setBoolean(key, value);
1086 }
1087 }
1088
1089
1090
1091
1092
1093
1094
1095 public void setFloat(@NotNull final String key, final float value) {
1096 setString(key, Float.toString(value));
1097 }
1098
1099
1100
1101
1102
1103
1104
1105 public void setInt(@NotNull final String key, final int value) {
1106 props.setProperty(key, String.valueOf(value));
1107 LOGGER.debug("Setting: {}='{}'", key, value);
1108 }
1109
1110
1111
1112
1113
1114
1115
1116 public void setIntIfNotNull(@NotNull final String key, @Nullable final Integer value) {
1117 if (null != value) {
1118 setInt(key, value);
1119 }
1120 }
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134 @SuppressFBWarnings(justification = "try with resource will clenaup the resources", value = {"OBL_UNSATISFIED_OBLIGATION"})
1135 public void mergeProperties(@NotNull final File filePath) throws FileNotFoundException, IOException {
1136 try (FileInputStream fis = new FileInputStream(filePath)) {
1137 mergeProperties(fis);
1138 }
1139 }
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153 @SuppressFBWarnings(justification = "try with resource will clenaup the resources", value = {"OBL_UNSATISFIED_OBLIGATION"})
1154 public void mergeProperties(@NotNull final String filePath) throws FileNotFoundException, IOException {
1155 try (FileInputStream fis = new FileInputStream(filePath)) {
1156 mergeProperties(fis);
1157 }
1158 }
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170 public void mergeProperties(@NotNull final InputStream stream) throws IOException {
1171 props.load(stream);
1172 logProperties("Properties updated via merge", props);
1173 }
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184 @Nullable
1185 public File getFile(@NotNull final String key) {
1186 final String file = getString(key);
1187 if (file == null) {
1188 return null;
1189 }
1190 return new File(file);
1191 }
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207 File getDataFile(@NotNull final String key) {
1208 final String file = getString(key);
1209 LOGGER.debug("Settings.getDataFile() - file: '{}'", file);
1210 if (file == null) {
1211 return null;
1212 }
1213 if (file.startsWith("[JAR]")) {
1214 LOGGER.debug("Settings.getDataFile() - transforming filename");
1215 final File jarPath = getJarPath();
1216 LOGGER.debug("Settings.getDataFile() - jar file: '{}'", jarPath.toString());
1217 final File retVal = new File(jarPath, file.substring(6));
1218 LOGGER.debug("Settings.getDataFile() - returning: '{}'", retVal);
1219 return retVal;
1220 }
1221 return new File(file);
1222 }
1223
1224
1225
1226
1227
1228
1229
1230 private File getJarPath() {
1231 String decodedPath = ".";
1232 String jarPath = "";
1233 final ProtectionDomain domain = Settings.class.getProtectionDomain();
1234 if (domain != null && domain.getCodeSource() != null && domain.getCodeSource().getLocation() != null) {
1235 jarPath = Settings.class.getProtectionDomain().getCodeSource().getLocation().getPath();
1236 }
1237 try {
1238 decodedPath = URLDecoder.decode(jarPath, StandardCharsets.UTF_8.name());
1239 } catch (UnsupportedEncodingException ex) {
1240 LOGGER.trace("", ex);
1241 }
1242
1243 final File path = new File(decodedPath);
1244 if (path.getName().toLowerCase().endsWith(".jar")) {
1245 return path.getParentFile();
1246 } else {
1247 return new File(".");
1248 }
1249 }
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261 public String getString(@NotNull final String key, @Nullable final String defaultValue) {
1262 return System.getProperty(key, props.getProperty(key, defaultValue));
1263 }
1264
1265
1266
1267
1268
1269
1270
1271 public synchronized File getTempDirectory() throws IOException {
1272 if (tempDirectory == null) {
1273 final File baseTemp = new File(getString(Settings.KEYS.TEMP_DIRECTORY, System.getProperty("java.io.tmpdir")));
1274 tempDirectory = FileUtils.createTempDirectory(baseTemp);
1275 }
1276 return tempDirectory;
1277 }
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288 public String getString(@NotNull final String key) {
1289 return System.getProperty(key, props.getProperty(key));
1290 }
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301 public String[] getArray(@NotNull final String key) {
1302 final String string = getString(key);
1303 if (string != null) {
1304 if (string.charAt(0) == '{' || string.charAt(0) == '[') {
1305 try {
1306 return objectMapper.readValue(string, String[].class);
1307 } catch (JsonProcessingException e) {
1308 throw new IllegalStateException("Unable to read value '" + string + "' as an array");
1309 }
1310 } else {
1311 return string.split(ARRAY_SEP);
1312 }
1313 }
1314 return null;
1315 }
1316
1317
1318
1319
1320
1321
1322
1323 public void removeProperty(@NotNull final String key) {
1324 props.remove(key);
1325 }
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338 public int getInt(@NotNull final String key) throws InvalidSettingException {
1339 try {
1340 return Integer.parseInt(getString(key));
1341 } catch (NumberFormatException ex) {
1342 throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
1343 }
1344 }
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357 public int getInt(@NotNull final String key, int defaultValue) {
1358 int value;
1359 try {
1360 value = Integer.parseInt(getString(key));
1361 } catch (NumberFormatException ex) {
1362 if (!getString(key, "").isEmpty()) {
1363 LOGGER.debug("Could not convert property '{}={}' to an int; using {} instead.",
1364 key, getPrintableValue(key, getString(key)), defaultValue);
1365 }
1366 value = defaultValue;
1367 }
1368 return value;
1369 }
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382 public long getLong(@NotNull final String key) throws InvalidSettingException {
1383 try {
1384 return Long.parseLong(getString(key));
1385 } catch (NumberFormatException ex) {
1386 throw new InvalidSettingException("Could not convert property '" + key + "' to a long.", ex);
1387 }
1388 }
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402 public boolean getBoolean(@NotNull final String key) throws InvalidSettingException {
1403 return Boolean.parseBoolean(getString(key));
1404 }
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418 public boolean getBoolean(@NotNull final String key, boolean defaultValue) {
1419 return Boolean.parseBoolean(getString(key, Boolean.toString(defaultValue)));
1420 }
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434 public float getFloat(@NotNull final String key, float defaultValue) {
1435 float retValue = defaultValue;
1436 try {
1437 retValue = Float.parseFloat(getString(key));
1438 } catch (Throwable ex) {
1439 LOGGER.trace("ignore", ex);
1440 }
1441 return retValue;
1442 }
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457 public String getConnectionString(String connectionStringKey, String dbFileNameKey)
1458 throws IOException, InvalidSettingException {
1459 final String connStr = getString(connectionStringKey);
1460 if (connStr == null) {
1461 final String msg = String.format("Invalid properties file; %s is missing.", connectionStringKey);
1462 throw new InvalidSettingException(msg);
1463 }
1464 if (connStr.contains("%s")) {
1465 final File directory = getH2DataDirectory();
1466 LOGGER.debug("Data directory: {}", directory);
1467 String fileName = null;
1468 if (dbFileNameKey != null) {
1469 fileName = getString(dbFileNameKey);
1470 }
1471 if (fileName == null) {
1472 final String msg = String.format("Invalid properties file to get a file based connection string; '%s' must be defined.",
1473 dbFileNameKey);
1474 throw new InvalidSettingException(msg);
1475 }
1476 if (connStr.startsWith("jdbc:h2:file:") && fileName.endsWith(".mv.db")) {
1477 fileName = fileName.substring(0, fileName.length() - 6);
1478 }
1479
1480 final File dbFile = new File(directory, fileName);
1481 final String cString = String.format(connStr, dbFile.getCanonicalPath());
1482 LOGGER.debug("Connection String: '{}'", cString);
1483 return cString;
1484 }
1485 return connStr;
1486 }
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496 public File getDataDirectory() throws IOException {
1497 final File path = getDataFile(Settings.KEYS.DATA_DIRECTORY);
1498 if (path != null && (path.exists() || path.mkdirs())) {
1499 return path;
1500 }
1501 throw new IOException(String.format("Unable to create the data directory '%s'",
1502 (path == null) ? "unknown" : path.getAbsolutePath()));
1503 }
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513 public File getH2DataDirectory() throws IOException {
1514 final String h2Test = getString(Settings.KEYS.H2_DATA_DIRECTORY);
1515 final File path;
1516 if (h2Test != null && !h2Test.isEmpty()) {
1517 path = getDataFile(Settings.KEYS.H2_DATA_DIRECTORY);
1518 } else {
1519 path = getDataFile(Settings.KEYS.DATA_DIRECTORY);
1520 }
1521 if (path != null && (path.exists() || path.mkdirs())) {
1522 return path;
1523 }
1524 throw new IOException(String.format("Unable to create the h2 data directory '%s'",
1525 (path == null) ? "unknown" : path.getAbsolutePath()));
1526 }
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536 public File getTempFile(@NotNull final String prefix, @NotNull final String extension) throws IOException {
1537 final File dir = getTempDirectory();
1538 final String tempFileName = String.format("%s%s.%s", prefix, UUID.randomUUID(), extension);
1539 final File tempFile = new File(dir, tempFileName);
1540 if (tempFile.exists()) {
1541 return getTempFile(prefix, extension);
1542 }
1543 return tempFile;
1544 }
1545 }