1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.el.convert;
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 javax.faces.context.FacesContext;
27 import javax.faces.el.EvaluationException;
28 import javax.faces.el.VariableResolver;
29 import java.beans.FeatureDescriptor;
30 import java.util.Collection;
31 import java.util.HashSet;
32 import java.util.Iterator;
33 import org.apache.myfaces.el.VariableResolverImpl;
34
35
36
37
38
39
40
41
42 @SuppressWarnings("deprecation")
43 public final class VariableResolverToELResolver extends ELResolver
44 {
45
46
47 private static final ThreadLocal<Collection<String>> propertyGuardThreadLocal
48 = new ThreadLocal<Collection<String>>();
49
50
51
52
53
54
55
56
57
58
59
60
61 private static Collection<String> getPropertyGuard()
62 {
63 Collection<String> propertyGuard = propertyGuardThreadLocal.get();
64
65 if (propertyGuard == null)
66 {
67 propertyGuard = new HashSet<String>();
68 propertyGuardThreadLocal.set(propertyGuard);
69 }
70
71 return propertyGuard;
72 }
73
74 private VariableResolver variableResolver;
75 private boolean isDefaultLegacyVariableResolver;
76
77
78
79
80 public VariableResolverToELResolver(final VariableResolver variableResolver)
81 {
82 this.variableResolver = variableResolver;
83 this.isDefaultLegacyVariableResolver = (this.variableResolver instanceof VariableResolverImpl);
84 }
85
86
87
88
89 public VariableResolver getVariableResolver()
90 {
91 return variableResolver;
92 }
93
94 @Override
95 public Object getValue(ELContext context, Object base, Object property) throws NullPointerException,
96 PropertyNotFoundException, ELException
97 {
98 if (isDefaultLegacyVariableResolver)
99 {
100
101
102
103 return null;
104 }
105 if (base != null)
106 {
107 return null;
108 }
109 if (property == null)
110 {
111 throw new PropertyNotFoundException();
112 }
113
114 context.setPropertyResolved(true);
115
116 if (!(property instanceof String))
117 {
118 return null;
119 }
120
121 final String strProperty = (String) property;
122
123 Collection<String> propertyGuard = getPropertyGuard();
124
125 Object result = null;
126 try
127 {
128
129 if(!propertyGuard.contains(strProperty))
130 {
131 propertyGuard.add(strProperty);
132 result = variableResolver.resolveVariable(facesContext(context), strProperty);
133 }
134 }
135 catch (javax.faces.el.PropertyNotFoundException e)
136 {
137 context.setPropertyResolved(false);
138 throw new PropertyNotFoundException(e.getMessage(), e);
139 }
140 catch (EvaluationException e)
141 {
142 context.setPropertyResolved(false);
143 throw new ELException(e.getMessage(), e);
144 }
145 catch (RuntimeException e)
146 {
147 context.setPropertyResolved(false);
148 throw e;
149 }
150 finally
151 {
152 propertyGuard.remove(strProperty);
153
154
155
156 if (propertyGuard.isEmpty())
157 {
158 propertyGuardThreadLocal.remove();
159 }
160
161
162 context.setPropertyResolved(result != null);
163 }
164
165 return result;
166 }
167
168
169 private static FacesContext facesContext(ELContext context)
170 {
171 return (FacesContext) context.getContext(FacesContext.class);
172 }
173
174 @Override
175 public Class<?> getCommonPropertyType(ELContext context, Object base)
176 {
177 if (isDefaultLegacyVariableResolver)
178 {
179 return null;
180 }
181 if (base != null)
182 {
183 return null;
184 }
185
186 return String.class;
187 }
188
189 @Override
190 public void setValue(ELContext context, Object base, Object property, Object value) throws NullPointerException,
191 PropertyNotFoundException, PropertyNotWritableException, ELException
192 {
193 if (isDefaultLegacyVariableResolver)
194 {
195 return;
196 }
197 if ((base == null) && (property == null))
198 {
199 throw new PropertyNotFoundException();
200 }
201 }
202
203 @Override
204 public boolean isReadOnly(ELContext context, Object base, Object property) throws NullPointerException,
205 PropertyNotFoundException, ELException
206 {
207 if (isDefaultLegacyVariableResolver)
208 {
209 return false;
210 }
211 if ((base == null) && (property == null))
212 {
213 throw new PropertyNotFoundException();
214 }
215
216 return false;
217 }
218
219 @Override
220 public Class<?> getType(ELContext context, Object base, Object property) throws NullPointerException,
221 PropertyNotFoundException, ELException
222 {
223 if (isDefaultLegacyVariableResolver)
224 {
225 return null;
226 }
227 if ((base == null) && (property == null))
228 {
229 throw new PropertyNotFoundException();
230 }
231
232 return null;
233 }
234
235 @Override
236 public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base)
237 {
238 if (isDefaultLegacyVariableResolver)
239 {
240 return null;
241 }
242 return null;
243 }
244
245 }