KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > runtime > JspContextWrapper


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

17
18 package org.apache.jasper.runtime;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Writer JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.el.ELContext;
29 import javax.servlet.Servlet JavaDoc;
30 import javax.servlet.ServletConfig JavaDoc;
31 import javax.servlet.ServletContext JavaDoc;
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.ServletRequest JavaDoc;
34 import javax.servlet.ServletResponse JavaDoc;
35 import javax.servlet.http.HttpSession JavaDoc;
36 import javax.servlet.jsp.JspContext JavaDoc;
37 import javax.servlet.jsp.JspWriter JavaDoc;
38 import javax.servlet.jsp.PageContext JavaDoc;
39 import javax.servlet.jsp.el.ELException JavaDoc;
40 import javax.servlet.jsp.el.ExpressionEvaluator JavaDoc;
41 import javax.servlet.jsp.el.VariableResolver JavaDoc;
42 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
43 import javax.servlet.jsp.tagext.VariableInfo JavaDoc;
44
45 import org.apache.jasper.compiler.Localizer;
46 import org.apache.jasper.util.Enumerator;
47
48 /**
49  * Implementation of a JSP Context Wrapper.
50  *
51  * The JSP Context Wrapper is a JspContext created and maintained by a tag
52  * handler implementation. It wraps the Invoking JSP Context, that is, the
53  * JspContext instance passed to the tag handler by the invoking page via
54  * setJspContext().
55  *
56  * @author Kin-man Chung
57  * @author Jan Luehe
58  * @author Jacob Hookom
59  */

