|
20 | 20 | */ |
21 | 21 | package eu.openanalytics.containerproxy.spec.expression; |
22 | 22 |
|
| 23 | +import eu.openanalytics.containerproxy.ContainerProxyException; |
23 | 24 | import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
24 | 25 | import org.springframework.context.ApplicationContext; |
25 | 26 | import org.springframework.context.ConfigurableApplicationContext; |
|
31 | 32 | import org.springframework.context.expression.StandardBeanExpressionResolver; |
32 | 33 | import org.springframework.core.convert.ConversionService; |
33 | 34 | import org.springframework.expression.Expression; |
| 35 | +import org.springframework.expression.ExpressionException; |
34 | 36 | import org.springframework.expression.ExpressionParser; |
| 37 | +import org.springframework.expression.ParseException; |
35 | 38 | import org.springframework.expression.ParserContext; |
| 39 | +import org.springframework.expression.spel.SpelEvaluationException; |
36 | 40 | import org.springframework.expression.spel.standard.SpelExpressionParser; |
37 | 41 | import org.springframework.expression.spel.support.StandardEvaluationContext; |
38 | 42 | import org.springframework.expression.spel.support.StandardTypeConverter; |
@@ -78,27 +82,33 @@ public SpecExpressionResolver(ApplicationContext appContext) { |
78 | 82 | public <T> T evaluate(String expression, SpecExpressionContext context, Class<T> resType) { |
79 | 83 | if (expression == null) return null; |
80 | 84 | if (expression.isEmpty()) return null; |
81 | | - |
82 | | - Expression expr = this.expressionParser.parseExpression(expression, this.beanExpressionParserContext); |
83 | | - |
84 | | - ConfigurableBeanFactory beanFactory = ((ConfigurableApplicationContext) appContext).getBeanFactory(); |
85 | | - |
86 | | - StandardEvaluationContext sec = evaluationCache.get(context); |
87 | | - if (sec == null) { |
88 | | - sec = new StandardEvaluationContext(); |
89 | | - sec.setRootObject(context); |
90 | | - sec.addPropertyAccessor(new BeanExpressionContextAccessor()); |
91 | | - sec.addPropertyAccessor(new BeanFactoryAccessor()); |
92 | | - sec.addPropertyAccessor(new MapAccessor()); |
93 | | - sec.addPropertyAccessor(new EnvironmentAccessor()); |
94 | | - sec.setBeanResolver(new BeanFactoryResolver(appContext)); |
95 | | - sec.setTypeLocator(new StandardTypeLocator(beanFactory.getBeanClassLoader())); |
96 | | - ConversionService conversionService = beanFactory.getConversionService(); |
97 | | - if (conversionService != null) sec.setTypeConverter(new StandardTypeConverter(conversionService)); |
98 | | - evaluationCache.put(context, sec); |
| 85 | + |
| 86 | + try { |
| 87 | + Expression expr = this.expressionParser.parseExpression(expression, this.beanExpressionParserContext); |
| 88 | + |
| 89 | + ConfigurableBeanFactory beanFactory = ((ConfigurableApplicationContext) appContext).getBeanFactory(); |
| 90 | + |
| 91 | + StandardEvaluationContext sec = evaluationCache.get(context); |
| 92 | + if (sec == null) { |
| 93 | + sec = new StandardEvaluationContext(); |
| 94 | + sec.setRootObject(context); |
| 95 | + sec.addPropertyAccessor(new BeanExpressionContextAccessor()); |
| 96 | + sec.addPropertyAccessor(new BeanFactoryAccessor()); |
| 97 | + sec.addPropertyAccessor(new MapAccessor()); |
| 98 | + sec.addPropertyAccessor(new EnvironmentAccessor()); |
| 99 | + sec.setBeanResolver(new BeanFactoryResolver(appContext)); |
| 100 | + sec.setTypeLocator(new StandardTypeLocator(beanFactory.getBeanClassLoader())); |
| 101 | + ConversionService conversionService = beanFactory.getConversionService(); |
| 102 | + if (conversionService != null) sec.setTypeConverter(new StandardTypeConverter(conversionService)); |
| 103 | + evaluationCache.put(context, sec); |
| 104 | + } |
| 105 | + |
| 106 | + return expr.getValue(sec, resType); |
| 107 | + } catch (ExpressionException ex) { |
| 108 | + throw new SpelException(ex, expression); |
| 109 | + } catch (Throwable ex) { |
| 110 | + throw new SpelException(ex, expression); |
99 | 111 | } |
100 | | - |
101 | | - return expr.getValue(sec, resType); |
102 | 112 | } |
103 | 113 |
|
104 | 114 | public String evaluateToString(String expression, SpecExpressionContext context) { |
|
0 commit comments