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 | 0 | 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 | 0 | Collection<String> propertyGuard = propertyGuardThreadLocal.get(); |
64 | |
|
65 | 0 | if (propertyGuard == null) |
66 | |
{ |
67 | 0 | propertyGuard = new HashSet<String>(); |
68 | 0 | propertyGuardThreadLocal.set(propertyGuard); |
69 | |
} |
70 | |
|
71 | 0 | return propertyGuard; |
72 | |
} |
73 | |
|
74 | |
private VariableResolver variableResolver; |
75 | |
private boolean isDefaultLegacyVariableResolver; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public VariableResolverToELResolver(final VariableResolver variableResolver) |
81 | 0 | { |
82 | 0 | this.variableResolver = variableResolver; |
83 | 0 | this.isDefaultLegacyVariableResolver = (this.variableResolver instanceof VariableResolverImpl); |
84 | 0 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public VariableResolver getVariableResolver() |
90 | |
{ |
91 | 0 | return variableResolver; |
92 | |
} |
93 | |
|
94 | |
@Override |
95 | |
public Object getValue(ELContext context, Object base, Object property) throws NullPointerException, |
96 | |
PropertyNotFoundException, ELException |
97 | |
{ |
98 | 0 | if (isDefaultLegacyVariableResolver) |
99 | |
{ |
100 | |
|
101 | |
|
102 | |
|
103 | 0 | return null; |
104 | |
} |
105 | 0 | if (base != null) |
106 | |
{ |
107 | 0 | return null; |
108 | |
} |
109 | 0 | if (property == null) |
110 | |
{ |
111 | 0 | throw new PropertyNotFoundException(); |
112 | |
} |
113 | |
|
114 | 0 | context.setPropertyResolved(true); |
115 | |
|
116 | 0 | if (!(property instanceof String)) |
117 | |
{ |
118 | 0 | return null; |
119 | |
} |
120 | |
|
121 | 0 | final String strProperty = (String) property; |
122 | |
|
123 | 0 | Collection<String> propertyGuard = getPropertyGuard(); |
124 | |
|
125 | 0 | Object result = null; |
126 | |
try |
127 | |
{ |
128 | |
|
129 | 0 | if(!propertyGuard.contains(strProperty)) |
130 | |
{ |
131 | 0 | propertyGuard.add(strProperty); |
132 | 0 | result = variableResolver.resolveVariable(facesContext(context), strProperty); |
133 | |
} |
134 | |
} |
135 | 0 | catch (javax.faces.el.PropertyNotFoundException e) |
136 | |
{ |
137 | 0 | context.setPropertyResolved(false); |
138 | 0 | throw new PropertyNotFoundException(e.getMessage(), e); |
139 | |
} |
140 | 0 | catch (EvaluationException e) |
141 | |
{ |
142 | 0 | context.setPropertyResolved(false); |
143 | 0 | throw new ELException(e.getMessage(), e); |
144 | |
} |
145 | 0 | catch (RuntimeException e) |
146 | |
{ |
147 | 0 | context.setPropertyResolved(false); |
148 | 0 | throw e; |
149 | |
} |
150 | |
finally |
151 | |
{ |
152 | 0 | propertyGuard.remove(strProperty); |
153 | |
|
154 | |
|
155 | |
|
156 | 0 | if (propertyGuard.isEmpty()) |
157 | |
{ |
158 | 0 | propertyGuardThreadLocal.remove(); |
159 | |
} |
160 | |
|
161 | |
|
162 | 0 | context.setPropertyResolved(result != null); |
163 | 0 | } |
164 | |
|
165 | 0 | return result; |
166 | |
} |
167 | |
|
168 | |
|
169 | |
private static FacesContext facesContext(ELContext context) |
170 | |
{ |
171 | 0 | return (FacesContext) context.getContext(FacesContext.class); |
172 | |
} |
173 | |
|
174 | |
@Override |
175 | |
public Class<?> getCommonPropertyType(ELContext context, Object base) |
176 | |
{ |
177 | 0 | if (isDefaultLegacyVariableResolver) |
178 | |
{ |
179 | 0 | return null; |
180 | |
} |
181 | 0 | if (base != null) |
182 | |
{ |
183 | 0 | return null; |
184 | |
} |
185 | |
|
186 | 0 | 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 | 0 | if (isDefaultLegacyVariableResolver) |
194 | |
{ |
195 | 0 | return; |
196 | |
} |
197 | 0 | if ((base == null) && (property == null)) |
198 | |
{ |
199 | 0 | throw new PropertyNotFoundException(); |
200 | |
} |
201 | 0 | } |
202 | |
|
203 | |
@Override |
204 | |
public boolean isReadOnly(ELContext context, Object base, Object property) throws NullPointerException, |
205 | |
PropertyNotFoundException, ELException |
206 | |
{ |
207 | 0 | if (isDefaultLegacyVariableResolver) |
208 | |
{ |
209 | 0 | return false; |
210 | |
} |
211 | 0 | if ((base == null) && (property == null)) |
212 | |
{ |
213 | 0 | throw new PropertyNotFoundException(); |
214 | |
} |
215 | |
|
216 | 0 | return false; |
217 | |
} |
218 | |
|
219 | |
@Override |
220 | |
public Class<?> getType(ELContext context, Object base, Object property) throws NullPointerException, |
221 | |
PropertyNotFoundException, ELException |
222 | |
{ |
223 | 0 | if (isDefaultLegacyVariableResolver) |
224 | |
{ |
225 | 0 | return null; |
226 | |
} |
227 | 0 | if ((base == null) && (property == null)) |
228 | |
{ |
229 | 0 | throw new PropertyNotFoundException(); |
230 | |
} |
231 | |
|
232 | 0 | return null; |
233 | |
} |
234 | |
|
235 | |
@Override |
236 | |
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) |
237 | |
{ |
238 | 0 | if (isDefaultLegacyVariableResolver) |
239 | |
{ |
240 | 0 | return null; |
241 | |
} |
242 | 0 | return null; |
243 | |
} |
244 | |
|
245 | |
} |