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