View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.el.unified.resolver.implicitobject;
20  
21  import javax.el.ELContext;
22  import javax.el.ELException;
23  import javax.el.ELResolver;
24  import javax.el.PropertyNotFoundException;
25  import javax.el.PropertyNotWritableException;
26  import java.beans.FeatureDescriptor;
27  import java.util.ArrayList;
28  import java.util.HashMap;
29  import java.util.Iterator;
30  import java.util.Map;
31  
32  /**
33   * See JSF 1.2 spec sections 5.6.1.1 and 5.6.2.1
34   * 
35   * @author Stan Silvert
36   */
37  public class ImplicitObjectResolver extends ELResolver
38  {
39  
40      private Map<String, ImplicitObject> implicitObjects;
41  
42      /**
43       * Static factory for an ELResolver for resolving implicit objects in JSPs. See JSF 1.2 spec section 5.6.1.1
44       */
45      public static ELResolver makeResolverForJSP()
46      {
47          Map<String, ImplicitObject> forJSPList = new HashMap<String, ImplicitObject>(8);//4
48          ImplicitObject io1 = new FacesContextImplicitObject();
49          forJSPList.put(io1.getName(), io1);
50          ImplicitObject io2 = new ViewImplicitObject();
51          forJSPList.put(io2.getName(), io2);
52          ImplicitObject io3 = new ResourceImplicitObject();
53          forJSPList.put(io3.getName(), io3);
54          ImplicitObject io4 = new ViewScopeImplicitObject();
55          forJSPList.put(io4.getName(), io4);
56          return new ImplicitObjectResolver(forJSPList);
57      }
58  
59      /**
60       * Static factory for an ELResolver for resolving implicit objects in all of Faces. See JSF 1.2 spec section 5.6.1.2
61       */
62      public static ELResolver makeResolverForFaces()
63      {
64          Map<String, ImplicitObject> forFacesList = new HashMap<String, ImplicitObject>(30);//14
65          ImplicitObject io1 = new ApplicationImplicitObject();
66          forFacesList.put(io1.getName(), io1);
67          ImplicitObject io2 = new ApplicationScopeImplicitObject();
68          forFacesList.put(io2.getName(), io2);
69          ImplicitObject io3 = new CookieImplicitObject();
70          forFacesList.put(io3.getName(), io3);
71          ImplicitObject io4 = new FacesContextImplicitObject();
72          forFacesList.put(io4.getName(), io4);
73          ImplicitObject io5 = new HeaderImplicitObject();
74          forFacesList.put(io5.getName(), io5);
75          ImplicitObject io6 = new HeaderValuesImplicitObject();
76          forFacesList.put(io6.getName(), io6);
77          ImplicitObject io7 = new InitParamImplicitObject();
78          forFacesList.put(io7.getName(), io7);
79          ImplicitObject io8 = new ParamImplicitObject();
80          forFacesList.put(io8.getName(), io8);
81          ImplicitObject io9 = new ParamValuesImplicitObject();
82          forFacesList.put(io9.getName(), io9);
83          ImplicitObject io10 = new RequestImplicitObject();
84          forFacesList.put(io10.getName(), io10);
85          ImplicitObject io11 = new RequestScopeImplicitObject();
86          forFacesList.put(io11.getName(), io11);
87          ImplicitObject io12 = new SessionImplicitObject();
88          forFacesList.put(io12.getName(), io12);
89          ImplicitObject io13 = new SessionScopeImplicitObject();
90          forFacesList.put(io13.getName(), io13);
91          ImplicitObject io14 = new ViewImplicitObject();
92          forFacesList.put(io14.getName(), io14);
93          ImplicitObject io15 = new ComponentImplicitObject();
94          forFacesList.put(io15.getName(), io15);
95          ImplicitObject io16 = new ResourceImplicitObject();
96          forFacesList.put(io16.getName(), io16);
97          ImplicitObject io17 = new ViewScopeImplicitObject();
98          forFacesList.put(io17.getName(), io17);
99          ImplicitObject io18 = new CompositeComponentImplicitObject();
100         forFacesList.put(io18.getName(), io18);
101         ImplicitObject io19 = new FlowScopeImplicitObject();
102         forFacesList.put(io19.getName(), io19);
103         return new ImplicitObjectResolver(forFacesList);
104     }
105 
106     private ImplicitObjectResolver()
107     {
108         super();
109         this.implicitObjects = new HashMap<String, ImplicitObject>();
110     }
111 
112     /** Creates a new instance of ImplicitObjectResolverForJSP */
113     private ImplicitObjectResolver(Map<String, ImplicitObject> implicitObjects)
114     {
115         this();
116         this.implicitObjects = implicitObjects;
117     }
118 
119     @Override
120     public void setValue(ELContext context, Object base, Object property, Object value) throws NullPointerException,
121         PropertyNotFoundException, PropertyNotWritableException, ELException
122     {
123 
124         if (base != null)
125         {
126             return;
127         }
128         if (property == null)
129         {
130             throw new PropertyNotFoundException();
131         }
132         if (!(property instanceof String))
133         {
134             return;
135         }
136 
137         String strProperty = property.toString();
138 
139         if (implicitObjects.containsKey(strProperty))
140         {
141             throw new PropertyNotWritableException();
142         }
143     }
144 
145     @Override
146     public boolean isReadOnly(ELContext context, Object base, Object property) throws NullPointerException,
147         PropertyNotFoundException, ELException
148     {
149 
150         if (base != null)
151         {
152             return false;
153         }
154         if (property == null)
155         {
156             throw new PropertyNotFoundException();
157         }
158         if (!(property instanceof String))
159         {
160             return false;
161         }
162 
163         String strProperty = property.toString();
164 
165         if (implicitObjects.containsKey(strProperty))
166         {
167             context.setPropertyResolved(true);
168             return true;
169         }
170 
171         return false;
172     }
173 
174     @Override
175     public Object getValue(ELContext context, Object base, Object property) throws NullPointerException,
176         PropertyNotFoundException, ELException
177     {
178 
179         if (base != null)
180         {
181             return null;
182         }
183         if (property == null)
184         {
185             throw new PropertyNotFoundException();
186         }
187         if (!(property instanceof String))
188         {
189             return null;
190         }
191 
192         String strProperty = property.toString();
193 
194         ImplicitObject obj = implicitObjects.get(strProperty);
195         if (obj != null)
196         {
197             context.setPropertyResolved(true);
198             return obj.getValue(context);
199         }
200 
201         return null;
202     }
203 
204     @Override
205     public Class<?> getType(ELContext context, Object base, Object property) throws NullPointerException,
206         PropertyNotFoundException, ELException
207     {
208 
209         if (base != null)
210         {
211             return null;
212         }
213         if (property == null)
214         {
215             throw new PropertyNotFoundException();
216         }
217         if (!(property instanceof String))
218         {
219             return null;
220         }
221 
222         String strProperty = property.toString();
223 
224         if (implicitObjects.containsKey(strProperty))
225         {
226             context.setPropertyResolved(true);
227         }
228 
229         return null;
230     }
231 
232     @Override
233     public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base)
234     {
235         if (base != null)
236         {
237             return null;
238         }
239 
240         ArrayList<FeatureDescriptor> descriptors = new ArrayList<FeatureDescriptor>(implicitObjects.size());
241 
242         for (ImplicitObject obj : implicitObjects.values())
243         {
244             descriptors.add(obj.getDescriptor());
245         }
246 
247         return descriptors.iterator();
248     }
249 
250     @Override
251     public Class<?> getCommonPropertyType(ELContext context, Object base)
252     {
253         if (base != null)
254         {
255             return null;
256         }
257 
258         return String.class;
259     }
260 
261 }