View Javadoc

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  public abstract class AbstractMessagingExceptionStrategy extends AbstractExceptionListener implements MessagingExceptionHandler
33  {
34      /**
35       * {@inheritDoc}
36       */
37      public MuleEvent handleException(Exception e, MuleEvent event)
38      {
39          if (enableNotifications)
40          {
41              fireNotification(new ExceptionNotification(e));
42          }
43  
44          logException(e);
45          handleTransaction(e);
46  
47          Throwable t = getExceptionType(e, RoutingException.class);
48          if (t != null)
49          {
50              RoutingException re = (RoutingException) t;
51              handleRoutingException(re.getMuleMessage(), re.getRoute(), e);
52  
53              event.getMessage().setPayload(NullPayload.getInstance());
54              event.getMessage().setExceptionPayload(new DefaultExceptionPayload(e));
55              return event;
56          }
57  
58          t = getExceptionType(e, MessagingException.class);
59          if (t != null)
60          {
61              MessagingException me = (MessagingException) t;
62              handleMessagingException(me.getMuleMessage(), e);
63  
64              event.getMessage().setPayload(NullPayload.getInstance());
65              event.getMessage().setExceptionPayload(new DefaultExceptionPayload(e));
66              return event;
67          }
68  
69          t = getExceptionType(e, LifecycleException.class);
70          if (t != null)
71          {
72              LifecycleException le = (LifecycleException) t;
73              handleLifecycleException(le.getComponent(), e);
74              event.getMessage().setPayload(NullPayload.getInstance());
75              event.getMessage().setExceptionPayload(new DefaultExceptionPayload(e));
76              return event;
77          }
78  
79          handleStandardException(e);
80          event.getMessage().setPayload(NullPayload.getInstance());
81          event.getMessage().setExceptionPayload(new DefaultExceptionPayload(e));
82          return event;
83      }
84  
85      public void handleMessagingException(MuleMessage message, Throwable t)
86      {
87          defaultHandler(t);
88          routeException(messageFromContextIfAvailable(message), null, t);
89      }
90  
91      public void handleRoutingException(MuleMessage message, MessageProcessor target, Throwable t)
92      {
93          defaultHandler(t);
94          routeException(messageFromContextIfAvailable(message), target, t);
95      }
96  
97      public void handleLifecycleException(Object component, Throwable t)
98      {
99          // Do nothing special here. Overriding implmentations may want alter the
100         // behaviour
101         handleStandardException(t);
102         logger.error("The object that failed was: \n" + ObjectUtils.toString(component, "null"));
103     }
104 
105     public void handleStandardException(Throwable t)
106     {
107         // Attempt to send the exception details to an endpoint if one is set
108         if (RequestContext.getEventContext() != null)
109         {
110             handleMessagingException(RequestContext.getEventContext().getMessage(), t);
111         }
112         else
113         {
114             logger.info("There is no current event available, routing Null message with the exception");
115             handleMessagingException(new DefaultMuleMessage(NullPayload.getInstance(), muleContext), t);
116         }
117     }
118 
119     protected void defaultHandler(Throwable t)
120     {
121         if (RequestContext.getEvent() != null)
122         {
123             RequestContext.setExceptionPayload(new DefaultExceptionPayload(t));
124         }
125     }
126 
127     protected MuleMessage messageFromContextIfAvailable(MuleMessage message)
128     {
129         if (null != RequestContext.getEvent())
130         {
131             return RequestContext.getEvent().getMessage();
132         }
133         else
134         {
135             return message;
136         }
137     }
138 
139 }