KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > action > AbstractAction


1 /*
2  * Copyright 2002-2006 the original author or authors.
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.webflow.action;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.springframework.beans.factory.BeanInitializationException;
21 import org.springframework.beans.factory.InitializingBean;
22 import org.springframework.util.ClassUtils;
23 import org.springframework.webflow.core.collection.AttributeMap;
24 import org.springframework.webflow.execution.Action;
25 import org.springframework.webflow.execution.Event;
26 import org.springframework.webflow.execution.RequestContext;
27 import org.springframework.webflow.execution.support.EventFactorySupport;
28
29 /**
30  * Base action that provides assistance commonly needed by action
31  * implementations. This includes:
32  * <ul>
33  * <li>Implementing {@link InitializingBean} to receive an init callback
34  * when deployed within a Spring bean factory.
35  * <li>Exposing convenient event factory methods to create common result
36  * {@link Event} objects such as "success" and "error".
37  * <li>A hook for inserting action pre and post execution logic.
38  * </ul>
39  *
40  * @author Keith Donald
41  * @author Erwin Vervaet
42  */

43 public abstract class AbstractAction implements Action, InitializingBean {
44
45     /**
46      * Logger, usable in subclasses.
47      */

48     protected final Log logger = LogFactory.getLog(getClass());
49
50     /**
51      * Returns the helper delegate for creating action execution result events.
52      * @return the event factory support
53      */

54     public EventFactorySupport getEventFactorySupport() {
55         return new EventFactorySupport();
56     }
57
58     public void afterPropertiesSet() throws Exception JavaDoc {
59         try {
60             initAction();
61         }
62         catch (Exception JavaDoc ex) {
63             throw new BeanInitializationException("Initialization of this Action failed: " + ex.getMessage(), ex);
64         }
65     }
66
67     /**
68      * Action initializing callback, may be overriden by subclasses to perform
69      * custom initialization logic.
70      * <p>
71      * Keep in mind that this hook will only be invoked when this action is
72      * deployed in a Spring application context since it uses the Spring
73      * {@link InitializingBean} mechanism to trigger action initialisation.
74      */

75     protected void initAction() throws Exception JavaDoc {
76     }
77
78     /**
79      * Returns a "success" result event.
80      */

81     protected Event success() {
82         return getEventFactorySupport().success(this);
83     }
84
85     /**
86      * Returns a "success" result event with the provided result object as a
87      * parameter.
88      * @param result the action success result
89      */

90     protected Event success(Object JavaDoc result) {
91         return getEventFactorySupport().success(this, result);
92     }
93
94     /**
95      * Returns an "error" result event.
96      */

97     protected Event error() {
98         return getEventFactorySupport().error(this);
99     }
100
101     /**
102      * Returns an "error" result event caused by the provided exception.
103      * @param e the exception that caused the error event, to be configured as
104      * an event attribute
105      */

106     protected Event error(Exception JavaDoc e) {
107         return getEventFactorySupport().error(this, e);
108     }
109
110     /**
111      * Returns a "yes" result event.
112      */

113     protected Event yes() {
114         return getEventFactorySupport().yes(this);
115     }
116
117     /**
118      * Returns a "no" result event.
119      */

120     protected Event no() {
121         return getEventFactorySupport().no(this);
122     }
123
124     /**
125      * Returns yes() if the boolean result is true, no() if false.
126      * @param booleanResult the boolean
127      * @return yes or no
128      */

129     protected Event result(boolean booleanResult) {
130         return getEventFactorySupport().event(this, booleanResult);
131     }
132
133     /**
134      * Returns a result event for this action with the specified identifier.
135      * Typically called as part of return, for example:
136      *
137      * <pre>
138      * protected Event doExecute(RequestContext context) {
139      * // do some work
140      * if (some condition) {
141      * return result(&quot;success&quot;);
142      * } else {
143      * return result(&quot;error&quot;);
144      * }
145      * }
146      * </pre>
147      *
148      * Consider calling the error() or success() factory methods for returning
149      * common results.
150      * @param eventId the result event identifier
151      * @return the action result event
152      */

153     protected Event result(String JavaDoc eventId) {
154         return getEventFactorySupport().event(this, eventId);
155     }
156
157     /**
158      * Returns a result event for this action with the specified identifier and
159      * the specified set of attributes. Typically called as part of return, for
160      * example:
161      *
162      * <pre>
163      * protected Event doExecute(RequestContext context) {
164      * // do some work
165      * AttributeMap resultAttributes = new AttributeMap();
166      * resultAttributes.put(&quot;name&quot;, &quot;value&quot;);
167      * if (some condition) {
168      * return result(&quot;success&quot;, resultAttributes);
169      * } else {
170      * return result(&quot;error&quot;, resultAttributes);
171      * }
172      * }
173      * </pre>
174      *
175      * Consider calling the error() or success() factory methods for returning
176      * common results.
177      * @param eventId the result event identifier
178      * @param resultAttributes the event attributes
179      * @return the action result event
180      */

181     protected Event result(String JavaDoc eventId, AttributeMap resultAttributes) {
182         return getEventFactorySupport().event(this, eventId, resultAttributes);
183     }
184
185     /**
186      * Returns a result event for this action with the specified identifier and
187      * a single attribute.
188      * @param eventId the result id
189      * @param resultAttributeName the attribute name
190      * @param resultAttributeValue the attribute value
191      * @return the action result event
192      */

193     protected Event result(String JavaDoc eventId, String JavaDoc resultAttributeName, Object JavaDoc resultAttributeValue) {
194         return getEventFactorySupport().event(this, eventId, resultAttributeName, resultAttributeValue);
195     }
196
197     public final Event execute(RequestContext context) throws Exception JavaDoc {
198         if (logger.isDebugEnabled()) {
199             logger.debug("Action '" + getActionNameForLogging() + "' beginning execution");
200         }
201         Event result = doPreExecute(context);
202         if (result == null) {
203             result = doExecute(context);
204             if (logger.isDebugEnabled()) {
205                 if (result != null) {
206                     logger.debug("Action '" + getActionNameForLogging() + "' completed execution; result is '" + result.getId() + "'");
207                 }
208                 else {
209                     logger.debug("Action '" + getActionNameForLogging() + "' completed execution; result is [null]");
210                 }
211             }
212             doPostExecute(context);
213         }
214         else {
215             if (logger.isInfoEnabled()) {
216                 logger.info("Action execution disallowed; pre-execution result is '" + result.getId() + "'");
217             }
218         }
219         return result;
220     }
221     
222     // subclassing hooks
223

224     /**
225      * Internal helper to return the name of this action for logging
226      * purposes. Defaults to the short class name.
227      * @see ClassUtils#getShortName(java.lang.Class)
228      */

229     protected String JavaDoc getActionNameForLogging() {
230         return ClassUtils.getShortName(getClass());
231     }
232
233     /**
234      * Pre-action-execution hook, subclasses may override. If this method
235      * returns a non-<code>null</code> event, the <code>doExecute()</code>
236      * method will <b>not</b> be called and the returned event will be used to
237      * select a transition to trigger in the calling action state. If this
238      * method returns <code>null</code>, <code>doExecute()</code> will be
239      * called to obtain an action result event.
240      * <p>
241      * This implementation just returns <code>null</code>.
242      * @param context the action execution context, for accessing and setting
243      * data in "flow scope" or "request scope"
244      * @return the non-<code>null</code> action result, in which case the
245      * <code>doExecute()</code> will not be called, or <code>null</code> if
246      * the <code>doExecute()</code> method should be called to obtain the
247      * action result
248      * @throws Exception an <b>unrecoverable</b> exception occured, either
249      * checked or unchecked
250      */

251     protected Event doPreExecute(RequestContext context) throws Exception JavaDoc {
252         return null;
253     }
254
255     /**
256      * Template hook method subclasses should override to encapsulate their
257      * specific action execution logic.
258      * @param context the action execution context, for accessing and setting
259      * data in "flow scope" or "request scope"
260      * @return the action result event
261      * @throws Exception an <b>unrecoverable</b> exception occured, either
262      * checked or unchecked
263      */

264     protected abstract Event doExecute(RequestContext context) throws Exception JavaDoc;
265
266     /**
267      * Post-action execution hook, subclasses may override. Will only be called
268      * if <code>doExecute()</code> was called, e.g. when <code>doPreExecute()</code>
269      * returned <code>null</code>.
270      * <p>
271      * This implementation does nothing.
272      * @param context the action execution context, for accessing and setting
273      * data in "flow scope" or "request scope"
274      * @throws Exception an <b>unrecoverable</b> exception occured, either
275      * checked or unchecked
276      */

277     protected void doPostExecute(RequestContext context) throws Exception JavaDoc {
278     }
279 }
Popular Tags