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.s2.detector.impl;
18  
19  import java.util.HashSet;
20  import java.util.Set;
21  
22  import org.seasar.cubby.plugins.s2.detector.ClassDetector;
23  import org.seasar.cubby.plugins.s2.detector.DetectClassProcessor;
24  import org.seasar.framework.container.ComponentDef;
25  import org.seasar.framework.container.cooldeploy.CoolComponentAutoRegister;
26  import org.seasar.framework.util.ClassUtil;
27  import org.seasar.framework.util.Disposable;
28  import org.seasar.framework.util.DisposableUtil;
29  
30  /**
31   * クラスパスを走査してクラスを検出するクラスの実装です。
32   * <p>
33   * Cool Deploy の挙動と合わせるため、{@link CoolComponentAutoRegister} を継承しています。
34   * </p>
35   * 
36   * @author baba
37   */
38  public class ClassDetectorImpl extends CoolComponentAutoRegister implements
39  		ClassDetector, Disposable {
40  
41  	/** スーパークラスの初期化メソッドをクリアします。 */
42  	public static final String INIT_METHOD = null;
43  
44  	/** 初期化フラグ。 */
45  	private boolean initialized;
46  
47  	/** 検出されたクラスを処理するクラスの配列です。 */
48  	private DetectClassProcessor[] processors;
49  
50  	/** 処理済みのクラス。 */
51  	private final Set<String> processedClasses = new HashSet<String>();
52  
53  	/**
54  	 * {@inheritDoc}
55  	 */
56  	@Override
57  	public void processClass(final String packageName,
58  			final String shortClassName) {
59  		final String fqcn = ClassUtil.concatName(packageName, shortClassName);
60  		if (!processedClasses.contains(fqcn)) {
61  			processedClasses.add(fqcn);
62  			for (final DetectClassProcessor processor : processors) {
63  				processor.processClass(packageName, shortClassName);
64  			}
65  		}
66  	}
67  
68  	/**
69  	 * {@inheritDoc}
70  	 */
71  	public void detect() {
72  		if (!initialized) {
73  			processedClasses.clear();
74  			processors = (DetectClassProcessor[]) getContainer().getRoot()
75  					.findAllComponents(DetectClassProcessor.class);
76  			registerAll();
77  			processedClasses.clear();
78  			DisposableUtil.add(this);
79  			initialized = true;
80  		}
81  	}
82  
83  	/**
84  	 * {@inheritDoc}
85  	 */
86  	public void dispose() {
87  		initialized = false;
88  	}
89  
90  	/**
91  	 * {@inheritDoc}
92  	 */
93  	@SuppressWarnings("unchecked")
94  	@Override
95  	protected ComponentDef createComponentDef(final Class componentClass) {
96  		throw new UnsupportedOperationException();
97  	}
98  
99  }