Coverage Report - org.mule.routing.filters.WildcardFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
WildcardFilter
90%
38/42
95%
21/22
2.75
 
 1  
 /*
 2  
  * $Id: WildcardFilter.java 10489 2008-01-23 17:53:38Z dfeist $
 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.routing.filters;
 12  
 
 13  
 import org.mule.api.MuleMessage;
 14  
 import org.mule.api.routing.filter.Filter;
 15  
 import org.mule.api.routing.filter.ObjectFilter;
 16  
 import org.mule.util.StringUtils;
 17  
 
 18  
 import org.apache.commons.logging.Log;
 19  
 import org.apache.commons.logging.LogFactory;
 20  
 
 21  
 /**
 22  
  * <code>WildcardFilter</code> is used to match Strings against wildcards. It
 23  
  * performs matches with "*", i.e. "jms.events.*" would catch "jms.events.customer"
 24  
  * and "jms.events.receipts". This filter accepts a comma-separated list of patterns,
 25  
  * so more than one filter pattern can be matched for a given argument:
 26  
  * "jms.events.*, jms.actions.*" will match "jms.events.system" and "jms.actions" but
 27  
  * not "jms.queue".
 28  
  */
 29  
 
 30  
 public class WildcardFilter implements Filter, ObjectFilter
 31  
 {
 32  422
     protected final Log logger = LogFactory.getLog(this.getClass());
 33  
 
 34  
     protected volatile String pattern;
 35  
     protected volatile String[] patterns;
 36  422
     private volatile boolean caseSensitive = true;
 37  
 
 38  
     public WildcardFilter()
 39  
     {
 40  10
         super();
 41  10
     }
 42  
 
 43  
     public WildcardFilter(String pattern)
 44  412
     {
 45  412
         this.setPattern(pattern);
 46  412
     }
 47  
 
 48  
     public boolean accept(MuleMessage message)
 49  
     {
 50  
         try
 51  
         {
 52  58
             return accept(message.getPayloadAsString());
 53  
         }
 54  0
         catch (Exception e)
 55  
         {
 56  0
             logger.warn("An exception occured while filtering", e);
 57  0
             return false;
 58  
         }
 59  
     }
 60  
 
 61  
     public boolean accept(Object object)
 62  
     {
 63  1292
         if (object == null)
 64  
         {
 65  0
             return false;
 66  
         }
 67  
 
 68  1292
         String[] currentPatterns = this.patterns;
 69  1292
         if (currentPatterns != null)
 70  
         {
 71  9552
             for (int x = 0; x < currentPatterns.length; x++)
 72  
             {
 73  
                 boolean foundMatch;
 74  9108
                 String pattern = currentPatterns[x];
 75  
 
 76  9108
                 if ("*".equals(pattern) || "**".equals(pattern))
 77  
                 {
 78  4
                     return true;
 79  
                 }
 80  
 
 81  9104
                 String candidate = object.toString();
 82  
 
 83  9104
                 if (!isCaseSensitive())
 84  
                 {
 85  50
                     pattern = pattern.toLowerCase();
 86  50
                     candidate = candidate.toLowerCase();
 87  
                 }
 88  
 
 89  9104
                 int i = pattern.indexOf('*');
 90  9104
                 if (i == -1)
 91  
                 {
 92  7194
                     foundMatch = pattern.equals(candidate);
 93  
                 }
 94  
                 else
 95  
                 {
 96  1910
                     int i2 = pattern.indexOf('*', i + 1);
 97  1910
                     if (i2 > 1)
 98  
                     {
 99  30
                         foundMatch = candidate.indexOf(pattern.substring(1, i2)) > -1;
 100  
                     }
 101  1880
                     else if (i == 0)
 102  
                     {
 103  22
                         foundMatch = candidate.endsWith(pattern.substring(1));
 104  
                     }
 105  
                     else
 106  
                     {
 107  1858
                         foundMatch = candidate.startsWith(pattern.substring(0, i));
 108  
                     }
 109  
                 }
 110  
 
 111  9104
                 if (foundMatch)
 112  
                 {
 113  840
                     return true;
 114  
                 }
 115  
             }
 116  
         }
 117  
 
 118  448
         return false;
 119  
     }
 120  
 
 121  
     public String getPattern()
 122  
     {
 123  2
         return pattern;
 124  
     }
 125  
 
 126  
     public void setPattern(String pattern)
 127  
     {
 128  436
         this.pattern = pattern;
 129  436
         this.patterns = StringUtils.splitAndTrim(pattern, ",");
 130  436
     }
 131  
 
 132  
     public boolean isCaseSensitive()
 133  
     {
 134  9104
         return caseSensitive;
 135  
     }
 136  
 
 137  
     public void setCaseSensitive(boolean caseSensitive)
 138  
     {
 139  68
         this.caseSensitive = caseSensitive;
 140  68
     }
 141  
 
 142  
 }