KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > ExpressionEvaluatorImpl


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 JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
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 JavaDoc _contributions;
49
50     public void setApplicationSpecification(IApplicationSpecification applicationSpecification)
51     {
52         _applicationSpecification = applicationSpecification;
53     }
54
55     public void initializeService()
56     {
57         if (_applicationSpecification.checkExtension(Tapestry.OGNL_TYPE_CONVERTER))
58             _typeConverter = (TypeConverter) _applicationSpecification.getExtension(
59                     Tapestry.OGNL_TYPE_CONVERTER,
60                     TypeConverter.class);
61
62         Iterator JavaDoc i = _contributions.iterator();
63
64         while (i.hasNext())
65         {
66             PropertyAccessorContribution c = (PropertyAccessorContribution) i.next();
67
68             OgnlRuntime.setPropertyAccessor(c.getSubjectClass(), c.getAccessor());
69         }
70
71     }
72
73     public Object JavaDoc read(Object JavaDoc target, String JavaDoc expression)
74     {
75         return readCompiled(target, _expressionCache.getCompiledExpression(expression));
76     }
77
78     public Object JavaDoc readCompiled(Object JavaDoc target, Object JavaDoc expression)
79     {
80         try
81         {
82             Map JavaDoc context = createContext(target);
83
84             return Ognl.getValue(expression, context, target);
85         }
86         catch (Exception JavaDoc ex)
87         {
88             throw new ApplicationRuntimeException(ImplMessages.unableToReadExpression(ImplMessages
89                     .parsedExpression(), target, ex), target, null, ex);
90         }
91     }
92
93     private Map JavaDoc createContext(Object JavaDoc target)
94     {
95         Map JavaDoc result = Ognl.createDefaultContext(target, _ognlResolver);
96
97         if (_typeConverter != null)
98             Ognl.setTypeConverter(result, _typeConverter);
99
100         return result;
101     }
102
103     public void write(Object JavaDoc target, String JavaDoc expression, Object JavaDoc value)
104     {
105         writeCompiled(target, _expressionCache.getCompiledExpression(expression), value);
106     }
107
108     public void writeCompiled(Object JavaDoc target, Object JavaDoc expression, Object JavaDoc value)
109     {
110         try
111         {
112             Map JavaDoc context = createContext(target);
113
114             Ognl.setValue(expression, context, target, value);
115         }
116         catch (Exception JavaDoc ex)
117         {
118             throw new ApplicationRuntimeException(ImplMessages.unableToWriteExpression(ImplMessages
119                     .parsedExpression(), target, value, ex), target, null, ex);
120         }
121
122     }
123
124     public boolean isConstant(String JavaDoc expression)
125     {
126         Object JavaDoc compiled = _expressionCache.getCompiledExpression(expression);
127
128         try
129         {
130             return Ognl.isConstant(compiled);
131         }
132         catch (Exception JavaDoc ex)
133         {
134             throw new ApplicationRuntimeException(ImplMessages.isConstantExpressionError(
135                     expression,
136                     ex), ex);
137         }
138     }
139
140     public void setExpressionCache(ExpressionCache expressionCache)
141     {
142         _expressionCache = expressionCache;
143     }
144
145     public void setContributions(List JavaDoc contributions)
146     {
147         _contributions = contributions;
148     }
149 }
Popular Tags