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.action;
17  
18  import java.lang.reflect.InvocationTargetException;
19  import java.lang.reflect.Method;
20  import java.util.Map;
21  
22  /**
23   * アクションの基底クラスです。
24   * <p>
25   * アクションはビューのコントローラーの役割を果たします。
26   * </p>
27   * 
28   * @author agata
29   * @author baba
30   * @since 1.0.0
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  	 * @since 1.1.0
91  	 */
92  	public void invokeInitializeMethod(final Method actionMethod) {
93  		if (actionMethod.isAnnotationPresent(InitializeMethod.class)) {
94  			final InitializeMethod initializeMethod = actionMethod
95  					.getAnnotation(InitializeMethod.class);
96  			final String methodName = initializeMethod.value();
97  			this.invoke(methodName);
98  		} else {
99  			this.initialize();
100 		}
101 	}
102 
103 	/**
104 	 * アクションメソッドが {@link InitializeMethod} で装飾されていない場合に
105 	 * {@link #invokeInitializeMethod(Method)} から呼ばれるメソッドです。
106 	 */
107 	protected void initialize() {
108 	}
109 
110 	/**
111 	 * フォーワードの直前に呼ばれます。
112 	 * <p>
113 	 * 指定されたアクションメソッドが {@link PreRenderMethod} でメソッド名が指定されている場合はそのメソッドを呼び出します。
114 	 * そうでない場合は {@link #prerender()} を呼び出します。
115 	 * </p>
116 	 * <p>
117 	 * 対象のActionクラスのフォワード先で必ず使用する共通のデータなどを取得する目的で使用します。
118 	 * </p>
119 	 * 
120 	 * @param actionMethod
121 	 *            アクションメソッド
122 	 * @since 1.1.0
123 	 */
124 	public void invokePreRenderMethod(final Method actionMethod) {
125 		if (actionMethod.isAnnotationPresent(PreRenderMethod.class)) {
126 			final PreRenderMethod preRenderMethod = actionMethod
127 					.getAnnotation(PreRenderMethod.class);
128 			final String methodName = preRenderMethod.value();
129 			this.invoke(methodName);
130 		} else {
131 			this.prerender();
132 		}
133 	}
134 
135 	/**
136 	 * アクションメソッドが {@link PreRenderMethod} で装飾されていない場合に
137 	 * {@link #invokePreRenderMethod(Method)} から呼ばれるメソッドです。
138 	 */
139 	protected void prerender() {
140 	}
141 
142 	/**
143 	 * フォワードの直後に呼ばれます。
144 	 * <p>
145 	 * 指定されたアクションメソッドが {@link PostRenderMethod} でメソッド名が指定されている場合はそのメソッドを呼び出します。
146 	 * そうでない場合は {@link #postrender()} を呼び出します。
147 	 * </p>
148 	 * <p>
149 	 * 通常はあまり使用することはないでしょう。
150 	 * </p>
151 	 * 
152 	 * @param actionMethod
153 	 *            アクションメソッド
154 	 * @since 1.1.0
155 	 */
156 	public void invokePostRenderMethod(final Method actionMethod) {
157 		if (actionMethod.isAnnotationPresent(PostRenderMethod.class)) {
158 			final PostRenderMethod postRenderMethod = actionMethod
159 					.getAnnotation(PostRenderMethod.class);
160 			final String methodName = postRenderMethod.value();
161 			this.invoke(methodName);
162 		} else {
163 			this.postrender();
164 		}
165 	}
166 
167 	/**
168 	 * アクションメソッドが {@link PostRenderMethod} で装飾されていない場合に
169 	 * {@link #invokePostRenderMethod(Method)} から呼ばれるメソッドです。
170 	 */
171 	protected void postrender() {
172 	}
173 
174 	/**
175 	 * このアクションに定義された指定されたメソッド名のメソッドを実行します。
176 	 * 
177 	 * @param methodName
178 	 *            メソッド名
179 	 * @since 1.1.0
180 	 */
181 	protected void invoke(final String methodName) {
182 		try {
183 			final Method method = this.getClass().getMethod(methodName);
184 			method.invoke(this);
185 		} catch (final NoSuchMethodException e) {
186 			throw new ActionException(e);
187 		} catch (final IllegalAccessException e) {
188 			throw new ActionException(e);
189 		} catch (final InvocationTargetException e) {
190 			throw new ActionException(e);
191 		}
192 	}
193 
194 }