1 7 package com.inversoft.verge.mvc; 8 9 10 import javax.servlet.http.HttpServletRequest ; 11 import javax.servlet.http.HttpServletResponse ; 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 95 public class DefaultMVCMediator implements MVCMediator { 96 97 100 private static final Logger logger = Logger.getLogger(DefaultMVCMediator.class); 101 102 103 106 public DefaultMVCMediator() { 107 } 108 109 110 121 public void mediate(HttpServletRequest request, HttpServletResponse response) 122 throws MVCException { 123 124 String 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 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 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 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 modelParser.preExecute(mvcRequest); 207 controllerParser.preExecute(mvcRequest); 208 validatorParser.preExecute(mvcRequest); 209 210 modelParser.execute(mvcRequest); 212 213 validatorParser.execute(mvcRequest); 215 216 controllerParser.execute(mvcRequest); 218 } 219 } 220 | Popular Tags |