1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }