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