KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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.HashMap JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.springframework.beans.MutablePropertyValues;
27 import org.springframework.beans.PropertyValue;
28 import org.springframework.util.ObjectUtils;
29
30 /**
31  * Visitor base class for traversing {@link BeanDefinition} objects, in particular
32  * the property values and constructor argument values contained in them.
33  *
34  * <p>The abstract {@link #resolveStringValue} method has to be implemented
35  * in concrete subclasses, following arbitrary resolution strategies.
36  *
37  * <p>Used by {@link PropertyPlaceholderConfigurer} to parse all String values
38  * contained in a BeanDefinition, resolving any placeholders found.
39  *
40  * @author Juergen Hoeller
41  * @since 1.2
42  * @see BeanDefinition
43  * @see BeanDefinition#getPropertyValues
44  * @see BeanDefinition#getConstructorArgumentValues
45  * @see #resolveStringValue(String)
46  * @see PropertyPlaceholderConfigurer
47  */

48 public abstract class BeanDefinitionVisitor {
49
50     /**
51      * Traverse the given BeanDefinition object and the MutablePropertyValues
52      * and ConstructorArgumentValues contained in them.
53      * @param beanDefinition the BeanDefinition object to traverse
54      * @see #resolveStringValue(String)
55      */

56     public void visitBeanDefinition(BeanDefinition beanDefinition) {
57         visitBeanClassName(beanDefinition);
58         visitScope(beanDefinition);
59         visitPropertyValues(beanDefinition.getPropertyValues());
60         ConstructorArgumentValues cas = beanDefinition.getConstructorArgumentValues();
61         visitIndexedArgumentValues(cas.getIndexedArgumentValues());
62         visitGenericArgumentValues(cas.getGenericArgumentValues());
63     }
64
65     protected void visitBeanClassName(BeanDefinition beanDefinition) {
66         String JavaDoc beanClassName = beanDefinition.getBeanClassName();
67         if (beanClassName != null) {
68             String JavaDoc resolvedName = resolveStringValue(beanClassName);
69             if (!beanClassName.equals(resolvedName)) {
70                 beanDefinition.setBeanClassName(resolvedName);
71             }
72         }
73     }
74
75     protected void visitScope(BeanDefinition beanDefinition) {
76         String JavaDoc scope = beanDefinition.getScope();
77         if (scope != null) {
78             String JavaDoc resolvedScope = resolveStringValue(scope);
79             if (!scope.equals(resolvedScope)) {
80                 beanDefinition.setScope(resolvedScope);
81             }
82         }
83     }
84
85     protected void visitPropertyValues(MutablePropertyValues pvs) {
86         PropertyValue[] pvArray = pvs.getPropertyValues();
87         for (int i = 0; i < pvArray.length; i++) {
88             PropertyValue pv = pvArray[i];
89             Object JavaDoc newVal = resolveValue(pv.getValue());
90             if (!ObjectUtils.nullSafeEquals(newVal, pv.getValue())) {
91                 pvs.addPropertyValue(pv.getName(), newVal);
92             }
93         }
94     }
95
96     protected void visitIndexedArgumentValues(Map JavaDoc ias) {
97         for (Iterator JavaDoc it = ias.values().iterator(); it.hasNext();) {
98             ConstructorArgumentValues.ValueHolder valueHolder =
99                     (ConstructorArgumentValues.ValueHolder) it.next();
100             Object JavaDoc newVal = resolveValue(valueHolder.getValue());
101             if (!ObjectUtils.nullSafeEquals(newVal, valueHolder.getValue())) {
102                 valueHolder.setValue(newVal);
103             }
104         }
105     }
106
107     protected void visitGenericArgumentValues(List JavaDoc gas) {
108         for (Iterator JavaDoc it = gas.iterator(); it.hasNext();) {
109             ConstructorArgumentValues.ValueHolder valueHolder =
110                     (ConstructorArgumentValues.ValueHolder) it.next();
111             Object JavaDoc newVal = resolveValue(valueHolder.getValue());
112             if (!ObjectUtils.nullSafeEquals(newVal, valueHolder.getValue())) {
113                 valueHolder.setValue(newVal);
114             }
115         }
116     }
117
118     protected Object JavaDoc resolveValue(Object JavaDoc value) {
119         if (value instanceof BeanDefinition) {
120             visitBeanDefinition((BeanDefinition) value);
121         }
122         else if (value instanceof BeanDefinitionHolder) {
123             visitBeanDefinition(((BeanDefinitionHolder) value).getBeanDefinition());
124         }
125         else if (value instanceof RuntimeBeanReference) {
126       RuntimeBeanReference ref = (RuntimeBeanReference) value;
127       String JavaDoc newBeanName = resolveStringValue(ref.getBeanName());
128             if (!newBeanName.equals(ref.getBeanName())) {
129                 return new RuntimeBeanReference(newBeanName);
130             }
131         }
132         else if (value instanceof List JavaDoc) {
133             visitList((List JavaDoc) value);
134         }
135         else if (value instanceof Set JavaDoc) {
136             visitSet((Set JavaDoc) value);
137         }
138         else if (value instanceof Map JavaDoc) {
139             visitMap((Map JavaDoc) value);
140         }
141         else if (value instanceof TypedStringValue) {
142             TypedStringValue typedStringValue = (TypedStringValue) value;
143             String JavaDoc stringValue = typedStringValue.getValue();
144             if (stringValue != null) {
145                 String JavaDoc visitedString = resolveStringValue(stringValue);
146                 typedStringValue.setValue(visitedString);
147             }
148         }
149         else if (value instanceof String JavaDoc) {
150             return resolveStringValue((String JavaDoc) value);
151         }
152         return value;
153     }
154
155     protected void visitList(List JavaDoc listVal) {
156         for (int i = 0; i < listVal.size(); i++) {
157             Object JavaDoc elem = listVal.get(i);
158             Object JavaDoc newVal = resolveValue(elem);
159             if (!ObjectUtils.nullSafeEquals(newVal, elem)) {
160                 listVal.set(i, newVal);
161             }
162         }
163     }
164
165     protected void visitSet(Set JavaDoc setVal) {
166         for (Iterator JavaDoc it = new HashSet JavaDoc(setVal).iterator(); it.hasNext();) {
167             Object JavaDoc elem = it.next();
168             Object JavaDoc newVal = resolveValue(elem);
169             if (!ObjectUtils.nullSafeEquals(newVal, elem)) {
170                 setVal.remove(elem);
171                 setVal.add(newVal);
172             }
173         }
174     }
175
176     protected void visitMap(Map JavaDoc mapVal) {
177         for (Iterator JavaDoc it = new HashMap JavaDoc(mapVal).entrySet().iterator(); it.hasNext();) {
178             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
179             Object JavaDoc key = entry.getKey();
180             Object JavaDoc newKey = resolveValue(key);
181             boolean isNewKey = !ObjectUtils.nullSafeEquals(key, newKey);
182             Object JavaDoc val = entry.getValue();
183             Object JavaDoc newVal = resolveValue(val);
184             if (isNewKey) {
185                 mapVal.remove(key);
186             }
187             if (isNewKey || !ObjectUtils.nullSafeEquals(newVal, val)) {
188                 mapVal.put(newKey, newVal);
189             }
190         }
191     }
192
193     /**
194      * Resolve the given String value, for example parsing placeholders.
195      * @param strVal the original String value
196      * @return the resolved String value
197      */

198     protected abstract String JavaDoc resolveStringValue(String JavaDoc strVal);
199
200 }
201
Popular Tags