KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > PropertyResourceConfigurer


1 /*
2  * Copyright 2002-2006 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 JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.Properties JavaDoc;
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.io.support.PropertiesLoaderSupport;
27 import org.springframework.util.ObjectUtils;
28
29 /**
30  * Allows for configuration of individual bean property values from a property resource,
31  * i.e. a properties file. Useful for custom config files targetted at system
32  * administrators that override bean properties configured in the application context.
33  *
34  * <p>2 concrete implementations are provided in the distribution:
35  * <ul>
36  * <li>PropertyOverrideConfigurer for "beanName.property=value" style overriding
37  * (<i>pushing</i> values from a properties file into bean definitions)
38  * <li>PropertyPlaceholderConfigurer for replacing "${...}" placeholders
39  * (<i>pulling</i> values from a properties file into bean definitions)
40  * </ul>
41  *
42  * <p>Property values can be converted after reading them in, through overriding
43  * the <code>convertPropertyValue</code> method. For example, encrypted values
44  * can be detected and decrypted accordingly before processing them.
45  *
46  * @author Juergen Hoeller
47  * @since 02.10.2003
48  * @see PropertyOverrideConfigurer
49  * @see PropertyPlaceholderConfigurer
50  * @see #convertPropertyValue
51  */

52 public abstract class PropertyResourceConfigurer extends PropertiesLoaderSupport
53         implements BeanFactoryPostProcessor, Ordered {
54
55     private int order = Ordered.LOWEST_PRECEDENCE; // default: same as non-Ordered
56

57
58     public void setOrder(int order) {
59       this.order = order;
60     }
61
62     public int getOrder() {
63       return order;
64     }
65
66
67     public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
68         try {
69             Properties JavaDoc mergedProps = mergeProperties();
70
71             // Convert the merged properties, if necessary.
72
convertProperties(mergedProps);
73
74             // Let the subclass process the properties.
75
processProperties(beanFactory, mergedProps);
76         }
77         catch (IOException JavaDoc ex) {
78             throw new BeanInitializationException("Could not load properties", ex);
79         }
80     }
81
82     /**
83      * Convert the given merged properties, converting property values
84      * if necessary. The result will then be processed.
85      * <p>Default implementation will invoke <code>convertPropertyValue</code>
86      * for each property value, replacing the original with the converted value.
87      * @see #convertPropertyValue
88      * @see #processProperties
89      */

90     protected void convertProperties(Properties JavaDoc props) {
91         Enumeration JavaDoc propertyNames = props.propertyNames();
92         while (propertyNames.hasMoreElements()) {
93             String JavaDoc propertyName = (String JavaDoc) propertyNames.nextElement();
94             String JavaDoc propertyValue = props.getProperty(propertyName);
95             String JavaDoc convertedValue = convertPropertyValue(propertyValue);
96             if (!ObjectUtils.nullSafeEquals(propertyValue, convertedValue)) {
97                 props.setProperty(propertyName, convertedValue);
98             }
99         }
100     }
101
102     /**
103      * Convert the given property value from the properties source
104      * to the value that should be applied.
105      * <p>Default implementation simply returns the original value.
106      * Can be overridden in subclasses, for example to detect
107      * encrypted values and decrypt them accordingly.
108      * @param originalValue the original value from the properties source
109      * (properties file or local "properties")
110      * @return the converted value, to be used for processing
111      * @see #setProperties
112      * @see #setLocations
113      * @see #setLocation
114      */

115     protected String JavaDoc convertPropertyValue(String JavaDoc originalValue) {
116         return originalValue;
117     }
118
119     /**
120      * Apply the given Properties to the bean factory.
121      * @param beanFactory the bean factory used by the application context
122      * @param props the Properties to apply
123      * @throws org.springframework.beans.BeansException in case of errors
124      */

125     protected abstract void processProperties(ConfigurableListableBeanFactory beanFactory, Properties JavaDoc props)
126             throws BeansException;
127
128 }
129
Popular Tags