Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
ExpressionBinding |
|
| 3.375;3.375 |
1 | // Copyright 2004, 2005 The Apache Software Foundation |
|
2 | // |
|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 | // you may not use this file except in compliance with the License. |
|
5 | // You may obtain a copy of the License at |
|
6 | // |
|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
|
8 | // |
|
9 | // Unless required by applicable law or agreed to in writing, software |
|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 | // See the License for the specific language governing permissions and |
|
13 | // limitations under the License. |
|
14 | ||
15 | package org.apache.tapestry.binding; |
|
16 | ||
17 | import ognl.Node; |
|
18 | import ognl.enhance.ExpressionAccessor; |
|
19 | import org.apache.hivemind.Location; |
|
20 | import org.apache.tapestry.BindingException; |
|
21 | import org.apache.tapestry.IComponent; |
|
22 | import org.apache.tapestry.coerce.ValueConverter; |
|
23 | import org.apache.tapestry.services.ExpressionCache; |
|
24 | import org.apache.tapestry.services.ExpressionEvaluator; |
|
25 | ||
26 | /** |
|
27 | * Implements a dynamic binding, based on evaluating an expression using an expression language. |
|
28 | * Tapestry's default expression language is the <a href="http://www.ognl.org/">Object Graph |
|
29 | * Navigation Language </a>. |
|
30 | * |
|
31 | * @see org.apache.tapestry.services.ExpressionEvaluator |
|
32 | * @author Howard Lewis Ship |
|
33 | * @since 2.2 |
|
34 | */ |
|
35 | ||
36 | public class ExpressionBinding extends AbstractBinding |
|
37 | { |
|
38 | /** |
|
39 | * The root object against which the nested property name is evaluated. |
|
40 | */ |
|
41 | ||
42 | private final IComponent _root; |
|
43 | ||
44 | /** |
|
45 | * If true, then the binding is invariant. |
|
46 | */ |
|
47 | ||
48 | 7 | private boolean _invariant = false; |
49 | ||
50 | /** |
|
51 | * Parsed OGNL expression. |
|
52 | */ |
|
53 | ||
54 | private Node _parsedExpression; |
|
55 | ||
56 | /** |
|
57 | * Compiled OGNL expression. |
|
58 | */ |
|
59 | ||
60 | private ExpressionAccessor _accessor; |
|
61 | ||
62 | /** |
|
63 | * Flag set to true once the binding has initialized. |
|
64 | */ |
|
65 | ||
66 | private boolean _initialized; |
|
67 | ||
68 | /** |
|
69 | * @since 4.0 |
|
70 | */ |
|
71 | ||
72 | private ExpressionEvaluator _evaluator; |
|
73 | ||
74 | /** @since 4.0 */ |
|
75 | ||
76 | private ExpressionCache _cache; |
|
77 | ||
78 | /** |
|
79 | * Used to detect previous failed attempts at writing values when compiling expressions so |
|
80 | * that as many expressions as possible can be fully compiled into their java byte form when |
|
81 | * all objects in the expression are available. |
|
82 | */ |
|
83 | private boolean _writeFailed; |
|
84 | ||
85 | /** |
|
86 | * Creates a {@link ExpressionBinding} from the root object and an OGNL expression. |
|
87 | * |
|
88 | * @param description |
|
89 | * Used by superclass constructor - {@link AbstractBinding#AbstractBinding(String, org.apache.tapestry.coerce.ValueConverter, org.apache.hivemind.Location)}. |
|
90 | * @param location |
|
91 | * Used by superclass constructor - {@link AbstractBinding#AbstractBinding(String, org.apache.tapestry.coerce.ValueConverter, org.apache.hivemind.Location)}. |
|
92 | * @param valueConverter |
|
93 | * Used by superclass constructor - {@link AbstractBinding#AbstractBinding(String, org.apache.tapestry.coerce.ValueConverter, org.apache.hivemind.Location)}. |
|
94 | * @param root |
|
95 | * The object this binding should be resolved against. |
|
96 | * @param expression |
|
97 | * The string expression. |
|
98 | * @param evaluator |
|
99 | * Evaluator used to parse and run the expression. |
|
100 | * @param cache |
|
101 | * Expression cache which does efficient caching of parsed expressions. |
|
102 | */ |
|
103 | public ExpressionBinding(String description, Location location, ValueConverter valueConverter, |
|
104 | IComponent root, String expression, ExpressionEvaluator evaluator, |
|
105 | ExpressionCache cache) |
|
106 | { |
|
107 | 7 | super(expression, valueConverter, location); |
108 | ||
109 | 7 | _root = root; |
110 | 7 | _evaluator = evaluator; |
111 | 7 | _cache = cache; |
112 | 7 | } |
113 | ||
114 | /** |
|
115 | * Gets the value of the property path, with the assistance of the {@link ExpressionEvaluator}. |
|
116 | * |
|
117 | * @throws BindingException |
|
118 | * if an exception is thrown accessing the property. |
|
119 | */ |
|
120 | ||
121 | public Object getObject() |
|
122 | { |
|
123 | 4 | initialize(); |
124 | ||
125 | 4 | return resolveExpression(); |
126 | } |
|
127 | ||
128 | private Object resolveExpression() |
|
129 | { |
|
130 | try |
|
131 | { |
|
132 | 4 | if (_accessor == null && !_writeFailed) |
133 | { |
|
134 | 3 | _parsedExpression = (Node)_cache.getCompiledExpression(_root, _description); |
135 | 2 | _accessor = _parsedExpression.getAccessor(); |
136 | } |
|
137 | ||
138 | 3 | if (_accessor != null) |
139 | 2 | return _evaluator.read(_root, _accessor); |
140 | ||
141 | 1 | return _evaluator.readCompiled(_root, _parsedExpression); |
142 | } |
|
143 | 1 | catch (Throwable t) |
144 | { |
|
145 | 1 | throw new BindingException(t.getMessage(), this, t); |
146 | } |
|
147 | } |
|
148 | ||
149 | /** |
|
150 | * Returns true if the binding is expected to always return the same value. |
|
151 | */ |
|
152 | ||
153 | public boolean isInvariant() |
|
154 | { |
|
155 | 4 | initialize(); |
156 | ||
157 | 3 | return _invariant; |
158 | } |
|
159 | ||
160 | /** |
|
161 | * Sets up the helper object, but also optimizes the property path and determines if the binding |
|
162 | * is invarant. |
|
163 | */ |
|
164 | ||
165 | private void initialize() |
|
166 | { |
|
167 | 11 | if (_initialized) |
168 | 4 | return; |
169 | ||
170 | 7 | _initialized = true; |
171 | ||
172 | try |
|
173 | { |
|
174 | 7 | _parsedExpression = (Node)_cache.getCompiledExpression(_description); |
175 | 6 | _invariant = _evaluator.isConstant(_description); |
176 | } |
|
177 | 1 | catch (Exception ex) |
178 | { |
|
179 | 1 | throw new BindingException(ex.getMessage(), this, ex); |
180 | 6 | } |
181 | 6 | } |
182 | ||
183 | /** |
|
184 | * Updates the property for the binding to the given value. |
|
185 | * |
|
186 | * @throws BindingException |
|
187 | * if the property can't be updated (typically due to an security problem, or a |
|
188 | * missing mutator method). |
|
189 | */ |
|
190 | ||
191 | public void setObject(Object value) |
|
192 | { |
|
193 | 3 | initialize(); |
194 | ||
195 | 3 | if (_invariant) |
196 | 1 | throw createReadOnlyBindingException(this); |
197 | ||
198 | try |
|
199 | { |
|
200 | 2 | if (_accessor == null) |
201 | { |
|
202 | 2 | _evaluator.writeCompiled(_root, _parsedExpression, value); |
203 | ||
204 | 1 | if (!_writeFailed) |
205 | { |
|
206 | // re-parse expression as compilation may be possible now that it potentially has a value |
|
207 | try { |
|
208 | 1 | _parsedExpression = (Node)_cache.getCompiledExpression(_root, _description); |
209 | ||
210 | 1 | _accessor = _parsedExpression.getAccessor(); |
211 | 0 | } catch (Throwable t) { |
212 | ||
213 | // ignore re-read failures as they aren't supposed to be happening now anyways |
|
214 | // and a more user friendly version will be available if someone actually calls |
|
215 | // getObject |
|
216 | ||
217 | // if writing fails then we're probably screwed...so don't do it again |
|
218 | 0 | if (value != null) |
219 | 0 | _writeFailed = true; |
220 | 1 | } |
221 | } |
|
222 | } else |
|
223 | 0 | _evaluator.write(_root, _accessor, value); |
224 | } |
|
225 | 1 | catch (Throwable ex) |
226 | { |
|
227 | 1 | throw new BindingException(ex.getMessage(), this, ex); |
228 | 1 | } |
229 | 1 | } |
230 | ||
231 | /** |
|
232 | * Returns the a String representing the property path. This includes the |
|
233 | * {@link IComponent#getExtendedId() extended id}of the root component and the property path |
|
234 | * ... once the binding is used, these may change due to optimization of the property path. |
|
235 | */ |
|
236 | ||
237 | public String toString() |
|
238 | { |
|
239 | 2 | StringBuffer buffer = new StringBuffer(); |
240 | ||
241 | 2 | buffer.append("ExpressionBinding["); |
242 | 2 | buffer.append(_root.getExtendedId()); |
243 | ||
244 | 2 | if (_description != null) |
245 | { |
|
246 | 2 | buffer.append(' '); |
247 | 2 | buffer.append(_description); |
248 | } |
|
249 | ||
250 | 2 | buffer.append(']'); |
251 | ||
252 | 2 | return buffer.toString(); |
253 | } |
|
254 | ||
255 | /** @since 4.0 */ |
|
256 | public Object getComponent() |
|
257 | { |
|
258 | 1 | return _root; |
259 | } |
|
260 | } |