View Javadoc

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