View Javadoc
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.RequestContext;
10  import org.mule.api.ExceptionPayload;
11  import org.mule.api.MuleEvent;
12  import org.mule.api.exception.MessagingExceptionHandler;
13  import org.mule.api.processor.MessageProcessor;
14  import org.mule.api.routing.RoutingException;
15  import org.mule.api.transaction.Transaction;
16  import org.mule.api.transaction.TransactionException;
17  import org.mule.context.notification.ExceptionNotification;
18  import org.mule.management.stats.FlowConstructStatistics;
19  import org.mule.message.DefaultExceptionPayload;
20  import org.mule.transaction.TransactionCoordination;
21  import org.mule.transport.NullPayload;
22  
23  /**
24   * <code>DefaultExceptionStrategy</code> provides a default exception handling
25   * strategy.
26   */
27  
28  public abstract class AbstractMessagingExceptionStrategy extends AbstractExceptionListener implements MessagingExceptionHandler
29  {
30      /**
31       * {@inheritDoc}
32       */
33      public MuleEvent handleException(Exception e, MuleEvent event)
34      {
35          if (enableNotifications)
36          {
37              fireNotification(new ExceptionNotification(e));
38          }
39  
40          logException(e);
41          doHandleException(e, event);
42          closeStream(event.getMessage());
43  
44          event.getMessage().setPayload(NullPayload.getInstance());
45          ExceptionPayload exceptionPayload = new DefaultExceptionPayload(e);
46          event.getMessage().setExceptionPayload(exceptionPayload);
47          if (RequestContext.getEvent() != null)
48          {
49              RequestContext.setExceptionPayload(exceptionPayload);
50          }
51          return event;
52      }
53  
54      protected void doHandleException(Exception ex, MuleEvent event)
55      {
56          // Left this here for backwards-compatibility, remove in the next major version.
57          defaultHandler(ex);
58  
59          MessageProcessor target = null;
60          if (ex instanceof RoutingException)
61          {
62              target = ((RoutingException) ex).getRoute();
63          }
64          if (isRollback(ex))
65          {
66              rollback();
67  
68              logger.debug("Routing exception message");
69              routeException(event, target, ex);
70          }
71          else
72          {
73              logger.debug("Routing exception message");
74              routeException(event, target, ex);
75  
76              logger.debug("Committing transaction");
77              commit();
78          }
79      }
80  
81      protected void commit()
82      {
83          Transaction tx = TransactionCoordination.getInstance().getTransaction();
84          if (tx != null)
85          {
86              try
87              {
88                  tx.commit();
89              }
90              catch (TransactionException e)
91              {
92                  logger.error(e);
93              }
94          }
95      }
96  
97      protected void rollback()
98      {
99          Transaction tx = TransactionCoordination.getInstance().getTransaction();
100         if (tx != null)
101         {
102             try
103             {
104                 tx.rollback();
105             }
106             catch (TransactionException e)
107             {
108                 logger.error(e);
109             }
110         }
111     }
112     
113     /**
114      * @deprecated Override doHandleException(Exception e, MuleEvent event) instead 
115      */
116     // Left this here for backwards-compatibility, remove in the next major version.
117     protected void defaultHandler(Throwable t)   
118     {
119         // empty
120     }
121 }