60 public class JspContextWrapper extends PageContext JavaDoc implements VariableResolver JavaDoc {
61
62     // Invoking JSP context
63
private PageContext JavaDoc invokingJspCtxt;
64
65     private transient HashMap JavaDoc<String JavaDoc, Object JavaDoc> pageAttributes;
66
67     // ArrayList of NESTED scripting variables
68
private ArrayList JavaDoc nestedVars;
69
70     // ArrayList of AT_BEGIN scripting variables
71
private ArrayList JavaDoc atBeginVars;
72
73     // ArrayList of AT_END scripting variables
74
private ArrayList JavaDoc atEndVars;
75
76     private Map JavaDoc aliases;
77
78     private HashMap JavaDoc<String JavaDoc, Object JavaDoc> originalNestedVars;
79
80     public JspContextWrapper(JspContext JavaDoc jspContext, ArrayList JavaDoc nestedVars,
81             ArrayList JavaDoc atBeginVars, ArrayList JavaDoc atEndVars, Map JavaDoc aliases) {
82         this.invokingJspCtxt = (PageContext JavaDoc) jspContext;
83         this.nestedVars = nestedVars;
84         this.atBeginVars = atBeginVars;
85         this.atEndVars = atEndVars;
86         this.pageAttributes = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(16);
87         this.aliases = aliases;
88
89         if (nestedVars != null) {
90             this.originalNestedVars = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(nestedVars.size());
91         }
92         syncBeginTagFile();
93     }
94
95     public void initialize(Servlet JavaDoc servlet, ServletRequest JavaDoc request,
96             ServletResponse JavaDoc response, String JavaDoc errorPageURL,
97             boolean needsSession, int bufferSize, boolean autoFlush)
98             throws IOException JavaDoc, IllegalStateException JavaDoc, IllegalArgumentException JavaDoc {
99     }
100
101     public Object JavaDoc getAttribute(String JavaDoc name) {
102
103         if (name == null) {
104             throw new NullPointerException JavaDoc(Localizer
105                     .getMessage("jsp.error.attribute.null_name"));
106         }
107
108         return pageAttributes.get(name);
109     }
110
111     public Object JavaDoc getAttribute(String JavaDoc name, int scope) {
112
113         if (name == null) {
114             throw new NullPointerException JavaDoc(Localizer
115                     .getMessage("jsp.error.attribute.null_name"));
116         }
117
118         if (scope == PAGE_SCOPE) {
119             return pageAttributes.get(name);
120         }
121
122         return invokingJspCtxt.getAttribute(name, scope);
123     }
124
125     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
126
127         if (name == null) {
128             throw new NullPointerException JavaDoc(Localizer
129                     .getMessage("jsp.error.attribute.null_name"));
130         }
131
132         if (value != null) {
133             pageAttributes.put(name, value);
134         } else {
135             removeAttribute(name, PAGE_SCOPE);
136         }
137     }
138
139     public void setAttribute(String JavaDoc name, Object JavaDoc value, int scope) {
140
141         if (name == null) {
142             throw new NullPointerException JavaDoc(Localizer
143                     .getMessage("jsp.error.attribute.null_name"));
144         }
145
146         if (scope == PAGE_SCOPE) {
147             if (value != null) {
148                 pageAttributes.put(name, value);
149             } else {
150                 removeAttribute(name, PAGE_SCOPE);
151             }
152         } else {
153             invokingJspCtxt.setAttribute(name, value, scope);
154         }
155     }
156
157     public Object JavaDoc findAttribute(String JavaDoc name) {
158
159         if (name == null) {
160             throw new NullPointerException JavaDoc(Localizer
161                     .getMessage("jsp.error.attribute.null_name"));
162         }
163
164         Object JavaDoc o = pageAttributes.get(name);
165         if (o == null) {
166             o = invokingJspCtxt.getAttribute(name, REQUEST_SCOPE);
167             if (o == null) {
168                 if (getSession() != null) {
169                     o = invokingJspCtxt.getAttribute(name, SESSION_SCOPE);
170                 }
171                 if (o == null) {
172                     o = invokingJspCtxt.getAttribute(name, APPLICATION_SCOPE);
173                 }
174             }
175         }
176
177         return o;
178     }
179
180     public void removeAttribute(String JavaDoc name) {
181
182         if (name == null) {
183             throw new NullPointerException JavaDoc(Localizer
184                     .getMessage("jsp.error.attribute.null_name"));
185         }
186
187         pageAttributes.remove(name);
188         invokingJspCtxt.removeAttribute(name, REQUEST_SCOPE);
189         if (getSession() != null) {
190             invokingJspCtxt.removeAttribute(name, SESSION_SCOPE);
191         }
192         invokingJspCtxt.removeAttribute(name, APPLICATION_SCOPE);
193     }
194
195     public void removeAttribute(String JavaDoc name, int scope) {
196
197         if (name == null) {
198             throw new NullPointerException JavaDoc(Localizer
199                     .getMessage("jsp.error.attribute.null_name"));
200         }
201
202         if (scope == PAGE_SCOPE) {
203             pageAttributes.remove(name);
204         } else {
205             invokingJspCtxt.removeAttribute(name, scope);
206         }
207     }
208
209     public int getAttributesScope(String JavaDoc name) {
210
211         if (name == null) {
212             throw new NullPointerException JavaDoc(Localizer
213                     .getMessage("jsp.error.attribute.null_name"));
214         }
215
216         if (pageAttributes.get(name) != null) {
217             return PAGE_SCOPE;
218         } else {
219             return invokingJspCtxt.getAttributesScope(name);
220         }
221     }
222
223     public Enumeration JavaDoc<String JavaDoc> getAttributeNamesInScope(int scope) {
224         if (scope == PAGE_SCOPE) {
225             return new Enumerator(pageAttributes.keySet().iterator());
226         }
227
228         return invokingJspCtxt.getAttributeNamesInScope(scope);
229     }
230
231     public void release() {
232         invokingJspCtxt.release();
233     }
234
235     public JspWriter JavaDoc getOut() {
236         return invokingJspCtxt.getOut();
237     }
238
239     public HttpSession JavaDoc getSession() {
240         return invokingJspCtxt.getSession();
241     }
242
243     public Object JavaDoc getPage() {
244         return invokingJspCtxt.getPage();
245     }
246
247     public ServletRequest JavaDoc getRequest() {
248         return invokingJspCtxt.getRequest();
249     }
250
251     public ServletResponse JavaDoc getResponse() {
252         return invokingJspCtxt.getResponse();
253     }
254
255     public Exception JavaDoc getException() {
256         return invokingJspCtxt.getException();
257     }
258
259     public ServletConfig JavaDoc getServletConfig() {
260         return invokingJspCtxt.getServletConfig();
261     }
262
263     public ServletContext JavaDoc getServletContext() {
264         return invokingJspCtxt.getServletContext();
265     }
266
267     public void forward(String JavaDoc relativeUrlPath) throws ServletException JavaDoc,
268             IOException JavaDoc {
269         invokingJspCtxt.forward(relativeUrlPath);
270     }
271
272     public void include(String JavaDoc relativeUrlPath) throws ServletException JavaDoc,
273             IOException JavaDoc {
274         invokingJspCtxt.include(relativeUrlPath);
275     }
276
277     public void include(String JavaDoc relativeUrlPath, boolean flush)
278             throws ServletException JavaDoc, IOException JavaDoc {
279         include(relativeUrlPath, false); // XXX
280
}
281
282     public VariableResolver JavaDoc getVariableResolver() {
283         return this;
284     }
285
286     public BodyContent JavaDoc pushBody() {
287         return invokingJspCtxt.pushBody();
288     }
289
290     public JspWriter JavaDoc pushBody(Writer JavaDoc writer) {
291         return invokingJspCtxt.pushBody(writer);
292     }
293
294     public JspWriter JavaDoc popBody() {
295         return invokingJspCtxt.popBody();
296     }
297
298     public ExpressionEvaluator JavaDoc getExpressionEvaluator() {
299         return invokingJspCtxt.getExpressionEvaluator();
300     }
301
302     public void handlePageException(Exception JavaDoc ex) throws IOException JavaDoc,
303             ServletException JavaDoc {
304         // Should never be called since handleException() called with a
305
// Throwable in the generated servlet.
306
handlePageException((Throwable JavaDoc) ex);
307     }
308
309     public void handlePageException(Throwable JavaDoc t) throws IOException JavaDoc,
310             ServletException JavaDoc {
311         invokingJspCtxt.handlePageException(t);
312     }
313
314     /**
315      * VariableResolver interface
316      */

