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