KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyEditor JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.springframework.beans.BeansException;
24 import org.springframework.beans.PropertyEditorRegistrar;
25 import org.springframework.beans.factory.BeanClassLoaderAware;
26 import org.springframework.core.Ordered;
27 import org.springframework.util.ClassUtils;
28
29 /**
30  * {@link BeanFactoryPostProcessor} implementation that allows for convenient
31  * registration of custom {@link PropertyEditor property editors}.
32  *
33  * <p>As of Spring 2.0, the recommended usage is to use custom
34  * {@link PropertyEditorRegistrar} implementations that in turn register
35  * any desired editors on a given
36  * {@link org.springframework.beans.PropertyEditorRegistry registry}.
37  * Each PropertyEditorRegistrar can register any number of custom editors.
38  *
39  * <pre class="code">
40  * &lt;bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"&gt;
41  * &lt;property name="propertyEditorRegistrars"&gt;
42  * &lt;list&gt;
43  * &lt;bean class="mypackage.MyCustomDateEditorRegistrar"/&gt;
44  * &lt;bean class="mypackage.MyObjectEditorRegistrar"/&gt;
45  * &lt;/list&gt;
46  * &lt;/property&gt;
47  * &lt;/bean&gt;</pre>
48  *
49  * <p>Alternative configuration example with custom editor instances,
50  * assuming inner beans for <code>PropertyEditor</code> instances:
51  *
52  * <pre class="code">
53  * &lt;bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"&gt;
54  * &lt;property name="customEditors"&gt;
55  * &lt;map&gt;
56  * &lt;entry key="java.util.Date"&gt;
57  * &lt;bean class="mypackage.MyCustomDateEditor"/&gt;
58  * &lt;/entry&gt;
59  * &lt;entry key="mypackage.MyObject"&gt;
60  * &lt;bean id="myEditor" class="mypackage.MyObjectEditor"&gt;
61  * &lt;property name="myParam"&gt;&lt;value&gt;myValue&lt;/value&gt;&lt;/property&gt;
62  * &lt;/bean&gt;
63  * &lt;/entry&gt;
64  * &lt;/map&gt;
65  * &lt;/property&gt;
66  * &lt;/bean&gt;</pre>
67  *
68  * <p>Also supports "java.lang.String[]"-style array class names and primitive
69  * class names (e.g. "boolean"). Delegates to {@link ClassUtils} for actual
70  * class name resolution.
71  *
72  * <p><b>NOTE:</b> Custom property editors registered with this configurer do
73  * <i>not</i> apply to data binding. Custom editors for data binding need to
74  * be registered on the {@link org.springframework.validation.DataBinder}:
75  * Use a common base class or delegate to common PropertyEditorRegistrar
76  * implementations to reuse editor registration there.
77  *
78  * @author Juergen Hoeller
79  * @since 27.02.2004
80  * @see java.beans.PropertyEditor
81  * @see org.springframework.beans.PropertyEditorRegistrar
82  * @see ConfigurableBeanFactory#addPropertyEditorRegistrar
83  * @see ConfigurableBeanFactory#registerCustomEditor
84  * @see org.springframework.validation.DataBinder#registerCustomEditor
85  * @see org.springframework.web.servlet.mvc.BaseCommandController#setPropertyEditorRegistrars
86  * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder
87  */

88 public class CustomEditorConfigurer implements BeanFactoryPostProcessor, BeanClassLoaderAware, Ordered {
89
90     private int order = Ordered.LOWEST_PRECEDENCE; // default: same as non-Ordered
91

92     private PropertyEditorRegistrar[] propertyEditorRegistrars;
93
94     private Map JavaDoc customEditors;
95
96     private ClassLoader JavaDoc beanClassLoader = ClassUtils.getDefaultClassLoader();
97
98
99     public void setOrder(int order) {
100       this.order = order;
101     }
102
103     public int getOrder() {
104       return this.order;
105     }
106
107     /**
108      * Specify the {@link PropertyEditorRegistrar PropertyEditorRegistrars}
109      * to apply to beans defined within the current application context.
110      * <p>This allows for sharing <code>PropertyEditorRegistrars</code> with
111      * {@link org.springframework.validation.DataBinder DataBinders}, etc.
112      * Furthermore, it avoids the need for synchronization on custom editors:
113      * A <code>PropertyEditorRegistrar</code> will always create fresh editor
114      * instances for each bean creation attempt.
115      * @see ConfigurableListableBeanFactory#addPropertyEditorRegistrar
116      */

117     public void setPropertyEditorRegistrars(PropertyEditorRegistrar[] propertyEditorRegistrars) {
118         this.propertyEditorRegistrars = propertyEditorRegistrars;
119     }
120
121     /**
122      * Specify the custom editors to register via a {@link Map}, using the
123      * class name of the required type as the key and the {@link PropertyEditor}
124      * instance as the value.
125      * @param customEditors said <code>Map</code> of editors (can be <code>null</code>)
126      * @see ConfigurableListableBeanFactory#registerCustomEditor
127      */

128     public void setCustomEditors(Map JavaDoc customEditors) {
129         this.customEditors = customEditors;
130     }
131
132     public void setBeanClassLoader(ClassLoader JavaDoc beanClassLoader) {
133         this.beanClassLoader = beanClassLoader;
134     }
135
136
137     public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
138         if (this.propertyEditorRegistrars != null) {
139             for (int i = 0; i < this.propertyEditorRegistrars.length; i++) {
140                 beanFactory.addPropertyEditorRegistrar(this.propertyEditorRegistrars[i]);
141             }
142         }
143
144         if (this.customEditors != null) {
145             for (Iterator JavaDoc it = this.customEditors.entrySet().iterator(); it.hasNext();) {
146                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
147                 Object JavaDoc key = entry.getKey();
148                 Class JavaDoc requiredType = null;
149                 if (key instanceof Class JavaDoc) {
150                     requiredType = (Class JavaDoc) key;
151                 }
152                 else if (key instanceof String JavaDoc) {
153                     String JavaDoc className = (String JavaDoc) key;
154                     requiredType = ClassUtils.resolveClassName(className, this.beanClassLoader);
155                 }
156                 else {
157                     throw new IllegalArgumentException JavaDoc(
158                             "Invalid key [" + key + "] for custom editor: needs to be Class or String.");
159                 }
160                 Object JavaDoc value = entry.getValue();
161                 if (!(value instanceof PropertyEditor JavaDoc)) {
162                     throw new IllegalArgumentException JavaDoc("Mapped value [" + value + "] for custom editor key [" +
163                             key + "] is not of required type [" + PropertyEditor JavaDoc.class.getName() + "]");
164                 }
165                 beanFactory.registerCustomEditor(requiredType, (PropertyEditor JavaDoc) value);
166             }
167         }
168     }
169
170 }
171
Popular Tags