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