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