View Javadoc

1   /*
2    * $Id: WireTap.java 11343 2008-03-13 10:58:26Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.inbound;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.DefaultMuleSession;
15  import org.mule.NullSessionHandler;
16  import org.mule.RequestContext;
17  import org.mule.api.MessagingException;
18  import org.mule.api.MuleEvent;
19  import org.mule.api.MuleException;
20  import org.mule.api.MuleSession;
21  import org.mule.api.endpoint.OutboundEndpoint;
22  import org.mule.api.transport.DispatchException;
23  
24  /**
25   * An inbound router that can forward every message to another destination as defined
26   * in the "endpoint" property. This can be a logical destination of a URI. <p/> A
27   * filter can be applied to this router so that only events matching a criteria will
28   * be tapped.
29   */
30  public class WireTap extends SelectiveConsumer
31  {
32      private volatile OutboundEndpoint tap;
33  
34      public boolean isMatch(MuleEvent event) throws MessagingException
35      {
36          if (tap != null)
37          {
38              return super.isMatch(event);
39          }
40          else
41          {
42              logger.warn("No endpoint identifier is set on this wire tap");
43              return false;
44          }
45      }
46  
47      public MuleEvent[] process(MuleEvent event) throws MessagingException
48      {
49          RequestContext.setEvent(null);
50          try
51          {
52              //We have to create a new session for this dispatch, since the session may get altered
53              //using this call, changing the behaviour of the request
54              MuleSession session = new DefaultMuleSession(event.getMessage(), new NullSessionHandler(), getMuleContext());
55              tap.dispatch(new DefaultMuleEvent(event.getMessage(), tap, session, false));
56          }
57          catch (MessagingException e)
58          {
59              throw e;
60          }
61          catch (MuleException e)
62          {
63              throw new DispatchException(event.getMessage(), tap, e);
64          }
65          return super.process(event);
66      }
67  
68      public OutboundEndpoint getEndpoint()
69      {
70          return tap;
71      }
72  
73      public void setEndpoint(OutboundEndpoint endpoint) throws MuleException
74      {
75          this.tap = endpoint;
76      }
77  }