View Javadoc

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