1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package javax.faces.component; |
21 | |
|
22 | |
import java.util.logging.Level; |
23 | |
import java.util.logging.Logger; |
24 | |
|
25 | |
import javax.el.ELContext; |
26 | |
import javax.el.ELException; |
27 | |
import javax.el.PropertyNotFoundException; |
28 | |
import javax.el.PropertyNotWritableException; |
29 | |
import javax.el.ValueExpression; |
30 | |
import javax.faces.context.FacesContext; |
31 | |
import javax.faces.el.EvaluationException; |
32 | |
import javax.faces.el.ValueBinding; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 40 | class _ValueBindingToValueExpression extends ValueExpression implements StateHolder |
45 | |
{ |
46 | |
private static final long serialVersionUID = 8071429285360496554L; |
47 | |
|
48 | |
|
49 | 2 | private static final Logger log = Logger.getLogger(_ValueBindingToValueExpression.class.getName()); |
50 | |
|
51 | |
private ValueBinding _valueBinding; |
52 | |
|
53 | |
private boolean _transient; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
protected _ValueBindingToValueExpression() |
59 | 2 | { |
60 | 2 | } |
61 | |
|
62 | |
private ValueBinding getNotNullValueBinding() |
63 | |
{ |
64 | 26 | if (_valueBinding == null) |
65 | |
{ |
66 | 2 | throw new IllegalStateException("value binding is null"); |
67 | |
} |
68 | 24 | return _valueBinding; |
69 | |
} |
70 | |
|
71 | |
|
72 | |
public _ValueBindingToValueExpression(ValueBinding valueBinding) |
73 | 40 | { |
74 | 40 | if (valueBinding == null) |
75 | |
{ |
76 | 0 | throw new IllegalArgumentException("value binding must not be null"); |
77 | |
} |
78 | 40 | this._valueBinding = valueBinding; |
79 | 40 | } |
80 | |
|
81 | |
public ValueBinding getValueBinding() |
82 | |
{ |
83 | 12 | return _valueBinding; |
84 | |
} |
85 | |
|
86 | |
@Override |
87 | |
public boolean isReadOnly(final ELContext context) throws NullPointerException, PropertyNotFoundException, |
88 | |
ELException |
89 | |
{ |
90 | 4 | return invoke(new Invoker<Boolean>() |
91 | 8 | { |
92 | |
public Boolean invoke() |
93 | |
{ |
94 | 4 | return getNotNullValueBinding().isReadOnly(getFacesContext(context)); |
95 | |
} |
96 | |
}); |
97 | |
} |
98 | |
|
99 | |
@Override |
100 | |
public Object getValue(final ELContext context) throws NullPointerException, PropertyNotFoundException, ELException |
101 | |
{ |
102 | 4 | return invoke(new Invoker<Object>() |
103 | 4 | { |
104 | |
public Object invoke() |
105 | |
{ |
106 | 4 | return getNotNullValueBinding().getValue(getFacesContext(context)); |
107 | |
} |
108 | |
}); |
109 | |
} |
110 | |
|
111 | |
@Override |
112 | |
public Class<?> getType(final ELContext context) throws NullPointerException, PropertyNotFoundException, |
113 | |
ELException |
114 | |
{ |
115 | 6 | return invoke(new Invoker<Class<?>>() |
116 | 12 | { |
117 | |
public Class<?> invoke() |
118 | |
{ |
119 | 6 | return getNotNullValueBinding().getType(getFacesContext(context)); |
120 | |
} |
121 | |
}); |
122 | |
} |
123 | |
|
124 | |
@Override |
125 | |
public void setValue(final ELContext context, final Object value) throws NullPointerException, |
126 | |
PropertyNotFoundException, PropertyNotWritableException, ELException |
127 | |
{ |
128 | 6 | invoke(new Invoker<Object>() |
129 | 6 | { |
130 | |
public Object invoke() |
131 | |
{ |
132 | 6 | getNotNullValueBinding().setValue(getFacesContext(context), value); |
133 | 2 | return null; |
134 | |
} |
135 | |
}); |
136 | 2 | } |
137 | |
|
138 | |
@Override |
139 | |
public int hashCode() |
140 | |
{ |
141 | 14 | int prime = 31; |
142 | 14 | int result = 1; |
143 | 14 | result = prime * result + (_transient ? 1231 : 1237); |
144 | 14 | result = prime * result + ((_valueBinding == null) ? 0 : _valueBinding.hashCode()); |
145 | 14 | return result; |
146 | |
} |
147 | |
|
148 | |
@Override |
149 | |
public boolean equals(Object obj) |
150 | |
{ |
151 | 8 | if (this == obj) |
152 | |
{ |
153 | 2 | return true; |
154 | |
} |
155 | 6 | if (obj == null) |
156 | |
{ |
157 | 0 | return false; |
158 | |
} |
159 | 6 | if (getClass() != obj.getClass()) |
160 | |
{ |
161 | 2 | return false; |
162 | |
} |
163 | 4 | _ValueBindingToValueExpression other = (_ValueBindingToValueExpression)obj; |
164 | 4 | if (_transient != other._transient) |
165 | |
{ |
166 | 2 | return false; |
167 | |
} |
168 | 2 | if (_valueBinding == null) |
169 | |
{ |
170 | 0 | if (other._valueBinding != null) |
171 | |
{ |
172 | 0 | return false; |
173 | |
} |
174 | |
} |
175 | 2 | else if (!_valueBinding.equals(other._valueBinding)) |
176 | |
{ |
177 | 0 | return false; |
178 | |
} |
179 | 2 | return true; |
180 | |
} |
181 | |
|
182 | |
@Override |
183 | |
public boolean isLiteralText() |
184 | |
{ |
185 | 6 | return false; |
186 | |
} |
187 | |
|
188 | |
@Override |
189 | |
public String getExpressionString() |
190 | |
{ |
191 | 4 | return getNotNullValueBinding().getExpressionString(); |
192 | |
} |
193 | |
|
194 | |
@Override |
195 | |
public Class<?> getExpectedType() |
196 | |
{ |
197 | 4 | if (_valueBinding != null) |
198 | |
{ |
199 | |
try |
200 | |
{ |
201 | 2 | Object value = getNotNullValueBinding().getValue(FacesContext.getCurrentInstance()); |
202 | 2 | if (value != null) |
203 | |
{ |
204 | 2 | return value.getClass(); |
205 | |
} |
206 | |
} |
207 | 0 | catch (Throwable e) |
208 | |
{ |
209 | 0 | log.log(Level.WARNING, "Could not determine expected type for '" |
210 | |
+ _valueBinding.getExpressionString() + "': " |
211 | |
+ e.getMessage(), e); |
212 | 0 | } |
213 | |
} |
214 | 2 | return null; |
215 | |
} |
216 | |
|
217 | |
public void restoreState(FacesContext context, Object state) |
218 | |
{ |
219 | 0 | if (state instanceof ValueBinding) |
220 | |
{ |
221 | 0 | _valueBinding = (ValueBinding)state; |
222 | |
} |
223 | 0 | else if (state != null) |
224 | |
{ |
225 | 0 | Object[] stateArray = (Object[])state; |
226 | 0 | _valueBinding = (ValueBinding)_ClassUtils.newInstance((String)stateArray[0], ValueBinding.class); |
227 | 0 | ((StateHolder)_valueBinding).restoreState(context, stateArray[1]); |
228 | |
} |
229 | 0 | } |
230 | |
|
231 | |
public Object saveState(FacesContext context) |
232 | |
{ |
233 | 0 | if (!_transient) |
234 | |
{ |
235 | 0 | if (_valueBinding instanceof StateHolder) |
236 | |
{ |
237 | 0 | Object[] state = new Object[2]; |
238 | 0 | state[0] = _valueBinding.getClass().getName(); |
239 | 0 | state[1] = ((StateHolder)_valueBinding).saveState(context); |
240 | 0 | return state; |
241 | |
} |
242 | 0 | return _valueBinding; |
243 | |
} |
244 | 0 | return null; |
245 | |
} |
246 | |
|
247 | |
public void setTransient(boolean newTransientValue) |
248 | |
{ |
249 | 4 | _transient = newTransientValue; |
250 | 4 | } |
251 | |
|
252 | |
public boolean isTransient() |
253 | |
{ |
254 | 0 | return _transient; |
255 | |
} |
256 | |
|
257 | |
private FacesContext getFacesContext(ELContext context) |
258 | |
{ |
259 | 20 | if (context == null) |
260 | |
{ |
261 | 0 | throw new IllegalArgumentException("el context must not be null."); |
262 | |
} |
263 | |
|
264 | 20 | FacesContext facesContext = (FacesContext)context.getContext(FacesContext.class); |
265 | 20 | if (facesContext == null) |
266 | |
{ |
267 | 0 | throw new IllegalStateException("faces context not available in el context."); |
268 | |
} |
269 | |
|
270 | 20 | return facesContext; |
271 | |
} |
272 | |
|
273 | |
private <T> T invoke(Invoker<T> invoker) |
274 | |
{ |
275 | |
try |
276 | |
{ |
277 | 20 | return invoker.invoke(); |
278 | |
} |
279 | 4 | catch (javax.faces.el.PropertyNotFoundException e) |
280 | |
{ |
281 | 4 | throw new PropertyNotFoundException(e.getMessage(), e); |
282 | |
} |
283 | 4 | catch (EvaluationException e) |
284 | |
{ |
285 | 4 | throw new ELException(e.getMessage(), e); |
286 | |
} |
287 | |
} |
288 | |
|
289 | |
private interface Invoker<T> |
290 | |
{ |
291 | |
T invoke(); |
292 | |
} |
293 | |
} |