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