Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Action |
|
| 1.8181818181818181;1.818 |
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 | 45 | 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 | 1 | return errors; |
47 | } | |
48 | ||
49 | /** | |
50 | * アクションエラーオブジェクトをセットします。 | |
51 | * | |
52 | * @param errors | |
53 | * アクションエラーオブジェクト | |
54 | */ | |
55 | public void setErrors(final ActionErrors errors) { | |
56 | 4 | this.errors = errors; |
57 | 4 | } |
58 | ||
59 | /** | |
60 | * 揮発性メッセージを取得します。 | |
61 | * | |
62 | * @return 揮発性メッセージ | |
63 | */ | |
64 | public Map<String, Object> getFlash() { | |
65 | 1 | return flash; |
66 | } | |
67 | ||
68 | /** | |
69 | * 揮発性メッセージをセットします。 | |
70 | * | |
71 | * @param flash | |
72 | * 揮発性メッセージ | |
73 | */ | |
74 | public void setFlash(final Map<String, Object> flash) { | |
75 | 4 | this.flash = flash; |
76 | 4 | } |
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 | 3 | if (actionMethod.isAnnotationPresent(InitializeMethod.class)) { |
93 | 1 | final InitializeMethod initializeMethod = actionMethod |
94 | .getAnnotation(InitializeMethod.class); | |
95 | 1 | final String methodName = initializeMethod.value(); |
96 | 1 | this.invoke(methodName); |
97 | 1 | } else { |
98 | 2 | this.initialize(); |
99 | } | |
100 | 3 | } |
101 | ||
102 | /** | |
103 | * アクションメソッドが {@link InitializeMethod} で装飾されていない場合に | |
104 | * {@link #invokeInitializeMethod(Method)} から呼ばれるメソッドです。 | |
105 | */ | |
106 | protected void initialize() { | |
107 | 1 | } |
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 | 3 | if (actionMethod.isAnnotationPresent(PreRenderMethod.class)) { |
124 | 1 | final PreRenderMethod preRenderMethod = actionMethod |
125 | .getAnnotation(PreRenderMethod.class); | |
126 | 1 | final String methodName = preRenderMethod.value(); |
127 | 1 | this.invoke(methodName); |
128 | 1 | } else { |
129 | 2 | this.prerender(); |
130 | } | |
131 | 3 | } |
132 | ||
133 | /** | |
134 | * アクションメソッドが {@link PreRenderMethod} で装飾されていない場合に | |
135 | * {@link #invokePreRenderMethod(Method)} から呼ばれるメソッドです。 | |
136 | */ | |
137 | protected void prerender() { | |
138 | 1 | } |
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 | 3 | if (actionMethod.isAnnotationPresent(PostRenderMethod.class)) { |
155 | 1 | final PostRenderMethod postRenderMethod = actionMethod |
156 | .getAnnotation(PostRenderMethod.class); | |
157 | 1 | final String methodName = postRenderMethod.value(); |
158 | 1 | this.invoke(methodName); |
159 | 1 | } else { |
160 | 2 | this.postrender(); |
161 | } | |
162 | 3 | } |
163 | ||
164 | /** | |
165 | * アクションメソッドが {@link PostRenderMethod} で装飾されていない場合に | |
166 | * {@link #invokePostRenderMethod(Method)} から呼ばれるメソッドです。 | |
167 | */ | |
168 | protected void postrender() { | |
169 | 1 | } |
170 | ||
171 | /** | |
172 | * このアクションに定義された指定されたメソッド名のメソッドを実行します。 | |
173 | * | |
174 | * @param methodName | |
175 | * メソッド名 | |
176 | */ | |
177 | protected void invoke(final String methodName) { | |
178 | try { | |
179 | 3 | final Method method = this.getClass().getMethod(methodName); |
180 | 3 | method.invoke(this); |
181 | 0 | } catch (final NoSuchMethodException e) { |
182 | 0 | throw new ActionException(e); |
183 | 0 | } catch (final IllegalAccessException e) { |
184 | 0 | throw new ActionException(e); |
185 | 0 | } catch (final InvocationTargetException e) { |
186 | 0 | throw new ActionException(e); |
187 | 3 | } |
188 | 3 | } |
189 | ||
190 | } |