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  import java.util.Set;
25  import java.util.Map.Entry;
26  
27  import javax.el.ELContext;
28  import javax.el.PropertyNotFoundException;
29  import javax.faces.context.ExternalContext;
30  import javax.faces.context.FacesContext;
31  
32  /***
33   * <p><code>ELResolver</code> implementation that accesses scoped variables
34   * in the current request context.  See the JSF 1.2 Specification, section
35   * 5.6.2.7, for requirements implemented by this class.</p>
36   *
37   * @since 1.0.4
38   */
39  public class FacesScopedAttributeELResolver extends AbstractELResolver {
40      
41  
42      /***
43       * <p>Return the most general type this resolver accepts for the
44       * <code>property</code> argument.</p>
45       */
46      public Class getCommonPropertyType(ELContext context, Object base) {
47  
48          if (base != null) {
49              return null;
50          } else {
51              return String.class;
52          }
53  
54      }
55  
56  
57      /***
58       * <p>Return an <code>Iterator</code> over the attributes that this
59       * resolver knows how to deal with.</p>
60       *
61       * @param context <code>ELContext</code> for evaluating this value
62       * @param base Base object against which this evaluation occurs
63       */
64      public Iterator getFeatureDescriptors(ELContext context, Object base) {
65  
66          if (base != null) {
67              return null;
68          }
69  
70          // Create the variables we will need
71          FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
72          ExternalContext econtext = fcontext.getExternalContext();
73          List descriptors = new ArrayList();
74          Map map = null;
75          Set set = null;
76          Iterator items = null;
77          String key = null;
78          Object value = null;
79  
80          // Add feature descriptors for request scoped attributes
81          set = econtext.getRequestMap().entrySet();
82          items = set.iterator();
83          while (items.hasNext()) {
84              Entry item = (Entry) items.next();
85              key = (String) item.getKey();
86              value = item.getValue();
87              descriptors.add(descriptor(key, key, "Request Scope Attribute " + key,
88                                         false, false, true, value.getClass(), true));
89          }
90  
91          // Add feature descriptors for session scoped attributes
92          set = econtext.getSessionMap().entrySet();
93          items = set.iterator();
94          while (items.hasNext()) {
95              Entry item = (Entry) items.next();
96              key = (String) item.getKey();
97              value = item.getValue();
98              descriptors.add(descriptor(key, key, "Session Scope Attribute " + key,
99                                         false, false, true, value.getClass(), true));
100         }
101 
102         // Add feature descriptors for application scoped attributes
103         set = econtext.getApplicationMap().entrySet();
104         items = set.iterator();
105         while (items.hasNext()) {
106             Entry item = (Entry) items.next();
107             key = (String) item.getKey();
108             value = item.getValue();
109             descriptors.add(descriptor(key, key, "Application Scope Attribute " + key,
110                                        false, false, true, value.getClass(), true));
111         }
112 
113         // Return the accumulated descriptors
114         return descriptors.iterator();
115 
116     }
117 
118 
119 
120     /***
121      * <p>Return the Java type of the specified property.</p>
122      *
123      * @param context <code>ELContext</code> for evaluating this value
124      * @param base Base object against which this evaluation occurs
125      *  (must be null because we are evaluating a top level variable)
126      * @param property Property name to be accessed
127      */
128     public Class getType(ELContext context, Object base, Object property) {
129 
130         if (base != null) {
131             return null;
132         }
133         if (property == null) {
134             throw new PropertyNotFoundException("No property specified");
135         }
136         context.setPropertyResolved(true);
137         return Object.class;
138 
139     }
140 
141 
142     /***
143      * <p>Return an existing scoped object for the specified name (if any);
144      * otherwise, return <code>null</code>.</p>
145      *
146      * @param context <code>ELContext</code> for evaluating this value
147      * @param base Base object against which this evaluation occurs
148      *  (must be null because we are evaluating a top level variable)
149      * @param property Property name to be accessed
150      */
151     public Object getValue(ELContext context, Object base, Object property) {
152 
153         if (base != null) {
154             return null;
155         }
156         if (property == null) {
157             throw new PropertyNotFoundException("No property specified");
158         }
159 
160         FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
161         ExternalContext econtext = fcontext.getExternalContext();
162         Object value = null;
163         value = econtext.getRequestMap().get(property);
164         if (value != null) {
165             context.setPropertyResolved(true);
166             return value;
167         }
168         value = econtext.getSessionMap().get(property);
169         if (value != null) {
170             context.setPropertyResolved(true);
171             return value;
172         }
173         value = econtext.getApplicationMap().get(property);
174         if (value != null) {
175             context.setPropertyResolved(true);
176             return value;
177         }
178 
179         return null;
180 
181     }
182 
183 
184     /***
185      * <p>Return <code>true</code> if the specified property is read only.</p>
186      *
187      * @param context <code>ELContext</code> for evaluating this value
188      * @param base Base object against which this evaluation occurs
189      *  (must be null because we are evaluating a top level variable)
190      * @param property Property name to be accessed
191      */
192     public boolean isReadOnly(ELContext context, Object base, Object property) {
193 
194         if (base == null) {
195             context.setPropertyResolved(true);
196             return false;
197         }
198         return false;
199 
200     }
201 
202 
203 
204     /***
205      * <p>Set the value of a scoped object for the specified name.</p>
206      *
207      * @param context <code>ELContext</code> for evaluating this value
208      * @param base Base object against which this evaluation occurs
209      *  (must be null because we are evaluating a top level variable)
210      * @param property Property name to be accessed
211      * @param value New value to be set
212      */
213     public void setValue(ELContext context, Object base, Object property, Object value) {
214 
215         if (base != null) {
216             return;
217         }
218         if (property == null) {
219             throw new PropertyNotFoundException("No property specified");
220         }
221 
222         context.setPropertyResolved(true);
223         String key = property.toString();
224         Object result = null;
225         FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
226         ExternalContext econtext = fcontext.getExternalContext();
227 
228         if (econtext.getRequestMap().containsKey(property)) {
229             econtext.getRequestMap().put(key, value);
230         } else if (econtext.getSessionMap().containsKey(property)) {
231             econtext.getSessionMap().put(key, value);
232         } else if (econtext.getApplicationMap().containsKey(property)) {
233             econtext.getApplicationMap().put(key, value);
234         } else {
235             econtext.getRequestMap().put(key, value);
236         }
237 
238     }
239 
240 
241 }