1 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 36 public class ExpressionEvaluatorImpl implements ExpressionEvaluator 37 { 38 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 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 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 read(Object target, String expression) 74 { 75 return readCompiled(target, _expressionCache.getCompiledExpression(expression)); 76 } 77 78 public Object readCompiled(Object target, Object expression) 79 { 80 try 81 { 82 Map context = createContext(target); 83 84 return Ognl.getValue(expression, context, target); 85 } 86 catch (Exception ex) 87 { 88 throw new ApplicationRuntimeException(ImplMessages.unableToReadExpression(ImplMessages 89 .parsedExpression(), target, ex), target, null, ex); 90 } 91 } 92 93 private Map createContext(Object target) 94 { 95 Map 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 target, String expression, Object value) 104 { 105 writeCompiled(target, _expressionCache.getCompiledExpression(expression), value); 106 } 107 108 public void writeCompiled(Object target, Object expression, Object value) 109 { 110 try 111 { 112 Map context = createContext(target); 113 114 Ognl.setValue(expression, context, target, value); 115 } 116 catch (Exception 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 expression) 125 { 126 Object compiled = _expressionCache.getCompiledExpression(expression); 127 128 try 129 { 130 return Ognl.isConstant(compiled); 131 } 132 catch (Exception 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 contributions) 146 { 147 _contributions = contributions; 148 } 149 } | Popular Tags |