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