1
2
3
4
5
6
7
8
9
10
11 package org.mule.module.ognl.filters;
12
13 import org.mule.api.MuleMessage;
14 import org.mule.api.config.ConfigurationException;
15 import org.mule.api.routing.filter.Filter;
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 public class OGNLFilter implements Filter
23 {
24 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 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 this.compiledExpression = Ognl.parseExpression(expression);
47 this.expression = expression;
48 }
49 catch (OgnlException ex)
50 {
51 throw new ConfigurationException(ex);
52 }
53 }
54
55 public boolean accept(MuleMessage message)
56 {
57
58 if (message == null)
59 {
60 return false;
61 }
62
63 Object candidate = message.getPayload();
64
65 if (candidate == null)
66 {
67 return false;
68 }
69
70
71 if (compiledExpression == null)
72 {
73 logger.warn("No expression configured - rejecting message.");
74 return false;
75 }
76
77 try
78 {
79 Object result = Ognl.getValue(compiledExpression, candidate);
80
81 if (result instanceof Boolean)
82 {
83 return ((Boolean) result).booleanValue();
84 }
85 }
86 catch (OgnlException ex)
87 {
88 logger.error(ex);
89 }
90
91
92 return false;
93 }
94
95 }