KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > convert > support > CustomConverterConfigurer


1 /*
2  * Copyright 2002-2005 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.binding.convert.support;
17
18 import org.springframework.beans.BeansException;
19 import org.springframework.beans.factory.InitializingBean;
20 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
21 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
22 import org.springframework.binding.convert.ConversionExecutor;
23 import org.springframework.binding.convert.ConversionService;
24 import org.springframework.util.Assert;
25
26 /**
27  * Default, local implementation of a conversion service deployable within a
28  * Spring bean factory.
29  * <p>
30  * Acts as bean factory post processor, registering property editor adapters for
31  * each supported conversion with a <code>java.lang.String sourceClass</code>.
32  * This makes for very convenient use with the Spring container.
33  *
34  * @author Keith Donald
35  */

36 public class CustomConverterConfigurer implements BeanFactoryPostProcessor, InitializingBean {
37
38     private ConversionService conversionService;
39
40     public void setConversionService(ConversionService conversionService) {
41         this.conversionService = conversionService;
42     }
43
44     public void afterPropertiesSet() throws Exception JavaDoc {
45         Assert.notNull(conversionService, "The conversion service is required");
46     }
47
48     public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
49         ConversionExecutor[] executors = conversionService.getConversionExecutorsForSource(String JavaDoc.class);
50         for (int i = 0; i < executors.length; i++) {
51             ConverterPropertyEditorAdapter editor = new ConverterPropertyEditorAdapter(executors[i]);
52             beanFactory.registerCustomEditor(editor.getTargetClass(), editor);
53         }
54     }
55 }
Popular Tags