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.ArrayList;
20  import java.util.List;
21  
22  import org.seasar.cubby.plugins.s2.detector.ClassDetector;
23  import org.seasar.cubby.plugins.s2.detector.DetectClassProcessor;
24  import org.seasar.cubby.routing.PathResolver;
25  import org.seasar.cubby.spi.PathResolverProvider;
26  import org.seasar.framework.convention.NamingConvention;
27  import org.seasar.framework.util.ClassUtil;
28  import org.seasar.framework.util.Disposable;
29  import org.seasar.framework.util.DisposableUtil;
30  
31  /**
32   * クラスパスを走査しアクションクラスを登録します。
33   * 
34   * @author baba
35   * @since 2.0.0
36   */
37  public class S2PathResolverProvider implements PathResolverProvider,
38  		DetectClassProcessor, Disposable {
39  
40  	public static final String pathResolver_BINDING = "bindingType=must";
41  
42  	public static final String namingConvention_BINDING = "bindingType=must";
43  
44  	public static final String classDetector_BINDING = "bindingType=must";
45  
46  	/** パスに対応するアクションメソッドを解決するためのクラス。 */
47  	private PathResolver pathResolver;
48  
49  	/** 命名規約。 */
50  	private NamingConvention namingConvention;
51  
52  	/** クラスパスを走査してクラスを検出するクラス。 */
53  	public ClassDetector classDetector;
54  
55  	/** インスタンスが初期化済みであることを示します。 */
56  	private boolean initialized;
57  
58  	/** アクションクラスのリスト。 */
59  	private final List<Class<?>> actionClasses = new ArrayList<Class<?>>();
60  
61  	/**
62  	 * パスに対応するアクションメソッドを解決するためのクラスを設定します。
63  	 * 
64  	 * @param pathResolver
65  	 *            パスに対応するアクションメソッドを解決するためのクラス
66  	 * 
67  	 */
68  	public void setPathResolver(final PathResolver pathResolver) {
69  		this.pathResolver = pathResolver;
70  	}
71  
72  	/**
73  	 * クラスパスを走査してクラスを検出するクラスを設定します。
74  	 * 
75  	 * @param classDetector
76  	 *            クラスパスを走査してクラスを検出するクラス
77  	 */
78  	public void setClassDetector(final ClassDetector classDetector) {
79  		this.classDetector = classDetector;
80  	}
81  
82  	/**
83  	 * 命名規約を設定します。
84  	 * 
85  	 * @param namingConvention
86  	 *            命名規約
87  	 */
88  	public void setNamingConvention(final NamingConvention namingConvention) {
89  		this.namingConvention = namingConvention;
90  	}
91  
92  	/**
93  	 * 初期化します。
94  	 */
95  	public void initialize() {
96  		if (initialized) {
97  			return;
98  		}
99  		classDetector.detect();
100 		pathResolver.addAll(actionClasses);
101 
102 		DisposableUtil.add(this);
103 		initialized = true;
104 	}
105 
106 	/**
107 	 * {@inheritDoc}
108 	 */
109 	public void dispose() {
110 		actionClasses.clear();
111 		pathResolver.clear();
112 		initialized = false;
113 	}
114 
115 	/**
116 	 * {@inheritDoc}
117 	 * <p>
118 	 * 指定されたパッケージ名、クラス名から導出されるクラスがアクションクラスだった場合はルーティングを登録します。
119 	 * </p>
120 	 */
121 	public void processClass(final String packageName,
122 			final String shortClassName) {
123 		if (shortClassName.indexOf('$') != -1) {
124 			return;
125 		}
126 		final String className = ClassUtil.concatName(packageName,
127 				shortClassName);
128 		if (!namingConvention.isTargetClassName(className)) {
129 			return;
130 		}
131 		if (!className.endsWith(namingConvention.getActionSuffix())) {
132 			return;
133 		}
134 		final Class<?> clazz = ClassUtil.forName(className);
135 		if (namingConvention.isSkipClass(clazz)) {
136 			return;
137 		}
138 		if (Modifier.isAbstract(clazz.getModifiers())) {
139 			return;
140 		}
141 		actionClasses.add(clazz);
142 	}
143 
144 	/**
145 	 * {@inheritDoc}
146 	 */
147 	public PathResolver getPathResolver() {
148 		initialize();
149 		return pathResolver;
150 	}
151 
152 }