1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.jms.filters; |
12 | |
|
13 | |
import org.mule.api.MuleMessage; |
14 | |
import org.mule.api.routing.filter.Filter; |
15 | |
import org.mule.util.ClassUtils; |
16 | |
import static org.mule.util.ClassUtils.equal; |
17 | |
import static org.mule.util.ClassUtils.hash; |
18 | |
import org.mule.util.StringUtils; |
19 | |
|
20 | |
import java.util.regex.Pattern; |
21 | |
|
22 | |
import javax.jms.Message; |
23 | |
|
24 | |
import org.apache.commons.logging.Log; |
25 | |
import org.apache.commons.logging.LogFactory; |
26 | |
|
27 | 0 | public class JmsPropertyFilter implements Filter |
28 | |
{ |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 0 | private static Log logger = LogFactory.getLog(JmsPropertyFilter.class); |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | private String propertyName = null; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | private String propertyClass = null; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | private String expression = null; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 0 | private Pattern pattern = null; |
54 | |
|
55 | |
public boolean accept(MuleMessage message) |
56 | |
{ |
57 | 0 | if (StringUtils.isBlank(propertyName)) |
58 | |
{ |
59 | 0 | logger.warn("No property name was specified"); |
60 | 0 | return false; |
61 | |
} |
62 | |
|
63 | 0 | if (StringUtils.isBlank(expression) && pattern == null) |
64 | |
{ |
65 | 0 | logger.warn("Either no expression or pattern was specified"); |
66 | 0 | return false; |
67 | |
} |
68 | |
|
69 | 0 | if (message.getPayload() instanceof javax.jms.Message) |
70 | |
{ |
71 | |
try |
72 | |
{ |
73 | 0 | Message m = (javax.jms.Message) message.getPayload(); |
74 | |
|
75 | 0 | if (StringUtils.isBlank(propertyClass)) |
76 | |
{ |
77 | 0 | Object object = m.getObjectProperty(propertyName); |
78 | 0 | if (object == null) |
79 | |
{ |
80 | 0 | return false; |
81 | |
} |
82 | 0 | String value = object.toString(); |
83 | |
|
84 | 0 | if (pattern != null) |
85 | |
{ |
86 | 0 | return pattern.matcher(value).find(); |
87 | |
} |
88 | |
else |
89 | |
{ |
90 | 0 | return value.equals(expression); |
91 | |
} |
92 | |
} |
93 | 0 | else if (propertyClass.equals("java.lang.String")) |
94 | |
{ |
95 | 0 | String value = m.getStringProperty(propertyName); |
96 | 0 | if (value == null) |
97 | |
{ |
98 | 0 | return false; |
99 | |
} |
100 | |
|
101 | 0 | if (pattern != null) |
102 | |
{ |
103 | 0 | return pattern.matcher(value).find(); |
104 | |
} |
105 | |
else |
106 | |
{ |
107 | 0 | return value.equals(expression); |
108 | |
} |
109 | |
} |
110 | 0 | else if (propertyClass.equals("java.lang.Integer")) |
111 | |
{ |
112 | 0 | int value = m.getIntProperty(propertyName); |
113 | 0 | int match = Integer.parseInt(expression); |
114 | 0 | return (value == match); |
115 | |
} |
116 | 0 | else if (propertyClass.equals("java.lang.Short")) |
117 | |
{ |
118 | 0 | short value = m.getShortProperty(propertyName); |
119 | 0 | short match = Short.parseShort(expression); |
120 | 0 | return (value == match); |
121 | |
} |
122 | |
} |
123 | 0 | catch (NumberFormatException nfe) |
124 | |
{ |
125 | 0 | logger.warn("Unable to convert expression " + |
126 | |
expression + " to " + propertyClass + ": " + |
127 | |
nfe.toString()); |
128 | |
} |
129 | 0 | catch (Exception e) |
130 | |
{ |
131 | 0 | logger.warn("Error filtering on property " + propertyName |
132 | |
+ ": " + e.toString()); |
133 | 0 | } |
134 | |
} |
135 | |
else |
136 | |
{ |
137 | 0 | logger.warn("Expected a payload of javax.jms.Message but instead received " + |
138 | |
ClassUtils.getSimpleName(message.getPayload().getClass())); |
139 | |
} |
140 | |
|
141 | 0 | return false; |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public void setExpression(String expression) |
148 | |
{ |
149 | 0 | this.expression = expression; |
150 | 0 | } |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public String getExpression() |
156 | |
{ |
157 | 0 | return expression; |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
public void setPropertyName(String propertyName) |
164 | |
{ |
165 | 0 | this.propertyName = propertyName; |
166 | 0 | } |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
public String getPropertyName() |
172 | |
{ |
173 | 0 | return propertyName; |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
public void setPropertyClass(String propertyClass) |
180 | |
{ |
181 | 0 | this.propertyClass = propertyClass; |
182 | 0 | } |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public String getPropertyClass() |
188 | |
{ |
189 | 0 | return propertyClass; |
190 | |
} |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
public String getPattern() |
196 | |
{ |
197 | 0 | return (pattern == null ? null : pattern.pattern()); |
198 | |
} |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
public void setPattern(String pattern) |
204 | |
{ |
205 | 0 | this.pattern = Pattern.compile(pattern); |
206 | 0 | } |
207 | |
|
208 | |
public boolean equals(Object obj) |
209 | |
{ |
210 | 0 | if (this == obj) return true; |
211 | 0 | if (obj == null || getClass() != obj.getClass()) return false; |
212 | |
|
213 | 0 | final JmsPropertyFilter other = (JmsPropertyFilter) obj; |
214 | 0 | return equal(expression, other.expression) |
215 | |
&& equal(propertyClass, other.propertyClass) |
216 | |
&& equal(propertyName, other.propertyName) |
217 | |
&& equal(pattern, other.pattern); |
218 | |
} |
219 | |
|
220 | |
public int hashCode() |
221 | |
{ |
222 | 0 | return hash(new Object[]{this.getClass(), expression, propertyClass, propertyName, pattern}); |
223 | |
} |
224 | |
} |