KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > webapp > parser > StubPageContext


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.webapp.parser;
35
36 import com.icesoft.faces.util.IteratorEnumeration;
37
38 import javax.el.ELContext;
39 import javax.faces.context.ExternalContext;
40 import javax.servlet.*;
41 import javax.servlet.http.HttpSession JavaDoc;
42 import javax.servlet.jsp.JspWriter JavaDoc;
43 import javax.servlet.jsp.PageContext JavaDoc;
44 import javax.servlet.jsp.el.ExpressionEvaluator JavaDoc;
45 import javax.servlet.jsp.el.VariableResolver JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.io.PrintWriter JavaDoc;
48 import java.util.Enumeration JavaDoc;
49 import java.util.Hashtable JavaDoc;
50 import java.util.Map JavaDoc;
51
52 /**
53  * This is a stubbed out version of the PageContext. Only the minimum number of
54  * members required to support the parser are implemented.
55  */

56 public class StubPageContext extends PageContext JavaDoc {
57
58     private Map JavaDoc attributes = new Hashtable JavaDoc();
59     private HttpSession JavaDoc httpSession;
60     private ExternalContext externalContext;
61     private ServletRequest servletRequest;
62     private ServletResponse servletResponse;
63
64     /*
65     * @see javax.servlet.jsp.PageContext#initialize(javax.servlet.Servlet, javax.servlet.ServletRequest, javax.servlet.ServletResponse, java.lang.String, boolean, int, boolean)
66     */

67     public void initialize(Servlet servlet, ServletRequest servletRequest,
68                            ServletResponse servletResponse,
69                            String JavaDoc errorPageURL, boolean sessionSupport,
70                            int bufferSize, boolean autoFlush)
71             throws IOException JavaDoc, IllegalStateException JavaDoc,
72                    IllegalArgumentException JavaDoc {
73         this.servletResponse = servletResponse;
74         externalContext = javax.faces.context.FacesContext.getCurrentInstance()
75                 .getExternalContext();
76     }
77
78     /*
79     * @see javax.servlet.jsp.JspContext#setAttribute(java.lang.String, java.lang.Object)
80     */

81     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
82         setAttribute(name, value, PAGE_SCOPE);
83     }
84
85     /*
86      * @see javax.servlet.jsp.JspContext#setAttribute(java.lang.String, java.lang.Object, int)
87      */

88     public void setAttribute(String JavaDoc name, Object JavaDoc value, int scope) {
89         switch (scope) {
90             case PAGE_SCOPE:
91                 attributes.put(name, value);
92                 break;
93             case REQUEST_SCOPE:
94                 externalContext.getRequestMap().put(name, value);
95                 break;
96             case SESSION_SCOPE:
97                 if (httpSession == null) {
98                     throw new IllegalArgumentException JavaDoc(
99                             "Session is not stablished for this request");
100                 }
101                 httpSession.setAttribute(name, value);
102                 break;
103             case APPLICATION_SCOPE:
104                 externalContext.getApplicationMap().put(name, value);
105                 break;
106             default:
107                 throw new IllegalArgumentException JavaDoc(
108                         scope + " scope is not valid");
109         }
110     }
111
112     /*
113     * @see javax.servlet.jsp.JspContext#getAttribute(java.lang.String)
114     */

115     public Object JavaDoc getAttribute(String JavaDoc name) {
116         return getAttribute(name, PAGE_SCOPE);
117     }
118
119     /*
120      * @see javax.servlet.jsp.JspContext#getAttribute(java.lang.String, int)
121      */

122     public Object JavaDoc getAttribute(String JavaDoc name, int scope) {
123         switch (scope) {
124             case PAGE_SCOPE:
125                 return attributes.get(name);
126             case REQUEST_SCOPE:
127                 return externalContext.getRequestMap().get(name);
128             case SESSION_SCOPE:
129                 if (httpSession == null) {
130                     throw new IllegalArgumentException JavaDoc(
131                             "Session is not stablished for this request");
132                 }
133                 return httpSession.getAttribute(name);
134             case APPLICATION_SCOPE:
135                 return (externalContext.getApplicationMap().get(name));
136             default:
137                 throw new IllegalArgumentException JavaDoc(
138                         scope + " scope is not valid");
139         }
140     }
141
142     /*
143     * @see javax.servlet.jsp.JspContext#removeAttribute(java.lang.String)
144     */

145     public void removeAttribute(String JavaDoc name) {
146         removeAttribute(name, PAGE_SCOPE);
147     }
148
149     /*
150      * @see javax.servlet.jsp.JspContext#removeAttribute(java.lang.String, int)
151      */

152     public void removeAttribute(String JavaDoc name, int scope) {
153         switch (scope) {
154             case PAGE_SCOPE:
155                 attributes.remove(name);
156                 break;
157             case REQUEST_SCOPE:
158                 externalContext.getRequestMap().remove(name);
159                 break;
160             case SESSION_SCOPE:
161                 if (httpSession == null) {
162                     throw new IllegalArgumentException JavaDoc(
163                             "Session is not stablished for this request");
164                 }
165                 httpSession.removeAttribute(name);
166                 break;
167             case APPLICATION_SCOPE:
168                 externalContext.getApplicationMap().remove(name);
169                 break;
170             default:
171                 throw new IllegalArgumentException JavaDoc(
172                         scope + " scope is not valid");
173         }
174     }
175
176     /*
177      * @see javax.servlet.jsp.JspContext#getOut()
178      */

