1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.filters; |
12 | |
|
13 | |
import org.mule.config.ConfigurationException; |
14 | |
import org.mule.umo.UMOFilter; |
15 | |
import org.mule.umo.UMOMessage; |
16 | |
|
17 | |
import ognl.Ognl; |
18 | |
import ognl.OgnlException; |
19 | |
import org.apache.commons.logging.Log; |
20 | |
import org.apache.commons.logging.LogFactory; |
21 | |
|
22 | 0 | public class OGNLFilter implements UMOFilter |
23 | |
{ |
24 | 0 | protected final Log logger = LogFactory.getLog(this.getClass()); |
25 | |
|
26 | |
private volatile String expression; |
27 | |
private volatile Object compiledExpression; |
28 | |
|
29 | |
public String getExpression() |
30 | |
{ |
31 | 0 | return expression; |
32 | |
} |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public void setExpression(String expression) throws ConfigurationException |
43 | |
{ |
44 | |
try |
45 | |
{ |
46 | 0 | this.compiledExpression = Ognl.parseExpression(expression); |
47 | 0 | this.expression = expression; |
48 | |
} |
49 | 0 | catch (OgnlException ex) |
50 | |
{ |
51 | 0 | throw new ConfigurationException(ex); |
52 | 0 | } |
53 | 0 | } |
54 | |
|
55 | |
public boolean accept(UMOMessage message) |
56 | |
{ |
57 | |
|
58 | 0 | if (message == null) |
59 | |
{ |
60 | 0 | return false; |
61 | |
} |
62 | |
|
63 | 0 | Object candidate = message.getPayload(); |
64 | |
|
65 | 0 | if (candidate == null) |
66 | |
{ |
67 | 0 | return false; |
68 | |
} |
69 | |
|
70 | |
|
71 | 0 | if (compiledExpression == null) |
72 | |
{ |
73 | 0 | logger.warn("No expression configured - rejecting message."); |
74 | 0 | return false; |
75 | |
} |
76 | |
|
77 | |
try |
78 | |
{ |
79 | 0 | Object result = Ognl.getValue(compiledExpression, candidate); |
80 | |
|
81 | 0 | if (result instanceof Boolean) |
82 | |
{ |
83 | 0 | return ((Boolean) result).booleanValue(); |
84 | |
} |
85 | |
} |
86 | 0 | catch (OgnlException ex) |
87 | |
{ |
88 | 0 | logger.error(ex); |
89 | 0 | } |
90 | |
|
91 | |
|
92 | 0 | return false; |
93 | |
} |
94 | |
|
95 | |
} |