View Javadoc

1   /*
2    * $Id: EndpointMessageNotification.java 19191 2010-08-25 21:05:23Z tcarlson $
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  
41      static {
42          registerAction("received", MESSAGE_RECEIVED);
43          registerAction("dispatched", MESSAGE_DISPATCHED);
44          registerAction("sent", MESSAGE_SENT);
45          registerAction("requested", MESSAGE_REQUESTED);
46      }
47  
48      private String endpoint;
49  
50      public EndpointMessageNotification(MuleMessage resource,
51                                 ImmutableEndpoint endpoint,
52                                 String identifier,
53                                 int action)
54      {
55          super(cloneMessage(resource), action);
56          resourceIdentifier = identifier;
57          this.endpoint = endpoint.getEndpointURI().toString();
58      }
59  
60  
61      @Override
62      protected String getPayloadToString()
63      {
64          try
65          {
66              return ((MuleMessage)source).getPayloadAsString();
67          }
68          catch (Exception e)
69          {
70              return source.toString();
71          }
72      }
73  
74      @Override
75      public String toString()
76      {
77          return EVENT_NAME + "{action=" + getActionName(action) + ", endpoint: " + endpoint
78                          + ", resourceId=" + resourceIdentifier + ", timestamp=" + timestamp + ", serverId="
79                          + serverId + ", message: " + source + "}";
80      }
81  
82      public String getEndpoint()
83      {
84          return endpoint;
85      }
86  
87      public String getType()
88      {
89          return TYPE_TRACE;
90      }
91  
92  }