1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.smartlab.web;
24
25 import java.io.InputStream;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Locale;
30 import java.util.MissingResourceException;
31 import java.util.Properties;
32
33 import javax.servlet.ServletContext;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36
37 import net.smartlab.web.bean.Valorizer;
38
39 import org.apache.commons.beanutils.locale.LocaleBeanUtilsBean;
40 import org.apache.commons.beanutils.locale.LocaleConvertUtilsBean;
41 import org.apache.commons.collections.FastHashMap;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.apache.struts.Globals;
45 import org.apache.struts.action.ActionErrors;
46 import org.apache.struts.action.ActionForm;
47 import org.apache.struts.action.ActionForward;
48 import org.apache.struts.action.ActionMapping;
49 import org.apache.struts.action.ActionMessage;
50 import org.apache.struts.action.ActionMessages;
51 import org.apache.struts.action.DynaActionForm;
52
53
54
55
56
57
58
59
60
61
62
63 public abstract class Action extends org.apache.struts.action.Action {
64
65
66
67
68 public final static ActionForward DEFAULT_FORWARD = new ActionForward("default");
69
70
71
72
73 protected final Log logger = LogFactory.getLog(this.getClass());
74
75
76
77
78
79
80
81
82
83
84
85 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
86 HttpServletResponse response) throws Exception {
87 if (logger.isDebugEnabled()) {
88 logger.debug("execute(" + mapping.getPath() + ") - start");
89 }
90 ActionForward forward = null;
91 if (super.isCancelled(request)) {
92 if (logger.isDebugEnabled()) {
93 logger.debug("execute(" + mapping.getPath() + ") - cancel");
94 }
95 forward = this.cancel(form, request, response, mapping);
96 if (forward == DEFAULT_FORWARD) {
97 return mapping.getInputForward();
98 } else {
99 return forward;
100 }
101 }
102 forward = this.execute(form, request, response, mapping);
103 try {
104 if (request.getAttribute(Globals.ERROR_KEY) != null && forward == DEFAULT_FORWARD) {
105 if (logger.isDebugEnabled()) {
106 logger.debug("execute(" + mapping.getPath() + ") - errors");
107 if (logger.isTraceEnabled()) {
108 logger.trace(" " + request.getAttribute(Globals.ERROR_KEY));
109 }
110 }
111 return mapping.getInputForward();
112 }
113 return forward;
114 } finally {
115 if (logger.isTraceEnabled()) {
116 logger.trace(" forward = " + forward);
117 }
118 }
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132 protected abstract ActionForward execute(ActionForm form, HttpServletRequest request, HttpServletResponse response,
133 ActionMapping mapping) throws Exception;
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 protected ActionForward cancel(ActionForm form, HttpServletRequest request, HttpServletResponse response,
150 ActionMapping mapping) throws Exception {
151 return DEFAULT_FORWARD;
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166 protected void valorize(ActionForm form, Object bean, Locale locale) throws ActionException {
167 try {
168 if (logger.isDebugEnabled()) {
169 logger.debug("valorize(" + form + ", " + bean + ") - start");
170 }
171
172
173 Valorizer.copy(form, bean, locale);
174 } catch (Exception e) {
175 logger.debug("valorize(" + form + ", " + bean + ") - error", e);
176 throw new ActionException("action.error.valorize", e);
177 }
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192 protected void populate(ActionForm form, Object bean, Locale locale) throws ActionException {
193 try {
194 if (logger.isDebugEnabled()) {
195 logger.debug("populate(" + form + ", " + bean + ") - start");
196 }
197
198
199 Valorizer.copy(bean, form, locale);
200 } catch (Exception e) {
201 logger.debug("populate(" + form + ", " + bean + ") - error", e);
202 throw new ActionException("action.error.populate", e);
203 }
204 }
205
206
207
208
209
210
211 private LocaleBeanUtilsBean getConverter(Locale locale) {
212 LocaleConvertUtilsBean converter = new LocaleConvertUtilsBean() {
213 protected FastHashMap create(Locale locale) {
214 FastHashMap converter = super.create(locale);
215 converter.setFast(false);
216
217 converter.setFast(true);
218 return converter;
219 }
220 };
221 converter.setDefaultLocale(locale);
222 return new LocaleBeanUtilsBean(converter);
223 }
224
225
226
227
228
229
230
231
232
233 public void reset(ActionForm form, HttpServletRequest request, ActionMapping mapping) {
234 if (form instanceof DynaActionForm) {
235 ((DynaActionForm)form).initialize(mapping);
236 } else {
237 form.reset(mapping, request);
238 }
239 }
240
241
242
243
244
245
246
247 protected boolean hasErrors(HttpServletRequest request) {
248 return request.getAttribute(Globals.ERROR_KEY) != null;
249 }
250
251
252
253
254
255
256
257 protected void addError(ActionMessage message, HttpServletRequest request) {
258 this.addError(ActionMessages.GLOBAL_MESSAGE, message, request);
259 }
260
261
262
263
264
265
266
267
268 protected void addError(String property, ActionMessage message, HttpServletRequest request) {
269 ActionErrors errors = (ActionErrors)request.getAttribute(Globals.ERROR_KEY);
270 if (errors == null) {
271 errors = new ActionErrors();
272 request.setAttribute(Globals.ERROR_KEY, errors);
273 }
274 errors.add(property, message);
275 }
276
277
278
279
280
281
282
283
284 public boolean isChecked(String name, HttpServletRequest request) {
285 if ("on".equalsIgnoreCase(request.getParameter("group"))
286 || "true".equalsIgnoreCase(request.getParameter("group"))
287 || "yes".equalsIgnoreCase(request.getParameter("group"))) {
288 return true;
289 } else {
290 return false;
291 }
292 }
293
294
295
296
297
298
299 protected ServletContext getServletContext() {
300 return super.getServlet().getServletContext();
301 }
302
303
304
305
306
307
308
309 protected String getRealPath(String path) {
310 return this.getServletContext().getRealPath(path);
311 }
312
313
314
315
316
317
318 protected Properties getProperties() {
319 Class type = this.getClass();
320 List inputs = new ArrayList();
321 Properties result = new Properties();
322 while (type.getSuperclass().isAssignableFrom(Action.class)) {
323 try {
324 InputStream in = this.getClass().getResourceAsStream(this.getClass().getName() + ".prop");
325 inputs.add(in);
326 } catch (Exception e) {
327 if (logger.isDebugEnabled()) {
328 logger.debug("getProperties() - descend", e);
329 }
330 }
331 type = type.getSuperclass();
332 }
333 Iterator iterator = inputs.iterator();
334 while (iterator.hasNext()) {
335 try {
336 InputStream in = (InputStream)iterator.next();
337 Properties properties = new Properties();
338 properties.load(in);
339 result.putAll(properties);
340 } catch (Exception e) {
341 if (logger.isDebugEnabled()) {
342 logger.debug("getProperties() - load", e);
343 }
344 }
345 }
346 throw new MissingResourceException("No properties defined for action", this.getClass().getName(), null);
347 }
348 }