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