Coverage Report - org.mule.routing.filters.WildcardFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
WildcardFilter
0%
0/42
0%
0/10
2.75
 
 1  
 /*
 2  
  * $Id: WildcardFilter.java 7976 2007-08-21 14:26:13Z 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  0
     protected final Log logger = LogFactory.getLog(this.getClass());
 32  
 
 33  
     protected volatile String pattern;
 34  
     protected volatile String[] patterns;
 35  0
     private volatile boolean caseSensitive = true;
 36  
 
 37  
     public WildcardFilter()
 38  
     {
 39  0
         super();
 40  0
     }
 41  
 
 42  
     public WildcardFilter(String pattern)
 43  0
     {
 44  0
         this.setPattern(pattern);
 45  0
     }
 46  
 
 47  
     public boolean accept(UMOMessage message)
 48  
     {
 49  
         try
 50  
         {
 51  0
             return accept(message.getPayloadAsString());
 52  
         }
 53  0
         catch (Exception e)
 54  
         {
 55  
             // TODO MULE-863: If this is OK, do we need to log?
 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  0
         if (object == null)
 64  
         {
 65  0
             return false;
 66  
         }
 67  
 
 68  0
         String[] currentPatterns = this.patterns;
 69  0
         if (currentPatterns != null)
 70  
         {
 71  0
             for (int x = 0; x < currentPatterns.length; x++)
 72  
             {
 73  
                 boolean foundMatch;
 74  0
                 String pattern = currentPatterns[x];
 75  
 
 76  0
                 if ("*".equals(pattern) || "**".equals(pattern))
 77  
                 {
 78  0
                     return true;
 79  
                 }
 80  
 
 81  0
                 String candidate = object.toString();
 82  
 
 83  0
                 if (!isCaseSensitive())
 84  
                 {
 85  0
                     pattern = pattern.toLowerCase();
 86  0
                     candidate = candidate.toLowerCase();
 87  
                 }
 88  
 
 89  0
                 int i = pattern.indexOf('*');
 90  0
                 if (i == -1)
 91  
                 {
 92  0
                     foundMatch = pattern.equals(candidate);
 93  
                 }
 94  
                 else
 95  
                 {
 96  0
                     int i2 = pattern.indexOf('*', i + 1);
 97  0
                     if (i2 > 1)
 98  
                     {
 99  0
                         foundMatch = candidate.indexOf(pattern.substring(1, i2)) > -1;
 100  
                     }
 101  0
                     else if (i == 0)
 102  
                     {
 103  0
                         foundMatch = candidate.endsWith(pattern.substring(1));
 104  
                     }
 105  
                     else
 106  
                     {
 107  0
                         foundMatch = candidate.startsWith(pattern.substring(0, i));
 108  
                     }
 109  
                 }
 110  
 
 111  0
                 if (foundMatch)
 112  
                 {
 113  0
                     return true;
 114  
                 }
 115  
             }
 116  
         }
 117  
 
 118  0
         return false;
 119  
     }
 120  
 
 121  
     public String getPattern()
 122  
     {
 123  0
         return pattern;
 124  
     }
 125  
 
 126  
     public void setPattern(String pattern)
 127  
     {
 128  0
         this.pattern = pattern;
 129  0
         this.patterns = StringUtils.splitAndTrim(pattern, ",");
 130  0
     }
 131  
 
 132  
     public boolean isCaseSensitive()
 133  
     {
 134  0
         return caseSensitive;
 135  
     }
 136  
 
 137  
     public void setCaseSensitive(boolean caseSensitive)
 138  
     {
 139  0
         this.caseSensitive = caseSensitive;
 140  0
     }
 141  
 
 142  
 }