1 5 6 package org.infohazard.maverick.flow; 7 8 import java.util.HashMap ; 9 import java.util.Map ; 10 11 import javax.servlet.ServletConfig ; 12 import javax.servlet.ServletContext ; 13 import javax.servlet.ServletException ; 14 import javax.servlet.http.HttpServletRequest ; 15 import javax.servlet.http.HttpServletResponse ; 16 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 import org.infohazard.maverick.Dispatcher; 20 21 28 public class MaverickContext implements ControllerContext, ViewContext, TransformContext 29 { 30 35 private static Log log = LogFactory.getLog(MaverickContext.class); 36 37 42 protected Dispatcher dispatcher; 43 44 49 protected HttpServletRequest request; 50 51 56 protected HttpServletResponse response; 57 58 65 protected Object model; 66 67 72 protected Map controllerParams; 73 74 79 protected Map viewParams; 80 81 86 protected Map transformParams; 87 88 94 protected Transform[] transforms; 95 96 101 protected int nextTransform = 0; 102 103 108 protected int transformCount; 109 110 116 public MaverickContext(Dispatcher disp, HttpServletRequest req, HttpServletResponse res) 117 { 118 this.dispatcher = disp; 119 this.request = req; 120 this.response = res; 121 } 122 123 132 public HttpServletRequest getRequest() 133 { 134 return this.request; 135 } 136 137 145 public HttpServletResponse getRealResponse() 146 { 147 return this.response; 148 } 149 150 156 public HttpServletResponse getResponse() 157 { 158 return this.getRealResponse(); 159 } 160 161 168 public ServletConfig getServletConfig() 169 { 170 return this.dispatcher.getServletConfig(); 171 } 172 173 178 public ServletContext getServletContext() 179 { 180 return this.dispatcher.getServletContext(); 181 } 182 183 186 public void setControllerParam(String name, Object value) 187 { 188 if (this.controllerParams == null) 189 this.controllerParams = new HashMap (); 190 191 this.controllerParams.put(name, value); 192 } 193 194 197 public void setViewParam(String name, Object value) 198 { 199 if (this.viewParams == null) 200 this.viewParams = new HashMap (); 201 202 this.viewParams.put(name, value); 203 } 204 205 208 public void setTransformParam(String name, Object value) 209 { 210 if (this.transformParams == null) 211 this.transformParams = new HashMap (); 212 213 this.transformParams.put(name, value); 214 } 215 216 219 public void putAllControllerParams(Map addParams) 220 { 221 if (this.controllerParams == null) 222 this.controllerParams = new HashMap (); 223 224 this.controllerParams.putAll(addParams); 225 } 226 227 230 public void putAllViewParams(Map addParams) 231 { 232 if (this.viewParams == null) 233 this.viewParams = new HashMap (); 234 235 this.viewParams.putAll(addParams); 236 } 237 238 241 public void putAllTransformParams(Map addParams) 242 { 243 if (this.transformParams == null) 244 this.transformParams = new HashMap (); 245 246 this.transformParams.putAll(addParams); 247 } 248 249 252 public void setModel(Object mod) 253 { 254 this.model = mod; 255 } 256 257 261 public Object getModel() 262 { 263 return this.model; 264 } 265 266 269 public Map getControllerParams() 270 { 271 return this.controllerParams; 272 } 273 274 277 public Map getViewParams() 278 { 279 return this.viewParams; 280 } 281 282 285 public Map getTransformParams() 286 { 287 return this.transformParams; 288 } 289 290 292 public void setTransforms(Transform[] trans) 293 { 294 this.transforms = trans; 295 296 this.transformCount = determineMaxTransforms(); 298 299 if (this.transformCount > this.transforms.length) 300 this.transformCount = this.transforms.length; 301 302 if (log.isDebugEnabled()) 303 log.debug("Set " + trans.length + " transform(s), of which " 304 + this.transformCount + " will be executed"); 305 } 306 307 311 public TransformStep getNextStep() throws ServletException 312 { 313 if (log.isDebugEnabled()) 314 log.debug("Creating transform step " + this.nextTransform); 315 316 if (this.nextTransform >= this.transformCount) 317 { 318 log.debug("...which is the LastStep"); 319 return new LastStep(this); 320 } 321 else 322 { 323 Transform t = this.transforms[this.nextTransform++]; 324 return t.createStep(this); 325 } 326 } 327 328 331 public boolean halting() 332 { 333 return (this.transformCount != this.transforms.length); 334 } 335 336 349 protected int determineMaxTransforms() 350 { 351 if (this.dispatcher.getLimitTransformsParam() == null) 352 return Integer.MAX_VALUE; 353 354 String maxTransStr = this.request.getParameter(this.dispatcher.getLimitTransformsParam()); 355 356 if (maxTransStr == null) 357 return Integer.MAX_VALUE; 358 359 return Integer.parseInt(maxTransStr); 360 } 361 362 } 363 | Popular Tags |