317     public Object JavaDoc resolveVariable(String JavaDoc pName) throws ELException JavaDoc {
318         ELContext ctx = this.getELContext();
319         return ctx.getELResolver().getValue(ctx, null, pName);
320     }
321
322     /**
323      * Synchronize variables at begin of tag file
324      */

325     public void syncBeginTagFile() {
326         saveNestedVariables();
327     }
328
329     /**
330      * Synchronize variables before fragment invokation
331      */

332     public void syncBeforeInvoke() {
333         copyTagToPageScope(VariableInfo.NESTED);
334         copyTagToPageScope(VariableInfo.AT_BEGIN);
335     }
336
337     /**
338      * Synchronize variables at end of tag file
339      */

340     public void syncEndTagFile() {
341         copyTagToPageScope(VariableInfo.AT_BEGIN);
342         copyTagToPageScope(VariableInfo.AT_END);
343         restoreNestedVariables();
344     }
345
346     /**
347      * Copies the variables of the given scope from the virtual page scope of
348      * this JSP context wrapper to the page scope of the invoking JSP context.
349      *
350      * @param scope
351      * variable scope (one of NESTED, AT_BEGIN, or AT_END)
352      */

353     private void copyTagToPageScope(int scope) {
354         Iterator JavaDoc iter = null;
355
356         switch (scope) {
357         case VariableInfo.NESTED:
358             if (nestedVars != null) {
359                 iter = nestedVars.iterator();
360             }
361             break;
362         case VariableInfo.AT_BEGIN:
363             if (atBeginVars != null) {
364                 iter = atBeginVars.iterator();
365             }
366             break;
367         case VariableInfo.AT_END:
368             if (atEndVars != null) {
369                 iter = atEndVars.iterator();
370             }
371             break;
372         }
373
374         while ((iter != null) && iter.hasNext()) {
375             String JavaDoc varName = (String JavaDoc) iter.next();
376             Object JavaDoc obj = getAttribute(varName);
377             varName = findAlias(varName);
378             if (obj != null) {
379                 invokingJspCtxt.setAttribute(varName, obj);
380             } else {
381                 invokingJspCtxt.removeAttribute(varName, PAGE_SCOPE);
382             }
383         }
384     }
385
386     /**
387      * Saves the values of any NESTED variables that are present in the invoking
388      * JSP context, so they can later be restored.
389      */

390     private void saveNestedVariables() {
391         if (nestedVars != null) {
392             Iterator JavaDoc iter = nestedVars.iterator();
393             while (iter.hasNext()) {
394                 String JavaDoc varName = (String JavaDoc) iter.next();
395                 varName = findAlias(varName);
396                 Object JavaDoc obj = invokingJspCtxt.getAttribute(varName);
397                 if (obj != null) {
398                     originalNestedVars.put(varName, obj);
399                 }
400             }
401         }
402     }
403
404     /**
405      * Restores the values of any NESTED variables in the invoking JSP context.
406      */

407     private void restoreNestedVariables() {
408         if (nestedVars != null) {
409             Iterator JavaDoc iter = nestedVars.iterator();
410             while (iter.hasNext()) {
411                 String JavaDoc varName = (String JavaDoc) iter.next();
412                 varName = findAlias(varName);
413                 Object JavaDoc obj = originalNestedVars.get(varName);
414                 if (obj != null) {
415                     invokingJspCtxt.setAttribute(varName, obj);
416                 } else {
417                     invokingJspCtxt.removeAttribute(varName, PAGE_SCOPE);
418                 }
419             }
420         }
421     }
422
423     /**
424      * Checks to see if the given variable name is used as an alias, and if so,
425      * returns the variable name for which it is used as an alias.
426      *
427      * @param varName
428      * The variable name to check
429      * @return The variable name for which varName is used as an alias, or
430      * varName if it is not being used as an alias
431      */

432     private String JavaDoc findAlias(String JavaDoc varName) {
433
434         if (aliases == null)
435             return varName;
436
437         String JavaDoc alias = (String JavaDoc) aliases.get(varName);
438         if (alias == null) {
439             return varName;
440         }
441         return alias;
442     }
443
444     //private ELContextImpl elContext;
445

446     public ELContext getELContext() {
447         // instead decorate!!!
448

449         return this.invokingJspCtxt.getELContext();
450         
451         /*
452         if (this.elContext != null) {
453             JspFactory jspFact = JspFactory.getDefaultFactory();
454             ServletContext servletContext = this.getServletContext();
455             JspApplicationContextImpl jspCtx = (JspApplicationContextImpl) jspFact
456                     .getJspApplicationContext(servletContext);
457             this.elContext = jspCtx.createELContext(this);
458         }
459         return this.elContext;
460         */

461     }
462 }
463
Popular Tags