1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.settings4j.helper.spring;
21
22 import java.util.Properties;
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.settings4j.Settings4j;
26 import org.springframework.beans.factory.BeanDefinitionStoreException;
27 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
28 import org.springframework.util.PropertyPlaceholderHelper;
29 import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
30
31
32
33
34
35
36
37
38
39 public class Settings4jPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
40
41 private String prefix = StringUtils.EMPTY;
42
43 public void setPrefix(final String prefix) {
44 this.prefix = prefix;
45 }
46
47 @Override
48 protected String resolvePlaceholder(final String placeholder, final Properties props) {
49
50 String value = Settings4j.getString(this.prefix + placeholder);
51 if (value == null) {
52 value = props.getProperty(this.prefix + placeholder);
53 if (value == null) {
54 value = props.getProperty(placeholder);
55 }
56 }
57 return value;
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public static String parseStringValue(final String strVal) throws BeanDefinitionStoreException {
73 return parseStringValue(strVal, StringUtils.EMPTY);
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public static String parseStringValue(final String strVal, final String prefix)
91 throws BeanDefinitionStoreException {
92 return parseStringValue(strVal, prefix, new Properties());
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 public static String parseStringValue(final String strVal, final String prefix, final Properties props)
112 throws BeanDefinitionStoreException {
113 final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(DEFAULT_PLACEHOLDER_PREFIX,
114 DEFAULT_PLACEHOLDER_SUFFIX, DEFAULT_VALUE_SEPARATOR, false);
115 return helper.replacePlaceholders(strVal, new Settings4jPlaceholderConfigurerResolver(prefix, props));
116 }
117
118
119
120
121
122
123
124 private static final class Settings4jPlaceholderConfigurerResolver implements PlaceholderResolver {
125
126 private final String prefix;
127 private final Properties props;
128
129
130 public Settings4jPlaceholderConfigurerResolver(final String prefix, final Properties props) {
131 this.prefix = prefix;
132 this.props = props;
133 }
134
135 @Override
136 public String resolvePlaceholder(final String placeholderName) {
137 final Settings4jPlaceholderConfigurer placeholderConfigurer = new Settings4jPlaceholderConfigurer();
138 placeholderConfigurer.setPrefix(this.prefix);
139 return placeholderConfigurer.resolvePlaceholder(placeholderName, this.props);
140 }
141 }
142 }