View Javadoc

1   /*
2    * $Id: EndpointMessageNotification.java 23030 2011-09-26 18:02:33Z mike.schilling $
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.construct.FlowConstruct;
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_DISPATCH_BEGIN = MESSAGE_EVENT_ACTION_START_RANGE + 2;
39      public static final int MESSAGE_SEND_BEGIN = MESSAGE_EVENT_ACTION_START_RANGE + 3;
40      public static final int MESSAGE_REQUEST_BEGIN = MESSAGE_EVENT_ACTION_START_RANGE + 4;
41      public static final int MESSAGE_RESPONSE = MESSAGE_EVENT_ACTION_START_RANGE + 5;
42  
43      public static final int MESSAGE_DISPATCH_END = MESSAGE_EVENT_END_ACTION_START_RANGE + 1;
44      public static final int MESSAGE_SEND_END = MESSAGE_EVENT_END_ACTION_START_RANGE + 2;
45      public static final int MESSAGE_REQUEST_END = MESSAGE_EVENT_END_ACTION_START_RANGE + 3;
46  
47      /**
48       * For backwards compatibility.  BEGIN is chosen where it contains the message sent, and END where it contains the message
49       * received, again for backwards compatibility.
50       */
51      public static final int MESSAGE_DISPATCHED = MESSAGE_DISPATCH_BEGIN;
52      public static final int MESSAGE_SENT = MESSAGE_SEND_BEGIN;
53      public static final int MESSAGE_REQUESTED = MESSAGE_REQUEST_END;
54  
55  
56      static
57      {
58          registerAction("receive", MESSAGE_RECEIVED);
59          registerAction("response", MESSAGE_RESPONSE);
60  
61          registerAction("begin dispatch", MESSAGE_DISPATCH_BEGIN);
62          registerAction("begin send", MESSAGE_SEND_BEGIN);
63          registerAction("begin request", MESSAGE_REQUEST_BEGIN);
64  
65          registerAction("end dispatch", MESSAGE_DISPATCH_END);
66          registerAction("end send", MESSAGE_SEND_END);
67          registerAction("end request", MESSAGE_REQUEST_END);
68      }
69  
70      private String endpoint;
71      private ImmutableEndpoint immutableEndpoint;
72      private FlowConstruct flowConstruct;
73  
74      public EndpointMessageNotification(MuleMessage resource,
75                                 ImmutableEndpoint endpoint,
76                                 FlowConstruct flowConstruct,
77                                 int action)
78      {
79          super(cloneMessage(resource), action);
80          resourceIdentifier = flowConstruct != null ? flowConstruct.getName() : null;
81          this.endpoint = endpoint.getEndpointURI().toString();
82          this.immutableEndpoint = endpoint;
83          this.flowConstruct = flowConstruct;
84      }
85  
86  
87      @Override
88      protected String getPayloadToString()
89      {
90          try
91          {
92              return ((MuleMessage)source).getPayloadAsString();
93          }
94          catch (Exception e)
95          {
96              return source.toString();
97          }
98      }
99  
100     @Override
101     public String toString()
102     {
103         return EVENT_NAME + "{action=" + getActionName(action) + ", endpoint: " + endpoint
104                         + ", resourceId=" + resourceIdentifier + ", timestamp=" + timestamp + ", serverId="
105                         + serverId + ", message: " + source + "}";
106     }
107 
108     public String getEndpoint()
109     {
110         return endpoint;
111     }
112 
113     public ImmutableEndpoint getImmutableEndpoint()
114     {
115         return immutableEndpoint;
116     }
117 
118     public FlowConstruct getFlowConstruct()
119     {
120         return flowConstruct;
121     }
122 
123     @Override
124     public String getType()
125     {
126         return TYPE_TRACE;
127     }
128 
129     @Override
130     public MuleMessage getSource()
131     {
132         return (MuleMessage) super.getSource();
133     }
134 }