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