View Javadoc

1   /*
2    * Copyright 2004-2008 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.action;
17  
18  import java.lang.reflect.Method;
19  import java.util.Map;
20  
21  import org.seasar.framework.util.ClassUtil;
22  import org.seasar.framework.util.MethodUtil;
23  
24  /**
25   * アクションの基底クラスです。
26   * <p>
27   * アクションはビューのコントローラーの役割を果たします。
28   * </p>
29   * 
30   * @author agata
31   * @author baba
32   * @since 1.0.0
33   */
34  public abstract class Action {
35  
36  	/** アクションエラーオブジェクト。 */
37  	protected ActionErrors errors;
38  
39  	/** 揮発性メッセージ。 */
40  	protected Map<String, Object> flash;
41  
42  	/**
43  	 * アクションエラーオブジェクトを取得します。
44  	 * 
45  	 * @return アクションエラーオブジェクト
46  	 */
47  	public ActionErrors getErrors() {
48  		return errors;
49  	}
50  
51  	/**
52  	 * アクションエラーオブジェクトをセットします。
53  	 * 
54  	 * @param errors
55  	 *            アクションエラーオブジェクト
56  	 */
57  	public void setErrors(final ActionErrors errors) {
58  		this.errors = errors;
59  	}
60  
61  	/**
62  	 * 揮発性メッセージを取得します。
63  	 * 
64  	 * @return 揮発性メッセージ
65  	 */
66  	public Map<String, Object> getFlash() {
67  		return flash;
68  	}
69  
70  	/**
71  	 * 揮発性メッセージをセットします。
72  	 * 
73  	 * @param flash
74  	 *            揮発性メッセージ
75  	 */
76  	public void setFlash(final Map<String, Object> flash) {
77  		this.flash = flash;
78  	}
79  
80  	/**
81  	 * アクションメソッドの実行前に呼ばれます。
82  	 * <p>
83  	 * 指定されたアクションメソッドに {@link InitializeMethod} でメソッド名が指定されている場合はそのメソッドを呼び出します。
84  	 * そうでない場合は {{@link #initialize()} を呼び出します。
85  	 * </p>
86  	 * <p>
87  	 * パラメータのバインディング前に呼ばれるので、パラメータを使用したい場合はリクエストから直接取得する必要があります。
88  	 * </p>
89  	 * 
90  	 * @param actionMethod
91  	 *            アクションメソッド
92  	 * @since 1.1.0
93  	 */
94  	public void invokeInitializeMethod(final Method actionMethod) {
95  		if (actionMethod.isAnnotationPresent(InitializeMethod.class)) {
96  			final InitializeMethod initializeMethod = actionMethod
97  					.getAnnotation(InitializeMethod.class);
98  			final String methodName = initializeMethod.value();
99  			this.invoke(methodName);
100 		} else {
101 			this.initialize();
102 		}
103 	}
104 
105 	/**
106 	 * アクションメソッドが {@link InitializeMethod} で装飾されていない場合に
107 	 * {@link #invokeInitializeMethod(Method)} から呼ばれるメソッドです。
108 	 */
109 	protected void initialize() {
110 	}
111 
112 	/**
113 	 * フォーワードの直前に呼ばれます。
114 	 * <p>
115 	 * 指定されたアクションメソッドが {@link PreRenderMethod} でメソッド名が指定されている場合はそのメソッドを呼び出します。
116 	 * そうでない場合は {@link #prerender()} を呼び出します。
117 	 * </p>
118 	 * <p>
119 	 * 対象のActionクラスのフォワード先で必ず使用する共通のデータなどを取得する目的で使用します。
120 	 * </p>
121 	 * 
122 	 * @param actionMethod
123 	 *            アクションメソッド
124 	 * @since 1.1.0
125 	 */
126 	public void invokePreRenderMethod(final Method actionMethod) {
127 		if (actionMethod.isAnnotationPresent(PreRenderMethod.class)) {
128 			final PreRenderMethod preRenderMethod = actionMethod
129 					.getAnnotation(PreRenderMethod.class);
130 			final String methodName = preRenderMethod.value();
131 			this.invoke(methodName);
132 		} else {
133 			this.prerender();
134 		}
135 	}
136 
137 	/**
138 	 * アクションメソッドが {@link PreRenderMethod} で装飾されていない場合に
139 	 * {@link #invokePreRenderMethod(Method)} から呼ばれるメソッドです。
140 	 */
141 	protected void prerender() {
142 	}
143 
144 	/**
145 	 * フォワードの直後に呼ばれます。
146 	 * <p>
147 	 * 指定されたアクションメソッドが {@link PostRenderMethod} でメソッド名が指定されている場合はそのメソッドを呼び出します。
148 	 * そうでない場合は {@link #postrender()} を呼び出します。
149 	 * </p>
150 	 * <p>
151 	 * 通常はあまり使用することはないでしょう。
152 	 * </p>
153 	 * 
154 	 * @param actionMethod
155 	 *            アクションメソッド
156 	 * @since 1.1.0
157 	 */
158 	public void invokePostRenderMethod(final Method actionMethod) {
159 		if (actionMethod.isAnnotationPresent(PostRenderMethod.class)) {
160 			final PostRenderMethod postRenderMethod = actionMethod
161 					.getAnnotation(PostRenderMethod.class);
162 			final String methodName = postRenderMethod.value();
163 			this.invoke(methodName);
164 		} else {
165 			this.postrender();
166 		}
167 	}
168 
169 	/**
170 	 * アクションメソッドが {@link PostRenderMethod} で装飾されていない場合に
171 	 * {@link #invokePostRenderMethod(Method)} から呼ばれるメソッドです。
172 	 */
173 	protected void postrender() {
174 	}
175 
176 	/**
177 	 * このアクションに定義された指定されたメソッド名のメソッドを実行します。
178 	 * 
179 	 * @param methodName
180 	 *            メソッド名
181 	 * @since 1.1.0
182 	 */
183 	protected void invoke(final String methodName) {
184 		final Method method = ClassUtil.getMethod(this.getClass(), methodName,
185 				null);
186 		MethodUtil.invoke(method, this, null);
187 	}
188 
189 }