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<>(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<>(30); //19
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     public static ELResolver makeResolverForFacesCDI()
107     {
108         Map<String, ImplicitObject> forFacesCDIList = new HashMap<>(4); //2
109         ImplicitObject io;
110         
111         io = new ComponentImplicitObject();
112         forFacesCDIList.put(io.getName(), io);
113         
114         io = new CompositeComponentImplicitObject();
115         forFacesCDIList.put(io.getName(), io);
116         
117         return new ImplicitObjectResolver(forFacesCDIList);
118     }
119 
120     private ImplicitObjectResolver()
121     {
122         super();
123         this.implicitObjects = new HashMap<>();
124     }
125 
126     /** Creates a new instance of ImplicitObjectResolverForJSP */
127     private ImplicitObjectResolver(Map<String, ImplicitObject> implicitObjects)
128     {
129         this();
130         this.implicitObjects = implicitObjects;
131     }
132 
133     @Override
134     public void setValue(ELContext context, Object base, Object property, Object value) throws NullPointerException,
135         PropertyNotFoundException, PropertyNotWritableException, ELException
136     {
137 
138         if (base != null)
139         {
140             return;
141         }
142         if (property == null)
143         {
144             throw new PropertyNotFoundException();
145         }
146         if (!(property instanceof String))
147         {
148             return;
149         }
150 
151         String strProperty = property.toString();
152 
153         if (implicitObjects.containsKey(strProperty))
154         {
155             throw new PropertyNotWritableException();
156         }
157     }
158 
159     @Override
160     public boolean isReadOnly(ELContext context, Object base, Object property) throws NullPointerException,
161         PropertyNotFoundException, ELException
162     {
163 
164         if (base != null)
165         {
166             return false;
167         }
168         if (property == null)
169         {
170             throw new PropertyNotFoundException();
171         }
172         if (!(property instanceof String))
173         {
174             return false;
175         }
176 
177         String strProperty = property.toString();
178 
179         if (implicitObjects.containsKey(strProperty))
180         {
181             context.setPropertyResolved(true);
182             return true;
183         }
184 
185         return false;
186     }
187 
188     @Override
189     public Object getValue(ELContext context, Object base, Object property) throws NullPointerException,
190         PropertyNotFoundException, ELException
191     {
192 
193         if (base != null)
194         {
195             return null;
196         }
197         if (property == null)
198         {
199             throw new PropertyNotFoundException();
200         }
201         if (!(property instanceof String))
202         {
203             return null;
204         }
205 
206         String strProperty = property.toString();
207 
208         ImplicitObject obj = implicitObjects.get(strProperty);
209         if (obj != null)
210         {
211             context.setPropertyResolved(true);
212             return obj.getValue(context);
213         }
214 
215         return null;
216     }
217 
218     @Override
219     public Class<?> getType(ELContext context, Object base, Object property) throws NullPointerException,
220         PropertyNotFoundException, ELException
221     {
222 
223         if (base != null)
224         {
225             return null;
226         }
227         if (property == null)
228         {
229             throw new PropertyNotFoundException();
230         }
231         if (!(property instanceof String))
232         {
233             return null;
234         }
235 
236         String strProperty = property.toString();
237 
238         if (implicitObjects.containsKey(strProperty))
239         {
240             context.setPropertyResolved(true);
241         }
242 
243         return null;
244     }
245 
246     @Override
247     public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base)
248     {
249         if (base != null)
250         {
251             return null;
252         }
253 
254         ArrayList<FeatureDescriptor> descriptors = new ArrayList<>(implicitObjects.size());
255 
256         for (ImplicitObject obj : implicitObjects.values())
257         {
258             descriptors.add(obj.getDescriptor());
259         }
260 
261         return descriptors.iterator();
262     }
263 
264     @Override
265     public Class<?> getCommonPropertyType(ELContext context, Object base)
266     {
267         if (base != null)
268         {
269             return null;
270         }
271 
272         return String.class;
273     }
274 
275 }