001/* 002 * Copyright 2006-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 * https://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 */ 016package org.springframework.batch.support; 017 018import java.beans.PropertyEditor; 019import java.util.HashMap; 020import java.util.Map; 021import java.util.Map.Entry; 022 023import org.springframework.beans.PropertyEditorRegistrar; 024import org.springframework.beans.PropertyEditorRegistry; 025import org.springframework.beans.factory.config.CustomEditorConfigurer; 026import org.springframework.util.ClassUtils; 027 028/** 029 * A re-usable {@link PropertyEditorRegistrar} that can be used wherever one 030 * needs to register custom {@link PropertyEditor} instances with a 031 * {@link PropertyEditorRegistry} (like a bean wrapper, or a type converter). It 032 * is <b>not</b> thread safe, but useful where one is confident that binding or 033 * initialisation can only be single threaded (e.g in a standalone application 034 * with no threads). 035 * 036 * @author Dave Syer 037 * 038 */ 039public class DefaultPropertyEditorRegistrar implements PropertyEditorRegistrar { 040 041 private Map<Class<?>, PropertyEditor> customEditors; 042 043 /** 044 * Register the custom editors with the given registry. 045 * 046 * @see org.springframework.beans.PropertyEditorRegistrar#registerCustomEditors(org.springframework.beans.PropertyEditorRegistry) 047 */ 048 @Override 049 public void registerCustomEditors(PropertyEditorRegistry registry) { 050 if (this.customEditors != null) { 051 for (Entry<Class<?>, PropertyEditor> entry : customEditors.entrySet()) { 052 registry.registerCustomEditor(entry.getKey(), entry.getValue()); 053 } 054 } 055 } 056 057 /** 058 * Specify the {@link PropertyEditor custom editors} to register. 059 * 060 * 061 * @param customEditors a map of Class to PropertyEditor (or class name to 062 * PropertyEditor). 063 * @see CustomEditorConfigurer#setCustomEditors(Map) 064 */ 065 public void setCustomEditors(Map<? extends Object, ? extends PropertyEditor> customEditors) { 066 this.customEditors = new HashMap<Class<?>, PropertyEditor>(); 067 for (Entry<? extends Object, ? extends PropertyEditor> entry : customEditors.entrySet()) { 068 Object key = entry.getKey(); 069 Class<?> requiredType = null; 070 if (key instanceof Class<?>) { 071 requiredType = (Class<?>) key; 072 } 073 else if (key instanceof String) { 074 String className = (String) key; 075 requiredType = ClassUtils.resolveClassName(className, getClass().getClassLoader()); 076 } 077 else { 078 throw new IllegalArgumentException("Invalid key [" + key 079 + "] for custom editor: needs to be Class or String."); 080 } 081 PropertyEditor value = entry.getValue(); 082 this.customEditors.put(requiredType, value); 083 } 084 } 085 086}