View Javadoc

1   /*
2    * Copyright 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 net.sf.snamespaces.extensions;
18  
19  import java.beans.PropertyEditor;
20  
21  import org.springframework.beans.BeansException;
22  import org.springframework.beans.PropertyEditorRegistrar;
23  import org.springframework.beans.factory.BeanClassLoaderAware;
24  import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
25  import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
26  import org.springframework.core.Ordered;
27  import org.springframework.util.ClassUtils;
28  
29  /**
30   * TODO make doc
31   * 
32   * @author Jonhnny Weslley
33   * @version 1.00, 13/09/2007
34   * @since 1.0
35   */
36  public class SimpleCustomEditorConfigurer implements BeanFactoryPostProcessor,
37  		BeanClassLoaderAware, Ordered {
38  
39  	public static final String PROPERTY_EDITOR_BEAN_NAME_PREFIX = "property-editor-to:";
40  	public static final String PROPERTY_EDITOR_REGISTRAR_BEAN_NAME_PREFIX = "property-editor-registrar:";
41  
42  	private int order = LOWEST_PRECEDENCE;
43  	private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
44  
45  	public void setOrder(final int order) {
46  		this.order = order;
47  	}
48  
49  	public int getOrder() {
50  		return order;
51  	}
52  
53  	public void setBeanClassLoader(final ClassLoader beanClassLoader) {
54  		this.beanClassLoader = beanClassLoader;
55  	}
56  
57  	public void postProcessBeanFactory(
58  			final ConfigurableListableBeanFactory beanFactory) throws BeansException {
59  		final String[] customEditorBeanNames = beanFactory.getBeanNamesForType(PropertyEditor.class);
60  		for (final String beanName : customEditorBeanNames) {
61  			if (beanName.startsWith(PROPERTY_EDITOR_BEAN_NAME_PREFIX)) {
62  				registerCustomEditor(beanFactory, beanName);
63  			}
64  		}
65  
66  		final String[] registrarBeanNames = beanFactory.getBeanNamesForType(PropertyEditorRegistrar.class);
67  		for (final String beanName : registrarBeanNames) {
68  			if (beanName.startsWith(PROPERTY_EDITOR_REGISTRAR_BEAN_NAME_PREFIX)) {
69  				registerPropertyEditorRegistrar(beanFactory, beanName);
70  			}
71  		}
72  	}
73  
74  	private void registerCustomEditor(
75  			final ConfigurableListableBeanFactory beanFactory, final String beanName) {
76  		final String requiredTypeName = beanName.substring(PROPERTY_EDITOR_BEAN_NAME_PREFIX.length()); 
77  		final Class<?> requiredType = ClassUtils.resolveClassName(requiredTypeName, beanClassLoader);
78  		final Object propertyEditor = beanFactory.getBean(beanName);
79  		beanFactory.registerCustomEditor(requiredType, (PropertyEditor) propertyEditor);
80  	}
81  
82  	private void registerPropertyEditorRegistrar(
83  			final ConfigurableListableBeanFactory beanFactory, final String beanName) {
84  		final Object registrar = beanFactory.getBean(beanName);
85  		beanFactory.addPropertyEditorRegistrar((PropertyEditorRegistrar) registrar);
86  	}
87  
88  }