View Javadoc

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      {
50          // empty
51      }
52  
53      public MessageFilter(Filter filter)
54      {
55          this.filter = filter;
56      }
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      {
65          this.filter = filter;
66          this.throwOnUnaccepted = throwExceptionOnUnaccepted;
67          this.unacceptedMessageProcessor = messageProcessor;
68      }
69  
70      @Override
71      protected boolean accept(MuleEvent event)
72      {
73          if (filter == null)
74          {
75              return true;
76          }
77  
78          if (event != null)
79          {
80              return filter.accept(event.getMessage());
81          }
82          else
83          {
84              return false;
85          }
86      }
87  
88      @Override
89      protected MuleException filterUnacceptedException(MuleEvent event)
90      {
91          return new FilterUnacceptedException(CoreMessages.messageRejectedByFilter(), event, filter);        
92      }
93      
94      public Filter getFilter()
95      {
96          return filter;
97      }
98  
99      public void setFilter(Filter filter)
100     {
101         this.filter = filter;
102     }
103 
104     @Override
105     public String toString()
106     {
107         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         super.setMuleContext(context);
114         if (unacceptedMessageProcessor instanceof MuleContextAware)
115         {
116             ((MuleContextAware)unacceptedMessageProcessor).setMuleContext(context);
117         }
118     }
119 
120     public void setFlowConstruct(FlowConstruct flowConstruct)
121     {
122         if (unacceptedMessageProcessor instanceof FlowConstructAware)
123         {
124             ((FlowConstructAware)unacceptedMessageProcessor).setFlowConstruct(flowConstruct);
125         }
126     }
127 
128     public void initialise() throws InitialisationException
129     {
130         if (unacceptedMessageProcessor instanceof Initialisable)
131         {
132             ((Initialisable)unacceptedMessageProcessor).initialise();
133         }
134     }
135 
136     public void start() throws MuleException
137     {
138         if (unacceptedMessageProcessor instanceof Startable)
139         {
140             ((Startable)unacceptedMessageProcessor).start();
141         }
142     }
143 
144     public void stop() throws MuleException
145     {
146         if (unacceptedMessageProcessor instanceof Stoppable)
147         {
148             ((Stoppable)unacceptedMessageProcessor).stop();
149         }
150     }
151 
152     public void dispose()
153     {
154         if (unacceptedMessageProcessor instanceof Disposable)
155         {
156             ((Disposable)unacceptedMessageProcessor).dispose();
157         }
158     }
159 }