| 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 | @Override |
| 49 | public void registerCustomEditors(PropertyEditorRegistry registry) { |
| 50 | if (this.customEditors != null) { |
| 51 | for (Entry<Class<?>, PropertyEditor> entry : customEditors.entrySet()) { |
| 52 | registry.registerCustomEditor(entry.getKey(), entry.getValue()); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Specify the {@link PropertyEditor custom editors} to register. |
| 59 | * |
| 60 | * |
| 61 | * @param customEditors a map of Class to PropertyEditor (or class name to |
| 62 | * PropertyEditor). |
| 63 | * @see CustomEditorConfigurer#setCustomEditors(Map) |
| 64 | */ |
| 65 | public void setCustomEditors(Map<? extends Object, ? extends PropertyEditor> customEditors) { |
| 66 | this.customEditors = new HashMap<Class<?>, PropertyEditor>(); |
| 67 | for (Entry<? extends Object, ? extends PropertyEditor> entry : customEditors.entrySet()) { |
| 68 | Object key = entry.getKey(); |
| 69 | Class<?> requiredType = null; |
| 70 | if (key instanceof Class<?>) { |
| 71 | requiredType = (Class<?>) key; |
| 72 | } |
| 73 | else if (key instanceof String) { |
| 74 | String className = (String) key; |
| 75 | requiredType = ClassUtils.resolveClassName(className, getClass().getClassLoader()); |
| 76 | } |
| 77 | else { |
| 78 | throw new IllegalArgumentException("Invalid key [" + key |
| 79 | + "] for custom editor: needs to be Class or String."); |
| 80 | } |
| 81 | PropertyEditor value = entry.getValue(); |
| 82 | this.customEditors.put(requiredType, value); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | } |