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 public class OGNLFilter implements Filter
26 {
27 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 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 this.compiledExpression = Ognl.parseExpression(expression);
50 this.expression = expression;
51 }
52 catch (OgnlException ex)
53 {
54 throw new ConfigurationException(ex);
55 }
56 }
57
58 public boolean accept(MuleMessage message)
59 {
60
61 if (message == null)
62 {
63 return false;
64 }
65
66 Object candidate = message.getPayload();
67
68 if (candidate == null)
69 {
70 return false;
71 }
72
73
74 if (compiledExpression == null)
75 {
76 logger.warn("No expression configured - rejecting message.");
77 return false;
78 }
79
80 try
81 {
82 Object result = Ognl.getValue(compiledExpression, candidate);
83
84 if (result instanceof Boolean)
85 {
86 return ((Boolean) result).booleanValue();
87 }
88 }
89 catch (OgnlException ex)
90 {
91 logger.error(ex);
92 }
93
94
95 return false;
96 }
97
98 public boolean equals(Object obj)
99 {
100 if (this == obj) return true;
101 if (obj == null || getClass() != obj.getClass()) return false;
102
103 final OGNLFilter other = (OGNLFilter) obj;
104 return equal(expression, other.expression);
105 }
106
107 public int hashCode()
108 {
109 return hash(new Object[]{this.getClass(), expression});
110 }
111 }