1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.services.impl; |
16 |
| |
17 |
| import java.util.Iterator; |
18 |
| import java.util.List; |
19 |
| import java.util.Map; |
20 |
| |
21 |
| import ognl.ClassResolver; |
22 |
| import ognl.Ognl; |
23 |
| import ognl.OgnlRuntime; |
24 |
| import ognl.TypeConverter; |
25 |
| |
26 |
| import org.apache.hivemind.ApplicationRuntimeException; |
27 |
| import org.apache.tapestry.Tapestry; |
28 |
| import org.apache.tapestry.services.ExpressionCache; |
29 |
| import org.apache.tapestry.services.ExpressionEvaluator; |
30 |
| import org.apache.tapestry.spec.IApplicationSpecification; |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| public class ExpressionEvaluatorImpl implements ExpressionEvaluator |
37 |
| { |
38 |
| |
39 |
| |
40 |
| private final ClassResolver _ognlResolver = new OgnlClassResolver(); |
41 |
| |
42 |
| private ExpressionCache _expressionCache; |
43 |
| |
44 |
| private IApplicationSpecification _applicationSpecification; |
45 |
| |
46 |
| private TypeConverter _typeConverter; |
47 |
| |
48 |
| private List _contributions; |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| private Map _defaultContext; |
54 |
| |
55 |
42
| public void setApplicationSpecification(IApplicationSpecification applicationSpecification)
|
56 |
| { |
57 |
42
| _applicationSpecification = applicationSpecification;
|
58 |
| } |
59 |
| |
60 |
42
| public void initializeService()
|
61 |
| { |
62 |
42
| if (_applicationSpecification.checkExtension(Tapestry.OGNL_TYPE_CONVERTER))
|
63 |
1
| _typeConverter = (TypeConverter) _applicationSpecification.getExtension(
|
64 |
| Tapestry.OGNL_TYPE_CONVERTER, |
65 |
| TypeConverter.class); |
66 |
| |
67 |
42
| Iterator i = _contributions.iterator();
|
68 |
| |
69 |
42
| while (i.hasNext())
|
70 |
| { |
71 |
80
| PropertyAccessorContribution c = (PropertyAccessorContribution) i.next();
|
72 |
| |
73 |
80
| OgnlRuntime.setPropertyAccessor(c.getSubjectClass(), c.getAccessor());
|
74 |
| } |
75 |
| |
76 |
42
| _defaultContext = Ognl.createDefaultContext(null, _ognlResolver, _typeConverter);
|
77 |
| |
78 |
| } |
79 |
| |
80 |
58
| public Object read(Object target, String expression)
|
81 |
| { |
82 |
58
| return readCompiled(target, _expressionCache.getCompiledExpression(expression));
|
83 |
| } |
84 |
| |
85 |
2429
| public Object readCompiled(Object target, Object expression)
|
86 |
| { |
87 |
2429
| try
|
88 |
| { |
89 |
2429
| Map context = createContext(target);
|
90 |
| |
91 |
2429
| return Ognl.getValue(expression, context, target);
|
92 |
| } |
93 |
| catch (Exception ex) |
94 |
| { |
95 |
2
| throw new ApplicationRuntimeException(ImplMessages.unableToReadExpression(ImplMessages
|
96 |
| .parsedExpression(), target, ex), target, null, ex); |
97 |
| } |
98 |
| } |
99 |
| |
100 |
3848
| private Map createContext(Object target)
|
101 |
| { |
102 |
3848
| Map result = Ognl.createDefaultContext(target, _ognlResolver);
|
103 |
| |
104 |
3848
| if (_typeConverter != null)
|
105 |
1
| Ognl.setTypeConverter(result, _typeConverter);
|
106 |
| |
107 |
3848
| return result;
|
108 |
| } |
109 |
| |
110 |
3
| public void write(Object target, String expression, Object value)
|
111 |
| { |
112 |
3
| writeCompiled(target, _expressionCache.getCompiledExpression(expression), value);
|
113 |
| } |
114 |
| |
115 |
1419
| public void writeCompiled(Object target, Object expression, Object value)
|
116 |
| { |
117 |
1419
| try
|
118 |
| { |
119 |
1419
| Map context = createContext(target);
|
120 |
| |
121 |
1419
| Ognl.setValue(expression, context, target, value);
|
122 |
| } |
123 |
| catch (Exception ex) |
124 |
| { |
125 |
1
| throw new ApplicationRuntimeException(ImplMessages.unableToWriteExpression(ImplMessages
|
126 |
| .parsedExpression(), target, value, ex), target, null, ex); |
127 |
| } |
128 |
| |
129 |
| } |
130 |
| |
131 |
946
| public boolean isConstant(String expression)
|
132 |
| { |
133 |
946
| Object compiled = _expressionCache.getCompiledExpression(expression);
|
134 |
| |
135 |
946
| try
|
136 |
| { |
137 |
946
| return Ognl.isConstant(compiled, _defaultContext);
|
138 |
| } |
139 |
| catch (Exception ex) |
140 |
| { |
141 |
1
| throw new ApplicationRuntimeException(ImplMessages.isConstantExpressionError(
|
142 |
| expression, |
143 |
| ex), ex); |
144 |
| } |
145 |
| } |
146 |
| |
147 |
64
| public void setExpressionCache(ExpressionCache expressionCache)
|
148 |
| { |
149 |
64
| _expressionCache = expressionCache;
|
150 |
| } |
151 |
| |
152 |
42
| public void setContributions(List contributions)
|
153 |
| { |
154 |
42
| _contributions = contributions;
|
155 |
| } |
156 |
| } |