View Javadoc

1   /*
2    * Copyright 2004-2010 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  
17  package org.seasar.cubby.plugins.spring.spi;
18  
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.LinkedHashSet;
22  import java.util.Set;
23  
24  import org.seasar.cubby.converter.Converter;
25  import org.seasar.cubby.spi.impl.AbstractCachedConverterProvider;
26  import org.springframework.beans.factory.BeanFactoryUtils;
27  import org.springframework.context.ApplicationContext;
28  import org.springframework.context.ApplicationContextAware;
29  
30  /**
31   * コンバータプロバイダの実装クラスです。
32   * 
33   * @author suzuki-kei
34   * @author someda
35   */
36  public class SpringConverterProvider extends AbstractCachedConverterProvider
37  		implements ApplicationContextAware {
38  
39  	private ApplicationContext applicationContext;
40  
41  	private boolean initialized = false;
42  
43  	private final Class<Converter> converterClass = Converter.class;
44  
45  	private final Set<Converter> converters = new LinkedHashSet<Converter>();
46  
47  	/**
48  	 * アプリケーションコンテキストを設定します。
49  	 * 
50  	 * @param applicationContext
51  	 *            アプリケーションコンテキスト
52  	 */
53  	public void setApplicationContext(
54  			final ApplicationContext applicationContext) {
55  		this.applicationContext = applicationContext;
56  	}
57  
58  	/**
59  	 * このオブジェクトを初期化します。
60  	 */
61  	public void initialize() {
62  		if (initialized) {
63  			return;
64  		}
65  		String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
66  				applicationContext, converterClass);
67  		for (String name : names) {
68  			Converter converter = converterClass.cast(applicationContext
69  					.getBean(name, converterClass));
70  			converters.add(converter);
71  		}
72  		initialized = true;
73  	}
74  
75  	@Override
76  	protected Collection<Converter> getConverters() {
77  		return Collections.unmodifiableCollection(this.converters);
78  	}
79  
80  }