Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Action |
|
| 0.0;0 |
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 | 42 | 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 | 12 | return errors; |
49 | } | |
50 | ||
51 | /** | |
52 | * アクションエラーオブジェクトをセットします。 | |
53 | * | |
54 | * @param errors | |
55 | * アクションエラーオブジェクト | |
56 | */ | |
57 | public void setErrors(final ActionErrors errors) { | |
58 | 36 | this.errors = errors; |
59 | 36 | } |
60 | ||
61 | /** | |
62 | * 揮発性メッセージを取得します。 | |
63 | * | |
64 | * @return 揮発性メッセージ | |
65 | */ | |
66 | public Map<String, Object> getFlash() { | |
67 | 5 | return flash; |
68 | } | |
69 | ||
70 | /** | |
71 | * 揮発性メッセージをセットします。 | |
72 | * | |
73 | * @param flash | |
74 | * 揮発性メッセージ | |
75 | */ | |
76 | public void setFlash(final Map<String, Object> flash) { | |
77 | 36 | this.flash = flash; |
78 | 36 | } |
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 | 2 | this.initialize(); |
96 | 2 | if (actionMethod.isAnnotationPresent(InitializeMethod.class)) { |
97 | 0 | final InitializeMethod initializeMethod = actionMethod |
98 | .getAnnotation(InitializeMethod.class); | |
99 | 0 | final String methodName = initializeMethod.value(); |
100 | 0 | this.invoke(methodName); |
101 | 0 | } else { |
102 | 2 | this.initialize(); |
103 | } | |
104 | 2 | } |
105 | ||
106 | /** | |
107 | * アクションメソッドが {@link InitializeMethod} で装飾されていない場合に | |
108 | * {@link #invokeInitializeMethod(Method)} から呼ばれるメソッドです。 | |
109 | */ | |
110 | protected void initialize() { | |
111 | 4 | } |
112 | ||
113 | /** | |
114 | * フォーワードの直前に呼ばれます。 | |
115 | * <p> | |
116 | * 指定されたアクションメソッドが {@link PreRenderMethod} でメソッド名が指定されている場合はそのメソッドを呼び出します。 | |
117 | * そうでない場合は {@link #prerender()} を呼び出します。 | |
118 | * </p> | |
119 | * <p> | |
120 | * 対象のActionクラスのフォワード先で必ず使用する共通のデータなどを取得する目的で使用します。 | |
121 | * </p> | |
122 | * | |
123 | * @param actionMethod | |
124 | * アクションメソッド | |
125 | * @since 1.1.0 | |
126 | */ | |
127 | public void invokePreRenderMethod(final Method actionMethod) { | |
128 | 5 | if (actionMethod.isAnnotationPresent(PreRenderMethod.class)) { |
129 | 0 | final PreRenderMethod preRenderMethod = actionMethod |
130 | .getAnnotation(PreRenderMethod.class); | |
131 | 0 | final String methodName = preRenderMethod.value(); |
132 | 0 | this.invoke(methodName); |
133 | 0 | } else { |
134 | 5 | this.prerender(); |
135 | } | |
136 | 5 | } |
137 | ||
138 | /** | |
139 | * アクションメソッドが {@link PreRenderMethod} で装飾されていない場合に | |
140 | * {@link #invokePreRenderMethod(Method)} から呼ばれるメソッドです。 | |
141 | */ | |
142 | protected void prerender() { | |
143 | 5 | } |
144 | ||
145 | /** | |
146 | * フォワードの直後に呼ばれます。 | |
147 | * <p> | |
148 | * 指定されたアクションメソッドが {@link PostRenderMethod} でメソッド名が指定されている場合はそのメソッドを呼び出します。 | |
149 | * そうでない場合は {@link #postrender()} を呼び出します。 | |
150 | * </p> | |
151 | * <p> | |
152 | * 通常はあまり使用することはないでしょう。 | |
153 | * </p> | |
154 | * | |
155 | * @param actionMethod | |
156 | * アクションメソッド | |
157 | * @since 1.1.0 | |
158 | */ | |
159 | public void invokePostRenderMethod(final Method actionMethod) { | |
160 | 5 | if (actionMethod.isAnnotationPresent(PostRenderMethod.class)) { |
161 | 0 | final PostRenderMethod postRenderMethod = actionMethod |
162 | .getAnnotation(PostRenderMethod.class); | |
163 | 0 | final String methodName = postRenderMethod.value(); |
164 | 0 | this.invoke(methodName); |
165 | 0 | } else { |
166 | 5 | this.postrender(); |
167 | } | |
168 | 5 | } |
169 | ||
170 | /** | |
171 | * アクションメソッドが {@link PostRenderMethod} で装飾されていない場合に | |
172 | * {@link #invokePostRenderMethod(Method)} から呼ばれるメソッドです。 | |
173 | */ | |
174 | protected void postrender() { | |
175 | 5 | } |
176 | ||
177 | /** | |
178 | * このアクションに定義された指定されたメソッド名のメソッドを実行します。 | |
179 | * | |
180 | * @param methodName | |
181 | * メソッド名 | |
182 | * @since 1.1.0 | |
183 | */ | |
184 | protected void invoke(final String methodName) { | |
185 | 0 | final Method method = ClassUtil.getMethod(this.getClass(), methodName, |
186 | null); | |
187 | 0 | MethodUtil.invoke(method, this, null); |
188 | 0 | } |
189 | ||
190 | } |