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