KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > servlet > jsp > el > ImplicitObjectELResolver


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 javax.servlet.jsp.el;
19
20 import java.beans.FeatureDescriptor JavaDoc;
21 import java.util.AbstractMap JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import javax.el.ELContext;
33 import javax.el.ELException;
34 import javax.el.ELResolver;
35 import javax.el.PropertyNotFoundException;
36 import javax.el.PropertyNotWritableException;
37 import javax.servlet.http.Cookie JavaDoc;
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpSession JavaDoc;
40 import javax.servlet.jsp.JspContext JavaDoc;
41 import javax.servlet.jsp.PageContext JavaDoc;
42
43 /**
44  *
45  * @since 2.1
46  */

47 public class ImplicitObjectELResolver extends ELResolver {
48
49     private final static String JavaDoc[] SCOPE_NAMES = new String JavaDoc[] {
50             "applicationScope", "cookie", "header", "headerValues",
51             "initParam", "pageContext", "pageScope", "param", "paramValues",
52             "requestScope", "sessionScope" };
53
54     private final static int APPLICATIONSCOPE = 0;
55
56     private final static int COOKIE = 1;
57
58     private final static int HEADER = 2;
59
60     private final static int HEADERVALUES = 3;
61
62     private final static int INITPARAM = 4;
63
64     private final static int PAGECONTEXT = 5;
65
66     private final static int PAGESCOPE = 6;
67
68     private final static int PARAM = 7;
69
70     private final static int PARAM_VALUES = 8;
71
72     private final static int REQUEST_SCOPE = 9;
73
74     private final static int SESSION_SCOPE = 10;
75
76     public ImplicitObjectELResolver() {
77         super();
78     }
79
80     public Object JavaDoc getValue(ELContext context, Object JavaDoc base, Object JavaDoc property)
81             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException JavaDoc {
82         if (context == null) {
83             throw new NullPointerException JavaDoc();
84         }
85
86         if (base == null && property != null) {
87             int idx = Arrays.binarySearch(SCOPE_NAMES, property.toString());
88
89             if (idx >= 0) {
90                 PageContext JavaDoc page = (PageContext JavaDoc) context
91                         .getContext(JspContext JavaDoc.class);
92                 context.setPropertyResolved(true);
93                 switch (idx) {
94                 case APPLICATIONSCOPE:
95                     return ScopeManager.get(page).getApplicationScope();
96                 case COOKIE:
97                     return ScopeManager.get(page).getCookie();
98                 case HEADER:
99                     return ScopeManager.get(page).getHeader();
100                 case HEADERVALUES:
101                     return ScopeManager.get(page).getHeaderValues();
102                 case INITPARAM:
103                     return ScopeManager.get(page).getInitParam();
104                 case PAGECONTEXT:
105                     return ScopeManager.get(page).getPageContext();
106                 case PAGESCOPE:
107                     return ScopeManager.get(page).getPageScope();
108                 case PARAM:
109                     return ScopeManager.get(page).getParam();
110                 case PARAM_VALUES:
111                     return ScopeManager.get(page).getParamValues();
112                 case REQUEST_SCOPE:
113                     return ScopeManager.get(page).getRequestScope();
114                 case SESSION_SCOPE:
115                     return ScopeManager.get(page).getSessionScope();
116                 }
117             }
118         }
119         return null;
120     }
121
122     public Class JavaDoc getType(ELContext context, Object JavaDoc base, Object JavaDoc property)
123             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException JavaDoc {
124         if (context == null) {
125             throw new NullPointerException JavaDoc();
126         }
127
128         if (base == null && property != null) {
129             int idx = Arrays.binarySearch(SCOPE_NAMES, property.toString());
130             if (idx >= 0) {
131                 context.setPropertyResolved(true);
132             }
133         }
134         return null;
135     }
136
137     public void setValue(ELContext context, Object JavaDoc base, Object JavaDoc property,
138             Object JavaDoc value) throws NullPointerException JavaDoc,
139             PropertyNotFoundException, PropertyNotWritableException,
140             ELException JavaDoc {
141         if (context == null) {
142             throw new NullPointerException JavaDoc();
143         }
144
145         if (base == null && property != null) {
146             int idx = Arrays.binarySearch(SCOPE_NAMES, property.toString());
147             if (idx >= 0) {
148                 context.setPropertyResolved(true);
149                 throw new PropertyNotWritableException();
150             }
151         }
152     }
153
154     public boolean isReadOnly(ELContext context, Object JavaDoc base, Object JavaDoc property)
155             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException JavaDoc {
156         if (context == null) {
157             throw new NullPointerException JavaDoc();
158         }
159
160         if (base == null && property != null) {
161             int idx = Arrays.binarySearch(SCOPE_NAMES, property.toString());
162             if (idx >= 0) {
163                 context.setPropertyResolved(true);
164                 return true;
165             }
166         }
167         return false;
168     }
169
170     public Iterator JavaDoc<FeatureDescriptor JavaDoc> getFeatureDescriptors(ELContext context, Object JavaDoc base) {
171         List JavaDoc<FeatureDescriptor JavaDoc> feats = new ArrayList JavaDoc<FeatureDescriptor JavaDoc>(
172                 SCOPE_NAMES.length);
173         FeatureDescriptor JavaDoc feat;
174         for (int i = 0; i < SCOPE_NAMES.length; i++) {
175             feat = new FeatureDescriptor JavaDoc();
176             feat.setDisplayName(SCOPE_NAMES[i]);
177             feat.setExpert(false);
178             feat.setHidden(false);
179             feat.setName(SCOPE_NAMES[i]);
180             feat.setPreferred(true);
181             feat.setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE);
182             feat.setValue(TYPE, String JavaDoc.class);
183             feats.add(feat);
184         }
185         return feats.iterator();
186     }
187
188     public Class JavaDoc<String JavaDoc> getCommonPropertyType(ELContext context, Object JavaDoc base) {
189         if (base == null) {
190             return String JavaDoc.class;
191         }
192         return null;
193     }
194
195     private static class ScopeManager {
196         private final static String JavaDoc MNGR_KEY = ScopeManager.class.getName();
197
198         private final PageContext JavaDoc page;
199
200         private Map JavaDoc applicationScope;
201
202         private Map JavaDoc cookie;
203
204         private Map JavaDoc header;
205
206         private Map JavaDoc headerValues;
207
208         private Map JavaDoc initParam;
209
210         private Map JavaDoc pageScope;
211
212         private Map JavaDoc param;
213
214         private Map JavaDoc paramValues;
215
216         private Map JavaDoc requestScope;
217
218         private Map JavaDoc sessionScope;
219
220         public ScopeManager(PageContext JavaDoc page) {
221             this.page = page;
222         }
223
224         public static ScopeManager get(PageContext JavaDoc page) {
225             ScopeManager mngr = (ScopeManager) page.getAttribute(MNGR_KEY);
226             if (mngr == null) {
227                 mngr = new ScopeManager(page);
228                 page.setAttribute(MNGR_KEY, mngr);
229             }
230             return mngr;
231         }
232
233         public Map JavaDoc getApplicationScope() {
234             if (this.applicationScope == null) {
235                 this.applicationScope = new ScopeMap() {
236                     protected void setAttribute(String JavaDoc name, Object JavaDoc value) {
237                         page.getServletContext().setAttribute(name, value);
238                     }
239
240                     protected void removeAttribute(String JavaDoc name) {
241                         page.getServletContext().removeAttribute(name);
242                     }
243
244                     protected Enumeration JavaDoc getAttributeNames() {
245                         return page.getServletContext().getAttributeNames();
246                     }
247
248                     protected Object JavaDoc getAttribute(String JavaDoc name) {
249                         return page.getServletContext().getAttribute(name);
250                     }
251                 };
252             }
253             return this.applicationScope;
254         }
255
256         public Map JavaDoc getCookie() {
257             if (this.cookie == null) {
258                 this.cookie = new ScopeMap() {
259                     protected Enumeration JavaDoc getAttributeNames() {
260                         Cookie JavaDoc[] c = ((HttpServletRequest JavaDoc) page.getRequest())
261                                 .getCookies();
262                         if (c != null) {
263                             Vector JavaDoc v = new Vector JavaDoc();
264                             for (int i = 0; i < c.length; i++) {
265                                 v.add(c[i].getName());
266                             }
267                             return v.elements();
268                         }
269                         return null;
270                     }
271
272                     protected Object JavaDoc getAttribute(String JavaDoc name) {
273                         Cookie JavaDoc[] c = ((HttpServletRequest JavaDoc) page.getRequest())
274                                 .getCookies();
275                         if (c != null) {
276                             for (int i = 0; i < c.length; i++) {
277                                 if (name.equals(c[i].getName())) {
278                                     return c[i];
279                                 }
280                             }
281                         }
282                         return null;
283                     }
284
285                 };
286             }
287             return this.cookie;
288         }
289
290         public Map JavaDoc getHeader() {
291             if (this.header == null) {
292                 this.header = new ScopeMap() {
293                     protected Enumeration JavaDoc getAttributeNames() {
294                         return ((HttpServletRequest JavaDoc) page.getRequest())
295                                 .getHeaderNames();
296                     }
297
298                     protected Object JavaDoc getAttribute(String JavaDoc name) {
299                         return ((HttpServletRequest JavaDoc) page.getRequest())
300                                 .getHeader(name);
301                     }
302                 };
303             }
304             return this.header;
305         }
306
307         public Map JavaDoc getHeaderValues() {
308             if (this.headerValues == null) {
309                 this.headerValues = new ScopeMap() {
310                     protected Enumeration JavaDoc getAttributeNames() {
311                         return ((HttpServletRequest JavaDoc) page.getRequest())
312                                 .getHeaderNames();
313                     }
314
315                     protected Object JavaDoc getAttribute(String JavaDoc name) {
316                         Enumeration JavaDoc e = ((HttpServletRequest JavaDoc) page.getRequest())
317                                 .getHeaders(name);
318                         if (e != null) {
319                             List JavaDoc list = new ArrayList JavaDoc();
320                             while (e.hasMoreElements()) {
321                                 list.add(e.nextElement().toString());
322                             }
323                             return (String JavaDoc[]) list.toArray(new String JavaDoc[list
324                                     .size()]);
325                         }
326                         return null;
327                     }
328
329                 };
330             }
331             return this.headerValues;
332         }
333
334         public Map JavaDoc getInitParam() {
335             if (this.initParam == null) {
336                 this.initParam = new ScopeMap() {
337                     protected Enumeration JavaDoc getAttributeNames() {
338                         return page.getServletContext().getInitParameterNames();
339                     }
340
341                     protected Object JavaDoc getAttribute(String JavaDoc name) {
342                         return page.getServletContext().getInitParameter(name);
343                     }
344                 };
345             }
346             return this.initParam;
347         }
348
349         public PageContext JavaDoc getPageContext() {
350             return this.page;
351         }
352
353         public Map JavaDoc getPageScope() {
354             if (this.pageScope == null) {
355                 this.pageScope = new ScopeMap() {
356                     protected void setAttribute(String JavaDoc name, Object JavaDoc value) {
357                         page.setAttribute(name, value);
358                     }
359
360                     protected void removeAttribute(String JavaDoc name) {
361                         page.removeAttribute(name);
362                     }
363
364                     protected Enumeration JavaDoc getAttributeNames() {
365                         return page
366                                 .getAttributeNamesInScope(PageContext.PAGE_SCOPE);
367                     }
368
369                     protected Object JavaDoc getAttribute(String JavaDoc name) {
370                         return page.getAttribute(name);
371                     }
372                 };
373             }
374             return this.pageScope;
375         }
376
377         public Map JavaDoc getParam() {
378             if (this.param == null) {
379                 this.param = new ScopeMap() {
380                     protected Enumeration JavaDoc getAttributeNames() {
381                         return page.getRequest().getParameterNames();
382                     }
383
384                     protected Object JavaDoc getAttribute(String JavaDoc name) {
385                         return page.getRequest().getParameter(name);
386                     }
387                 };
388             }
389             return this.param;
390         }
391
392         public Map JavaDoc getParamValues() {
393             if (this.paramValues == null) {
394                 this.paramValues = new ScopeMap() {
395                     protected Object JavaDoc getAttribute(String JavaDoc name) {
396                         return page.getRequest().getParameterValues(name);
397                     }
398
399                     protected Enumeration JavaDoc getAttributeNames() {
400                         return page.getRequest().getParameterNames();
401                     }
402                 };
403             }
404             return this.paramValues;
405         }
406
407         public Map JavaDoc getRequestScope() {
408             if (this.requestScope == null) {
409                 this.requestScope = new ScopeMap() {
410                     protected void setAttribute(String JavaDoc name, Object JavaDoc value) {
411                         page.getRequest().setAttribute(name, value);
412                     }
413
414                     protected void removeAttribute(String JavaDoc name) {
415                         page.getRequest().removeAttribute(name);
416                     }
417
418                     protected Enumeration JavaDoc getAttributeNames() {
419                         return page.getRequest().getAttributeNames();
420                     }
421
422                     protected Object JavaDoc getAttribute(String JavaDoc name) {
423                         return page.getRequest().getAttribute(name);
424                     }
425                 };
426             }
427             return this.requestScope;
428         }
429
430         public Map JavaDoc getSessionScope() {
431             if (this.sessionScope == null) {
432                 this.sessionScope = new ScopeMap() {
433                     protected void setAttribute(String JavaDoc name, Object JavaDoc value) {
434                         ((HttpServletRequest JavaDoc) page.getRequest()).getSession()
435                                 .setAttribute(name, value);
436                     }
437
438                     protected void removeAttribute(String JavaDoc name) {
439                         HttpSession JavaDoc session = page.getSession();
440                         if (session != null) {
441                             session.removeAttribute(name);
442                         }
443                     }
444
445                     protected Enumeration JavaDoc getAttributeNames() {
446                         HttpSession JavaDoc session = page.getSession();
447                         if (session != null) {
448                             return session.getAttributeNames();
449                         }
450                         return null;
451                     }
452
453                     protected Object JavaDoc getAttribute(String JavaDoc name) {
454                         HttpSession JavaDoc session = page.getSession();
455                         if (session != null) {
456                             return session.getAttribute(name);
457                         }
458                         return null;
459                     }
460                 };
461             }
462             return this.sessionScope;
463         }
464     }
465
466     private abstract static class ScopeMap extends AbstractMap JavaDoc {
467
468         protected abstract Enumeration JavaDoc getAttributeNames();
469
470         protected abstract Object JavaDoc getAttribute(String JavaDoc name);
471
472         protected void removeAttribute(String JavaDoc name) {
473             throw new UnsupportedOperationException JavaDoc();
474         }
475
476         protected void setAttribute(String JavaDoc name, Object JavaDoc value) {
477             throw new UnsupportedOperationException JavaDoc();
478         }
479
480         public final Set JavaDoc entrySet() {
481             Enumeration JavaDoc e = getAttributeNames();
482             Set JavaDoc set = new HashSet JavaDoc();
483             if (e != null) {
484                 while (e.hasMoreElements()) {
485                     set.add(new ScopeEntry((String JavaDoc) e.nextElement()));
486                 }
487             }
488             return set;
489         }
490
491         private class ScopeEntry implements Map.Entry JavaDoc {
492
493             private final String JavaDoc key;
494
495             public ScopeEntry(String JavaDoc key) {
496                 this.key = key;
497             }
498
499             public Object JavaDoc getKey() {
500                 return (Object JavaDoc) this.key;
501             }
502
503             public Object JavaDoc getValue() {
504                 return getAttribute(this.key);
505             }
506
507             public Object JavaDoc setValue(Object JavaDoc value) {
508                 if (value == null) {
509                     removeAttribute(this.key);
510                 } else {
511                     setAttribute(this.key, value);
512                 }
513                 return null;
514             }
515
516             public boolean equals(Object JavaDoc obj) {
517                 return (obj != null && this.hashCode() == obj.hashCode());
518             }
519
520             public int hashCode() {
521                 return this.key.hashCode();
522             }
523
524         }
525
526         public final Object JavaDoc get(Object JavaDoc key) {
527             if (key != null) {
528                 return getAttribute(key.toString());
529             }
530             return null;
531         }
532
533         public final Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
534             if (key == null) {
535                 throw new NullPointerException JavaDoc();
536             }
537             if (value == null) {
538                 this.removeAttribute(key.toString());
539             } else {
540                 this.setAttribute(key.toString(), value);
541             }
542             return null;
543         }
544
545         public final Object JavaDoc remove(Object JavaDoc key) {
546             if (key == null) {
547                 throw new NullPointerException JavaDoc();
548             }
549             this.removeAttribute(key.toString());
550             return null;
551         }
552
553     }
554
555 }
556
Popular Tags