View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.context.notification;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleRuntimeException;
11  import org.mule.api.NamedObject;
12  import org.mule.api.construct.FlowConstruct;
13  import org.mule.api.context.notification.BlockingServerEvent;
14  import org.mule.api.context.notification.ServerNotification;
15  import org.mule.api.processor.MessageProcessor;
16  import org.mule.util.ObjectUtils;
17  
18  import java.lang.reflect.Method;
19  
20  public class MessageProcessorNotification extends ServerNotification implements BlockingServerEvent
21  {
22  
23      private static final long serialVersionUID = 1L;
24  
25      public static final int MESSAGE_PROCESSOR_PRE_INVOKE = MESSAGE_PROCESSOR_EVENT_ACTION_START_RANGE + 1;
26      public static final int MESSAGE_PROCESSOR_POST_INVOKE = MESSAGE_PROCESSOR_EVENT_ACTION_START_RANGE + 2;
27  
28      private final transient MessageProcessor processor;
29      private String messageProcessorName;
30  
31      static
32      {
33          registerAction("message processor pre invoke", MESSAGE_PROCESSOR_PRE_INVOKE);
34          registerAction("message processor post invoke", MESSAGE_PROCESSOR_POST_INVOKE);
35      }
36  
37  
38      public MessageProcessorNotification(FlowConstruct flowConstruct, MuleEvent event, MessageProcessor processor, int action)
39      {
40          super(event, action, flowConstruct != null ? flowConstruct.getName() : null);
41  
42          this.processor = processor;
43  
44          // can't extract it to a method and still kepp the field final, have to leave it inline
45          try
46          {
47              // TODO would love to see MP.getName() in the API. Avoid BeanUtils.getProperty() overhead here
48              
49              try
50              {
51                  final Method method = processor.getClass().getMethod("getName");
52                  // invoke existing getName(), but provide same fallback if it returned nothing
53                  messageProcessorName = ObjectUtils.toString(method.invoke(processor), toString(processor));
54              }
55              catch (NoSuchMethodException e)
56              {
57                  // no such method, fallback to MP class name + NamedObject
58                  messageProcessorName = toString(processor);
59              }
60          }
61          catch (Exception e)
62          {
63              throw new MuleRuntimeException(e);
64          }
65      }
66  
67      @Override
68      public MuleEvent getSource()
69      {
70          if (source instanceof String)
71          {
72              return null;
73          }
74          return (MuleEvent) super.getSource();
75      }
76  
77      public MessageProcessor getProcessor()
78      {
79          return processor;
80      }
81  
82      public String getFriendlyProcessorName()
83      {
84          return messageProcessorName;
85      }
86  
87      protected String toString(Object obj)
88      {
89          if (obj == null)
90          {
91              return "";
92          }
93  
94          String name;
95          if (obj instanceof NamedObject)
96          {
97              name = String.format("%s '%s'", obj.getClass().getName(), ((NamedObject) obj).getName());
98          }
99          else
100         {
101             name = ObjectUtils.identityToString(obj);
102         }
103         return name;
104     }
105 }