1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.property.processor;
17
18 import java.util.List;
19 import java.util.Properties;
20
21 import org.kuali.common.util.PropertyUtils;
22 import org.kuali.common.util.Str;
23 import org.kuali.common.util.property.Constants;
24 import org.kuali.common.util.property.GlobalPropertiesMode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.util.PropertyPlaceholderHelper;
28
29 public class ResolvePlaceholdersProcessor implements PropertyProcessor {
30
31 private static final Logger logger = LoggerFactory.getLogger(ResolvePlaceholdersProcessor.class);
32
33 PropertyPlaceholderHelper helper;
34 GlobalPropertiesMode globalPropertiesMode = GlobalPropertiesMode.BOTH;
35
36 public ResolvePlaceholdersProcessor() {
37 this(new PropertyPlaceholderHelper(Constants.DEFAULT_PLACEHOLDER_PREFIX, Constants.DEFAULT_PLACEHOLDER_SUFFIX));
38 }
39
40 public ResolvePlaceholdersProcessor(PropertyPlaceholderHelper helper) {
41 this(helper, GlobalPropertiesMode.BOTH);
42 }
43
44 public ResolvePlaceholdersProcessor(PropertyPlaceholderHelper helper, GlobalPropertiesMode globalPropertiesMode) {
45 super();
46 this.helper = helper;
47 this.globalPropertiesMode = globalPropertiesMode;
48 }
49
50 @Override
51 public void process(Properties properties) {
52 Properties resolvedProperties = getResolvedProperties(properties, helper);
53 logger.info("Resolved {} property values", resolvedProperties.size());
54 properties.putAll(resolvedProperties);
55 }
56
57 protected Properties getResolvedProperties(Properties props, PropertyPlaceholderHelper helper) {
58 Properties global = PropertyUtils.getProperties(props, globalPropertiesMode);
59 List<String> keys = PropertyUtils.getSortedKeys(props);
60 Properties newProps = new Properties();
61 for (String key : keys) {
62 String originalValue = props.getProperty(key);
63 String resolvedValue = helper.replacePlaceholders(originalValue, global);
64 if (!resolvedValue.equals(originalValue)) {
65 logger.debug("Resolved property '" + key + "' [{}] -> [{}]", Str.flatten(originalValue), Str.flatten(resolvedValue));
66 }
67 newProps.setProperty(key, resolvedValue);
68 }
69 return newProps;
70 }
71
72 public PropertyPlaceholderHelper getHelper() {
73 return helper;
74 }
75
76 public void setHelper(PropertyPlaceholderHelper helper) {
77 this.helper = helper;
78 }
79
80 public GlobalPropertiesMode getGlobalPropertiesMode() {
81 return globalPropertiesMode;
82 }
83
84 public void setGlobalPropertiesMode(GlobalPropertiesMode globalPropertiesMode) {
85 this.globalPropertiesMode = globalPropertiesMode;
86 }
87 }