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;
20  
21  import javax.el.ELContext;
22  import javax.faces.context.FacesContext;
23  import java.beans.FeatureDescriptor;
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.Arrays;
27  
28  /**
29   * <p>
30   * This composite el resolver will be used at the top level resolver for faces
31   * ({@link javax.faces.application.Application#getELResolver()})
32   * and jsp (the one we add with {@link javax.servlet.jsp.JspApplicationContext#addELResolver(javax.el.ELResolver)}.
33   * It keeps track of its scope to let the variable resolver {@link org.apache.myfaces.el.VariableResolverImpl}
34   * know in which scope it is executed. This is
35   * necessarry to call either the faces or the jsp resolver head.
36   * </p>
37   * <p>
38   * This implementation does nothing if there is no actual faces context. This is necessarry since we registered our
39   * resolvers into the jsp engine. Therefore we have to make sure that jsp only pages where no faces context is available
40   * are still working
41   * </p>
42   *
43   * @author Mathias Broekelmann (latest modification by $Author: lu4242 $)
44   * @version $Revision: 1232199 $ $Date: 2012-01-16 17:23:49 -0500 (Mon, 16 Jan 2012) $
45   */
46  public final class FacesCompositeELResolver extends org.apache.myfaces.el.CompositeELResolver
47  {
48      private final Scope _scope;
49  
50      public enum Scope
51      {
52          Faces, JSP, NONE
53      }
54      
55      public static final String SCOPE = "org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.Scope";
56      
57      public FacesCompositeELResolver(final Scope scope)
58      {
59          if (scope == null)
60          {
61              throw new IllegalArgumentException("scope must not be one of " + Arrays.toString(Scope.values()));
62          }
63          _scope = scope;
64      }
65  
66      @Override
67      public Class<?> getCommonPropertyType(final ELContext context, final Object base)
68      {
69          final FacesContext facesContext = FacesContext.getCurrentInstance();
70          if (facesContext == null)
71          {
72              return null;
73          }
74          final Map<Object, Object> requestMap = facesContext.getAttributes();
75          Scope prevScope = null;
76          try
77          {
78              prevScope = getScope(requestMap);
79              setScope(requestMap);
80              return super.getCommonPropertyType(context, base);
81          }
82          finally
83          {
84              if(prevScope != null)
85              {
86                  setScope(requestMap, prevScope);
87              }
88              else
89              {
90                  unsetScope(requestMap);
91              }
92          }
93  
94      }
95  
96      @Override
97      public Iterator<FeatureDescriptor> getFeatureDescriptors(final ELContext context, final Object base)
98      {
99          final FacesContext facesContext = FacesContext.getCurrentInstance();
100         if (facesContext == null)
101         {
102             return null;
103         }
104         final Map<Object, Object> requestMap = facesContext.getAttributes();
105         Scope prevScope = null;
106         try
107         {
108             prevScope = getScope(requestMap);
109             setScope(requestMap);
110             return super.getFeatureDescriptors(context, base);
111 
112         }
113         finally
114         {
115             if(prevScope != null)
116             {
117                 setScope(requestMap, prevScope);
118             }
119             else
120             {
121                 unsetScope(requestMap);
122             }
123         }
124     }
125 
126     @Override
127     public Class<?> getType(final ELContext context, final Object base, final Object property)
128     {
129         final FacesContext facesContext = FacesContext.getCurrentInstance();
130         if (facesContext == null)
131         {
132             return null;
133         }
134         final Map<Object, Object> requestMap = facesContext.getAttributes();
135         Scope prevScope = null;
136         try
137         {
138             prevScope = getScope(requestMap);
139             setScope(requestMap);
140             return super.getType(context, base, property);
141         }
142         finally
143         {
144             if(prevScope != null)
145             {
146                 setScope(requestMap, prevScope);
147             }
148             else
149             {
150                 unsetScope(requestMap);
151             }
152         }
153     }
154 
155     @Override
156     public Object getValue(final ELContext context, final Object base, final Object property)
157     {
158         final FacesContext facesContext = FacesContext.getCurrentInstance();
159         if (facesContext == null)
160         {
161             return null;
162         }
163         final Map<Object, Object> requestMap = facesContext.getAttributes();
164         Scope prevScope = null;
165         try
166         {
167             prevScope = getScope(requestMap);
168             setScope(requestMap);
169             return super.getValue(context, base, property);
170         }
171         finally
172         {
173             if(prevScope != null)
174             {
175                 setScope(requestMap, prevScope);
176             }
177             else
178             {
179                 unsetScope(requestMap);
180             }
181         }
182     }
183 
184     @Override
185     public boolean isReadOnly(final ELContext context, final Object base, final Object property)
186     {
187         final FacesContext facesContext = FacesContext.getCurrentInstance();
188         if (facesContext == null)
189         {
190             return false;
191         }
192         final Map<Object, Object> requestMap = facesContext.getAttributes();
193         Scope prevScope = null;
194         try
195         {
196             prevScope = getScope(requestMap);
197             setScope(requestMap);
198             return super.isReadOnly(context, base, property);
199         }
200         finally
201         {
202             if(prevScope != null)
203             {
204                 setScope(requestMap, prevScope);
205             }
206             else
207             {
208                 unsetScope(requestMap);
209             }
210         }
211     }
212 
213     @Override
214     public void setValue(final ELContext context, final Object base, final Object property, final Object val)
215     {
216         final FacesContext facesContext = FacesContext.getCurrentInstance();
217         if (facesContext == null)
218         {
219             return;
220         }
221         final Map<Object, Object> requestMap = facesContext.getAttributes();
222         Scope prevScope = null;
223         try
224         {
225             prevScope = getScope(requestMap);
226             setScope(requestMap);
227             super.setValue(context, base, property, val);
228 
229         }
230         finally
231         {
232             if(prevScope != null)
233             {
234                 setScope(requestMap, prevScope);
235             }
236             else
237             {
238                 unsetScope(requestMap);
239             }
240         }
241     }
242 
243     private void setScope(final Map<Object, Object> attributes)
244     {
245         attributes.put(SCOPE, _scope);
246     }
247     
248     private Scope getScope(final Map<Object, Object> attributes)
249     {
250         return (Scope) attributes.get(SCOPE);
251     }
252 
253     private void setScope(final Map<Object, Object> attributes, Scope prevScope)
254     {
255         attributes.put(SCOPE, prevScope);
256     }
257 
258     private static void unsetScope(final Map<Object, Object> attributes)
259     {
260         //attributes.remove(SCOPE);
261         attributes.put(SCOPE, Scope.NONE);
262     }
263 }