KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > ext > servlet > AllHttpScopesHashModel


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.ext.servlet;
54
55 import java.util.HashMap JavaDoc;
56 import java.util.Map JavaDoc;
57
58 import javax.servlet.ServletContext JavaDoc;
59 import javax.servlet.http.HttpServletRequest JavaDoc;
60 import javax.servlet.http.HttpSession JavaDoc;
61
62 import freemarker.template.ObjectWrapper;
63 import freemarker.template.SimpleHash;
64 import freemarker.template.TemplateModel;
65 import freemarker.template.TemplateModelException;
66
67 /**
68  * An extension of SimpleHash that looks up keys in the hash, then in the
69  * request, session, and servlet context scopes. Makes "Application", "Session"
70  * and "Request" keys largely obsolete, however we keep them for backward
71  * compatibility (also, "Request" is required for proper operation of JSP
72  * taglibs).
73  * It is on purpose that we didn't override <tt>keys</tt> and <tt>values</tt>
74  * methods. That way, only those variables assigned into the hash directly by a
75  * subclass of <tt>FreemarkerServlet</tt> that overrides
76  * <tt>preTemplateProcess</tt>) are discovered as "page" variables by the FM
77  * JSP PageContext implementation.
78  * @author Attila Szegedi
79  * @version $Id: AllHttpScopesHashModel.java,v 1.5 2003/01/12 23:40:14 revusky Exp $
80  */

81 class AllHttpScopesHashModel extends SimpleHash
82 {
83     private final ObjectWrapper wrapper;
84     private final ServletContext JavaDoc context;
85     private final HttpServletRequest JavaDoc request;
86     private final Map JavaDoc unlistedModels = new HashMap JavaDoc();
87      
88     AllHttpScopesHashModel(ObjectWrapper wrapper, ServletContext JavaDoc context, HttpServletRequest JavaDoc request) {
89         this.wrapper = wrapper;
90         this.context = context;
91         this.request = request;
92     }
93     
94     /**
95      * Stores a model in the hash so that it doesn't show up in <tt>keys()</tt>
96      * and <tt>values()</tt> methods. Used to put the Application, Session,
97      * Request, RequestParameters and JspTaglibs objects.
98      * @param key the key under which the model is stored
99      * @param model the stored model
100      */

101     void putUnlistedModel(String JavaDoc key, TemplateModel model)
102     {
103         unlistedModels.put(key, model);
104     }
105
106     public TemplateModel get(String JavaDoc key) throws TemplateModelException {
107         // Lookup in page scope
108
TemplateModel model = super.get(key);
109         if(model != null) {
110             return model;
111         }
112
113         // Look in unlisted models
114
model = (TemplateModel)unlistedModels.get(key);
115         if(model != null) {
116             return model;
117         }
118         
119         // Lookup in request scope
120
Object JavaDoc obj = request.getAttribute(key);
121         if(obj != null) {
122             return wrapper.wrap(obj);
123         }
124
125         // Lookup in session scope
126
HttpSession JavaDoc session = request.getSession(false);
127         if(session != null) {
128             obj = session.getAttribute(key);
129             if(obj != null) {
130                 return wrapper.wrap(obj);
131             }
132         }
133
134         // Lookup in application scope
135
obj = context.getAttribute(key);
136         if(obj != null) {
137             return wrapper.wrap(obj);
138         }
139
140         // return wrapper's null object (probably null).
141
return wrapper.wrap(null);
142     }
143 }
144
Popular Tags