Coverage Report - org.mule.routing.MessageFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageFilter
0%
0/40
0%
0/30
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.routing;
 8  
 
 9  
 import org.mule.api.MuleContext;
 10  
 import org.mule.api.MuleEvent;
 11  
 import org.mule.api.MuleException;
 12  
 import org.mule.api.construct.FlowConstruct;
 13  
 import org.mule.api.construct.FlowConstructAware;
 14  
 import org.mule.api.context.MuleContextAware;
 15  
 import org.mule.api.lifecycle.Disposable;
 16  
 import org.mule.api.lifecycle.Initialisable;
 17  
 import org.mule.api.lifecycle.InitialisationException;
 18  
 import org.mule.api.lifecycle.Lifecycle;
 19  
 import org.mule.api.lifecycle.Startable;
 20  
 import org.mule.api.lifecycle.Stoppable;
 21  
 import org.mule.api.processor.InterceptingMessageProcessor;
 22  
 import org.mule.api.processor.MessageProcessor;
 23  
 import org.mule.api.routing.filter.Filter;
 24  
 import org.mule.api.routing.filter.FilterUnacceptedException;
 25  
 import org.mule.config.i18n.CoreMessages;
 26  
 import org.mule.processor.AbstractFilteringMessageProcessor;
 27  
 
 28  
 /**
 29  
  * Implementation of {@link InterceptingMessageProcessor} that filters message flow
 30  
  * using a {@link Filter}. Is the filter accepts the message then message flow
 31  
  * continues to the next message processor. If the filter does not accept the message
 32  
  * processor and a message processor is configured for handling unaccepted message
 33  
  * then this will be invoked, otherwise <code>null</code> will be returned.
 34  
  * <p/>
 35  
  * <b>EIP Reference:</b> <a
 36  
  * href="http://www.eaipatterns.com/Filter.html">http://www.eaipatterns
 37  
  * .com/Filter.html<a/>
 38  
  */
 39  
 public class MessageFilter extends AbstractFilteringMessageProcessor implements FlowConstructAware, Lifecycle
 40  
 {
 41  
     protected Filter filter;
 42  
 
 43  
     /**
 44  
      * For IoC only
 45  
      * 
 46  
      * @deprecated Use MessageFilter(Filter filter)
 47  
      */
 48  
     @Deprecated
 49  
     public MessageFilter()
 50  
     {
 51  0
         super();
 52  0
     }
 53  
 
 54  
     public MessageFilter(Filter filter)
 55  
     {
 56  0
         super();
 57  0
         this.filter = filter;
 58  0
     }
 59  
 
 60  
     /**
 61  
      * @param filter
 62  
      * @param throwExceptionOnUnaccepted throw a FilterUnacceptedException when a
 63  
      *            message is rejected by the filter?
 64  
      * @param messageProcessor used to handler unaccepted messages
 65  
      */
 66  
     public MessageFilter(Filter filter, boolean throwExceptionOnUnaccepted, MessageProcessor messageProcessor)
 67  0
     {
 68  0
         this.filter = filter;
 69  0
         this.throwOnUnaccepted = throwExceptionOnUnaccepted;
 70  0
         this.unacceptedMessageProcessor = messageProcessor;
 71  0
         setUnacceptedMessageProcessor(unacceptedMessageProcessor);
 72  0
     }
 73  
 
 74  
     @Override
 75  
     protected boolean accept(MuleEvent event)
 76  
     {
 77  0
         if (filter == null)
 78  
         {
 79  0
             return true;
 80  
         }
 81  
 
 82  0
         if (event != null)
 83  
         {
 84  0
             return filter.accept(event.getMessage());
 85  
         }
 86  
         else
 87  
         {
 88  0
             return false;
 89  
         }
 90  
     }
 91  
 
 92  
     @Override
 93  
     protected MuleException filterUnacceptedException(MuleEvent event)
 94  
     {
 95  0
         return new FilterUnacceptedException(CoreMessages.messageRejectedByFilter(), event, filter);
 96  
     }
 97  
 
 98  
     public Filter getFilter()
 99  
     {
 100  0
         return filter;
 101  
     }
 102  
 
 103  
     public void setFilter(Filter filter)
 104  
     {
 105  0
         this.filter = filter;
 106  0
     }
 107  
 
 108  
     @Override
 109  
     public String toString()
 110  
     {
 111  0
         return (filter == null ? "null filter" : filter.getClass().getName()) + " (wrapped by "
 112  
                + this.getClass().getSimpleName() + ")";
 113  
     }
 114  
 
 115  
     @Override
 116  
     public void setMuleContext(MuleContext context)
 117  
     {
 118  0
         super.setMuleContext(context);
 119  0
         if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof MuleContextAware)
 120  
         {
 121  0
             ((MuleContextAware) unacceptedMessageProcessor).setMuleContext(context);
 122  
         }
 123  0
     }
 124  
 
 125  
     public void setFlowConstruct(FlowConstruct flowConstruct)
 126  
     {
 127  0
         if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof FlowConstructAware)
 128  
         {
 129  0
             ((FlowConstructAware) unacceptedMessageProcessor).setFlowConstruct(flowConstruct);
 130  
         }
 131  0
     }
 132  
 
 133  
     public void initialise() throws InitialisationException
 134  
     {
 135  0
         if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof Initialisable)
 136  
         {
 137  0
             ((Initialisable) unacceptedMessageProcessor).initialise();
 138  
         }
 139  0
     }
 140  
 
 141  
     public void start() throws MuleException
 142  
     {
 143  0
         if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof Startable)
 144  
         {
 145  0
             ((Startable) unacceptedMessageProcessor).start();
 146  
         }
 147  0
     }
 148  
 
 149  
     public void stop() throws MuleException
 150  
     {
 151  0
         if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof Stoppable)
 152  
         {
 153  0
             ((Stoppable) unacceptedMessageProcessor).stop();
 154  
         }
 155  0
     }
 156  
 
 157  
     public void dispose()
 158  
     {
 159  0
         if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof Disposable)
 160  
         {
 161  0
             ((Disposable) unacceptedMessageProcessor).dispose();
 162  
         }
 163  0
     }
 164  
 }