Coverage Report - org.mule.exception.AbstractMessagingExceptionStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractMessagingExceptionStrategy
0%
0/50
0%
0/14
2.571
 
 1  
 /*
 2  
  * $Id: AbstractMessagingExceptionStrategy.java 19288 2010-09-01 22:07:54Z epere4 $
 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.exception;
 12  
 
 13  
 import org.mule.DefaultMuleMessage;
 14  
 import org.mule.RequestContext;
 15  
 import org.mule.api.MessagingException;
 16  
 import org.mule.api.MuleEvent;
 17  
 import org.mule.api.MuleMessage;
 18  
 import org.mule.api.exception.MessagingExceptionHandler;
 19  
 import org.mule.api.lifecycle.LifecycleException;
 20  
 import org.mule.api.processor.MessageProcessor;
 21  
 import org.mule.api.routing.RoutingException;
 22  
 import org.mule.context.notification.ExceptionNotification;
 23  
 import org.mule.message.DefaultExceptionPayload;
 24  
 import org.mule.transport.NullPayload;
 25  
 import org.mule.util.ObjectUtils;
 26  
 
 27  
 /**
 28  
  * <code>DefaultExceptionStrategy</code> provides a default exception handling
 29  
  * strategy.
 30  
  */
 31  
 
 32  0
 public abstract class AbstractMessagingExceptionStrategy extends AbstractExceptionListener implements MessagingExceptionHandler
 33  
 {
 34  
     /**
 35  
      * {@inheritDoc}
 36  
      */
 37  
     public MuleEvent handleException(Exception e, MuleEvent event)
 38  
     {
 39  0
         if (enableNotifications)
 40  
         {
 41  0
             fireNotification(new ExceptionNotification(e));
 42  
         }
 43  
 
 44  0
         logException(e);
 45  0
         handleTransaction(e);
 46  
 
 47  0
         Throwable t = getExceptionType(e, RoutingException.class);
 48  0
         if (t != null)
 49  
         {
 50  0
             RoutingException re = (RoutingException) t;
 51  0
             handleRoutingException(re.getMuleMessage(), re.getRoute(), e);
 52  
 
 53  0
             event.getMessage().setPayload(NullPayload.getInstance());
 54  0
             event.getMessage().setExceptionPayload(new DefaultExceptionPayload(e));
 55  0
             return event;
 56  
         }
 57  
 
 58  0
         t = getExceptionType(e, MessagingException.class);
 59  0
         if (t != null)
 60  
         {
 61  0
             MessagingException me = (MessagingException) t;
 62  0
             handleMessagingException(me.getMuleMessage(), e);
 63  
 
 64  0
             event.getMessage().setPayload(NullPayload.getInstance());
 65  0
             event.getMessage().setExceptionPayload(new DefaultExceptionPayload(e));
 66  0
             return event;
 67  
         }
 68  
 
 69  0
         t = getExceptionType(e, LifecycleException.class);
 70  0
         if (t != null)
 71  
         {
 72  0
             LifecycleException le = (LifecycleException) t;
 73  0
             handleLifecycleException(le.getComponent(), e);
 74  0
             event.getMessage().setPayload(NullPayload.getInstance());
 75  0
             event.getMessage().setExceptionPayload(new DefaultExceptionPayload(e));
 76  0
             return event;
 77  
         }
 78  
 
 79  0
         handleStandardException(e);
 80  0
         event.getMessage().setPayload(NullPayload.getInstance());
 81  0
         event.getMessage().setExceptionPayload(new DefaultExceptionPayload(e));
 82  0
         return event;
 83  
     }
 84  
 
 85  
     public void handleMessagingException(MuleMessage message, Throwable t)
 86  
     {
 87  0
         defaultHandler(t);
 88  0
         routeException(messageFromContextIfAvailable(message), null, t);
 89  0
     }
 90  
 
 91  
     public void handleRoutingException(MuleMessage message, MessageProcessor target, Throwable t)
 92  
     {
 93  0
         defaultHandler(t);
 94  0
         routeException(messageFromContextIfAvailable(message), target, t);
 95  0
     }
 96  
 
 97  
     public void handleLifecycleException(Object component, Throwable t)
 98  
     {
 99  
         // Do nothing special here. Overriding implmentations may want alter the
 100  
         // behaviour
 101  0
         handleStandardException(t);
 102  0
         logger.error("The object that failed was: \n" + ObjectUtils.toString(component, "null"));
 103  0
     }
 104  
 
 105  
     public void handleStandardException(Throwable t)
 106  
     {
 107  
         // Attempt to send the exception details to an endpoint if one is set
 108  0
         if (RequestContext.getEventContext() != null)
 109  
         {
 110  0
             handleMessagingException(RequestContext.getEventContext().getMessage(), t);
 111  
         }
 112  
         else
 113  
         {
 114  0
             logger.info("There is no current event available, routing Null message with the exception");
 115  0
             handleMessagingException(new DefaultMuleMessage(NullPayload.getInstance(), muleContext), t);
 116  
         }
 117  0
     }
 118  
 
 119  
     protected void defaultHandler(Throwable t)
 120  
     {
 121  0
         if (RequestContext.getEvent() != null)
 122  
         {
 123  0
             RequestContext.setExceptionPayload(new DefaultExceptionPayload(t));
 124  
         }
 125  0
     }
 126  
 
 127  
     protected MuleMessage messageFromContextIfAvailable(MuleMessage message)
 128  
     {
 129  0
         if (null != RequestContext.getEvent())
 130  
         {
 131  0
             return RequestContext.getEvent().getMessage();
 132  
         }
 133  
         else
 134  
         {
 135  0
             return message;
 136  
         }
 137  
     }
 138  
 
 139  
 }