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.util;
17  
18  import static org.seasar.cubby.CubbyConstants.ATTR_ACTION_CONTEXT;
19  import static org.seasar.cubby.internal.util.RequestUtils.getAttribute;
20  
21  import java.lang.reflect.Method;
22  
23  import javax.servlet.ServletRequest;
24  import javax.servlet.http.HttpServletRequest;
25  
26  import org.seasar.cubby.action.Action;
27  import org.seasar.cubby.action.ActionClass;
28  import org.seasar.cubby.action.ActionContext;
29  import org.seasar.cubby.action.ActionResult;
30  import org.seasar.cubby.internal.controller.ThreadContext;
31  
32  /**
33   * アクションのユーティリティクラスです。
34   * 
35   * @author baba
36   */
37  public class ActionUtils {
38  
39  	/**
40  	 * アクションコンテキストを取得します。
41  	 * <p>
42  	 * 実行中のスレッドに対応する要求の属性からオブジェクトを取得します。
43  	 * </p>
44  	 * 
45  	 * @return アクションコンテキスト
46  	 */
47  	public static ActionContext actionContext() {
48  		final HttpServletRequest request = ThreadContext.getRequest();
49  		return actionContext(request);
50  	}
51  
52  	/**
53  	 * アクションコンテキストを取得します。
54  	 * <p>
55  	 * 指定された要求の属性からオブジェクトを取得します。
56  	 * </p>
57  	 * 
58  	 * @param request
59  	 *            要求
60  	 * @return アクションコンテキスト
61  	 */
62  	public static ActionContext actionContext(final ServletRequest request) {
63  		final ActionContext actionContext = getAttribute(request,
64  				ATTR_ACTION_CONTEXT);
65  		return actionContext;
66  	}
67  
68  	/**
69  	 * 指定されたクラスがアクションクラスかを示します。
70  	 * <p>
71  	 * アクションクラスは以下のいずれかの条件を満たす必要があります。
72  	 * </p>
73  	 * <ul>
74  	 * <li>{@link Action} を実装している</li>
75  	 * <li>{@link ActionClass} で修飾されている</li>
76  	 * </ul>
77  	 * 
78  	 * @param clazz
79  	 *            クラス
80  	 * @return 指定されたクラスがアクションクラスの場合は <code>true</code>、そうでない場合は
81  	 *         <code>false</code>
82  	 */
83  	public static boolean isActionClass(final Class<?> clazz) {
84  		if (Action.class.isAssignableFrom(clazz)) {
85  			return true;
86  		}
87  		if (clazz.isAnnotationPresent(ActionClass.class)) {
88  			return true;
89  		}
90  		return false;
91  	}
92  
93  	/**
94  	 * 指定されたメソッドがアクションメソッドかを示します。
95  	 * <p>
96  	 * アクションメソッドは以下の条件を満たす必要があります。
97  	 * </p>
98  	 * <ul>
99  	 * <li>publicなインスタンスメソッド</li>
100 	 * <li>戻り値が{@code ActionResult}</li>
101 	 * <li>引数が0</li>
102 	 * </ul>
103 	 * 
104 	 * @param method
105 	 *            メソッド
106 	 * @return 指定されたメソッドがアクションメソッドの場合は <code>true</code>、そうでない場合は
107 	 *         <code>false</code>
108 	 */
109 	public static boolean isActionMethod(final Method method) {
110 		return ActionResult.class.isAssignableFrom(method.getReturnType())
111 				&& (method.getParameterTypes().length == 0);
112 	}
113 
114 }