1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.io.Reader;
23 import java.io.Writer;
24 import java.nio.charset.Charset;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.Enumeration;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Properties;
32 import java.util.Set;
33 import java.util.TreeSet;
34
35 import org.apache.commons.io.FileUtils;
36 import org.apache.commons.io.IOUtils;
37 import org.apache.commons.lang3.StringUtils;
38 import org.jasypt.util.text.TextEncryptor;
39 import org.kuali.common.util.property.Constants;
40 import org.kuali.common.util.property.GlobalPropertiesMode;
41 import org.kuali.common.util.property.processor.AddPropertiesProcessor;
42 import org.kuali.common.util.property.processor.PropertyProcessor;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.util.PropertyPlaceholderHelper;
46
47
48
49
50
51
52 public class PropertyUtils {
53
54 private static final Logger logger = LoggerFactory.getLogger(PropertyUtils.class);
55
56 private static final String XML_EXTENSION = ".xml";
57 private static final String ENV_PREFIX = "env";
58 private static final String DEFAULT_ENCODING = Charset.defaultCharset().name();
59 private static final String DEFAULT_XML_ENCODING = "UTF-8";
60
61
62
63
64
65
66
67
68 public static void decrypt(Properties properties, TextEncryptor encryptor) {
69 decrypt(properties, encryptor, null, null);
70 }
71
72
73
74
75
76
77
78
79 public static Properties getEncryptedProperties(Properties properties) {
80 List<String> keys = getSortedKeys(properties);
81 Properties encrypted = new Properties();
82 for (String key : keys) {
83 String value = properties.getProperty(key);
84 if (isEncryptedPropertyValue(value)) {
85 encrypted.setProperty(key, value);
86 }
87 }
88 return encrypted;
89 }
90
91
92
93
94
95
96
97
98 public static void decrypt(Properties properties, TextEncryptor encryptor, List<String> includes, List<String> excludes) {
99 List<String> keys = getSortedKeys(properties, includes, excludes);
100 for (String key : keys) {
101 String value = properties.getProperty(key);
102 if (isEncryptedPropertyValue(value)) {
103 String decryptedValue = decryptPropertyValue(encryptor, value);
104 properties.setProperty(key, decryptedValue);
105 }
106 }
107 }
108
109
110
111
112 public static boolean isEncryptedPropertyValue(String value) {
113 return StringUtils.startsWith(value, Constants.ENCRYPTION_PREFIX) && StringUtils.endsWith(value, Constants.ENCRYPTION_SUFFIX);
114 }
115
116
117
118
119
120
121
122
123 public static void encrypt(Properties properties, TextEncryptor encryptor) {
124 encrypt(properties, encryptor, null, null);
125 }
126
127
128
129
130
131
132
133
134 public static void encrypt(Properties properties, TextEncryptor encryptor, List<String> includes, List<String> excludes) {
135 List<String> keys = getSortedKeys(properties, includes, excludes);
136 for (String key : keys) {
137 String originalValue = properties.getProperty(key);
138 String encryptedValue = encryptPropertyValue(encryptor, originalValue);
139 properties.setProperty(key, encryptedValue);
140 }
141 }
142
143
144
145
146
147
148
149
150 public static String decryptPropertyValue(TextEncryptor encryptor, String value) {
151
152 Assert.isTrue(StringUtils.startsWith(value, Constants.ENCRYPTION_PREFIX), "value does not start with " + Constants.ENCRYPTION_PREFIX);
153 Assert.isTrue(StringUtils.endsWith(value, Constants.ENCRYPTION_SUFFIX), "value does not end with " + Constants.ENCRYPTION_SUFFIX);
154
155
156 int start = Constants.ENCRYPTION_PREFIX.length();
157 int end = StringUtils.length(value) - Constants.ENCRYPTION_SUFFIX.length();
158 String unwrapped = StringUtils.substring(value, start, end);
159
160
161 return encryptor.decrypt(unwrapped);
162 }
163
164
165
166
167
168
169
170
171 public static String encryptPropertyValue(TextEncryptor encryptor, String value) {
172 String encryptedValue = encryptor.encrypt(value);
173 StringBuilder sb = new StringBuilder();
174 sb.append(Constants.ENCRYPTION_PREFIX);
175 sb.append(encryptedValue);
176 sb.append(Constants.ENCRYPTION_SUFFIX);
177 return sb.toString();
178 }
179
180 public static void overrideWithGlobalValues(Properties properties, GlobalPropertiesMode mode) {
181 List<String> keys = PropertyUtils.getSortedKeys(properties);
182 Properties global = PropertyUtils.getProperties(mode);
183 for (String key : keys) {
184 String globalValue = global.getProperty(key);
185 if (!StringUtils.isBlank(globalValue)) {
186 properties.setProperty(key, globalValue);
187 }
188 }
189 }
190
191 public static final Properties combine(List<Properties> properties) {
192 Properties combined = new Properties();
193 for (Properties p : properties) {
194 combined.putAll(PropertyUtils.toEmpty(p));
195 }
196 return combined;
197 }
198
199 public static final Properties combine(Properties... properties) {
200 return combine(Arrays.asList(properties));
201 }
202
203 public static final void process(Properties properties, PropertyProcessor processor) {
204 process(properties, Collections.singletonList(processor));
205 }
206
207 public static final void process(Properties properties, List<PropertyProcessor> processors) {
208 for (PropertyProcessor processor : CollectionUtils.toEmptyList(processors)) {
209 processor.process(properties);
210 }
211 }
212
213 public static final Properties toEmpty(Properties properties) {
214 return properties == null ? new Properties() : properties;
215 }
216
217 public static final boolean isSingleUnresolvedPlaceholder(String string) {
218 return isSingleUnresolvedPlaceholder(string, Constants.DEFAULT_PLACEHOLDER_PREFIX, Constants.DEFAULT_PLACEHOLDER_SUFFIX);
219 }
220
221 public static final boolean isSingleUnresolvedPlaceholder(String string, String prefix, String suffix) {
222 int prefixMatches = StringUtils.countMatches(string, prefix);
223 int suffixMatches = StringUtils.countMatches(string, suffix);
224 boolean startsWith = StringUtils.startsWith(string, prefix);
225 boolean endsWith = StringUtils.endsWith(string, suffix);
226 return prefixMatches == 1 && suffixMatches == 1 && startsWith && endsWith;
227 }
228
229 public static final boolean containsUnresolvedPlaceholder(String string) {
230 return containsUnresolvedPlaceholder(string, Constants.DEFAULT_PLACEHOLDER_PREFIX, Constants.DEFAULT_PLACEHOLDER_SUFFIX);
231 }
232
233 public static final boolean containsUnresolvedPlaceholder(String string, String prefix, String suffix) {
234 int beginIndex = StringUtils.indexOf(string, prefix);
235 if (beginIndex == -1) {
236 return false;
237 }
238 return StringUtils.indexOf(string, suffix) != -1;
239 }
240
241
242
243
244
245 public static final Properties getResolvedProperties(Properties properties) {
246 return getResolvedProperties(properties, Constants.DEFAULT_PROPERTY_PLACEHOLDER_HELPER, Constants.DEFAULT_GLOBAL_PROPERTIES_MODE);
247 }
248
249
250
251
252
253 public static final Properties getResolvedProperties(Properties properties, GlobalPropertiesMode globalPropertiesMode) {
254 return getResolvedProperties(properties, Constants.DEFAULT_PROPERTY_PLACEHOLDER_HELPER, globalPropertiesMode);
255 }
256
257
258
259
260
261 public static final Properties getResolvedProperties(Properties properties, PropertyPlaceholderHelper helper) {
262 return getResolvedProperties(properties, helper, Constants.DEFAULT_GLOBAL_PROPERTIES_MODE);
263 }
264
265
266
267
268
269 public static final Properties getResolvedProperties(Properties properties, PropertyPlaceholderHelper helper, GlobalPropertiesMode globalPropertiesMode) {
270 Properties global = PropertyUtils.getProperties(properties, globalPropertiesMode);
271 List<String> keys = PropertyUtils.getSortedKeys(properties);
272 Properties newProperties = new Properties();
273 for (String key : keys) {
274 String originalValue = properties.getProperty(key);
275 String resolvedValue = helper.replacePlaceholders(originalValue, global);
276 if (!resolvedValue.equals(originalValue)) {
277 logger.debug("Resolved property '" + key + "' [{}] -> [{}]", Str.flatten(originalValue), Str.flatten(resolvedValue));
278 newProperties.setProperty(key, resolvedValue);
279 }
280 }
281 return newProperties;
282 }
283
284
285
286
287 public static final List<String> getValues(Properties properties, List<String> keys) {
288 List<String> values = new ArrayList<String>();
289 for (String key : keys) {
290 values.add(properties.getProperty(key));
291 }
292 return values;
293 }
294
295
296
297
298 public static final List<String> getEndsWithKeys(Properties properties, String suffix) {
299 List<String> keys = getSortedKeys(properties);
300 List<String> matches = new ArrayList<String>();
301 for (String key : keys) {
302 if (StringUtils.endsWith(key, suffix)) {
303 matches.add(key);
304 }
305 }
306 return matches;
307 }
308
309
310
311
312 public static final void trim(Properties properties, String includesCSV, String excludesCSV) {
313 List<String> includes = CollectionUtils.getTrimmedListFromCSV(includesCSV);
314 List<String> excludes = CollectionUtils.getTrimmedListFromCSV(excludesCSV);
315 trim(properties, includes, excludes);
316 }
317
318
319
320
321 public static final void trim(Properties properties, List<String> includes, List<String> excludes) {
322 List<String> keys = getSortedKeys(properties);
323 for (String key : keys) {
324 if (!include(key, includes, excludes)) {
325 logger.debug("Removing [{}]", key);
326 properties.remove(key);
327 }
328 }
329 }
330
331
332
333
334
335
336
337
338
339 public static final boolean include(String value, List<String> includes, List<String> excludes) {
340 if (isSingleWildcardMatch(value, excludes)) {
341
342 return false;
343 } else {
344
345 return CollectionUtils.isEmpty(includes) || isSingleWildcardMatch(value, includes);
346 }
347 }
348
349 public static final boolean isSingleWildcardMatch(String s, List<String> patterns) {
350 for (String pattern : CollectionUtils.toEmptyList(patterns)) {
351 if (isSingleWildcardMatch(s, pattern)) {
352 return true;
353 }
354 }
355 return false;
356 }
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376 public static final boolean isSingleWildcardMatch(String value, String pattern) {
377 if (value == null && pattern == null) {
378
379 return true;
380 } else if (value != null && pattern == null || value == null && pattern != null) {
381
382 return false;
383 } else if (pattern.equals(Constants.WILDCARD)) {
384
385 return true;
386 } else if (StringUtils.countMatches(pattern, Constants.WILDCARD) > 1) {
387
388 throw new IllegalArgumentException("Pattern [" + pattern + "] is not supported. Only one wildcard is allowed in the pattern");
389 } else if (!StringUtils.contains(pattern, Constants.WILDCARD)) {
390
391 return StringUtils.equals(value, pattern);
392 } else {
393
394
395
396 int pos = StringUtils.indexOf(pattern, Constants.WILDCARD);
397 int suffixPos = pos + Constants.WILDCARD.length();
398 boolean nullPrefix = pos == 0;
399 boolean nullSuffix = suffixPos >= pattern.length();
400 String prefix = nullPrefix ? null : StringUtils.substring(pattern, 0, pos);
401 String suffix = nullSuffix ? null : StringUtils.substring(pattern, suffixPos);
402 boolean prefixMatch = nullPrefix || StringUtils.startsWith(value, prefix);
403 boolean suffixMatch = nullSuffix || StringUtils.endsWith(value, suffix);
404 return prefixMatch && suffixMatch;
405 }
406 }
407
408
409
410
411 public static final Properties getProperties(Properties properties, String include, String exclude) {
412 List<String> keys = getSortedKeys(properties, include, exclude);
413 Properties newProperties = new Properties();
414 for (String key : keys) {
415 String value = properties.getProperty(key);
416 newProperties.setProperty(key, value);
417 }
418 return newProperties;
419 }
420
421
422
423
424 public static final List<String> getSortedKeys(Properties properties, String include, String exclude) {
425 return getSortedKeys(properties, CollectionUtils.toEmptyList(include), CollectionUtils.toEmptyList(exclude));
426 }
427
428
429
430
431 public static final List<String> getSortedKeys(Properties properties, List<String> includes, List<String> excludes) {
432 List<String> keys = getSortedKeys(properties);
433 List<String> includedKeys = new ArrayList<String>();
434 for (String key : keys) {
435 if (include(key, includes, excludes)) {
436 includedKeys.add(key);
437 }
438 }
439 return includedKeys;
440 }
441
442
443
444
445 public static final List<String> getStartsWithKeys(Properties properties, String prefix) {
446 List<String> keys = getSortedKeys(properties);
447 List<String> matches = new ArrayList<String>();
448 for (String key : keys) {
449 if (StringUtils.startsWith(key, prefix)) {
450 matches.add(key);
451 }
452 }
453 return matches;
454 }
455
456
457
458
459 public static final List<String> getSortedKeys(Properties properties) {
460 List<String> keys = new ArrayList<String>(properties.stringPropertyNames());
461 Collections.sort(keys);
462 return keys;
463 }
464
465 public static final String toString(Properties properties) {
466 List<String> keys = getSortedKeys(properties);
467 StringBuilder sb = new StringBuilder();
468 for (String key : keys) {
469 String value = Str.flatten(properties.getProperty(key));
470 sb.append(key + "=" + value + "\n");
471 }
472 return sb.toString();
473 }
474
475 public static final void info(Properties properties) {
476 properties = toEmpty(properties);
477 logger.info("--- Displaying {} properties ---\n\n{}", properties.size(), toString(properties));
478 }
479
480 public static final void debug(Properties properties) {
481 properties = toEmpty(properties);
482 logger.debug("--- Displaying {} properties ---\n\n{}", properties.size(), toString(properties));
483 }
484
485
486
487
488 public static final void store(Properties properties, File file) {
489 store(properties, file, null);
490 }
491
492
493
494
495 public static final void store(Properties properties, File file, String encoding) {
496 store(properties, file, encoding, null);
497 }
498
499
500
501
502 public static final void store(Properties properties, File file, String encoding, String comment) {
503 OutputStream out = null;
504 Writer writer = null;
505 try {
506 out = FileUtils.openOutputStream(file);
507 String path = file.getCanonicalPath();
508 boolean xml = isXml(path);
509 Properties sorted = getSortedProperties(properties);
510 comment = getComment(encoding, comment, xml);
511 if (xml) {
512 logger.info("Storing XML properties - [{}] encoding={}", path, StringUtils.defaultIfBlank(encoding, DEFAULT_ENCODING));
513 if (encoding == null) {
514 sorted.storeToXML(out, comment);
515 } else {
516 sorted.storeToXML(out, comment, encoding);
517 }
518 } else {
519 writer = LocationUtils.getWriter(out, encoding);
520 logger.info("Storing properties - [{}] encoding={}", path, StringUtils.defaultIfBlank(encoding, DEFAULT_ENCODING));
521 sorted.store(writer, comment);
522 }
523 } catch (IOException e) {
524 throw new IllegalStateException("Unexpected IO error", e);
525 } finally {
526 IOUtils.closeQuietly(writer);
527 IOUtils.closeQuietly(out);
528 }
529 }
530
531
532
533
534
535 public static final Properties getGlobalProperties() {
536 return getProperties(Constants.DEFAULT_GLOBAL_PROPERTIES_MODE);
537 }
538
539
540
541
542
543 public static final Properties getGlobalProperties(Properties properties) {
544 return getProperties(properties, Constants.DEFAULT_GLOBAL_PROPERTIES_MODE);
545 }
546
547
548
549
550
551
552
553 public static final Properties getProperties(Properties properties, GlobalPropertiesMode mode) {
554 Properties newProperties = duplicate(properties);
555 List<PropertyProcessor> modifiers = getPropertyProcessors(mode);
556 for (PropertyProcessor modifier : modifiers) {
557 modifier.process(newProperties);
558 }
559 return newProperties;
560 }
561
562
563
564
565
566
567
568 public static final Properties getProperties(GlobalPropertiesMode mode) {
569 return getProperties(new Properties(), mode);
570 }
571
572
573
574
575 public static final String getProperty(String key, GlobalPropertiesMode mode) {
576 return getProperty(key, new Properties(), mode);
577 }
578
579
580
581
582
583 public static final String getProperty(String key, Properties properties, GlobalPropertiesMode mode) {
584 return getProperties(properties, mode).getProperty(key);
585 }
586
587
588
589
590 public static final List<PropertyProcessor> getPropertyProcessors(GlobalPropertiesMode mode) {
591 List<PropertyProcessor> processors = new ArrayList<PropertyProcessor>();
592 switch (mode) {
593 case NONE:
594 return processors;
595 case ENVIRONMENT:
596 processors.add(new AddPropertiesProcessor(getEnvAsProperties()));
597 return processors;
598 case SYSTEM:
599 processors.add(new AddPropertiesProcessor(System.getProperties()));
600 return processors;
601 case BOTH:
602 processors.add(new AddPropertiesProcessor(getEnvAsProperties()));
603 processors.add(new AddPropertiesProcessor(System.getProperties()));
604 return processors;
605 default:
606 throw new IllegalStateException(mode + " is unknown");
607 }
608 }
609
610
611
612
613 public static final Properties convert(Map<String, String> map) {
614 Properties props = new Properties();
615 for (String key : map.keySet()) {
616 String value = map.get(key);
617 props.setProperty(key, value);
618 }
619 return props;
620 }
621
622
623
624
625 public static final Properties duplicate(Properties properties) {
626 Properties newProperties = new Properties();
627 newProperties.putAll(properties);
628 return newProperties;
629 }
630
631
632
633
634 public static Properties getEnvAsProperties() {
635 return getEnvAsProperties(ENV_PREFIX);
636 }
637
638
639
640
641 public static Properties getEnvAsProperties(String prefix) {
642 Properties properties = convert(System.getenv());
643 return getPrefixedProperties(properties, prefix);
644 }
645
646
647
648
649 public static final boolean isXml(String location) {
650 return StringUtils.endsWithIgnoreCase(location, XML_EXTENSION);
651 }
652
653
654
655
656 public static final Properties load(File file) {
657 return load(file, null);
658 }
659
660
661
662
663 public static final Properties load(File file, String encoding) {
664 String location = LocationUtils.getCanonicalPath(file);
665 return load(location, encoding);
666 }
667
668
669
670
671 public static final Properties load(String location) {
672 return load(location, null);
673 }
674
675
676
677
678 public static final Properties load(String location, String encoding) {
679 InputStream in = null;
680 Reader reader = null;
681 try {
682 Properties properties = new Properties();
683 boolean xml = isXml(location);
684 location = getCanonicalLocation(location);
685 if (xml) {
686 in = LocationUtils.getInputStream(location);
687 logger.info("Loading XML properties - [{}]", location);
688 properties.loadFromXML(in);
689 } else {
690 logger.info("Loading properties - [{}] encoding={}", location, StringUtils.defaultIfBlank(encoding, DEFAULT_ENCODING));
691 reader = LocationUtils.getBufferedReader(location, encoding);
692 properties.load(reader);
693 }
694 return properties;
695 } catch (IOException e) {
696 throw new IllegalStateException("Unexpected IO error", e);
697 } finally {
698 IOUtils.closeQuietly(in);
699 IOUtils.closeQuietly(reader);
700 }
701 }
702
703 protected static String getCanonicalLocation(String location) {
704 if (LocationUtils.isExistingFile(location)) {
705 return LocationUtils.getCanonicalPath(new File(location));
706 } else {
707 return location;
708 }
709 }
710
711
712
713
714
715 public static final Properties getPrefixedProperties(Properties properties, String prefix) {
716 if (StringUtils.isBlank(prefix)) {
717 return duplicate(properties);
718 }
719 Properties newProperties = new Properties();
720 for (String key : properties.stringPropertyNames()) {
721 String value = properties.getProperty(key);
722 String newKey = StringUtils.startsWith(key, prefix + ".") ? key : prefix + "." + key;
723 newProperties.setProperty(newKey, value);
724 }
725 return newProperties;
726 }
727
728
729
730
731 public static final Properties reformatKeysAsEnvVars(Properties properties) {
732 Properties newProperties = new Properties();
733 for (String key : properties.stringPropertyNames()) {
734 String value = properties.getProperty(key);
735 String newKey = StringUtils.upperCase(StringUtils.replace(key, ".", "-"));
736 newProperties.setProperty(newKey, value);
737 }
738 return newProperties;
739 }
740
741
742
743
744
745 public static final void addOrOverrideProperty(Properties properties, String key, String newValue, Mode propertyOverwriteMode) {
746 String oldValue = properties.getProperty(key);
747 if (StringUtils.equals(newValue, oldValue)) {
748
749 return;
750 }
751 boolean overwrite = !StringUtils.isBlank(oldValue);
752
753
754 String logNewValue = newValue;
755 String logOldValue = oldValue;
756 if (obscure(key)) {
757 logNewValue = "PROTECTED";
758 logOldValue = "PROTECTED";
759 }
760
761 if (overwrite) {
762
763
764 Object[] args = new Object[] { key, Str.flatten(logNewValue), Str.flatten(logOldValue) };
765 ModeUtils.validate(propertyOverwriteMode, "Overriding [{}={}] was [{}]", args, "Override of existing property [" + key + "] is not allowed.");
766 } else {
767
768 logger.info("Adding [{}={}]", key, Str.flatten(logNewValue));
769 }
770 properties.setProperty(key, newValue);
771 }
772
773 protected static boolean obscure(String key) {
774 if (StringUtils.containsIgnoreCase(key, ".password")) {
775 return true;
776 }
777 if (StringUtils.containsIgnoreCase(key, ".secret")) {
778 return true;
779 }
780 if (StringUtils.containsIgnoreCase(key, ".private")) {
781 return true;
782 }
783 return false;
784 }
785
786 private static final String getDefaultComment(String encoding, boolean xml) {
787 if (encoding == null) {
788 if (xml) {
789
790 return "encoding.default=" + DEFAULT_XML_ENCODING;
791 } else {
792
793 return "encoding.default=" + DEFAULT_ENCODING;
794 }
795 } else {
796 return "encoding.specified=" + encoding;
797 }
798 }
799
800 private static final String getComment(String encoding, String comment, boolean xml) {
801 if (StringUtils.isBlank(comment)) {
802 return getDefaultComment(encoding, xml);
803 } else {
804 return comment + "\n#" + getDefaultComment(encoding, xml);
805 }
806 }
807
808
809
810
811 private static final SortedProperties getSortedProperties(Properties properties) {
812 SortedProperties sp = new PropertyUtils().new SortedProperties();
813 sp.putAll(properties);
814 return sp;
815 }
816
817
818
819
820 private class SortedProperties extends Properties {
821
822 private static final long serialVersionUID = 1330825236411537386L;
823
824
825
826
827 @Override
828 public Set<Object> keySet() {
829 return Collections.unmodifiableSet(new TreeSet<Object>(super.keySet()));
830 }
831
832
833
834
835 @Override
836 public synchronized Enumeration<Object> keys() {
837 return Collections.enumeration(new TreeSet<Object>(super.keySet()));
838 }
839 }
840
841
842
843
844
845
846
847
848
849
850
851
852 public static final void addListComparisonProperties(Properties properties, ComparisonResults listComparison, List<String> propertyNames) {
853
854 Assert.isTrue(propertyNames.size() == 3);
855
856 properties.setProperty(propertyNames.get(0), CollectionUtils.getCSV(listComparison.getAdded()));
857 properties.setProperty(propertyNames.get(1), CollectionUtils.getCSV(listComparison.getSame()));
858 properties.setProperty(propertyNames.get(2), CollectionUtils.getCSV(listComparison.getDeleted()));
859 }
860
861 }