01: /*
02: * Copyright 2004-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: * Registers all 'from string' converters known to a conversion service with
28: * a 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
37: BeanFactoryPostProcessor, InitializingBean {
38:
39: private ConversionService conversionService;
40:
41: /**
42: * Create a new configurer.
43: * @param conversionService the conversion service to take converters from
44: */
45: public void setConversionService(ConversionService conversionService) {
46: this .conversionService = conversionService;
47: }
48:
49: public void afterPropertiesSet() throws Exception {
50: Assert.notNull(conversionService,
51: "The conversion service is required");
52: }
53:
54: public void postProcessBeanFactory(
55: ConfigurableListableBeanFactory beanFactory)
56: throws BeansException {
57: ConversionExecutor[] executors = conversionService
58: .getConversionExecutorsForSource(String.class);
59: for (int i = 0; i < executors.length; i++) {
60: ConverterPropertyEditorAdapter editor = new ConverterPropertyEditorAdapter(
61: executors[i]);
62: beanFactory.registerCustomEditor(editor.getTargetClass(),
63: editor);
64: }
65: }
66: }
|