Coverage Report - org.mule.context.notification.MessageProcessorNotification
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageProcessorNotification
0%
0/26
0%
0/8
0
 
 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  0
 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  0
         registerAction("message processor pre invoke", MESSAGE_PROCESSOR_PRE_INVOKE);
 34  0
         registerAction("message processor post invoke", MESSAGE_PROCESSOR_POST_INVOKE);
 35  0
     }
 36  
 
 37  
 
 38  
     public MessageProcessorNotification(FlowConstruct flowConstruct, MuleEvent event, MessageProcessor processor, int action)
 39  
     {
 40  0
         super(event, action, flowConstruct != null ? flowConstruct.getName() : null);
 41  
 
 42  0
         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  0
                 final Method method = processor.getClass().getMethod("getName");
 52  
                 // invoke existing getName(), but provide same fallback if it returned nothing
 53  0
                 messageProcessorName = ObjectUtils.toString(method.invoke(processor), toString(processor));
 54  
             }
 55  0
             catch (NoSuchMethodException e)
 56  
             {
 57  
                 // no such method, fallback to MP class name + NamedObject
 58  0
                 messageProcessorName = toString(processor);
 59  0
             }
 60  
         }
 61  0
         catch (Exception e)
 62  
         {
 63  0
             throw new MuleRuntimeException(e);
 64  0
         }
 65  0
     }
 66  
 
 67  
     @Override
 68  
     public MuleEvent getSource()
 69  
     {
 70  0
         if (source instanceof String)
 71  
         {
 72  0
             return null;
 73  
         }
 74  0
         return (MuleEvent) super.getSource();
 75  
     }
 76  
 
 77  
     public MessageProcessor getProcessor()
 78  
     {
 79  0
         return processor;
 80  
     }
 81  
 
 82  
     public String getFriendlyProcessorName()
 83  
     {
 84  0
         return messageProcessorName;
 85  
     }
 86  
 
 87  
     protected String toString(Object obj)
 88  
     {
 89  0
         if (obj == null)
 90  
         {
 91  0
             return "";
 92  
         }
 93  
 
 94  
         String name;
 95  0
         if (obj instanceof NamedObject)
 96  
         {
 97  0
             name = String.format("%s '%s'", obj.getClass().getName(), ((NamedObject) obj).getName());
 98  
         }
 99  
         else
 100  
         {
 101  0
             name = ObjectUtils.identityToString(obj);
 102  
         }
 103  0
         return name;
 104  
     }
 105  
 }