KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > form > FormControllerHandler


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.controller.form;
8
9
10 import java.lang.reflect.Method JavaDoc;
11
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import javax.servlet.http.HttpServletResponse JavaDoc;
14
15 import org.apache.log4j.Logger;
16
17 import com.inversoft.beans.BeanException;
18 import com.inversoft.util.ReflectionException;
19 import com.inversoft.util.ReflectionTools;
20 import com.inversoft.verge.mvc.MVCConstants;
21 import com.inversoft.verge.mvc.MVCException;
22 import com.inversoft.verge.mvc.MVCRequest;
23 import com.inversoft.verge.mvc.controller.Action;
24 import com.inversoft.verge.mvc.controller.ControllerHandler;
25 import com.inversoft.verge.mvc.controller.DefaultLongTxnHandler;
26 import com.inversoft.verge.mvc.controller.GenericResult;
27 import com.inversoft.verge.mvc.controller.LongTxnHandler;
28 import com.inversoft.verge.mvc.controller.LongTxnSetup;
29 import com.inversoft.verge.mvc.controller.Result;
30 import com.inversoft.verge.mvc.controller.form.config.ActionConfig;
31 import com.inversoft.verge.mvc.controller.form.config.FormConfig;
32 import com.inversoft.verge.mvc.controller.form.config.FormConfigStruct;
33 import com.inversoft.verge.mvc.controller.form.config.FormMVCConfigRegistry;
34 import com.inversoft.verge.mvc.controller.form.config.MappingConfig;
35 import com.inversoft.verge.mvc.controller.form.config.ValidatorConfig;
36
37
38 /**
39  * <p>
40  * This class is the controller handler for the form based
41  * MVC. This attempts to find the FormConfig for the form
42  * that was submitted, if it is found, then it tries to find
43  * an ActionConfig within that form using the controller
44  * definition, which is the action. Finally, it calls the
45  * action handler associated with the ActionConfig.
46  * </p>
47  *
48  * @author Brian Pontarelli
49  * @since 2.0
50  * @version 2.0
51  */

