View Javadoc

1   /*
2    * $Id: DefaultSystemExceptionStrategy.java 20358 2010-11-26 20:15:18Z tcarlson $
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.RequestContext;
14  import org.mule.api.MuleContext;
15  import org.mule.api.exception.SystemExceptionHandler;
16  import org.mule.api.transport.Connectable;
17  import org.mule.context.notification.ExceptionNotification;
18  import org.mule.message.DefaultExceptionPayload;
19  import org.mule.transport.ConnectException;
20  
21  import java.lang.reflect.InvocationTargetException;
22  
23  /**
24   * Log exception, fire a notification, and clean up transaction if any.
25   */
26  public class DefaultSystemExceptionStrategy extends AbstractExceptionListener implements SystemExceptionHandler
27  {
28      /** 
29       * For IoC only 
30       * @deprecated Use DefaultSystemExceptionStrategy(MuleContext muleContext) instead 
31       */
32      public DefaultSystemExceptionStrategy()
33      {
34          super();
35      }
36      
37      public DefaultSystemExceptionStrategy(MuleContext muleContext)
38      {
39          super();
40          setMuleContext(muleContext);
41      }
42  
43      public void handleException(Exception e)
44      {
45          Connectable connectable = null;
46  
47          // unwrap any exception caused by using reflection apis, but only the top layer
48          if (e instanceof InvocationTargetException)
49          {
50              Throwable t = e.getCause();
51              // just because API accepts Exception, not Throwable :\
52              e = t instanceof Exception ? (Exception) t : new Exception(t);
53          }
54  
55          if (enableNotifications)
56          {
57              fireNotification(new ExceptionNotification(e));
58          }
59  
60          if (e instanceof ConnectException)
61          {
62              logger.info("Exception caught is a ConnectException, attempting to reconnect...");
63              connectable = ((ConnectException) e).getFailed();
64              try
65              {
66                  logger.debug("Disconnecting " + connectable.getClass().getName());
67                  connectable.disconnect();
68              }
69              catch (Exception e1)
70              {
71                  logger.error(e1.getMessage());
72              }
73          }
74  
75          logException(e);
76          
77          handleTransaction(e);
78  
79          if (RequestContext.getEvent() != null)
80          {
81              RequestContext.setExceptionPayload(new DefaultExceptionPayload(e));
82          }
83          
84          if (connectable != null)
85          {
86              // Reconnect (retry policy will go into effect here if configured)
87              try
88              {
89                  logger.debug("Reconnecting " + connectable.getClass().getName());
90                  connectable.connect();
91              }
92              catch (Exception e2)
93              {
94                  logger.error(e2.getMessage());
95              }
96          }
97      }
98  }