KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > DefaultMVCMediator


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;
8
9
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.http.HttpServletResponse JavaDoc;
12
13 import org.apache.log4j.Logger;
14 import org.apache.log4j.Priority;
15
16 import com.inversoft.verge.mvc.controller.ControllerParser;
17 import com.inversoft.verge.mvc.model.ModelParser;
18 import com.inversoft.verge.mvc.validator.ValidatorParser;
19
20
21 /**
22  * <p>
23  * This class is the default implementation of the MVCMediator
24  * interface. The default functionality two steps:
25  * </p>
26  *
27  * <ul>
28  * <li>
29  * Determine the Model implementation and locate the
30  * ModelParser to call. Call this class to handle the population
31  * of the model instances from the HttpServletRequest or any
32  * other applicable sources.
33  * </li>
34  * <li>
35  * Determine the Controller implemenation and locate the
36  * ControllerParser to call. Call this class to handle the
37  * parsing of the HttpServletRequest and controller logic.
38  * </li>
39  * </ul>
40  *
41  * <p>
42  * This mediator uses the MVCRepository to lookup the
43  * ModelParser instance and the ControllerParser instance.
44  * These are determined in by the following methods:
45  * </p>
46  *
47  * <h1>Model</h1>
48  * <ul>
49  * <li>
50  * Check the HttpServletRequest for a parameter named
51  * modelType
52  * </li>
53  * <li>
54  * Check the ServletContext for a context parameter named
55  * DEFAULT_MODEL_TYPE which determines the default type
56  * of controller to use
57  * </li>
58  * <li>
59  * Produce an error
60  * </li>
61  * </ul>
62  *
63  * <h1>Controller</h1>
64  * <ul>
65  * <li>
66  * Check the HttpServletRequest for a parameter named
67  * controllerType
68  * </li>
69  * <li>
70  * Check the ServletContext for a context parameter named
71  * DEFAULT_CONTROLLER_TYPE which determines the default type
72  * of controller to use
73  * </li>
74  * <li>
75  * Produce an error
76  * </li>
77  * </ul>
78  *
79  * <p>
80  * Based on the type, an instance of either the {@link
81  * com.inversoft.verge.mvc.model.ModelParser
82  * com.inversoft.verge.mvc.model.ModelParser} or {@link
83  * com.inversoft.verge.mvc.controller.ControllerParser
84  * com.inversoft.verge.mvc.controller.ControllerParser}
85  * class is retrieved from the MVCRepository, if one exists.
86  * If one does not exist, an exception is thrown. These
87  * instances are called to handle the model and controller
88  * components of the MVC System.
89  * </p>
90  *
91  * @author Brian Pontarelli
92  * @since 2.0
93  * @version 2.0
94  */

