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;
20  
21  import java.util.List;
22  
23  import javax.el.ELContext;
24  import javax.el.ELException;
25  import javax.el.ELResolver;
26  import javax.faces.context.FacesContext;
27  import javax.faces.el.EvaluationException;
28  import javax.faces.el.PropertyNotFoundException;
29  import javax.faces.el.PropertyResolver;
30  
31  /**
32   * @author Manfred Geiler (latest modification by $Author: bommel $)
33   * @author Anton Koinov
34   * @version $Revision: 1187700 $ $Date: 2011-10-22 07:19:37 -0500 (Sat, 22 Oct 2011) $
35   */
36  @SuppressWarnings("deprecation")
37  public final class PropertyResolverImpl extends PropertyResolver
38  {
39      // ~ Public PropertyResolver Methods
40      // ----------------------------------------
41  
42      @Override
43      public Object getValue(final Object base, final Object property) throws EvaluationException,
44          PropertyNotFoundException
45      {
46          if (base == null)
47          {
48              return null;
49          }
50          if (property == null)
51          {
52              return null;
53          }
54          return invokeResolver(new ResolverInvoker<Object>(base, property)
55          {
56              @Override
57              public Object invoke(ELResolver resolver, ELContext context)
58              {
59                  return getELResolver().getValue(getELContext(), base, property);
60              }
61          });
62      }
63  
64      @Override
65      public Object getValue(final Object base, final int index) throws EvaluationException, PropertyNotFoundException
66      {
67          return getValue(base, Integer.valueOf(index));
68      }
69  
70      @Override
71      public void setValue(final Object base, final Object property, final Object newValue) throws EvaluationException,
72          PropertyNotFoundException
73      {
74          if (base == null || property == null || isReadOnly (base, property))
75              throw new PropertyNotFoundException();
76  
77          invokeResolver(new ResolverInvoker<Object>(base, property)
78          {
79              @Override
80              public Object invoke(ELResolver resolver, ELContext context)
81              {
82                  resolver.setValue(context, base, property, newValue);
83                  return null;
84              }
85  
86              @Override
87              String getMessage()
88              {
89                  return super.getMessage() + " newValue: '" + newValue + "'";
90              }
91          });
92      }
93  
94      @Override
95      public void setValue(Object base, int index, Object newValue) throws EvaluationException, PropertyNotFoundException
96      {
97          if (base == null)
98              throw new PropertyNotFoundException();
99  
100         if (base instanceof Object[])
101         {
102             if (index < 0 || index >= ((Object[])base).length)
103             {
104                 throw new PropertyNotFoundException();
105             }
106         }
107         else if (base instanceof List)
108         {
109             if (index < 0 || index >= ((List<?>)base).size())
110             {
111                 throw new PropertyNotFoundException();
112             }
113         }
114 
115         setValue(base, Integer.valueOf(index), newValue);
116     }
117 
118     @Override
119     public boolean isReadOnly(final Object base, final Object property)
120     {
121         return invokeResolver(new ResolverInvoker<Boolean>(base, property)
122         {
123             @Override
124             public Boolean invoke(ELResolver resolver, ELContext context)
125             {
126                 return Boolean.valueOf(getELResolver().isReadOnly(getELContext(), base, property));
127             }
128         });
129     }
130 
131     @Override
132     public boolean isReadOnly(final Object base, final int index)
133     {
134         return isReadOnly(base, Integer.valueOf(index));
135     }
136 
137     @Override
138     public Class getType(final Object base, final Object property)
139     {
140         if (base == null || property == null)
141             throw new PropertyNotFoundException();
142 
143         return invokeResolver(new ResolverInvoker<Class<?>>(base, property)
144         {
145             @Override
146             public Class<?> invoke(final ELResolver resolver, final ELContext context)
147             {
148                 return resolver.getType(context, base, property);
149             }
150         });
151     }
152 
153     @Override
154     public Class getType(Object base, int index)
155     {
156         if (base == null)
157             throw new PropertyNotFoundException();
158 
159         if (base instanceof Object[])
160         {
161             if (index < 0 || index >= ((Object[])base).length)
162             {
163                 throw new PropertyNotFoundException();
164             }
165         }
166         else if (base instanceof List)
167         {
168             if (index < 0 || index >= ((List<?>)base).size())
169             {
170                 throw new PropertyNotFoundException();
171             }
172         }
173 
174         return getType(base, Integer.valueOf(index));
175     }
176 
177     // ~ Internal Helper Methods
178     // ------------------------------------------------
179 
180     ELContext getELContext()
181     {
182         return getFacesContext().getELContext();
183     }
184 
185     ELResolver getELResolver()
186     {
187         return getFacesContext().getApplication().getELResolver();
188     }
189 
190     FacesContext getFacesContext()
191     {
192         return FacesContext.getCurrentInstance();
193     }
194 
195     <T> T invokeResolver(ResolverInvoker<T> invoker) throws EvaluationException, PropertyNotFoundException
196     {
197         try
198         {
199             return invoker.invoke(getELResolver(), getELContext());
200         }
201         catch (javax.el.PropertyNotFoundException e)
202         {
203             throw new PropertyNotFoundException("property not found: " + invoker.getMessage() + ": " + e.getMessage(),
204                 e);
205         }
206         catch (ELException e)
207         {
208             throw new EvaluationException("exception: " + invoker.getMessage() + ": " + e.getMessage(), e);
209         }
210         catch (RuntimeException e)
211         {
212             throw new RuntimeException("runtime exception: " + invoker.getMessage() + ": " + e.getMessage(), e);
213         }
214     }
215 
216     abstract static class ResolverInvoker<T>
217     {
218         private final Object _base;
219         private final Object _property;
220 
221         ResolverInvoker(final Object base, final Object property)
222         {
223             _base = base;
224             _property = property;
225         }
226 
227         abstract T invoke(ELResolver resolver, ELContext context);
228 
229         String getMessage()
230         {
231             return "base: '" + _base + "' property/index: '" + _property + "'";
232         }
233     }
234 }