View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.endpoint.outbound;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleException;
11  import org.mule.api.endpoint.OutboundEndpoint;
12  import org.mule.api.processor.MessageProcessor;
13  import org.mule.context.notification.EndpointMessageNotification;
14  import org.mule.transport.AbstractConnector;
15  import org.mule.util.ObjectUtils;
16  
17  /**
18   * Publishes a {@link EndpointMessageNotification}'s when a message is sent or
19   * dispatched.
20   */
21  
22  public class OutboundNotificationMessageProcessor implements MessageProcessor
23  {
24  
25      private OutboundEndpoint endpoint;
26  
27      public OutboundNotificationMessageProcessor(OutboundEndpoint endpoint)
28      {
29          this.endpoint = endpoint;
30      }
31  
32      public MuleEvent process(MuleEvent event) throws MuleException
33      {
34          AbstractConnector connector = (AbstractConnector) endpoint.getConnector();
35          if (connector.isEnableMessageEvents())
36          {
37              String component = null;
38              if (event.getFlowConstruct() != null)
39              {
40                  component = event.getFlowConstruct().getName();
41              }
42  
43              int notificationAction;
44              if (event.getEndpoint().getExchangePattern().hasResponse())
45              {
46                  notificationAction = EndpointMessageNotification.MESSAGE_SENT;
47              }
48              else
49              {
50                  notificationAction = EndpointMessageNotification.MESSAGE_DISPATCHED;
51              }
52              connector.fireNotification(new EndpointMessageNotification(event.getMessage(),
53                  event.getEndpoint(), component, notificationAction));
54          }
55  
56          return event;
57      }
58  
59      @Override
60      public String toString()
61      {
62          return ObjectUtils.toString(this);
63      }
64  }