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