KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2005 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.util.Collections JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.springframework.beans.BeansException;
26 import org.springframework.beans.factory.BeanInitializationException;
27
28 /**
29  * A property resource configurer that overrides bean property values in an application
30  * context definition. It <i>pushes</i> values from a properties file into bean definitions.
31  *
32  * <p>Configuration lines are expected to be of the following form:
33  *
34  * <pre class="code">beanName.property=value</pre>
35  *
36  * Example properties file:
37  *
38  * <pre class="code">dataSource.driverClassName=com.mysql.jdbc.Driver
39  * dataSource.url=jdbc:mysql:mydb</pre>
40  *
41  * In contrast to PropertyPlaceholderConfigurer, the original definition can have default
42  * values or no values at all for such bean properties. If an overriding properties file does
43  * not have an entry for a certain bean property, the default context definition is used.
44  *
45  * <p>Note that the context definition <i>is not</i> aware of being overridden;
46  * so this is not immediately obvious when looking at the XML definition file.
47  *
48  * <p>In case of multiple PropertyOverrideConfigurers that define different values for
49  * the same bean property, the <i>last</i> one will win (due to the overriding mechanism).
50  *
51  * <p>Property values can be converted after reading them in, through overriding
52  * the <code>convertPropertyValue</code> method. For example, encrypted values
53  * can be detected and decrypted accordingly before processing them.
54  *
55  * @author Juergen Hoeller
56  * @author Rod Johnson
57  * @since 12.03.2003
58  * @see #convertPropertyValue
59  * @see PropertyPlaceholderConfigurer
60  */

61 public class PropertyOverrideConfigurer extends PropertyResourceConfigurer {
62
63     public static final String JavaDoc DEFAULT_BEAN_NAME_SEPARATOR = ".";
64
65
66     private String JavaDoc beanNameSeparator = DEFAULT_BEAN_NAME_SEPARATOR;
67
68     private boolean ignoreInvalidKeys = false;
69
70     /** Contains names of beans that have overrides */
71     private Set JavaDoc beanNames = Collections.synchronizedSet(new HashSet JavaDoc());
72
73
74     /**
75      * Set the separator to expect between bean name and property path.
76      * Default is a dot (".").
77      */

78     public void setBeanNameSeparator(String JavaDoc beanNameSeparator) {
79         this.beanNameSeparator = beanNameSeparator;
80     }
81
82     /**
83      * Set whether to ignore invalid keys. Default is "false".
84      * <p>If you ignore invalid keys, keys that do not follow the
85      * 'beanName.property' format will just be logged as warning.
86      * This allows to have arbitrary other keys in a properties file.
87      */

88     public void setIgnoreInvalidKeys(boolean ignoreInvalidKeys) {
89         this.ignoreInvalidKeys = ignoreInvalidKeys;
90     }
91
92
93     protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties JavaDoc props)
94             throws BeansException {
95
96         for (Enumeration JavaDoc names = props.propertyNames(); names.hasMoreElements();) {
97             String JavaDoc key = (String JavaDoc) names.nextElement();
98             try {
99                 processKey(beanFactory, key, props.getProperty(key));
100             }
101             catch (BeansException ex) {
102                 String JavaDoc msg = "Could not process key '" + key + "' in PropertyOverrideConfigurer";
103                 if (!this.ignoreInvalidKeys) {
104                     throw new BeanInitializationException(msg, ex);
105                 }
106                 if (logger.isDebugEnabled()) {
107                     logger.debug(msg, ex);
108                 }
109             }
110         }
111     }
112
113     /**
114      * Process the given key as 'beanName.property' entry.
115      */

116     protected void processKey(ConfigurableListableBeanFactory factory, String JavaDoc key, String JavaDoc value)
117             throws BeansException {
118
119         int separatorIndex = key.indexOf(this.beanNameSeparator);
120         if (separatorIndex == -1) {
121             throw new BeanInitializationException("Invalid key '" + key +
122                     "': expected 'beanName" + this.beanNameSeparator + "property'");
123         }
124         String JavaDoc beanName = key.substring(0, separatorIndex);
125         String JavaDoc beanProperty = key.substring(separatorIndex+1);
126         this.beanNames.add(beanName);
127         applyPropertyValue(factory, beanName, beanProperty, value);
128         if (logger.isDebugEnabled()) {
129             logger.debug("Property '" + key + "' set to value [" + value + "]");
130         }
131     }
132
133     /**
134      * Apply the given property value to the corresponding bean.
135      */

136     protected void applyPropertyValue(
137         ConfigurableListableBeanFactory factory, String JavaDoc beanName, String JavaDoc property, String JavaDoc value) {
138
139         BeanDefinition bd = factory.getBeanDefinition(beanName);
140         bd.getPropertyValues().addPropertyValue(property, value);
141     }
142
143
144     /**
145      * Were there overrides for this bean?
146      * Only valid after processing has occurred at least once.
147      * @param beanName name of the bean to query status for
148      * @return whether there were property overrides for
149      * the named bean
150      */

151     public boolean hasPropertyOverridesFor(String JavaDoc beanName) {
152         return this.beanNames.contains(beanName);
153     }
154
155 }
156
Popular Tags