View Javadoc

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