1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.language.juel; |
18 |
|
|
19 |
|
import javax.el.ELContext; |
20 |
|
import javax.el.ExpressionFactory; |
21 |
|
import javax.el.ValueExpression; |
22 |
|
|
23 |
|
import de.odysseus.el.util.SimpleContext; |
24 |
|
|
25 |
|
import org.apache.camel.Exchange; |
26 |
|
import org.apache.camel.Message; |
27 |
|
import org.apache.camel.impl.ExpressionSupport; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
public class JuelExpression extends ExpressionSupport<Exchange> { |
33 |
|
|
34 |
|
private final String expression; |
35 |
|
private final Class<?> type; |
36 |
|
private ExpressionFactory expressionFactory; |
37 |
|
|
38 |
3 |
public JuelExpression(String expression, Class<?> type) { |
39 |
3 |
this.expression = expression; |
40 |
3 |
this.type = type; |
41 |
3 |
} |
42 |
|
|
43 |
|
public static JuelExpression el(String expression) { |
44 |
0 |
return new JuelExpression(expression, Object.class); |
45 |
|
} |
46 |
|
|
47 |
|
public Object evaluate(Exchange exchange) { |
48 |
|
|
49 |
|
|
50 |
3 |
ELContext context = populateContext(createContext(), exchange); |
51 |
3 |
ValueExpression valueExpression = getExpressionFactory().createValueExpression(context, expression, type); |
52 |
3 |
return valueExpression.getValue(context); |
53 |
|
} |
54 |
|
|
55 |
|
public ExpressionFactory getExpressionFactory() { |
56 |
12 |
if (expressionFactory == null) { |
57 |
3 |
expressionFactory = ExpressionFactory.newInstance(); |
58 |
|
} |
59 |
12 |
return expressionFactory; |
60 |
|
} |
61 |
|
|
62 |
|
public void setExpressionFactory(ExpressionFactory expressionFactory) { |
63 |
0 |
this.expressionFactory = expressionFactory; |
64 |
0 |
} |
65 |
|
|
66 |
|
protected ELContext populateContext(ELContext context, Exchange exchange) { |
67 |
3 |
setVariable(context, "exchange", exchange, Exchange.class); |
68 |
3 |
setVariable(context, "in", exchange.getIn(), Message.class); |
69 |
3 |
setVariable(context, "out", exchange.getOut(), Message.class); |
70 |
3 |
return context; |
71 |
|
} |
72 |
|
|
73 |
|
protected void setVariable(ELContext context, String name, Object value, Class<?> type) { |
74 |
9 |
ValueExpression valueExpression = getExpressionFactory().createValueExpression(value, type); |
75 |
9 |
SimpleContext simpleContext = (SimpleContext) context; |
76 |
9 |
simpleContext.setVariable(name, valueExpression); |
77 |
9 |
} |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
protected ELContext createContext() { |
83 |
3 |
return new SimpleContext(); |
84 |
|
} |
85 |
|
|
86 |
|
protected String assertionFailureMessage(Exchange exchange) { |
87 |
0 |
return expression; |
88 |
|
} |
89 |
|
} |