2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

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.shale.test.el;
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.el.ELContext;
26  import javax.el.PropertyNotFoundException;
27  import javax.el.PropertyNotWritableException;
28  import javax.faces.component.UIViewRoot;
29  import javax.faces.context.ExternalContext;
30  import javax.faces.context.FacesContext;
31  
32  /***
33   * <p><code>ELResolver</code> implementation that accesses implicit objects
34   * in the current request context.  See the JSF 1.2 Specification, section
35   * 5.6.2.1, for requirements implemented by this class.</p>
36   *
37   * @since 1.0.4
38   */
39  public class FacesImplicitObjectELResolver extends AbstractELResolver {
40      
41  
42      /***
43       * <p>The names of all implicit objects recognized by this resolver.</p>
44       */
45      private static final String[] NAMES =
46      { "application", "applicationScope", "cookie", "facesContext",
47        "header", "headerValues", "initParam", "param", "paramValues",
48        "request", "requestScope", "session", "sessionScope", "view" };
49  
50  
51      /***
52       * <p>The property types corresponding to the implicit object names.</p>
53       */
54      private static final Class[] TYPES =
55      { Object.class, Map.class, Map.class, FacesContext.class,
56        Map.class, Map.class, Map.class, Map.class, Map.class,
57        Object.class, Map.class, Object.class, Map.class, UIViewRoot.class };
58  
59  
60      /***
61       * <p>The settable value types corresponding to the implicit
62       * object names.</p>
63       */
64      private static final Class[] VALUES =
65      { null, Object.class, null, null,
66        null, null, null, null, null,
67        null, Object.class, null, Object.class, null };
68  
69  
70      /***
71       * <p>Return the most general type this resolver accepts for the
72       * <code>property</code> argument.</p>
73       */
74      public Class getCommonPropertyType(ELContext context, Object base) {
75  
76          if (base != null) {
77              return null;
78          } else {
79              return String.class;
80          }
81  
82      }
83  
84  
85      /***
86       * <p>Return an <code>Iterator</code> over the attributes that this
87       * resolver knows how to deal with.</p>
88       *
89       * @param context <code>ELContext</code> for evaluating this value
90       * @param base Base object against which this evaluation occurs
91       */
92      public Iterator getFeatureDescriptors(ELContext context, Object base) {
93  
94          if (base != null) {
95              return null;
96          }
97  
98          // Create the variables we will need
99          FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
100         List descriptors = new ArrayList();
101 
102         // Add feature descriptors for each implicit object
103         for (int i = 0; i < NAMES.length; i++) {
104             descriptors.add(descriptor(NAMES[i], NAMES[i], NAMES[i],
105                                    false, false, true, TYPES[i], true));
106         }
107 
108         // Return the accumulated descriptors
109         return descriptors.iterator();
110 
111     }
112 
113 
114 
115     /***
116      * <p>Return the Java type of the specified property.</p>
117      *
118      * @param context <code>ELContext</code> for evaluating this value
119      * @param base Base object against which this evaluation occurs
120      *  (must be null because we are evaluating a top level variable)
121      * @param property Property name to be accessed
122      */
123     public Class getType(ELContext context, Object base, Object property) {
124 
125         if (base != null) {
126             return null;
127         }
128         if (property == null) {
129             throw new PropertyNotFoundException("No property specified");
130         }
131         String name = property.toString();
132         for (int i = 0; i < NAMES.length; i++) {
133             if (name.equals(NAMES[i])) {
134                 context.setPropertyResolved(true);
135                 return VALUES[i];
136             }
137         }
138         return null;
139 
140     }
141 
142 
143     /***
144      * <p>Return an existing scoped object for the specified name (if any);
145      * otherwise, return <code>null</code>.</p>
146      *
147      * @param context <code>ELContext</code> for evaluating this value
148      * @param base Base object against which this evaluation occurs
149      *  (must be null because we are evaluating a top level variable)
150      * @param property Property name to be accessed
151      */
152     public Object getValue(ELContext context, Object base, Object property) {
153 
154         if (base != null) {
155             return null;
156         }
157         if (property == null) {
158             throw new PropertyNotFoundException("No property specified");
159         }
160 
161         FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
162         ExternalContext econtext = fcontext.getExternalContext();
163         String name = property.toString();
164 
165         if (name.equals("application")) {
166             context.setPropertyResolved(true);
167             return econtext.getContext();
168         } else if (name.equals("applicationScope")) {
169             context.setPropertyResolved(true);
170             return econtext.getApplicationMap();
171         } else if (name.equals("cookie")) {
172             context.setPropertyResolved(true);
173             return econtext.getRequestCookieMap();
174         } else if (name.equals("facesContext")) {
175             context.setPropertyResolved(true);
176             return fcontext;
177         } else if (name.equals("header")) {
178             context.setPropertyResolved(true);
179             return econtext.getRequestHeaderMap();
180         } else if (name.equals("headerValues")) {
181             context.setPropertyResolved(true);
182             return econtext.getRequestHeaderValuesMap();
183         } else if (name.equals("initParam")) {
184             context.setPropertyResolved(true);
185             return econtext.getInitParameterMap();
186         } else if (name.equals("param")) {
187             context.setPropertyResolved(true);
188             return econtext.getRequestParameterMap();
189         } else if (name.equals("paramValues")) {
190             context.setPropertyResolved(true);
191             return econtext.getRequestParameterValuesMap();
192         } else if (name.equals("request")) {
193             context.setPropertyResolved(true);
194             return econtext.getRequest();
195         } else if (name.equals("requestScope")) {
196             context.setPropertyResolved(true);
197             return econtext.getRequestMap();
198         } else if (name.equals("session")) {
199             context.setPropertyResolved(true);
200             return econtext.getSession(true);
201         } else if (name.equals("sessionScope")) {
202             context.setPropertyResolved(true);
203             return econtext.getSessionMap();
204         } else if (name.equals("view")) {
205             context.setPropertyResolved(true);
206             return fcontext.getViewRoot();
207         }
208 
209         return null;
210 
211     }
212 
213 
214     /***
215      * <p>Return <code>true</code> if the specified property is read only.</p>
216      *
217      * @param context <code>ELContext</code> for evaluating this value
218      * @param base Base object against which this evaluation occurs
219      *  (must be null because we are evaluating a top level variable)
220      * @param property Property name to be accessed
221      */
222     public boolean isReadOnly(ELContext context, Object base, Object property) {
223 
224         if (base != null) {
225             return false;
226         }
227         if (property == null) {
228             throw new PropertyNotFoundException("No property specified");
229         }
230         String name = property.toString();
231         for (int i = 0; i < NAMES.length; i++) {
232             if (name.equals(NAMES[i])) {
233                 context.setPropertyResolved(true);
234                 return true;
235             }
236         }
237         return false;
238 
239     }
240 
241 
242 
243     /***
244      * <p>Set the value of a scoped object for the specified name.</p>
245      *
246      * @param context <code>ELContext</code> for evaluating this value
247      * @param base Base object against which this evaluation occurs
248      *  (must be null because we are evaluating a top level variable)
249      * @param property Property name to be accessed
250      * @param value New value to be set
251      */
252     public void setValue(ELContext context, Object base, Object property, Object value) {
253 
254         if (base != null) {
255             return;
256         }
257         if (property == null) {
258             throw new PropertyNotFoundException("No property specified");
259         }
260 
261         String name = property.toString();
262         for (int i = 0; i < NAMES.length; i++) {
263             if (name.equals(NAMES[i])) {
264                 context.setPropertyResolved(true);
265                 throw new PropertyNotWritableException(name);
266             }
267         }
268 
269     }
270 
271 
272 }