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