View Javadoc

1   /*
2    * $Id: MessageProcessorNotification.java 20808 2010-12-20 22:21:26Z dzapata $
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.MuleEvent;
14  import org.mule.api.MuleRuntimeException;
15  import org.mule.api.NamedObject;
16  import org.mule.api.context.notification.BlockingServerEvent;
17  import org.mule.api.context.notification.ServerNotification;
18  import org.mule.api.processor.MessageProcessor;
19  import org.mule.util.ObjectUtils;
20  
21  import java.lang.reflect.Method;
22  
23  public class MessageProcessorNotification extends ServerNotification implements BlockingServerEvent
24  {
25  
26      private static final long serialVersionUID = 1L;
27  
28      public static final int MESSAGE_PROCESSOR_PRE_INVOKE = MESSAGE_PROCESSOR_EVENT_ACTION_START_RANGE + 1;
29      public static final int MESSAGE_PROCESSOR_POST_INVOKE = MESSAGE_PROCESSOR_EVENT_ACTION_START_RANGE + 2;
30  
31      private final transient MessageProcessor processor;
32      private String messageProcessorName;
33  
34      static
35      {
36          registerAction("message processor pre invoke", MESSAGE_PROCESSOR_PRE_INVOKE);
37          registerAction("message processor post invoke", MESSAGE_PROCESSOR_POST_INVOKE);
38      }
39  
40  
41      public MessageProcessorNotification(MuleEvent event, MessageProcessor processor, int action)
42      {
43          super(event, action, event.getFlowConstruct() != null ? event.getFlowConstruct().getName() : null);
44  
45          this.processor = processor;
46  
47          // can't extract it to a method and still kepp the field final, have to leave it inline
48          try
49          {
50              // TODO would love to see MP.getName() in the API. Avoid BeanUtils.getProperty() overhead here
51              
52              try
53              {
54                  final Method method = processor.getClass().getMethod("getName");
55                  // invoke existing getName(), but provide same fallback if it returned nothing
56                  messageProcessorName = ObjectUtils.toString(method.invoke(processor), toString(processor));
57              }
58              catch (NoSuchMethodException e)
59              {
60                  // no such method, fallback to MP class name + NamedObject
61                  messageProcessorName = toString(processor);
62              }
63          }
64          catch (Exception e)
65          {
66              throw new MuleRuntimeException(e);
67          }
68      }
69  
70      @Override
71      public MuleEvent getSource()
72      {
73          return (MuleEvent) super.getSource();
74      }
75  
76      public MessageProcessor getProcessor()
77      {
78          return processor;
79      }
80  
81      public String getFriendlyProcessorName()
82      {
83          return messageProcessorName;
84      }
85  
86      protected String toString(Object obj)
87      {
88          if (obj == null)
89          {
90              return "";
91          }
92  
93          String name;
94          if (obj instanceof NamedObject)
95          {
96              name = String.format("%s '%s'", obj.getClass().getName(), ((NamedObject) obj).getName());
97          }
98          else
99          {
100             name = ObjectUtils.identityToString(obj);
101         }
102         return name;
103     }
104 }