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.*;
22  import java.beans.FeatureDescriptor;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  /**
29   * See JSF 1.2 spec sections 5.6.1.1 and 5.6.2.1
30   * 
31   * @author Stan Silvert
32   */
33  public class ImplicitObjectResolver extends ELResolver
34  {
35  
36      private Map<String, ImplicitObject> implicitObjects;
37  
38      /**
39       * Static factory for an ELResolver for resolving implicit objects in JSPs. See JSF 1.2 spec section 5.6.1.1
40       */
41      public static ELResolver makeResolverForJSP()
42      {
43          Map<String, ImplicitObject> forJSPList = new HashMap<String, ImplicitObject>(2);
44          ImplicitObject io1 = new FacesContextImplicitObject();
45          forJSPList.put(io1.getName(), io1);
46          ImplicitObject io2 = new ViewImplicitObject();
47          forJSPList.put(io2.getName(), io2);
48          return new ImplicitObjectResolver(forJSPList);
49      }
50  
51      /**
52       * Static factory for an ELResolver for resolving implicit objects in all of Faces. See JSF 1.2 spec section 5.6.1.2
53       */
54      public static ELResolver makeResolverForFaces()
55      {
56          Map<String, ImplicitObject> forFacesList = new HashMap<String, ImplicitObject>(14);
57          ImplicitObject io1 = new ApplicationImplicitObject();
58          forFacesList.put(io1.getName(), io1);
59          ImplicitObject io2 = new ApplicationScopeImplicitObject();
60          forFacesList.put(io2.getName(), io2);
61          ImplicitObject io3 = new CookieImplicitObject();
62          forFacesList.put(io3.getName(), io3);
63          ImplicitObject io4 = new FacesContextImplicitObject();
64          forFacesList.put(io4.getName(), io4);
65          ImplicitObject io5 = new HeaderImplicitObject();
66          forFacesList.put(io5.getName(), io5);
67          ImplicitObject io6 = new HeaderValuesImplicitObject();
68          forFacesList.put(io6.getName(), io6);
69          ImplicitObject io7 = new InitParamImplicitObject();
70          forFacesList.put(io7.getName(), io7);
71          ImplicitObject io8 = new ParamImplicitObject();
72          forFacesList.put(io8.getName(), io8);
73          ImplicitObject io9 = new ParamValuesImplicitObject();
74          forFacesList.put(io9.getName(), io9);
75          ImplicitObject io10 = new RequestImplicitObject();
76          forFacesList.put(io10.getName(), io10);
77          ImplicitObject io11 = new RequestScopeImplicitObject();
78          forFacesList.put(io11.getName(), io11);
79          ImplicitObject io12 = new SessionImplicitObject();
80          forFacesList.put(io12.getName(), io12);
81          ImplicitObject io13 = new SessionScopeImplicitObject();
82          forFacesList.put(io13.getName(), io13);
83          ImplicitObject io14 = new ViewImplicitObject();
84          forFacesList.put(io14.getName(), io14);
85          return new ImplicitObjectResolver(forFacesList);
86      }
87  
88      private ImplicitObjectResolver()
89      {
90          super();
91          this.implicitObjects = new HashMap<String, ImplicitObject>();
92      }
93  
94      /** Creates a new instance of ImplicitObjectResolverForJSP */
95      private ImplicitObjectResolver(Map<String, ImplicitObject> implicitObjects)
96      {
97          this();
98          this.implicitObjects = implicitObjects;
99      }
100 
101     @Override
102     public void setValue(ELContext context, Object base, Object property, Object value) throws NullPointerException,
103         PropertyNotFoundException, PropertyNotWritableException, ELException
104     {
105 
106         if (base != null)
107             return;
108         if (property == null)
109             throw new PropertyNotFoundException();
110         if (!(property instanceof String))
111             return;
112 
113         String strProperty = property.toString();
114 
115         if (implicitObjects.containsKey(strProperty))
116         {
117             throw new PropertyNotWritableException();
118         }
119     }
120 
121     @Override
122     public boolean isReadOnly(ELContext context, Object base, Object property) throws NullPointerException,
123         PropertyNotFoundException, ELException
124     {
125 
126         if (base != null)
127             return false;
128         if (property == null)
129             throw new PropertyNotFoundException();
130         if (!(property instanceof String))
131             return false;
132 
133         String strProperty = property.toString();
134 
135         if (implicitObjects.containsKey(strProperty))
136         {
137             context.setPropertyResolved(true);
138             return true;
139         }
140 
141         return false;
142     }
143 
144     @Override
145     public Object getValue(ELContext context, Object base, Object property) throws NullPointerException,
146         PropertyNotFoundException, ELException
147     {
148 
149         if (base != null)
150             return null;
151         if (property == null)
152             throw new PropertyNotFoundException();
153         if (!(property instanceof String))
154             return null;
155 
156         String strProperty = property.toString();
157 
158         ImplicitObject obj = implicitObjects.get(strProperty);
159         if (obj != null)
160         {
161             context.setPropertyResolved(true);
162             return obj.getValue(context);
163         }
164 
165         return null;
166     }
167 
168     @Override
169     public Class<?> getType(ELContext context, Object base, Object property) throws NullPointerException,
170         PropertyNotFoundException, ELException
171     {
172 
173         if (base != null)
174             return null;
175         if (property == null)
176             throw new PropertyNotFoundException();
177         if (!(property instanceof String))
178             return null;
179 
180         String strProperty = property.toString();
181 
182         if (implicitObjects.containsKey(strProperty))
183         {
184             context.setPropertyResolved(true);
185         }
186 
187         return null;
188     }
189 
190     @Override
191     public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base)
192     {
193         if (base != null)
194             return null;
195 
196         ArrayList<FeatureDescriptor> descriptors = new ArrayList<FeatureDescriptor>(implicitObjects.size());
197 
198         for (ImplicitObject obj : implicitObjects.values())
199         {
200             descriptors.add(obj.getDescriptor());
201         }
202 
203         return descriptors.iterator();
204     }
205 
206     @Override
207     public Class<?> getCommonPropertyType(ELContext context, Object base)
208     {
209         if (base != null)
210             return null;
211 
212         return String.class;
213     }
214 
215 }