View Javadoc

1   /*
2    * $Id: ComponentMessageNotification.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.component.Component;
16  import org.mule.api.context.notification.ServerNotification;
17  
18  /**
19   * These notifications are fired when before and after a service component is
20   * invoked.
21   */
22  public class ComponentMessageNotification extends ServerNotification
23  {
24  
25      private static final long serialVersionUID = -6369685122731797646L;
26  
27      public static final int COMPONENT_PRE_INVOKE = COMPONENT_EVENT_ACTION_START_RANGE + 1;
28      public static final int COMPONENT_POST_INVOKE = COMPONENT_EVENT_ACTION_START_RANGE + 2;
29  
30      static
31      {
32          registerAction("component pre invoke", COMPONENT_PRE_INVOKE);
33          registerAction("component post invoke", COMPONENT_POST_INVOKE);
34      }
35  
36      protected transient Component component;
37  
38      /**
39       * @param message
40       * @param action
41       */
42      public ComponentMessageNotification(MuleMessage message, Component component, int action)
43      {
44          super(cloneMessage(message), action);
45          this.component = component;
46          resourceIdentifier = component.getService().getName();
47  
48      }
49  
50      protected static MuleMessage cloneMessage(MuleMessage message)
51      {
52          // TODO we probably need to support deep cloning here
53          synchronized (message)
54          {
55              return new DefaultMuleMessage(message.getPayload(), message);
56          }
57      }
58  
59      protected String getPayloadToString()
60      {
61          try
62          {
63              return ((MuleMessage) source).getPayloadAsString();
64          }
65          catch (Exception e)
66          {
67              return source.toString();
68          }
69      }
70  
71      /**
72       * @return the message
73       */
74      public Component getComponent()
75      {
76          return component;
77      }
78  
79      public String toString()
80      {
81          return EVENT_NAME + "{action=" + getActionName(action) + ", message: " + source + ", resourceId="
82                 + resourceIdentifier + ", timestamp=" + timestamp + ", serverId=" + serverId + ", component: "
83                 + component + "}";
84      }
85  
86  }