| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.language.ognl; |
| 18 |
|
|
| 19 |
|
import java.util.HashMap; |
| 20 |
|
import java.util.Map; |
| 21 |
|
|
| 22 |
|
import ognl.Ognl; |
| 23 |
|
import ognl.OgnlContext; |
| 24 |
|
import ognl.OgnlException; |
| 25 |
|
|
| 26 |
|
import org.apache.camel.Exchange; |
| 27 |
|
import org.apache.camel.impl.ExpressionSupport; |
| 28 |
|
import org.apache.camel.language.ExpressionEvaluationException; |
| 29 |
|
import org.apache.camel.language.IllegalSyntaxException; |
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
public class OgnlExpression extends ExpressionSupport<Exchange> { |
| 35 |
|
|
| 36 |
|
private final String expressionString; |
| 37 |
|
private final Class<?> type; |
| 38 |
|
private Object expression; |
| 39 |
|
|
| 40 |
6 |
public OgnlExpression(OgnlLanguage language, String expressionString, Class<?> type) { |
| 41 |
6 |
this.expressionString = expressionString; |
| 42 |
6 |
this.type = type; |
| 43 |
|
try { |
| 44 |
6 |
this.expression = Ognl.parseExpression(expressionString); |
| 45 |
0 |
} catch (OgnlException e) { |
| 46 |
0 |
throw new IllegalSyntaxException(language, expressionString); |
| 47 |
6 |
} |
| 48 |
6 |
} |
| 49 |
|
|
| 50 |
|
public static OgnlExpression ognl(String expression) { |
| 51 |
0 |
return new OgnlExpression(new OgnlLanguage(), expression, Object.class); |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
public Object evaluate(Exchange exchange) { |
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
6 |
Map values = new HashMap(); |
| 59 |
6 |
populateContext(values, exchange); |
| 60 |
6 |
OgnlContext oglContext = new OgnlContext(); |
| 61 |
|
try { |
| 62 |
6 |
return Ognl.getValue(expression, oglContext, new RootObject(exchange)); |
| 63 |
0 |
} catch (OgnlException e) { |
| 64 |
0 |
throw new ExpressionEvaluationException(this, exchange, e); |
| 65 |
|
} |
| 66 |
|
} |
| 67 |
|
|
| 68 |
|
protected void populateContext(Map map, Exchange exchange) { |
| 69 |
6 |
map.put("exchange", exchange); |
| 70 |
6 |
map.put("in", exchange.getIn()); |
| 71 |
6 |
map.put("out", exchange.getOut()); |
| 72 |
6 |
} |
| 73 |
|
|
| 74 |
|
protected String assertionFailureMessage(Exchange exchange) { |
| 75 |
0 |
return expressionString; |
| 76 |
|
} |
| 77 |
|
} |