1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
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 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public final class FacesCompositeELResolver extends org.apache.myfaces.el.CompositeELResolver |
47 | |
{ |
48 | |
private final Scope _scope; |
49 | |
|
50 | 0 | public enum Scope |
51 | |
{ |
52 | 0 | 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 | 0 | { |
59 | 0 | if (scope == null) |
60 | |
{ |
61 | 0 | throw new IllegalArgumentException("scope must not be one of " + Arrays.toString(Scope.values())); |
62 | |
} |
63 | 0 | _scope = scope; |
64 | 0 | } |
65 | |
|
66 | |
private static FacesContext facesContext(final ELContext context) |
67 | |
{ |
68 | 0 | FacesContext facesContext = (FacesContext)context.getContext(FacesContext.class); |
69 | 0 | if (facesContext == null) |
70 | |
{ |
71 | 0 | facesContext = FacesContext.getCurrentInstance(); |
72 | |
} |
73 | 0 | return facesContext; |
74 | |
} |
75 | |
|
76 | |
@Override |
77 | |
public Class<?> getCommonPropertyType(final ELContext context, final Object base) |
78 | |
{ |
79 | 0 | final FacesContext facesContext = facesContext(context); |
80 | 0 | if (facesContext == null) |
81 | |
{ |
82 | 0 | return null; |
83 | |
} |
84 | 0 | final Map<Object, Object> requestMap = facesContext.getAttributes(); |
85 | 0 | Scope prevScope = null; |
86 | |
try |
87 | |
{ |
88 | 0 | prevScope = getScope(requestMap); |
89 | 0 | setScope(requestMap); |
90 | 0 | return super.getCommonPropertyType(context, base); |
91 | |
} |
92 | |
finally |
93 | |
{ |
94 | 0 | if(prevScope != null) |
95 | |
{ |
96 | 0 | setScope(requestMap, prevScope); |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | 0 | unsetScope(requestMap); |
101 | |
} |
102 | |
} |
103 | |
|
104 | |
} |
105 | |
|
106 | |
@Override |
107 | |
public Iterator<FeatureDescriptor> getFeatureDescriptors(final ELContext context, final Object base) |
108 | |
{ |
109 | 0 | final FacesContext facesContext = facesContext(context); |
110 | 0 | if (facesContext == null) |
111 | |
{ |
112 | 0 | return null; |
113 | |
} |
114 | 0 | final Map<Object, Object> requestMap = facesContext.getAttributes(); |
115 | 0 | Scope prevScope = null; |
116 | |
try |
117 | |
{ |
118 | 0 | prevScope = getScope(requestMap); |
119 | 0 | setScope(requestMap); |
120 | 0 | return super.getFeatureDescriptors(context, base); |
121 | |
|
122 | |
} |
123 | |
finally |
124 | |
{ |
125 | 0 | if(prevScope != null) |
126 | |
{ |
127 | 0 | setScope(requestMap, prevScope); |
128 | |
} |
129 | |
else |
130 | |
{ |
131 | 0 | unsetScope(requestMap); |
132 | |
} |
133 | |
} |
134 | |
} |
135 | |
|
136 | |
@Override |
137 | |
public Class<?> getType(final ELContext context, final Object base, final Object property) |
138 | |
{ |
139 | 0 | final FacesContext facesContext = facesContext(context); |
140 | 0 | if (facesContext == null) |
141 | |
{ |
142 | 0 | return null; |
143 | |
} |
144 | 0 | final Map<Object, Object> requestMap = facesContext.getAttributes(); |
145 | 0 | Scope prevScope = null; |
146 | |
try |
147 | |
{ |
148 | 0 | prevScope = getScope(requestMap); |
149 | 0 | setScope(requestMap); |
150 | 0 | return super.getType(context, base, property); |
151 | |
} |
152 | |
finally |
153 | |
{ |
154 | 0 | if(prevScope != null) |
155 | |
{ |
156 | 0 | setScope(requestMap, prevScope); |
157 | |
} |
158 | |
else |
159 | |
{ |
160 | 0 | unsetScope(requestMap); |
161 | |
} |
162 | |
} |
163 | |
} |
164 | |
|
165 | |
@Override |
166 | |
public Object getValue(final ELContext context, final Object base, final Object property) |
167 | |
{ |
168 | 0 | final FacesContext facesContext = facesContext(context); |
169 | 0 | if (facesContext == null) |
170 | |
{ |
171 | 0 | return null; |
172 | |
} |
173 | 0 | final Map<Object, Object> requestMap = facesContext.getAttributes(); |
174 | 0 | Scope prevScope = null; |
175 | |
try |
176 | |
{ |
177 | 0 | prevScope = getScope(requestMap); |
178 | 0 | setScope(requestMap); |
179 | 0 | return super.getValue(context, base, property); |
180 | |
} |
181 | |
finally |
182 | |
{ |
183 | 0 | if(prevScope != null) |
184 | |
{ |
185 | 0 | setScope(requestMap, prevScope); |
186 | |
} |
187 | |
else |
188 | |
{ |
189 | 0 | unsetScope(requestMap); |
190 | |
} |
191 | |
} |
192 | |
} |
193 | |
|
194 | |
@Override |
195 | |
public boolean isReadOnly(final ELContext context, final Object base, final Object property) |
196 | |
{ |
197 | 0 | final FacesContext facesContext = facesContext(context); |
198 | 0 | if (facesContext == null) |
199 | |
{ |
200 | 0 | return false; |
201 | |
} |
202 | 0 | final Map<Object, Object> requestMap = facesContext.getAttributes(); |
203 | 0 | Scope prevScope = null; |
204 | |
try |
205 | |
{ |
206 | 0 | prevScope = getScope(requestMap); |
207 | 0 | setScope(requestMap); |
208 | 0 | return super.isReadOnly(context, base, property); |
209 | |
} |
210 | |
finally |
211 | |
{ |
212 | 0 | if(prevScope != null) |
213 | |
{ |
214 | 0 | setScope(requestMap, prevScope); |
215 | |
} |
216 | |
else |
217 | |
{ |
218 | 0 | unsetScope(requestMap); |
219 | |
} |
220 | |
} |
221 | |
} |
222 | |
|
223 | |
@Override |
224 | |
public void setValue(final ELContext context, final Object base, final Object property, final Object val) |
225 | |
{ |
226 | 0 | final FacesContext facesContext = facesContext(context); |
227 | 0 | if (facesContext == null) |
228 | |
{ |
229 | 0 | return; |
230 | |
} |
231 | 0 | final Map<Object, Object> requestMap = facesContext.getAttributes(); |
232 | 0 | Scope prevScope = null; |
233 | |
try |
234 | |
{ |
235 | 0 | prevScope = getScope(requestMap); |
236 | 0 | setScope(requestMap); |
237 | 0 | super.setValue(context, base, property, val); |
238 | |
|
239 | |
} |
240 | |
finally |
241 | |
{ |
242 | 0 | if(prevScope != null) |
243 | |
{ |
244 | 0 | setScope(requestMap, prevScope); |
245 | |
} |
246 | |
else |
247 | |
{ |
248 | 0 | unsetScope(requestMap); |
249 | |
} |
250 | 0 | } |
251 | 0 | } |
252 | |
|
253 | |
private void setScope(final Map<Object, Object> attributes) |
254 | |
{ |
255 | 0 | attributes.put(SCOPE, _scope); |
256 | 0 | } |
257 | |
|
258 | |
private Scope getScope(final Map<Object, Object> attributes) |
259 | |
{ |
260 | 0 | return (Scope) attributes.get(SCOPE); |
261 | |
} |
262 | |
|
263 | |
private void setScope(final Map<Object, Object> attributes, Scope prevScope) |
264 | |
{ |
265 | 0 | attributes.put(SCOPE, prevScope); |
266 | 0 | } |
267 | |
|
268 | |
private static void unsetScope(final Map<Object, Object> attributes) |
269 | |
{ |
270 | |
|
271 | 0 | attributes.put(SCOPE, Scope.NONE); |
272 | 0 | } |
273 | |
} |