View Javadoc
1   /*
2    * Copyright 2002-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.beans.factory.config;
18  
19  import java.io.IOException;
20  import java.util.Enumeration;
21  import java.util.Properties;
22  
23  import org.springframework.beans.BeansException;
24  import org.springframework.beans.factory.BeanInitializationException;
25  import org.springframework.core.Ordered;
26  import org.springframework.core.PriorityOrdered;
27  import org.springframework.core.io.support.PropertiesLoaderSupport;
28  import org.springframework.util.ObjectUtils;
29  
30  /**
31   * Allows for configuration of individual bean property values from a property resource,
32   * i.e. a properties file. Useful for custom config files targeted at system
33   * administrators that override bean properties configured in the application context.
34   *
35   * <p>Two concrete implementations are provided in the distribution:
36   * <ul>
37   * <li>{@link PropertyOverrideConfigurer} for "beanName.property=value" style overriding
38   * (<i>pushing</i> values from a properties file into bean definitions)
39   * <li>{@link PropertyPlaceholderConfigurer} for replacing "${...}" placeholders
40   * (<i>pulling</i> values from a properties file into bean definitions)
41   * </ul>
42   *
43   * <p>Property values can be converted after reading them in, through overriding
44   * the {@link #convertPropertyValue} method. For example, encrypted values
45   * can be detected and decrypted accordingly before processing them.
46   *
47   * @author Juergen Hoeller
48   * @since 02.10.2003
49   * @see PropertyOverrideConfigurer
50   * @see PropertyPlaceholderConfigurer
51   */
52  public abstract class PropertyResourceConfigurer extends PropertiesLoaderSupport
53  		implements BeanFactoryPostProcessor, PriorityOrdered {
54  
55  	private int order = Ordered.LOWEST_PRECEDENCE;  // default: same as non-Ordered
56  
57  
58  	/**
59  	 * Set the order value of this object for sorting purposes.
60  	 * @see PriorityOrdered
61  	 */
62  	public void setOrder(int order) {
63  		this.order = order;
64  	}
65  
66  	@Override
67  	public int getOrder() {
68  		return this.order;
69  	}
70  
71  
72  	/**
73  	 * {@linkplain #mergeProperties Merge}, {@linkplain #convertProperties convert} and
74  	 * {@linkplain #processProperties process} properties against the given bean factory.
75  	 * @throws BeanInitializationException if any properties cannot be loaded
76  	 */
77  	@Override
78  	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
79  		try {
80  			Properties mergedProps = mergeProperties();
81  
82  			// Convert the merged properties, if necessary.
83  			convertProperties(mergedProps);
84  
85  			// Let the subclass process the properties.
86  			processProperties(beanFactory, mergedProps);
87  		}
88  		catch (IOException ex) {
89  			throw new BeanInitializationException("Could not load properties", ex);
90  		}
91  	}
92  
93  	/**
94  	 * Convert the given merged properties, converting property values
95  	 * if necessary. The result will then be processed.
96  	 * <p>The default implementation will invoke {@link #convertPropertyValue}
97  	 * for each property value, replacing the original with the converted value.
98  	 * @param props the Properties to convert
99  	 * @see #processProperties
100 	 */
101 	protected void convertProperties(Properties props) {
102 		Enumeration<?> propertyNames = props.propertyNames();
103 		while (propertyNames.hasMoreElements()) {
104 			String propertyName = (String) propertyNames.nextElement();
105 			String propertyValue = props.getProperty(propertyName);
106 			String convertedValue = convertProperty(propertyName, propertyValue);
107 			if (!ObjectUtils.nullSafeEquals(propertyValue, convertedValue)) {
108 				props.setProperty(propertyName, convertedValue);
109 			}
110 		}
111 	}
112 
113 	/**
114 	 * Convert the given property from the properties source to the value
115 	 * which should be applied.
116 	 * <p>The default implementation calls {@link #convertPropertyValue(String)}.
117 	 * @param propertyName the name of the property that the value is defined for
118 	 * @param propertyValue the original value from the properties source
119 	 * @return the converted value, to be used for processing
120 	 * @see #convertPropertyValue(String)
121 	 */
122 	protected String convertProperty(String propertyName, String propertyValue) {
123 		return convertPropertyValue(propertyValue);
124 	}
125 
126 	/**
127 	 * Convert the given property value from the properties source to the value
128 	 * which should be applied.
129 	 * <p>The default implementation simply returns the original value.
130 	 * Can be overridden in subclasses, for example to detect
131 	 * encrypted values and decrypt them accordingly.
132 	 * @param originalValue the original value from the properties source
133 	 * (properties file or local "properties")
134 	 * @return the converted value, to be used for processing
135 	 * @see #setProperties
136 	 * @see #setLocations
137 	 * @see #setLocation
138 	 * @see #convertProperty(String, String)
139 	 */
140 	protected String convertPropertyValue(String originalValue) {
141 		return originalValue;
142 	}
143 
144 
145 	/**
146 	 * Apply the given Properties to the given BeanFactory.
147 	 * @param beanFactory the BeanFactory used by the application context
148 	 * @param props the Properties to apply
149 	 * @throws org.springframework.beans.BeansException in case of errors
150 	 */
151 	protected abstract void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
152 			throws BeansException;
153 
154 }