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.context.notification;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.context.notification.ServerNotification;
11  import org.mule.api.endpoint.ImmutableEndpoint;
12  
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  
16  /**
17   * These notifications are fired when either a message is either: received by an
18   * endpoint, sent or dispatched from an endpoint or requested from an endpoint.
19   */
20  public class EndpointMessageNotification extends ServerNotification
21  {
22      /**
23       * Serial version
24       */
25      private static final long serialVersionUID = -5118299601117624094L;
26  
27      /**
28       * logger used by this class
29       */
30      protected static final Log logger = LogFactory.getLog(EndpointMessageNotification.class);
31  
32      public static final int MESSAGE_RECEIVED = MESSAGE_EVENT_ACTION_START_RANGE + 1;
33      public static final int MESSAGE_DISPATCHED = MESSAGE_EVENT_ACTION_START_RANGE + 2;
34      public static final int MESSAGE_SENT = MESSAGE_EVENT_ACTION_START_RANGE + 3;
35      public static final int MESSAGE_REQUESTED = MESSAGE_EVENT_ACTION_START_RANGE + 4;
36      public static final int MESSAGE_RESPONSE = MESSAGE_EVENT_ACTION_START_RANGE + 5;
37  
38      static
39      {
40          registerAction("received", MESSAGE_RECEIVED);
41          registerAction("dispatched", MESSAGE_DISPATCHED);
42          registerAction("sent", MESSAGE_SENT);
43          registerAction("requested", MESSAGE_REQUESTED);
44          registerAction("response", MESSAGE_RESPONSE);
45      }
46  
47      private String endpoint;
48  
49      public EndpointMessageNotification(MuleMessage resource,
50                                 ImmutableEndpoint endpoint,
51                                 String identifier,
52                                 int action)
53      {
54          super(cloneMessage(resource), action);
55          resourceIdentifier = identifier;
56          this.endpoint = endpoint.getEndpointURI().toString();
57      }
58  
59  
60      @Override
61      protected String getPayloadToString()
62      {
63          try
64          {
65              return ((MuleMessage)source).getPayloadAsString();
66          }
67          catch (Exception e)
68          {
69              return source.toString();
70          }
71      }
72  
73      @Override
74      public String toString()
75      {
76          return EVENT_NAME + "{action=" + getActionName(action) + ", endpoint: " + endpoint
77                          + ", resourceId=" + resourceIdentifier + ", timestamp=" + timestamp + ", serverId="
78                          + serverId + ", message: " + source + "}";
79      }
80  
81      public String getEndpoint()
82      {
83          return endpoint;
84      }
85  
86      @Override
87      public String getType()
88      {
89          return TYPE_TRACE;
90      }
91  
92      @Override
93      public MuleMessage getSource()
94      {
95          return (MuleMessage) super.getSource();
96      }
97  }