1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
33
34
35
36 @SuppressWarnings("deprecation")
37 public final class PropertyResolverImpl extends PropertyResolver
38 {
39
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 {
76 throw new PropertyNotFoundException();
77 }
78
79 invokeResolver(new ResolverInvoker<Object>(base, property)
80 {
81 @Override
82 public Object invoke(ELResolver resolver, ELContext context)
83 {
84 resolver.setValue(context, base, property, newValue);
85 return null;
86 }
87
88 @Override
89 String getMessage()
90 {
91 return super.getMessage() + " newValue: '" + newValue + "'";
92 }
93 });
94 }
95
96 @Override
97 public void setValue(Object base, int index, Object newValue) throws EvaluationException, PropertyNotFoundException
98 {
99 if (base == null)
100 {
101 throw new PropertyNotFoundException();
102 }
103
104 if (base instanceof Object[])
105 {
106 if (index < 0 || index >= ((Object[])base).length)
107 {
108 throw new PropertyNotFoundException();
109 }
110 }
111 else if (base instanceof List)
112 {
113 if (index < 0 || index >= ((List<?>)base).size())
114 {
115 throw new PropertyNotFoundException();
116 }
117 }
118
119 setValue(base, Integer.valueOf(index), newValue);
120 }
121
122 @Override
123 public boolean isReadOnly(final Object base, final Object property)
124 {
125 return invokeResolver(new ResolverInvoker<Boolean>(base, property)
126 {
127 @Override
128 public Boolean invoke(ELResolver resolver, ELContext context)
129 {
130 return Boolean.valueOf(getELResolver().isReadOnly(getELContext(), base, property));
131 }
132 });
133 }
134
135 @Override
136 public boolean isReadOnly(final Object base, final int index)
137 {
138 return isReadOnly(base, Integer.valueOf(index));
139 }
140
141 @Override
142 public Class getType(final Object base, final Object property)
143 {
144 if (base == null || property == null)
145 {
146 throw new PropertyNotFoundException();
147 }
148
149 return invokeResolver(new ResolverInvoker<Class<?>>(base, property)
150 {
151 @Override
152 public Class<?> invoke(final ELResolver resolver, final ELContext context)
153 {
154 return resolver.getType(context, base, property);
155 }
156 });
157 }
158
159 @Override
160 public Class getType(Object base, int index)
161 {
162 if (base == null)
163 {
164 throw new PropertyNotFoundException();
165 }
166
167 if (base instanceof Object[])
168 {
169 if (index < 0 || index >= ((Object[])base).length)
170 {
171 throw new PropertyNotFoundException();
172 }
173 }
174 else if (base instanceof List)
175 {
176 if (index < 0 || index >= ((List<?>)base).size())
177 {
178 throw new PropertyNotFoundException();
179 }
180 }
181
182 return getType(base, Integer.valueOf(index));
183 }
184
185
186
187
188 ELContext getELContext()
189 {
190 return getFacesContext().getELContext();
191 }
192
193 ELResolver getELResolver()
194 {
195 return getFacesContext().getApplication().getELResolver();
196 }
197
198 FacesContext getFacesContext()
199 {
200 return FacesContext.getCurrentInstance();
201 }
202
203 <T> T invokeResolver(ResolverInvoker<T> invoker) throws EvaluationException, PropertyNotFoundException
204 {
205 try
206 {
207 return invoker.invoke(getELResolver(), getELContext());
208 }
209 catch (javax.el.PropertyNotFoundException e)
210 {
211 throw new PropertyNotFoundException("property not found: " + invoker.getMessage() + ": " + e.getMessage(),
212 e);
213 }
214 catch (ELException e)
215 {
216 throw new EvaluationException("exception: " + invoker.getMessage() + ": " + e.getMessage(), e);
217 }
218 catch (RuntimeException e)
219 {
220 throw new RuntimeException("runtime exception: " + invoker.getMessage() + ": " + e.getMessage(), e);
221 }
222 }
223
224 abstract static class ResolverInvoker<T>
225 {
226 private final Object _base;
227 private final Object _property;
228
229 ResolverInvoker(final Object base, final Object property)
230 {
231 _base = base;
232 _property = property;
233 }
234
235 abstract T invoke(ELResolver resolver, ELContext context);
236
237 String getMessage()
238 {
239 return "base: '" + _base + "' property/index: '" + _property + "'";
240 }
241 }
242 }