|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ExpressionEvaluatorImpl.java | 100% | 100% | 100% | 100% |
|
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.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 |
* @author Howard M. Lewis Ship
|
|
34 |
* @since 4.0
|
|
35 |
*/
|
|
36 |
public class ExpressionEvaluatorImpl implements ExpressionEvaluator |
|
37 |
{ |
|
38 |
// Uses Thread's context class loader
|
|
39 |
|
|
40 |
private 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 | 45 |
public void setApplicationSpecification(IApplicationSpecification applicationSpecification) |
51 |
{ |
|
52 | 45 |
_applicationSpecification = applicationSpecification; |
53 |
} |
|
54 |
|
|
55 | 45 |
public void initializeService() |
56 |
{ |
|
57 | 45 |
if (_applicationSpecification.checkExtension(Tapestry.OGNL_TYPE_CONVERTER))
|
58 | 1 |
_typeConverter = (TypeConverter) _applicationSpecification.getExtension( |
59 |
Tapestry.OGNL_TYPE_CONVERTER, |
|
60 |
TypeConverter.class);
|
|
61 |
|
|
62 | 45 |
Iterator i = _contributions.iterator(); |
63 |
|
|
64 | 45 |
while (i.hasNext())
|
65 |
{ |
|
66 | 88 |
PropertyAccessorContribution c = (PropertyAccessorContribution) i.next(); |
67 |
|
|
68 | 88 |
OgnlRuntime.setPropertyAccessor(c.getSubjectClass(), c.getAccessor()); |
69 |
} |
|
70 |
|
|
71 |
} |
|
72 |
|
|
73 | 48 |
public Object read(Object target, String expression)
|
74 |
{ |
|
75 | 48 |
return readCompiled(target, _expressionCache.getCompiledExpression(expression));
|
76 |
} |
|
77 |
|
|
78 | 2449 |
public Object readCompiled(Object target, Object expression)
|
79 |
{ |
|
80 | 2449 |
try
|
81 |
{ |
|
82 | 2449 |
Map context = createContext(target); |
83 |
|
|
84 | 2449 |
return Ognl.getValue(expression, context, target);
|
85 |
} |
|
86 |
catch (Exception ex)
|
|
87 |
{ |
|
88 | 2 |
throw new ApplicationRuntimeException(ImplMessages.unableToReadExpression(ImplMessages |
89 |
.parsedExpression(), target, ex), target, null, ex);
|
|
90 |
} |
|
91 |
} |
|
92 |
|
|
93 | 3885 |
private Map createContext(Object target)
|
94 |
{ |
|
95 | 3885 |
Map result = Ognl.createDefaultContext(target, _ognlResolver); |
96 |
|
|
97 | 3885 |
if (_typeConverter != null) |
98 | 1 |
Ognl.setTypeConverter(result, _typeConverter); |
99 |
|
|
100 | 3885 |
return result;
|
101 |
} |
|
102 |
|
|
103 | 3 |
public void write(Object target, String expression, Object value) |
104 |
{ |
|
105 | 3 |
writeCompiled(target, _expressionCache.getCompiledExpression(expression), value); |
106 |
} |
|
107 |
|
|
108 | 1436 |
public void writeCompiled(Object target, Object expression, Object value) |
109 |
{ |
|
110 | 1436 |
try
|
111 |
{ |
|
112 | 1436 |
Map context = createContext(target); |
113 |
|
|
114 | 1436 |
Ognl.setValue(expression, context, target, value); |
115 |
} |
|
116 |
catch (Exception ex)
|
|
117 |
{ |
|
118 | 1 |
throw new ApplicationRuntimeException(ImplMessages.unableToWriteExpression(ImplMessages |
119 |
.parsedExpression(), target, value, ex), target, null, ex);
|
|
120 |
} |
|
121 |
|
|
122 |
} |
|
123 |
|
|
124 | 959 |
public boolean isConstant(String expression) |
125 |
{ |
|
126 | 959 |
Object compiled = _expressionCache.getCompiledExpression(expression); |
127 |
|
|
128 | 959 |
try
|
129 |
{ |
|
130 | 959 |
return Ognl.isConstant(compiled);
|
131 |
} |
|
132 |
catch (Exception ex)
|
|
133 |
{ |
|
134 | 1 |
throw new ApplicationRuntimeException(ImplMessages.isConstantExpressionError( |
135 |
expression, |
|
136 |
ex), ex); |
|
137 |
} |
|
138 |
} |
|
139 |
|
|
140 | 68 |
public void setExpressionCache(ExpressionCache expressionCache) |
141 |
{ |
|
142 | 68 |
_expressionCache = expressionCache; |
143 |
} |
|
144 |
|
|
145 | 45 |
public void setContributions(List contributions) |
146 |
{ |
|
147 | 45 |
_contributions = contributions; |
148 |
} |
|
149 |
} |
|