Source Code Cross Referenced for CustomEditorConfigurer.java in  » J2EE » spring-framework-2.5 » org » springframework » beans » factory » config » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » spring framework 2.5 » org.springframework.beans.factory.config 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002-2007 the original author or authors.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.springframework.beans.factory.config;
018:
019:        import java.beans.PropertyEditor;
020:        import java.util.Iterator;
021:        import java.util.Map;
022:
023:        import org.springframework.beans.BeansException;
024:        import org.springframework.beans.PropertyEditorRegistrar;
025:        import org.springframework.beans.factory.BeanClassLoaderAware;
026:        import org.springframework.core.Ordered;
027:        import org.springframework.util.ClassUtils;
028:
029:        /**
030:         * {@link BeanFactoryPostProcessor} implementation that allows for convenient
031:         * registration of custom {@link PropertyEditor property editors}.
032:         *
033:         * <p>As of Spring 2.0, the recommended usage is to use custom
034:         * {@link PropertyEditorRegistrar} implementations that in turn register
035:         * any desired editors on a given
036:         * {@link org.springframework.beans.PropertyEditorRegistry registry}.
037:         * Each PropertyEditorRegistrar can register any number of custom editors.
038:         *
039:         * <pre class="code">
040:         * &lt;bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"&gt;
041:         *   &lt;property name="propertyEditorRegistrars"&gt;
042:         *     &lt;list&gt;
043:         *       &lt;bean class="mypackage.MyCustomDateEditorRegistrar"/&gt;
044:         *       &lt;bean class="mypackage.MyObjectEditorRegistrar"/&gt;
045:         *     &lt;/list&gt;
046:         *   &lt;/property&gt;
047:         * &lt;/bean&gt;</pre>
048:         *
049:         * <p>Alternative configuration example with custom editor instances,
050:         * assuming inner beans for <code>PropertyEditor</code> instances:
051:         *
052:         * <pre class="code">
053:         * &lt;bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"&gt;
054:         *   &lt;property name="customEditors"&gt;
055:         *     &lt;map&gt;
056:         *       &lt;entry key="java.util.Date"&gt;
057:         *         &lt;bean class="mypackage.MyCustomDateEditor"/&gt;
058:         *       &lt;/entry&gt;
059:         *       &lt;entry key="mypackage.MyObject"&gt;
060:         *         &lt;bean id="myEditor" class="mypackage.MyObjectEditor"&gt;
061:         *           &lt;property name="myParam"&gt;&lt;value&gt;myValue&lt;/value&gt;&lt;/property&gt;
062:         *         &lt;/bean&gt;
063:         *       &lt;/entry&gt;
064:         *     &lt;/map&gt;
065:         *   &lt;/property&gt;
066:         * &lt;/bean&gt;</pre>
067:         *
068:         * <p>Also supports "java.lang.String[]"-style array class names and primitive
069:         * class names (e.g. "boolean"). Delegates to {@link ClassUtils} for actual
070:         * class name resolution.
071:         *
072:         * <p><b>NOTE:</b> Custom property editors registered with this configurer do
073:         * <i>not</i> apply to data binding. Custom editors for data binding need to
074:         * be registered on the {@link org.springframework.validation.DataBinder}:
075:         * Use a common base class or delegate to common PropertyEditorRegistrar
076:         * implementations to reuse editor registration there.
077:         *
078:         * @author Juergen Hoeller
079:         * @since 27.02.2004
080:         * @see java.beans.PropertyEditor
081:         * @see org.springframework.beans.PropertyEditorRegistrar
082:         * @see ConfigurableBeanFactory#addPropertyEditorRegistrar
083:         * @see ConfigurableBeanFactory#registerCustomEditor
084:         * @see org.springframework.validation.DataBinder#registerCustomEditor
085:         * @see org.springframework.web.servlet.mvc.BaseCommandController#setPropertyEditorRegistrars
086:         * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder
087:         */
088:        public class CustomEditorConfigurer implements 
089:                BeanFactoryPostProcessor, BeanClassLoaderAware, Ordered {
090:
091:            private int order = Ordered.LOWEST_PRECEDENCE; // default: same as non-Ordered
092:
093:            private PropertyEditorRegistrar[] propertyEditorRegistrars;
094:
095:            private Map customEditors;
096:
097:            private ClassLoader beanClassLoader = ClassUtils
098:                    .getDefaultClassLoader();
099:
100:            public void setOrder(int order) {
101:                this .order = order;
102:            }
103:
104:            public int getOrder() {
105:                return this .order;
106:            }
107:
108:            /**
109:             * Specify the {@link PropertyEditorRegistrar PropertyEditorRegistrars}
110:             * to apply to beans defined within the current application context.
111:             * <p>This allows for sharing <code>PropertyEditorRegistrars</code> with
112:             * {@link org.springframework.validation.DataBinder DataBinders}, etc.
113:             * Furthermore, it avoids the need for synchronization on custom editors:
114:             * A <code>PropertyEditorRegistrar</code> will always create fresh editor
115:             * instances for each bean creation attempt.
116:             * @see ConfigurableListableBeanFactory#addPropertyEditorRegistrar
117:             */
118:            public void setPropertyEditorRegistrars(
119:                    PropertyEditorRegistrar[] propertyEditorRegistrars) {
120:                this .propertyEditorRegistrars = propertyEditorRegistrars;
121:            }
122:
123:            /**
124:             * Specify the custom editors to register via a {@link Map}, using the
125:             * class name of the required type as the key and the {@link PropertyEditor}
126:             * instance as the value.
127:             * @param customEditors said <code>Map</code> of editors (can be <code>null</code>) 
128:             * @deprecated as of Spring 2.0.7, in favor of {@link #setPropertyEditorRegistrars}
129:             * @see ConfigurableListableBeanFactory#registerCustomEditor
130:             */
131:            public void setCustomEditors(Map customEditors) {
132:                this .customEditors = customEditors;
133:            }
134:
135:            public void setBeanClassLoader(ClassLoader beanClassLoader) {
136:                this .beanClassLoader = beanClassLoader;
137:            }
138:
139:            public void postProcessBeanFactory(
140:                    ConfigurableListableBeanFactory beanFactory)
141:                    throws BeansException {
142:                if (this .propertyEditorRegistrars != null) {
143:                    for (int i = 0; i < this .propertyEditorRegistrars.length; i++) {
144:                        beanFactory
145:                                .addPropertyEditorRegistrar(this .propertyEditorRegistrars[i]);
146:                    }
147:                }
148:
149:                if (this .customEditors != null) {
150:                    for (Iterator it = this .customEditors.entrySet().iterator(); it
151:                            .hasNext();) {
152:                        Map.Entry entry = (Map.Entry) it.next();
153:                        Object key = entry.getKey();
154:                        Class requiredType = null;
155:                        if (key instanceof  Class) {
156:                            requiredType = (Class) key;
157:                        } else if (key instanceof  String) {
158:                            String className = (String) key;
159:                            requiredType = ClassUtils.resolveClassName(
160:                                    className, this .beanClassLoader);
161:                        } else {
162:                            throw new IllegalArgumentException(
163:                                    "Invalid key ["
164:                                            + key
165:                                            + "] for custom editor: needs to be Class or String.");
166:                        }
167:                        Object value = entry.getValue();
168:                        if (!(value instanceof  PropertyEditor)) {
169:                            throw new IllegalArgumentException("Mapped value ["
170:                                    + value + "] for custom editor key [" + key
171:                                    + "] is not of required type ["
172:                                    + PropertyEditor.class.getName() + "]");
173:                        }
174:                        beanFactory.registerCustomEditor(requiredType,
175:                                (PropertyEditor) value);
176:                    }
177:                }
178:            }
179:
180:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.