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.s2.spi;
17  
18  import java.lang.reflect.Modifier;
19  import java.util.Arrays;
20  import java.util.Collection;
21  import java.util.LinkedHashSet;
22  import java.util.Set;
23  
24  import org.seasar.cubby.converter.Converter;
25  import org.seasar.cubby.plugins.s2.detector.ClassDetector;
26  import org.seasar.cubby.plugins.s2.detector.DetectClassProcessor;
27  import org.seasar.cubby.spi.impl.AbstractCachedConverterProvider;
28  import org.seasar.framework.container.S2Container;
29  import org.seasar.framework.convention.NamingConvention;
30  import org.seasar.framework.util.ClassUtil;
31  import org.seasar.framework.util.Disposable;
32  import org.seasar.framework.util.DisposableUtil;
33  
34  /**
35   * コンバータプロバイダの実装クラスです。
36   * 
37   * @author baba
38   * @since 2.0.0
39   */
40  public class S2ConverterProvider extends AbstractCachedConverterProvider
41  		implements DetectClassProcessor, Disposable {
42  
43  	public static final String s2Container_BINDING = "bindingType=must";
44  
45  	public static final String namingConvention_BINDING = "bindingType=must";
46  
47  	public static final String classDetector_BINDING = "bindingType=must";
48  
49  	/** S2 コンテナ。 */
50  	private S2Container s2Container;
51  
52  	/** 命名規約。 */
53  	private NamingConvention namingConvention;
54  
55  	/** クラスパスを走査してクラスを検出するクラス。 */
56  	private ClassDetector classDetector;
57  
58  	/** インスタンスが初期化済みであることを示します。 */
59  	private boolean initialized;
60  
61  	/** コンバータのセットです。 */
62  	private final Set<Converter> converters = new LinkedHashSet<Converter>();
63  
64  	/**
65  	 * S2 コンテナを設定します。
66  	 * 
67  	 * @param s2Container
68  	 *            S2 コンテナ
69  	 */
70  	public void setS2Container(final S2Container s2Container) {
71  		this.s2Container = s2Container;
72  	}
73  
74  	/**
75  	 * 命名規約を設定します。
76  	 * 
77  	 * @param namingConvention
78  	 *            命名規約
79  	 */
80  	public void setNamingConvention(final NamingConvention namingConvention) {
81  		this.namingConvention = namingConvention;
82  	}
83  
84  	/**
85  	 * クラスパスを走査してクラスを検出するクラスを設定します。
86  	 * 
87  	 * @param classDetector
88  	 *            クラスパスを走査してクラスを設定します。
89  	 */
90  	public void setClassDetector(final ClassDetector classDetector) {
91  		this.classDetector = classDetector;
92  	}
93  
94  	@Override
95  	protected Collection<Converter> getConverters() {
96  		return converters;
97  	}
98  
99  	/**
100 	 * インスタンスを初期化します。
101 	 */
102 	public void initialize() {
103 		if (initialized) {
104 			return;
105 		}
106 		classDetector.detect();
107 
108 		final Converter[] converters = (Converter[]) s2Container.getRoot()
109 				.findAllComponents(Converter.class);
110 		this.converters.addAll(Arrays.asList(converters));
111 		DisposableUtil.add(this);
112 		initialized = true;
113 	}
114 
115 	/**
116 	 * キャッシュ情報等を破棄し、インスタンスを未初期化状態に戻します。
117 	 * 
118 	 */
119 	public void dispose() {
120 		this.converters.clear();
121 		super.clear();
122 		initialized = false;
123 	}
124 
125 	/**
126 	 * {@inheritDoc}
127 	 */
128 	@Override
129 	public Converter getConverter(final Class<?> parameterType,
130 			final Class<?> objectType) {
131 		initialize();
132 		return super.getConverter(parameterType, objectType);
133 	}
134 
135 	/**
136 	 * {@inheritDoc}
137 	 * <p>
138 	 * 指定されたパッケージ名、クラス名から導出されるクラスがコンバータだった場合はファクトリにコンバータを登録します。
139 	 * </p>
140 	 */
141 	public void processClass(final String packageName,
142 			final String shortClassName) {
143 		if (shortClassName.indexOf('$') != -1) {
144 			return;
145 		}
146 		final String className = ClassUtil.concatName(packageName,
147 				shortClassName);
148 		if (!namingConvention.isTargetClassName(className)) {
149 			return;
150 		}
151 		if (!className.endsWith(namingConvention.getConverterSuffix())) {
152 			return;
153 		}
154 		final Class<?> clazz = ClassUtil.forName(className);
155 		if (namingConvention.isSkipClass(clazz)) {
156 			return;
157 		}
158 		if ((clazz.getModifiers() & Modifier.ABSTRACT) != 0) {
159 			return;
160 		}
161 		if (!Converter.class.isAssignableFrom(clazz)) {
162 			return;
163 		}
164 		final Converter converter = (Converter) s2Container.getRoot()
165 				.getComponent(clazz);
166 		this.converters.add(converter);
167 	}
168 
169 }