1 /*
2 * #%L
3 * settings4j
4 * ===============================================================
5 * Copyright (C) 2008 - 2015 Brabenetz Harald, Austria
6 * ===============================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * #L%
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 * Subclass of PropertyPlaceholderConfigurer uses the Settings4j API.
33 * <p>
34 * You can also configure a Prefix if
35 * </p>
36 *
37 * @see Settings4j#getString(String)
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 * Parse the given String with Placeholder "${...}" and returns the result.
62 * <p>
63 * Placeholders will be resolved with Settings4j.
64 * </p>
65 *
66 * @param strVal
67 * the String with the Paceholders
68 * @return the parsed String
69 * @throws BeanDefinitionStoreException
70 * if invalid values are encountered (Placeholders where no values where found).
71 */
72 public static String parseStringValue(final String strVal) throws BeanDefinitionStoreException {
73 return parseStringValue(strVal, StringUtils.EMPTY);
74 }
75
76 /**
77 * Parse the given String with Placeholder "${...}" and returns the result.
78 * <p>
79 * Placeholders will be resolved with Settings4j.
80 * </p>
81 *
82 * @param strVal
83 * the String with the Paceholders
84 * @param prefix
85 * for all placehodlers.
86 * @return the parsed String
87 * @throws BeanDefinitionStoreException
88 * if invalid values are encountered (Placeholders where no values where found).
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 * Parse the given String with Placeholder "${...}" and returns the result.
97 * <p>
98 * Placeholders will be resolved with Settings4j.
99 * </p>
100 *
101 * @param strVal
102 * the String with the Paceholders
103 * @param prefix
104 * for all placehodlers.
105 * @param props
106 * The default Properties if no Value where found
107 * @return the parsed String
108 * @throws BeanDefinitionStoreException
109 * if invalid values are encountered (Placeholders where no values where found).
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 * PlaceholderResolver implementation can be used for
120 * {@link PropertyPlaceholderHelper#replacePlaceholders(String, PlaceholderResolver)}.
121 *
122 * @author brabenetz
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 }