View Javadoc

1   /*
2    * $Id: WireTap.java 20555 2010-12-09 17:54:31Z aperepel $
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.AbstractMessageProcessorOwner;
20  import org.mule.util.ObjectUtils;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import java.util.Collections;
26  import java.util.List;
27  
28  /**
29   * The <code>WireTap</code> MessageProcessor allows inspection of messages in a flow.
30   * <p>
31   * The incoming message is is sent to both the primary and wiretap outputs. The flow
32   * of the primary output will be unmodified and a copy of the message used for the
33   * wiretap output.
34   * <p>
35   * An optional filter can be used to filter which message are sent to the wiretap
36   * output, this filter does not affect the flow to the primary output. If there is an
37   * error sending to the wiretap output no exception will be thrown but rather an
38   * error logged.
39   * <p>
40   * <b>EIP Reference:</b> <a href="http://www.eaipatterns.com/WireTap.html">http://www.eaipatterns.com/WireTap.html<a/>
41   */
42  public class WireTap extends AbstractMessageProcessorOwner implements MessageProcessor
43  {
44      protected final transient Log logger = LogFactory.getLog(getClass());
45      protected volatile MessageProcessor tap;
46      protected volatile Filter filter;
47  
48      protected MessageProcessor filteredTap = new WireTapFilter();
49  
50      public MuleEvent process(MuleEvent event) throws MuleException
51      {
52          if (tap == null)
53          {
54              return event;
55          }
56  
57          try
58          {
59              // Do we need this?
60              RequestContext.setEvent(null);
61              filteredTap.process(RequestContext.cloneAndUpdateEventEndpoint(event, tap));
62          }
63          catch (MuleException e)
64          {
65              logger.error("Exception sending to wiretap output " + tap, e);
66          }
67  
68          return event;
69      }
70  
71      public MessageProcessor getTap()
72      {
73          return tap;
74      }
75  
76      public void setTap(MessageProcessor tap)
77      {
78          this.tap = tap;
79      }
80  
81      @Deprecated
82      public void setMessageProcessor(MessageProcessor tap)
83      {
84          setTap(tap);
85      }
86      
87      public Filter getFilter()
88      {
89          return filter;
90      }
91  
92      public void setFilter(Filter filter)
93      {
94          this.filter = filter;
95      }
96  
97      private class WireTapFilter extends AbstractFilteringMessageProcessor
98      {
99          @Override
100         protected boolean accept(MuleEvent event)
101         {
102             if (filter == null)
103             {
104                 return true;
105             }
106             else
107             {
108                 return filter.accept(event.getMessage());
109             }
110         }
111 
112         @Override
113         protected MuleEvent processNext(MuleEvent event) throws MuleException
114         {
115             if (tap != null)
116             {
117                 tap.process(event);
118             }
119             return null;
120         }
121 
122         @Override
123         public String toString()
124         {
125             return ObjectUtils.toString(this);
126         }
127     }
128 
129     @Override
130     public String toString()
131     {
132         return ObjectUtils.toString(this);
133     }
134 
135         @Override
136     protected List<MessageProcessor> getOwnedMessageProcessors()
137     {
138         return Collections.singletonList(tap);
139     }
140 
141 }