179     public JspWriter JavaDoc getOut() {
180         return new JspWriterImpl(new PrintWriter JavaDoc(System.out));
181     }
182
183     /*
184      * @see javax.servlet.jsp.PageContext#getPage()
185      */

186     public Object JavaDoc getPage() {
187         throw new UnsupportedOperationException JavaDoc();
188     }
189
190     /*
191      * @see javax.servlet.jsp.PageContext#getServletConfig()
192      */

193     public ServletConfig getServletConfig() {
194         throw new UnsupportedOperationException JavaDoc();
195     }
196
197     /*
198      * @see javax.servlet.jsp.PageContext#getServletContext()
199      */

200     public ServletContext getServletContext() {
201         throw new UnsupportedOperationException JavaDoc();
202     }
203
204     public ELContext getELContext() {
205         throw new UnsupportedOperationException JavaDoc();
206     }
207
208     /*
209      * @see javax.servlet.jsp.PageContext#getRequest()
210      */

211     public ServletRequest getRequest() {
212         return servletRequest;
213     }
214
215     /*
216      * @see javax.servlet.jsp.PageContext#getResponse()
217      */

218     public ServletResponse getResponse() {
219         return servletResponse;
220     }
221
222     /*
223      * @see javax.servlet.jsp.PageContext#getSession()
224      */

225     public HttpSession JavaDoc getSession() {
226         return httpSession;
227     }
228
229     /*
230     * @see javax.servlet.jsp.PageContext#release()
231     */

232     public void release() {
233         throw new UnsupportedOperationException JavaDoc();
234     }
235
236     /*
237      * @see javax.servlet.jsp.PageContext#getException()
238      */

239     public Exception JavaDoc getException() {
240         throw new UnsupportedOperationException JavaDoc();
241     }
242
243     /*
244      * @see javax.servlet.jsp.PageContext#handlePageException(java.lang.Exception)
245      */

246     public void handlePageException(Exception JavaDoc arg0)
247             throws ServletException, IOException JavaDoc {
248         throw new UnsupportedOperationException JavaDoc();
249     }
250
251     /*
252      * @see javax.servlet.jsp.PageContext#forward(java.lang.String)
253      */

254     public void forward(String JavaDoc arg0) throws ServletException, IOException JavaDoc {
255         throw new UnsupportedOperationException JavaDoc();
256     }
257
258     /*
259      * @see javax.servlet.jsp.PageContext#include(java.lang.String)
260      */

261     public void include(String JavaDoc arg0) throws ServletException, IOException JavaDoc {
262         throw new UnsupportedOperationException JavaDoc();
263     }
264
265     /*
266      * @see javax.servlet.jsp.PageContext#include(java.lang.String, boolean)
267      */

268     public void include(String JavaDoc arg0, boolean arg1)
269             throws ServletException, IOException JavaDoc {
270         throw new UnsupportedOperationException JavaDoc();
271     }
272
273     /*
274      * @see javax.servlet.jsp.PageContext#handlePageException(java.lang.Throwable)
275      */

276     public void handlePageException(Throwable JavaDoc arg0)
277             throws ServletException, IOException JavaDoc {
278         throw new UnsupportedOperationException JavaDoc();
279     }
280
281     /*
282      * @see javax.servlet.jsp.JspContext#getAttributesScope(java.lang.String)
283      */

284     public int getAttributesScope(String JavaDoc arg0) {
285         throw new UnsupportedOperationException JavaDoc();
286     }
287
288
289     /*
290      * @see javax.servlet.jsp.JspContext#getAttributeNamesInScope(int)
291      */

292     public Enumeration getAttributeNamesInScope(int scope) {
293         switch (scope) {
294             case PAGE_SCOPE:
295                 return new IteratorEnumeration(
296                         ((Hashtable JavaDoc) attributes).keySet().iterator());
297             case REQUEST_SCOPE:
298                 return new IteratorEnumeration(
299                         externalContext.getRequestMap().keySet().iterator());
300             case SESSION_SCOPE:
301                 return new IteratorEnumeration(
302                         externalContext.getSessionMap().keySet().iterator());
303             case APPLICATION_SCOPE:
304                 return new IteratorEnumeration(
305                         externalContext.getApplicationMap()
306                                 .keySet().iterator());
307             default:
308                 throw new IllegalArgumentException JavaDoc(
309                         scope + " scope is not valid");
310         }
311
312     }
313
314     /*
315      * @see javax.servlet.jsp.JspContext#getExpressionEvaluator()
316      */

317     public ExpressionEvaluator JavaDoc getExpressionEvaluator() {
318         throw new UnsupportedOperationException JavaDoc();
319     }
320
321     /*
322      * @see javax.servlet.jsp.JspContext#getVariableResolver()
323      */

324     public VariableResolver JavaDoc getVariableResolver() {
325         throw new UnsupportedOperationException JavaDoc();
326     }
327
328     /*
329      * @see javax.servlet.jsp.JspContext#findAttribute(java.lang.String)
330      */

331     public Object JavaDoc findAttribute(String JavaDoc name) {
332         Object JavaDoc attribute;
333
334         //check page scope
335
attribute = attributes.get(name);
336         if (null != attribute) {
337             return attribute;
338         }
339
340         //check request scope
341
attribute = externalContext.getRequestMap().get(name);
342         if (null != attribute) {
343             return attribute;
344         }
345
346         //check session scope
347
if (httpSession != null) {
348             attribute = httpSession.getAttribute(name);
349         }
350         if (null != attribute) {
351             return attribute;
352         }
353
354         //return null or application scope value
355
return (externalContext.getApplicationMap().get(name));
356     }
357 }
358
Popular Tags