View Javadoc

1   /*
2    * $Id: MessageProcessorNotification.java 22847 2011-09-02 23:16:27Z 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.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.MessageExchangePattern;
16  import org.mule.api.MuleEvent;
17  import org.mule.api.NameableObject;
18  import org.mule.api.construct.FlowConstruct;
19  import org.mule.api.context.notification.BlockingServerEvent;
20  import org.mule.api.context.notification.ServerNotification;
21  import org.mule.api.processor.MessageProcessor;
22  import org.mule.session.DefaultMuleSession;
23  import org.mule.transport.NullPayload;
24  import org.mule.util.ObjectUtils;
25  
26  public class MessageProcessorNotification extends ServerNotification implements BlockingServerEvent
27  {
28  
29      private static final long serialVersionUID = 1L;
30  
31      public static final int MESSAGE_PROCESSOR_PRE_INVOKE = MESSAGE_PROCESSOR_EVENT_ACTION_START_RANGE + 1;
32      public static final int MESSAGE_PROCESSOR_POST_INVOKE = MESSAGE_PROCESSOR_EVENT_ACTION_START_RANGE + 2;
33  
34      private final transient MessageProcessor processor;
35  
36      static
37      {
38          registerAction("message processor pre invoke", MESSAGE_PROCESSOR_PRE_INVOKE);
39          registerAction("message processor post invoke", MESSAGE_PROCESSOR_POST_INVOKE);
40      }
41  
42      private static ThreadLocal<String> lastRootMessageId = new ThreadLocal<String>();
43  
44      public MessageProcessorNotification(FlowConstruct flowConstruct,
45                                          MuleEvent event,
46                                          MessageProcessor processor,
47                                          int action)
48      {
49          super(produceEvent(event, flowConstruct), action, flowConstruct != null ? flowConstruct.getName() : null);
50  
51          this.processor = processor;
52      }
53  
54      @Override
55      public MuleEvent getSource()
56      {
57          if (source instanceof String)
58          {
59              return null;
60          }
61          return (MuleEvent) super.getSource();
62      }
63  
64      public MessageProcessor getProcessor()
65      {
66          return processor;
67      }
68  
69      protected String toString(Object obj)
70      {
71          if (obj == null)
72          {
73              return "";
74          }
75  
76          String name;
77          if (obj instanceof NameableObject)
78          {
79              name = String.format("%s '%s'", obj.getClass().getName(), ((NameableObject) obj).getName());
80          }
81          else
82          {
83              name = ObjectUtils.identityToString(obj);
84          }
85          return name;
86      }
87  
88      /**
89       * If event is null, produce and event with the proper message root ID, to allow it to be correlated
90       * with others in the thread
91       */
92      private static MuleEvent produceEvent(MuleEvent sourceEvent, FlowConstruct flowConstruct)
93      {
94          String rootId = lastRootMessageId.get();
95          if (sourceEvent != null)
96          {
97              lastRootMessageId.set(sourceEvent.getMessage().getMessageRootId());
98              return sourceEvent;
99          }
100         else if (rootId != null && flowConstruct != null)
101         {
102             DefaultMuleMessage msg = new DefaultMuleMessage(NullPayload.getInstance(), flowConstruct.getMuleContext());
103             DefaultMuleSession session = new DefaultMuleSession(flowConstruct, flowConstruct.getMuleContext());
104             msg.setMessageRootId(rootId);
105             return new DefaultMuleEvent(msg, MessageExchangePattern.REQUEST_RESPONSE, session);
106         }
107         else
108         {
109             return null;
110         }
111     }
112 }