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