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