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