Coverage Report - org.mule.module.ognl.filters.OGNLFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
OGNLFilter
0%
0/29
0%
0/14
0
 
 1  
 /*
 2  
  * $Id: OGNLFilter.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.module.ognl.filters;
 12  
 
 13  
 import static org.mule.util.ClassUtils.equal;
 14  
 import static org.mule.util.ClassUtils.hash;
 15  
 import ognl.Ognl;
 16  
 import ognl.OgnlException;
 17  
 
 18  
 import org.mule.api.MuleMessage;
 19  
 import org.mule.api.config.ConfigurationException;
 20  
 import org.mule.api.routing.filter.Filter;
 21  
 
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 
 25  0
 public class OGNLFilter implements Filter
 26  
 {
 27  0
     protected final Log logger = LogFactory.getLog(this.getClass());
 28  
 
 29  
     private volatile String expression;
 30  
     private volatile Object compiledExpression;
 31  
 
 32  
     public String getExpression()
 33  
     {
 34  0
         return expression;
 35  
     }
 36  
 
 37  
     /**
 38  
      * Sets the expression for this filter. The argument must be a valid expression
 39  
      * as described in the OGNL documentation.
 40  
      *
 41  
      * @param expression the expression to use for message evaluation
 42  
      * @throws ConfigurationException if the expression cannot be parsed
 43  
      * @see Ognl#parseExpression(String)
 44  
      */
 45  
     public void setExpression(String expression) throws ConfigurationException
 46  
     {
 47  
         try
 48  
         {
 49  0
             this.compiledExpression = Ognl.parseExpression(expression);
 50  0
             this.expression = expression;
 51  
         }
 52  0
         catch (OgnlException ex)
 53  
         {
 54  0
             throw new ConfigurationException(ex);
 55  0
         }
 56  0
     }
 57  
 
 58  
     public boolean accept(MuleMessage message)
 59  
     {
 60  
         // no message: nothing to filter
 61  0
         if (message == null)
 62  
         {
 63  0
             return false;
 64  
         }
 65  
 
 66  0
         Object candidate = message.getPayload();
 67  
         // no payload: still nothing to filter
 68  0
         if (candidate == null)
 69  
         {
 70  0
             return false;
 71  
         }
 72  
 
 73  
         // no expression configured: we reject by default
 74  0
         if (compiledExpression == null)
 75  
         {
 76  0
             logger.warn("No expression configured - rejecting message.");
 77  0
             return false;
 78  
         }
 79  
 
 80  
         try
 81  
         {
 82  0
             Object result = Ognl.getValue(compiledExpression, candidate);
 83  
             // we only need to take boolean expressoin results into account
 84  0
             if (result instanceof Boolean)
 85  
             {
 86  0
                 return ((Boolean) result).booleanValue();
 87  
             }
 88  
         }
 89  0
         catch (OgnlException ex)
 90  
         {
 91  0
             logger.error(ex);
 92  0
         }
 93  
 
 94  
         // default action: reject
 95  0
         return false;
 96  
     }
 97  
 
 98  
     public boolean equals(Object obj)
 99  
     {
 100  0
         if (this == obj) return true;
 101  0
         if (obj == null || getClass() != obj.getClass()) return false;
 102  
 
 103  0
         final OGNLFilter other = (OGNLFilter) obj;
 104  0
         return equal(expression, other.expression);
 105  
     }
 106  
 
 107  
     public int hashCode()
 108  
     {
 109  0
         return hash(new Object[]{this.getClass(), expression});
 110  
     }
 111  
 }