View Javadoc

1   /*
2    * $Id: DefaultExceptionStrategy.java 8821 2007-10-04 13:38:03Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.impl;
12  
13  import org.mule.impl.message.ExceptionPayload;
14  import org.mule.providers.NullPayload;
15  import org.mule.umo.UMOMessage;
16  import org.mule.umo.endpoint.UMOImmutableEndpoint;
17  import org.mule.util.ObjectUtils;
18  
19  /**
20   * <code>DefaultExceptionStrategy</code> provides a default exception handling
21   * strategy.
22   */
23  
24  public class DefaultExceptionStrategy extends AbstractExceptionListener
25  {
26      public void handleMessagingException(UMOMessage message, Throwable t)
27      {
28          defaultHandler(t);
29          routeException(message, null, t);
30      }
31  
32      public void handleRoutingException(UMOMessage message, UMOImmutableEndpoint endpoint, Throwable t)
33      {
34          defaultHandler(t);
35          routeException(message, endpoint, t);
36      }
37  
38      public void handleLifecycleException(Object component, Throwable t)
39      {
40          // Do nothing special here. Overriding implmentations may want alter the
41          // behaviour
42          handleStandardException(t);
43          logger.error("The object that failed was: \n" + ObjectUtils.toString(component, "null"));
44      }
45  
46      public void handleStandardException(Throwable t)
47      {
48          markTransactionForRollback();
49          // Attempt to send the exception details to an endpoint if one is set
50          if (RequestContext.getEventContext() != null)
51          {
52              handleMessagingException(RequestContext.getEventContext().getMessage(), t);
53          }
54          else
55          {
56              logger.info("There is no current event available, routing Null message with the exception");
57              handleMessagingException(new MuleMessage(NullPayload.getInstance()), t);
58          }
59      }
60  
61      protected void defaultHandler(Throwable t)
62      {
63          if (RequestContext.getEvent() != null)
64          {
65              RequestContext.setExceptionPayload(new ExceptionPayload(t));
66          }
67      }
68  }