View Javadoc

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