52 public class FormControllerHandler implements ControllerHandler {
53
54     /**
55      * This classes logger
56      */

57     private static final Logger logger = Logger.getLogger(FormControllerHandler.class);
58
59     /**
60      * The long transaction handler
61      */

62     private volatile LongTxnHandler handler = new DefaultLongTxnHandler();
63
64
65     /**
66      * <p>
67      * Registers a new long transaction handler with this controller handler. This
68      * long transaction handler is responsible for handling any actions that are
69      * marked as long transactions in the configuration files. Only one long
70      * transaction handler can be registered at a time. Each susquent call to this
71      * method, overrides the previous handler.
72      * </p>
73      *
74      * <p>
75      * By default the {@link DefaultLongTxnHandler DefaultLongTxnHandler} class
76      * is the registered handler.
77      * </p>
78      *
79      * @param handler The new handler
80      */

81     public void registerHandler(LongTxnHandler handler) {
82         this.handler = handler;
83     }
84
85     /**
86      * Unregisters the single long transaction handler if one was ever registered.
87      */

88     public void unregisterHandler() {
89         this.handler = null;
90     }
91
92     /**
93      * This retrieves the flags from the configuration that determine if the
94      * model values should be set or if the validation should occur.
95      *
96      * @param mvcRequest The MVCRequest to setup
97      * @throws com.inversoft.verge.mvc.MVCException If there were any problems during setting
98      */

99     public void preExecute(MVCRequest mvcRequest) throws MVCException {
100
101         // Look for the form/action
102
FormMVCMetaData md = FormURLTools.decodeURL(
103             mvcRequest.getControllerInfo().getURLValues());
104
105         HttpServletRequest JavaDoc request = mvcRequest.getRequest();
106         if (md.getAction() == null) {
107             FormURLTools.locateAction(request, md);
108             if (md.getAction() == null) {
109                 throw new MVCException("Unable to locate valid action");
110             }
111         }
112
113         FormConfig config = null;
114         if (md.getForm() != null) {
115             config = (FormConfig) md.findFormConfig(request);
116         }
117
118         String JavaDoc actStr = md.getAction();
119         ActionConfig actionConfig =
120             FormMVCConfigRegistry.getInstance(request).lookupAction(actStr,
121                 config);
122         if (actionConfig == null) {
123             if (config == null) {
124                 throw new MVCException("Invalid action named: " + actStr);
125             } else {
126                 throw new MVCException("Invalid action named: " + actStr +
127                     " for form named: " + config.getName());
128             }
129         }
130
131         // Store the objects above so that they are not created later
132
FormConfigStruct struct = new FormConfigStruct(config, actionConfig, md);
133         mvcRequest.setConfiguration(struct);
134
135         if (config != null) {
136             mvcRequest.setModelEnabled(actionConfig.isModelEnabled());
137             mvcRequest.setValidationEnabled(actionConfig.isValidationEnabled());
138             mvcRequest.addValidatorHandlerToCall(MVCConstants.FORM_NAME);
139         }
140
141         // Create the action
142
HttpServletResponse JavaDoc response = mvcRequest.getResponse();
143         FormAction formAction = new FormAction(config, actStr, request, response,
144             mvcRequest.getRequestContext());
145         mvcRequest.setAction(formAction);
146     }
147
148     /**
149      * Handles the controller part of the Form-Based MVC. This method finds the
150      * action handler and calls with. It also creates the FormAction to pass to
151      * the action handler.
152      */

153     public Result execute(MVCRequest mvcRequest) throws MVCException {
154
155         HttpServletRequest JavaDoc request = mvcRequest.getRequest();
156         HttpServletResponse JavaDoc response = mvcRequest.getResponse();
157         FormConfigStruct struct = (FormConfigStruct) mvcRequest.getConfiguration();
158         FormConfig config = (FormConfig) struct.baseFormConfig;
159
160         // Check if the validation failed and there was a mapping
161
if (struct.failedValidatorConfig != null) {
162             return ((ValidatorConfig) struct.failedValidatorConfig).getFailureMapping();
163         }
164
165         // If there is no correct action, bail
166
ActionConfig actionConfig = struct.actionConfig;
167         Action action = mvcRequest.getAction();
168         assert (actionConfig != null) : "actionConfig == null";
169
170         preHandle(request, response, config, actionConfig, action);
171
172         Object JavaDoc result = null;
173         try {
174             result = actionConfig.getHandle().invokeHandle(action, request);
175         } catch (BeanException be) {
176             throw new MVCException(be);
177         }
178
179         return postHandle(request, response, config, actionConfig, action, result);
180     }
181
182     /**
183      * Handle the preHandle method call, and the long txn rendering if applicable.
184      */

185     void preHandle(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
186             FormConfig config, ActionConfig actionConfig, Action action)
187     throws MVCException {
188
189         // Call the preHandle method if it exists
190
Method JavaDoc preHandle = actionConfig.getPreHandleMethod();
191         if (preHandle != null) {
192             Object JavaDoc handler;
193             try {
194                 handler = actionConfig.getHandle().getWebBean().getInstance(request);
195             } catch (BeanException be) {
196                 throw new MVCException(be);
197             }
198
199             try {
200                 ReflectionTools.invokeMethod(preHandle, handler,
201                     new Object JavaDoc[]{action});
202             } catch (ReflectionException re) {
203                 throw new MVCException(re);
204             }
205         }
206
207         // Draw out the long transaction URL using a dynamic include
208
if (actionConfig.isLongTxnEnabled()) {
209             String JavaDoc url = actionConfig.getLongTxnStartURL();
210             if (url == null) {
211                 if (config != null) {
212                     LongTxnSetup setup = config.getLongTxnSetup();
213                     if (setup != null) {
214                         url = setup.getLongTxnStartURL(action);
215                     }
216                 }
217             }
218
219             if (handler != null) {
220                 handler.handleStartLongTxn(request, response, url);
221             }
222         }
223     }
224
225     /**
226      * Handle the postHandle method call, the result mapping and the long txn
227      * rendering if applicable.
228      */

229     Result postHandle(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
230             FormConfig config, ActionConfig actionConfig, Action action,
231             Object JavaDoc result)
232     throws MVCException {
233
234         // Call the postHandle method if it exists
235
Method JavaDoc postHandle = actionConfig.getPostHandleMethod();
236         if (postHandle != null) {
237
238             Object JavaDoc handler;
239             try {
240                 handler = actionConfig.getHandle().getWebBean().getInstance(request);
241             } catch (BeanException be) {
242                 throw new MVCException(be);
243             }
244
245             try {
246                 ReflectionTools.invokeMethod(postHandle, handler,
247                     new Object JavaDoc[]{action, result});
248             } catch (ReflectionException re) {
249                 throw new MVCException(re);
250             }
251         }
252
253         // Decode the result
254
Result ret = null;
255         if (result != null && result instanceof String JavaDoc) {
256
257             if (logger.isDebugEnabled()) {
258                 logger.debug("Looking up mapping named: " + result);
259             }
260
261             // The string should be a mapping, look it up
262
MappingConfig mapping =
263                 FormMVCConfigRegistry.getInstance(request).lookupMapping(
264                     (String JavaDoc) result, config);
265             if (mapping == null) {
266                 throw new MVCException("Invalid mapping name: " + result);
267             }
268
269             ret = mapping;
270         }
271
272         // Draw out the long transaction end URL using a dynamic include
273
if (actionConfig.isLongTxnEnabled() && handler != null) {
274             String JavaDoc url = actionConfig.getLongTxnEndURL();
275             if (url == null) {
276                 if (config != null) {
277                     LongTxnSetup setup = config.getLongTxnSetup();
278                     if (setup != null) {
279                         url = setup.getLongTxnEndURL(action);
280                     }
281                 }
282             }
283
284             // The result can not be a forward, so we need to create a new one
285
// with ret's data
286
if (ret != null && ret.isForward()) {
287                 ret = new GenericResult(ret.getURL(), ret.getCategory(), false);
288             }
289
290             handler.handleEndLongTxn(request, response, url, ret);
291
292             // Empty the result, we don't need it anymore
293
ret = null;
294         }
295
296         return ret;
297     }
298 }
Popular Tags