KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infohazard > maverick > flow > MaverickContext


1 /*
2  * $Id: MaverickContext.java,v 1.5 2004/06/07 20:39:00 eelco12 Exp $
3  * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/flow/MaverickContext.java,v $
4  */

5
6 package org.infohazard.maverick.flow;
7
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import javax.servlet.ServletConfig JavaDoc;
12 import javax.servlet.ServletContext JavaDoc;
13 import javax.servlet.ServletException JavaDoc;
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletResponse JavaDoc;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.infohazard.maverick.Dispatcher;
20
21 /**
22  * <p>
23  * MaverickContext is the concrete class which implements all the other
24  * contexts.
25  * Having one object minimizes object creation and data copying.
26  * </p>
27  */

28 public class MaverickContext implements ControllerContext, ViewContext, TransformContext
29 {
30     /**
31      * <p>
32      * MaverickContext logger.
33      * </p>
34      */

35     private static Log log = LogFactory.getLog(MaverickContext.class);
36     
37     /**
38      * <p>
39      * Our {@link Dispatcher} instance.
40      * </p>
41      */

42     protected Dispatcher dispatcher;
43
44     /**
45      * <p>
46      * Our {@link HttpServletRequest} instance.
47      * </p>
48      */

49     protected HttpServletRequest JavaDoc request;
50
51     /**
52      * <p>
53      * Our {@link HttpServletResponse} instance.
54      * </p>
55      */

56     protected HttpServletResponse JavaDoc response;
57     
58     /**
59      * <p>
60      * Our instance of the "model" object that the {@link Controller} exposes
61      * to a {@link View}, so that dynamic data can be rendered.
62      * Most Maverick Controller will set the model object during processing.
63      * </p>
64      */

65     protected Object JavaDoc model;
66     
67     /**
68      * <p>
69      * Our {@link Controller Controller's} optional parameters, if any.
70      * </p>
71      */

72     protected Map JavaDoc controllerParams;
73
74     /**
75      * <p>
76      * Our {@link View View's} optional parameters, if any.
77      * </p>
78      */

79     protected Map JavaDoc viewParams;
80
81     /**
82      * <p>
83      * Our {@link Transform pipeline's} optional parameters, if any.
84      * </p>
85      */

86     protected Map JavaDoc transformParams;
87     
88     /**
89      * <p>
90      * An array of pipeline transformations, which is set before the View is
91      * processed.
92      * </p>
93      */

94     protected Transform[] transforms;
95     
96     /**
97      * <p>
98      * The index of the next transform to execute.
99      * </p>
100      */

101     protected int nextTransform = 0;
102     
103     /**
104      * <p>
105      * The count of transforms to execute.
106      * </p>
107      */

108     protected int transformCount;
109     
110     /**
111      * <p>
112      * Convenience Constructor to pass instances of Dispatcher,
113      * HttpServletRequest, and HttpServletResponse.
114      * </p>
115      */

116     public MaverickContext(Dispatcher disp, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
117     {
118         this.dispatcher = disp;
119         this.request = req;
120         this.response = res;
121     }
122     
123     /**
124      * <p>
125      * Returns our HttpServletRequest.
126      * </p>
127      * @return our HttpServletRequest
128      * @see ControllerContext
129      * @see ViewContext
130      * @see TransformContext
131      */

132     public HttpServletRequest JavaDoc getRequest()
133     {
134         return this.request;
135     }
136     
137     /**
138      * <p>
139      * Returns the *real* response object.
140      * Do not use this unless you know are the tail!
141      * </p>
142      * @see ViewContext
143      * @see TransformContext
144      */

145     public HttpServletResponse JavaDoc getRealResponse()
146     {
147         return this.response;
148     }
149
150     /**
151      * <p>
152      * Returns our HttpServletResponse.
153      * </p>
154      * @see ControllerContext
155      */

156     public HttpServletResponse JavaDoc getResponse()
157     {
158         return this.getRealResponse();
159     }
160
161     /**
162      * <p>
163      * Returns our ServletConfig.
164      * </p>
165      * @return our ServletConfig
166      * @see ControllerContext
167      */

168     public ServletConfig JavaDoc getServletConfig()
169     {
170         return this.dispatcher.getServletConfig();
171     }
172     
173     /**
174      * @see ControllerContext
175      * @see ViewContext
176      * @see TransformContext
177      */

178     public ServletContext JavaDoc getServletContext()
179     {
180         return this.dispatcher.getServletContext();
181     }
182     
183     /**
184      * @see ControllerContext
185      */

186     public void setControllerParam(String JavaDoc name, Object JavaDoc value)
187     {
188         if (this.controllerParams == null)
189             this.controllerParams = new HashMap JavaDoc();
190             
191         this.controllerParams.put(name, value);
192     }
193
194     /**
195      * @see ControllerContext
196      */

197     public void setViewParam(String JavaDoc name, Object JavaDoc value)
198     {
199         if (this.viewParams == null)
200             this.viewParams = new HashMap JavaDoc();
201             
202         this.viewParams.put(name, value);
203     }
204
205     /**
206      * @see ControllerContext
207      */

208     public void setTransformParam(String JavaDoc name, Object JavaDoc value)
209     {
210         if (this.transformParams == null)
211             this.transformParams = new HashMap JavaDoc();
212             
213         this.transformParams.put(name, value);
214     }
215
216     /**
217      * Appends to existing parameters.
218      */

219     public void putAllControllerParams(Map JavaDoc addParams)
220     {
221         if (this.controllerParams == null)
222             this.controllerParams = new HashMap JavaDoc();
223             
224         this.controllerParams.putAll(addParams);
225     }
226
227     /**
228      * Appends to existing parameters.
229      */

230     public void putAllViewParams(Map JavaDoc addParams)
231     {
232         if (this.viewParams == null)
233             this.viewParams = new HashMap JavaDoc();
234             
235         this.viewParams.putAll(addParams);
236     }
237
238     /**
239      * Appends to existing parameters.
240      */

241     public void putAllTransformParams(Map JavaDoc addParams)
242     {
243         if (this.transformParams == null)
244             this.transformParams = new HashMap JavaDoc();
245             
246         this.transformParams.putAll(addParams);
247     }
248
249     /**
250      * @see ControllerContext
251      */

252     public void setModel(Object JavaDoc mod)
253     {
254         this.model = mod;
255     }
256     
257     /**
258      * @see ControllerContext
259      * @see ViewContext
260      */

261     public Object JavaDoc getModel()
262     {
263         return this.model;
264     }
265     
266     /**
267      * @see ControllerContext
268      */

269     public Map JavaDoc getControllerParams()
270     {
271         return this.controllerParams;
272     }
273     
274     /**
275      * @see ViewContext
276      */

277     public Map JavaDoc getViewParams()
278     {
279         return this.viewParams;
280     }
281     
282     /**
283      * @see TransformContext
284      */

285     public Map JavaDoc getTransformParams()
286     {
287         return this.transformParams;
288     }
289     
290     /**
291      */

292     public void setTransforms(Transform[] trans)
293     {
294         this.transforms = trans;
295
296         // Set the transformCount based on the transform limit parameter
297
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     /**
308      * @see ViewContext
309      * @see TransformContext
310      */

311     public TransformStep getNextStep() throws ServletException JavaDoc
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     /**
329      * @see TransformContext
330      */

331     public boolean halting()
332     {
333         return (this.transformCount != this.transforms.length);
334     }
335
336     /**
337      * Convenient method for obtaining the maximum number of transformations
338      * to allow in the pipeline. Uses a request parameter whose name is
339      * defined by the limitTransformsParam property on the Dispatcher, and
340      * which should have an integer value.
341      * If nothing is specified or transform limiting is disabled (because
342      * limitTransformsParam is null), this returns Integer.MAX_VALUE.
343      *
344      * @return The maximum number of transforms allowed, possibly
345      * Integer.MAX_VALUE.
346      * @throws NumberFormatException if the form parameter could not be
347      * converted to an integer.
348      */

349     protected int determineMaxTransforms()
350     {
351         if (this.dispatcher.getLimitTransformsParam() == null)
352             return Integer.MAX_VALUE;
353
354         String JavaDoc 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