View Javadoc

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