Coverage Report - org.mule.exception.DefaultServiceExceptionStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultServiceExceptionStrategy
0%
0/44
0%
0/26
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.exception;
 8  
 
 9  
 import org.mule.api.MuleContext;
 10  
 import org.mule.api.MuleEvent;
 11  
 import org.mule.api.MuleException;
 12  
 import org.mule.api.construct.FlowConstruct;
 13  
 import org.mule.api.lifecycle.Lifecycle;
 14  
 import org.mule.api.lifecycle.Stoppable;
 15  
 import org.mule.api.processor.MessageProcessor;
 16  
 import org.mule.config.DefaultMuleConfiguration;
 17  
 import org.mule.config.ExceptionHelper;
 18  
 import org.mule.management.stats.FlowConstructStatistics;
 19  
 import org.mule.management.stats.ServiceStatistics;
 20  
 import org.mule.util.CollectionUtils;
 21  
 
 22  
 import java.util.List;
 23  
 
 24  
 /**
 25  
  * <code>DefaultServiceExceptionStrategy</code> is the default exception handler
 26  
  * for components. The handler logs errors and will forward the message and exception
 27  
  * to an exception endpointUri if one is set on this Exception strategy
 28  
  */
 29  
 public class DefaultServiceExceptionStrategy extends AbstractMessagingExceptionStrategy
 30  
 {
 31  
     /** Stop the flow/service when an exception occurs.  You will need to restart the flow/service manually after this (e.g, using JMX). */
 32  
     private boolean stopMessageProcessing;
 33  
 
 34  
     /** 
 35  
      * For IoC only 
 36  
      * @deprecated Use DefaultServiceExceptionStrategy(MuleContext muleContext) instead
 37  
      */
 38  
     public DefaultServiceExceptionStrategy()
 39  
     {
 40  0
         super();
 41  0
     }
 42  
 
 43  
     public DefaultServiceExceptionStrategy(MuleContext muleContext)
 44  
     {
 45  0
         super();
 46  0
         setMuleContext(muleContext);
 47  0
     }
 48  
 
 49  
     @Override
 50  
     protected void doHandleException(Exception e, MuleEvent event)
 51  
     {
 52  0
         FlowConstructStatistics statistics = getFlowConstructStatistics(event.getFlowConstruct());
 53  
 
 54  0
         if (statistics != null && statistics.isEnabled())
 55  
         {
 56  0
             statistics.incExecutionError();
 57  
         }
 58  
 
 59  0
         super.doHandleException(DefaultMuleConfiguration.fullStackTraces ? e : (Exception) ExceptionHelper.sanitize(e), event);
 60  0
     }
 61  
 
 62  
     @Override
 63  
     protected void logFatal(MuleEvent event, Throwable t)
 64  
     {
 65  0
         FlowConstructStatistics statistics = getFlowConstructStatistics(event.getFlowConstruct());
 66  0
         if (statistics != null && statistics.isEnabled())
 67  
         {
 68  0
             statistics.incFatalError();
 69  
         }
 70  
 
 71  0
         super.logFatal(event, t);
 72  0
     }
 73  
 
 74  
     @Override
 75  
     protected void routeException(MuleEvent event, MessageProcessor target, Throwable t)
 76  
     {
 77  0
         super.routeException(event, target, t);
 78  0
         List<MessageProcessor> processors = getMessageProcessors();
 79  0
         if (CollectionUtils.isNotEmpty(processors) && getFlowConstructStatistics(event.getFlowConstruct()) instanceof ServiceStatistics)
 80  
         {
 81  0
             ServiceStatistics statistics = getServiceStatistics(event.getFlowConstruct());
 82  0
             if (statistics.isEnabled())
 83  
             {
 84  0
                 for (MessageProcessor endpoint : processors)
 85  
                 {
 86  0
                     statistics.getOutboundRouterStat().incrementRoutedMessage(endpoint);
 87  
                 }
 88  
             }
 89  
         }
 90  
 
 91  0
         if (stopMessageProcessing)
 92  
         {
 93  0
             stopFlowConstruct();
 94  
         }
 95  0
     }
 96  
 
 97  
     private void stopFlowConstruct()
 98  
     {
 99  0
         if (flowConstruct instanceof Stoppable)
 100  
         {
 101  0
             logger.info("Stopping flow '" + flowConstruct.getName() + "' due to exception");
 102  
 
 103  
             try
 104  
             {
 105  0
                 ((Lifecycle) flowConstruct).stop();
 106  
             }
 107  0
             catch (MuleException e)
 108  
             {
 109  0
                 logger.error("Unable to stop flow '" + flowConstruct.getName() + "'", e);
 110  0
             }
 111  
         }
 112  
         else
 113  
         {
 114  0
             logger.warn("Flow is not stoppable");
 115  
         }
 116  0
     }
 117  
 
 118  
     protected FlowConstructStatistics getFlowConstructStatistics(FlowConstruct flowConstruct)
 119  
     {
 120  0
         if (flowConstruct != null )
 121  
         {
 122  0
             return flowConstruct.getStatistics();
 123  
         }
 124  
         else
 125  
         {
 126  
             //this can happen, e.g. with event constructed to handle exceptions
 127  
             // logger.fatal("The Default Service Exception Strategy has been invoked but there is no current flow construct on the context. Please report this to dev@mule.codehaus.org");
 128  0
             return null;
 129  
         }
 130  
     }
 131  
 
 132  
     protected ServiceStatistics getServiceStatistics(FlowConstruct flowConstruct)
 133  
     {
 134  0
         FlowConstructStatistics stats = getFlowConstructStatistics(flowConstruct);
 135  0
         if (!(stats instanceof ServiceStatistics))
 136  
         {
 137  
             //this should never happen, but JIC
 138  0
             logger.fatal("The Default Service Exception Strategy has been invoked but there is no current service on the context. Please report this to dev@mule.codehaus.org");            
 139  0
             return null;
 140  
         }
 141  0
         return (ServiceStatistics) stats;
 142  
     }
 143  
 
 144  
     public boolean isStopMessageProcessing()
 145  
     {
 146  0
         return stopMessageProcessing;
 147  
     }
 148  
 
 149  
     public void setStopMessageProcessing(boolean stopMessageProcessing)
 150  
     {
 151  0
         this.stopMessageProcessing = stopMessageProcessing;
 152  0
     }
 153  
 }