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