01: /* ***** BEGIN LICENSE BLOCK *****
02: * Version: MPL 1.1
03: * The contents of this file are subject to the Mozilla Public License Version
04: * 1.1 (the "License"); you may not use this file except in compliance with
05: * the License. You may obtain a copy of the License at
06: * http://www.mozilla.org/MPL/
07: *
08: * Software distributed under the License is distributed on an "AS IS" basis,
09: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10: * for the specific language governing rights and limitations under the
11: * License.
12: *
13: * The Original Code is Riot.
14: *
15: * The Initial Developer of the Original Code is
16: * Neteye GmbH.
17: * Portions created by the Initial Developer are Copyright (C) 2006
18: * the Initial Developer. All Rights Reserved.
19: *
20: * Contributor(s):
21: * Felix Gnass [fgnass at neteye dot de]
22: *
23: * ***** END LICENSE BLOCK ***** */
24: package org.riotfamily.common.beans.config;
25:
26: import java.util.Enumeration;
27: import java.util.Properties;
28:
29: import org.springframework.beans.propertyeditors.PropertiesEditor;
30: import org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer;
31:
32: /**
33: * PropertyPlaceholderConfigurer that accepts wildcards to populate properties
34: * that expect a java.util.Properties value.
35: * <p>
36: * Example:
37: * <pre>
38: * <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
39: * <property name="freemarkerSettings" ref="${freemarker.*}" />
40: * </bean>
41: * </pre>
42: *
43: * The configurer will look for all properties start start with
44: * '<code>freemarker.</code>'. So having a properties file like this ...
45: *
46: * <pre>
47: * freemarker.number_format = 0.######
48: * freemarker.locale = en_US
49: * </pre>
50: *
51: * ... would be equivalent to writing:
52: * <pre>
53: * <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
54: * <property name="freemarkerSettings">
55: * <value>
56: * number_format = 0.######
57: * locale = en_US
58: * </value>
59: * </property>
60: * </bean>
61: * </pre>
62: *
63: * Spring's {@link PropertiesEditor} will then take care of converting the
64: * String value into a <code>java.util.Properties</code> object.
65: *
66: * @author Felix Gnass [fgnass at neteye dot de]
67: * @since 6.4
68: */
69: public class PropertiesPlaceholderConfigurer extends
70: ServletContextPropertyPlaceholderConfigurer {
71:
72: protected String resolvePlaceholder(String placeholder,
73: Properties props) {
74: int i = placeholder.indexOf('*');
75: if (i != -1) {
76: return resolveAll(props, placeholder.substring(0, i));
77: }
78: return super .resolvePlaceholder(placeholder, props);
79: }
80:
81: protected String resolveAll(Properties props, String prefix) {
82: StringBuffer sb = new StringBuffer();
83: Enumeration names = props.propertyNames();
84: while (names.hasMoreElements()) {
85: String name = (String) names.nextElement();
86: if (name.startsWith(prefix)
87: && name.length() > prefix.length()) {
88: sb.append(name.substring(prefix.length()));
89: sb.append('=');
90: sb.append(props.getProperty(name));
91: sb.append('\n');
92: }
93: }
94: return sb.toString();
95: }
96: }
|