KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > view > context > ChainedContext


1 /*
2  * Copyright 2002-2003 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.velocity.tools.view.context;
18
19 import java.util.HashMap JavaDoc;
20
21 import org.apache.velocity.VelocityContext;
22 import org.apache.velocity.context.Context;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27 import javax.servlet.ServletContext JavaDoc;
28
29
30 /**
31  * <p>Velocity context implementation specific to the Servlet environment.</p>
32  *
33  * <p>It provides the following special features:</p>
34  * <ul>
35  * <li>puts the request, response, session, and servlet context objects
36  * into the Velocity context for direct access, and keeps them
37  * read-only</li>
38  * <li>supports a read-only toolbox of view tools</li>
39  * <li>auto-searches servlet request attributes, session attributes and
40  * servlet context attribues for objects</li>
41  * </ul>
42  *
43  * <p>The {@link #internalGet(String key)} method implements the following search order
44  * for objects:</p>
45  * <ol>
46  * <li>toolbox</li>
47  * <li>servlet request, servlet response, servlet session, servlet context</li>
48  * <li>local hashtable of objects (traditional use)</li>
49  * <li>servlet request attribues, servlet session attribute, servlet context
50  * attributes</li>
51  * </ol>
52  *
53  * <p>The purpose of this class is to make it easy for web designer to work
54  * with Java servlet based web applications. They do not need to be concerned
55  * with the concepts of request, session or application attributes and the
56  * lifetime of objects in these scopes.</p>
57  *
58  * <p>Note that the put() method always puts objects into the local hashtable.
59  * </p>
60  *
61  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
62  * @author <a HREF="mailto:sidler@teamup.com">Gabe Sidler</a>
63  *
64  * @version $Id: ChainedContext.java,v 1.6 2004/02/18 20:07:58 nbubna Exp $
65  */

66 public class ChainedContext extends VelocityContext implements ViewContext
67 {
68
69     /**
70      * A local reference to the current servlet request.
71      */

72     private HttpServletRequest JavaDoc request;
73     
74     /**
75      * A local reference to the current servlet response.
76      */

77     private HttpServletResponse JavaDoc response;
78     
79     /**
80      * A local reference to the servlet session.
81      */

82     private HttpSession JavaDoc session;
83     
84     /**
85      * A local reference to the servlet context.
86      */

87     private ServletContext JavaDoc application;
88
89     /**
90      * The toolbox.
91      */

92     private ToolboxContext toolboxContext = null;
93
94
95     /**
96      * Default constructor.
97      */

98     public ChainedContext(Context ctx,
99                           HttpServletRequest JavaDoc request,
100                           HttpServletResponse JavaDoc response,
101                           ServletContext JavaDoc application)
102     {
103         super(null, ctx );
104
105         this.request = request;
106         this.response = response;
107         this.session = request.getSession(false);
108         this.application = application;
109     }
110
111
112     /**
113      * <p>Sets the toolbox of view tools.</p>
114      *
115      * @param box toolbox of view tools
116      */

117     public void setToolbox(ToolboxContext box)
118     {
119         toolboxContext = box;
120         // just in case the servlet toolbox manager
121
// had to create a new session to hold session tools
122
// let's make sure this context's session ref is current
123
session = request.getSession(false);
124     }
125
126
127     /**
128      * <p>Looks up and returns the object with the specified key.</p>
129      *
130      * <p>See the class documentation for more details.</p>
131      *
132      * @param key the key of the object requested
133      *
134      * @return the requested object or null if not found
135      */

136     public Object JavaDoc internalGet( String JavaDoc key )
137     {
138         Object JavaDoc o = null;
139
140         // search the toolbox
141
if (toolboxContext != null)
142         {
143             o = toolboxContext.get(key);
144             if (o != null)
145             {
146                 return o;
147             }
148         }
149
150         // make the four scopes of the Apocalypse Read only
151
if ( key.equals( REQUEST ))
152         {
153             return request;
154         }
155         else if( key.equals(RESPONSE) )
156         {
157             return response;
158         }
159         else if ( key.equals(SESSION) )
160         {
161             return session;
162         }
163         else if ( key.equals(APPLICATION))
164         {
165             return application;
166         }
167
168         // try the local hashtable
169
o = super.internalGet(key);
170         if (o != null)
171         {
172             return o;
173         }
174
175         // if not found, wander down the scopes...
176
return getAttribute(key);
177     }
178
179
180     /**
181      * <p>Searches for the named attribute in request, session (if valid),
182      * and application scope(s) in order and returns the value associated
183      * or null.</p>
184      *
185      * @since VelocityTools 1.1
186      */

187     public Object JavaDoc getAttribute(String JavaDoc key)
188     {
189         Object JavaDoc o = request.getAttribute(key);
190         if (o == null)
191         {
192             if (session != null)
193             {
194                 o = session.getAttribute(key);
195             }
196
197             if (o == null)
198             {
199                 o = application.getAttribute(key);
200             }
201         }
202         return o;
203     }
204
205
206     /**
207      * <p>Returns the current servlet request.</p>
208      */

209     public HttpServletRequest JavaDoc getRequest()
210     {
211         return request;
212     }
213
214
215     /**
216      * <p>Returns the current servlet response.</p>
217      */

218     public HttpServletResponse JavaDoc getResponse()
219     {
220         return response;
221     }
222
223
224     /**
225      * <p>Returns the servlet context.</p>
226      */

227     public ServletContext JavaDoc getServletContext()
228     {
229         return application;
230     }
231
232
233     /**
234      * <p>Returns a reference to the Velocity context (this object).</p>
235      */

236     public Context getVelocityContext()
237     {
238         return this;
239     }
240
241
242 } // ChainedContext
243
Popular Tags