95 public class DefaultMVCMediator implements MVCMediator {
96
97     /**
98      * This classes logger
99      */

100     private static final Logger logger = Logger.getLogger(DefaultMVCMediator.class);
101
102
103     /**
104      * Instantiates a new <code>DefaultMVCMediator</code>
105      */

106     public DefaultMVCMediator() {
107     }
108
109
110     /**
111      * Handles the mediation of the entire MVC system. The method of mediation
112      * is described in detail in the class comment.
113      *
114      * @param request The HttpServletRequest from which the clients request
115      * information may be retrieved
116      * @param response The HttpServletResponse which can be used to redirect
117      * or forward the the client to another view
118      * @throws com.inversoft.verge.mvc.MVCException If the model or controller type could not be
119      * determined or the model or controller parsers threw any exceptions
120      */

121     public void mediate(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
122     throws MVCException {
123
124         String JavaDoc modelName = request.getParameter(MVCConstants.MODEL_REQUEST_PARAM);
125         if (modelName == null) {
126             modelName = request.getSession().getServletContext().getInitParameter(
127                 MVCConstants.MODEL_CONTEXT_PARAM);
128         }
129
130         if (modelName == null) {
131             if (logger.isEnabledFor(Priority.WARN)) {
132                 logger.warn("Defaulting to default ModelParser. To get rid of this" +
133                     " warning you should add the " + MVCConstants.MODEL_CONTEXT_PARAM +
134                     " context parameter to web.xml with a value of 'default' or the" +
135                     " value you want");
136             }
137             modelName = MVCConstants.DEFAULT_NAME;
138         }
139
140         String JavaDoc controllerName = request.getParameter(MVCConstants.CONTROLLER_REQUEST_PARAM);
141         if (controllerName == null) {
142             controllerName = request.getSession().getServletContext().getInitParameter(
143                 MVCConstants.CONTROLLER_CONTEXT_PARAM);
144         }
145
146         if (controllerName == null) {
147             if (logger.isEnabledFor(Priority.WARN)) {
148                 logger.warn("Defaulting to default ControllerParser. To get rid of this" +
149                     " warning you should add the " + MVCConstants.CONTROLLER_CONTEXT_PARAM +
150                     " context parameter to web.xml with a value of 'default' or the" +
151                     " value you want");
152             }
153             controllerName = MVCConstants.DEFAULT_NAME;
154         }
155         
156         String JavaDoc validatorName = request.getParameter(MVCConstants.VALIDATOR_REQUEST_PARAM);
157         if (validatorName == null) {
158             validatorName = request.getSession().getServletContext().getInitParameter(
159                 MVCConstants.VALIDATOR_CONTEXT_PARAM);
160         }
161
162         if (validatorName == null) {
163             if (logger.isEnabledFor(Priority.WARN)) {
164                 logger.warn("Defaulting to default ValidatorParser. To get rid of this" +
165                     " warning you should add the " + MVCConstants.VALIDATOR_CONTEXT_PARAM +
166                     " context parameter to web.xml with a value of 'default' or the" +
167                     " value you want");
168             }
169             validatorName = MVCConstants.DEFAULT_NAME;
170         }
171
172         ModelParser modelParser = MVCRegistry.lookupModelParser(modelName);
173         if (modelParser == null) {
174             logger.error("No ModelParser registered under the type: " + modelName);
175             throw new MVCException("No ModelParser registered under the type: " +
176                 modelName);
177         }
178
179         ControllerParser controllerParser =
180             MVCRegistry.lookupControllerParser(controllerName);
181         if (controllerParser == null) {
182             logger.error("No ControllerParser registered under the name: " +
183                 controllerName);
184             throw new MVCException("No ControllerParser registered under the name: " +
185                 controllerName);
186         }
187         
188         ValidatorParser validatorParser =
189             MVCRegistry.lookupValidatorParser(validatorName);
190         if (validatorParser == null) {
191             logger.error("No ValidatorParser registered under the name: " +
192                 validatorName);
193             throw new MVCException("No ValidatorParser registered under the name: " +
194                 validatorName);
195         }
196
197         // Create the MVCRequest to be passed around
198
MVCRequest mvcRequest = new MVCRequest(request, response);
199         request.setAttribute(MVCConstants.MVC_REQUEST_KEY, mvcRequest);
200         mvcRequest.setControllerParser(controllerParser);
201         mvcRequest.setModelParser(modelParser);
202         mvcRequest.setValidatorParser(validatorParser);
203
204         // Call the Controller and Model parsers to allow them to setup the
205
// MVCRequest as they need to
206
modelParser.preExecute(mvcRequest);
207         controllerParser.preExecute(mvcRequest);
208         validatorParser.preExecute(mvcRequest);
209
210         // Delegate control to the ModelParser
211
modelParser.execute(mvcRequest);
212
213         // Delegate control to the ValidatorParser
214
validatorParser.execute(mvcRequest);
215
216         // Now delegate controll to the ControllerParser
217
controllerParser.execute(mvcRequest);
218     }
219 }
220
Popular Tags