Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ExpressionBinding |
|
| 3.5;3.5 |
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 | 0 | 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 | 0 | super(expression, valueConverter, location); |
108 | ||
109 | 0 | _root = root; |
110 | 0 | _evaluator = evaluator; |
111 | 0 | _cache = cache; |
112 | 0 | } |
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 | 0 | initialize(); |
124 | ||
125 | 0 | return resolveExpression(); |
126 | } | |
127 | ||
128 | private Object resolveExpression() | |
129 | { | |
130 | try | |
131 | { | |
132 | 0 | if (_accessor == null && !_writeFailed) |
133 | { | |
134 | 0 | _parsedExpression = (Node)_cache.getCompiledExpression(_root, _description); |
135 | 0 | _accessor = _parsedExpression.getAccessor(); |
136 | } | |
137 | ||
138 | 0 | if (_accessor != null) |
139 | 0 | return _evaluator.read(_root, _accessor); |
140 | ||
141 | 0 | return _evaluator.readCompiled(_root, _parsedExpression); |
142 | } | |
143 | 0 | catch (Throwable t) |
144 | { | |
145 | 0 | 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 | 0 | initialize(); |
156 | ||
157 | 0 | 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 | 0 | if (_initialized) |
168 | 0 | return; |
169 | ||
170 | 0 | _initialized = true; |
171 | ||
172 | try | |
173 | { | |
174 | 0 | _parsedExpression = (Node)_cache.getCompiledExpression(_description); |
175 | 0 | _invariant = _evaluator.isConstant(_description); |
176 | } | |
177 | 0 | catch (Exception ex) |
178 | { | |
179 | 0 | throw new BindingException(ex.getMessage(), this, ex); |
180 | 0 | } |
181 | 0 | } |
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 | 0 | initialize(); |
194 | ||
195 | 0 | if (_invariant) |
196 | 0 | throw createReadOnlyBindingException(this); |
197 | ||
198 | try | |
199 | { | |
200 | 0 | if (_accessor == null) |
201 | { | |
202 | 0 | _evaluator.writeCompiled(_root, _parsedExpression, value); |
203 | ||
204 | 0 | if (!_writeFailed) |
205 | { | |
206 | // re-parse expression as compilation may be possible now that it potentially has a value | |
207 | try { | |
208 | 0 | _parsedExpression = (Node)_cache.getCompiledExpression(_root, _description); |
209 | ||
210 | 0 | _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 | 0 | } |
221 | } | |
222 | } else | |
223 | { | |
224 | 0 | _evaluator.write(_root, _accessor, value); |
225 | } | |
226 | } | |
227 | 0 | catch (Throwable ex) |
228 | { | |
229 | 0 | throw new BindingException(ex.getMessage(), this, ex); |
230 | 0 | } |
231 | 0 | } |
232 | ||
233 | /** | |
234 | * Returns the a String representing the property path. This includes the | |
235 | * {@link IComponent#getExtendedId() extended id}of the root component and the property path | |
236 | * ... once the binding is used, these may change due to optimization of the property path. | |
237 | */ | |
238 | ||
239 | public String toString() | |
240 | { | |
241 | 0 | StringBuffer buffer = new StringBuffer(); |
242 | ||
243 | 0 | buffer.append("ExpressionBinding["); |
244 | 0 | buffer.append(_root.getExtendedId()); |
245 | ||
246 | 0 | if (_description != null) |
247 | { | |
248 | 0 | buffer.append(' '); |
249 | 0 | buffer.append(_description); |
250 | } | |
251 | ||
252 | 0 | buffer.append(']'); |
253 | ||
254 | 0 | return buffer.toString(); |
255 | } | |
256 | ||
257 | /** @since 4.0 */ | |
258 | public Object getComponent() | |
259 | { | |
260 | 0 | return _root; |
261 | } | |
262 | } |