1 | /* |
2 | * Copyright 2006-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 | package org.springframework.batch.support; |
17 | |
18 | import java.beans.PropertyEditor; |
19 | import java.util.HashMap; |
20 | import java.util.Map; |
21 | import java.util.Map.Entry; |
22 | |
23 | import org.springframework.beans.PropertyEditorRegistrar; |
24 | import org.springframework.beans.PropertyEditorRegistry; |
25 | import org.springframework.beans.factory.config.CustomEditorConfigurer; |
26 | import org.springframework.util.ClassUtils; |
27 | |
28 | /** |
29 | * A re-usable {@link PropertyEditorRegistrar} that can be used wherever one |
30 | * needs to register custom {@link PropertyEditor} instances with a |
31 | * {@link PropertyEditorRegistry} (like a bean wrapper, or a type converter). It |
32 | * is not thread safe, but useful where one is confident that binding or |
33 | * initialisation can only be single threaded (e.g in a standalone application |
34 | * with no threads). |
35 | * |
36 | * @author Dave Syer |
37 | * |
38 | */ |
39 | public class DefaultPropertyEditorRegistrar implements PropertyEditorRegistrar { |
40 | |
41 | private Map<Class<?>, PropertyEditor> customEditors; |
42 | |
43 | /** |
44 | * Register the custom editors with the given registry. |
45 | * |
46 | * @see org.springframework.beans.PropertyEditorRegistrar#registerCustomEditors(org.springframework.beans.PropertyEditorRegistry) |
47 | */ |
48 | public void registerCustomEditors(PropertyEditorRegistry registry) { |
49 | if (this.customEditors != null) { |
50 | for (Entry<Class<?>, PropertyEditor> entry : customEditors.entrySet()) { |
51 | registry.registerCustomEditor(entry.getKey(), entry.getValue()); |
52 | } |
53 | } |
54 | } |
55 | |
56 | /** |
57 | * Specify the {@link PropertyEditor custom editors} to register. |
58 | * |
59 | * |
60 | * @param customEditors a map of Class to PropertyEditor (or class name to |
61 | * PropertyEditor). |
62 | * @see CustomEditorConfigurer#setCustomEditors(Map) |
63 | */ |
64 | public void setCustomEditors(Map<? extends Object, ? extends PropertyEditor> customEditors) { |
65 | this.customEditors = new HashMap<Class<?>, PropertyEditor>(); |
66 | for (Entry<? extends Object, ? extends PropertyEditor> entry : customEditors.entrySet()) { |
67 | Object key = entry.getKey(); |
68 | Class<?> requiredType = null; |
69 | if (key instanceof Class<?>) { |
70 | requiredType = (Class<?>) key; |
71 | } |
72 | else if (key instanceof String) { |
73 | String className = (String) key; |
74 | requiredType = ClassUtils.resolveClassName(className, getClass().getClassLoader()); |
75 | } |
76 | else { |
77 | throw new IllegalArgumentException("Invalid key [" + key |
78 | + "] for custom editor: needs to be Class or String."); |
79 | } |
80 | PropertyEditor value = entry.getValue(); |
81 | this.customEditors.put(requiredType, value); |
82 | } |
83 | } |
84 | |
85 | } |