View Javadoc

1   /*
2    * $Id$
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.RequestContext;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.processor.MessageProcessor;
17  import org.mule.api.routing.filter.Filter;
18  import org.mule.processor.AbstractFilteringMessageProcessor;
19  import org.mule.processor.AbstractMessageObserver;
20  
21  /**
22   * The <code>WireTap</code> MessageProcessor allows inspection of messages in a flow.
23   * <p>
24   * The incoming message is is sent to both the primary and wiretap outputs. The flow
25   * of the primary output will be unmodified and a copy of the message used for the
26   * wiretap output.
27   * <p>
28   * An optional filter can be used to filter which message are sent to the wiretap
29   * output, this filter does not affect the flow to the primary output. If there is an
30   * error sending to the wiretap output no exception will be thrown but rather an
31   * error logged.
32   * <p>
33   * <b>EIP Reference:</b> <a href="http://www.eaipatterns.com/WireTap.html">http://www.eaipatterns.com/WireTap.html<a/>
34   */
35  public class WireTap extends AbstractMessageObserver
36  
37  {
38      protected volatile MessageProcessor tap;
39      protected volatile Filter filter;
40  
41      protected MessageProcessor filteredTap = new WireTapFilter();
42  
43      @Override
44      public void observe(MuleEvent event)
45      {
46          if (tap == null)
47          {
48              return;
49          }
50  
51          try
52          {
53              // Do we need this?
54              RequestContext.setEvent(null);
55              filteredTap.process(RequestContext.cloneAndUpdateEventEndpoint(event, tap));
56          }
57          catch (MuleException e)
58          {
59              logger.error("Exception sending to wiretap output " + tap, e);
60          }
61      }
62  
63      public MessageProcessor getTap()
64      {
65          return tap;
66      }
67  
68      public void setTap(MessageProcessor tap)
69      {
70          this.tap = tap;
71      }
72  
73      @Deprecated
74      public void setMessageProcessor(MessageProcessor tap)
75      {
76          setTap(tap);
77      }
78      
79      public Filter getFilter()
80      {
81          return filter;
82      }
83  
84      public void setFilter(Filter filter)
85      {
86          this.filter = filter;
87      }
88  
89      private class WireTapFilter extends AbstractFilteringMessageProcessor
90      {
91          @Override
92          protected boolean accept(MuleEvent event)
93          {
94              if (filter == null)
95              {
96                  return true;
97              }
98              else
99              {
100                 return filter.accept(event.getMessage());
101             }
102         }
103 
104         @Override
105         protected MuleEvent processNext(MuleEvent event) throws MuleException
106         {
107             if (tap != null)
108             {
109                 tap.process(event);
110             }
111             return null;
112         }
113     }
114 
115 }