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