View Javadoc

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