View Javadoc

1   /*
2    * $Id: InboundExceptionDetailsMessageProcessor.java 19191 2010-08-25 21:05:23Z 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.endpoint.inbound;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleException;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.processor.MessageProcessor;
17  import org.mule.api.transport.Connector;
18  import org.mule.config.ExceptionHelper;
19  import org.mule.util.ObjectUtils;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /**
25   * Sets error message properties as specified by the transport based on the exception
26   * type of the exception payload. This mechanism uses a transport properties file in
27   * the META-INF/services/org/mule/config directory called
28   * mule-exception-codes.properties. This property file maps the fully qualified class
29   * names of exceptions to the value of the property that should be set. The name of
30   * the property is defined by the error.code.property property in the same properties
31   * file.
32   */
33  public class InboundExceptionDetailsMessageProcessor implements MessageProcessor
34  {
35  
36      private static final Log logger = LogFactory.getLog(InboundExceptionDetailsMessageProcessor.class);
37  
38      protected Connector connector;
39  
40      public InboundExceptionDetailsMessageProcessor(Connector connector)
41      {
42          this.connector = connector;
43      }
44  
45      public MuleEvent process(MuleEvent event) throws MuleException
46      {
47          if (event != null)
48          {
49              MuleMessage resultMessage = event.getMessage();
50              if (resultMessage != null)
51              {
52                  if (resultMessage.getExceptionPayload() != null)
53                  {
54                      setExceptionDetails(resultMessage, connector, resultMessage.getExceptionPayload()
55                          .getException());
56                  }
57              }
58          }
59          return event;
60      }
61  
62      /**
63       * This method is used to set any additional and possibly transport specific
64       * information on the return message where it has an exception payload.
65       * 
66       * @param message
67       * @param exception
68       */
69      protected void setExceptionDetails(MuleMessage message, Connector connector, Throwable exception)
70      {
71          String propName = ExceptionHelper.getErrorCodePropertyName(connector.getProtocol());
72          // If we dont find a error code property we can assume there are not
73          // error code mappings for this connector
74          if (propName != null)
75          {
76              String code = ExceptionHelper.getErrorMapping(connector.getProtocol(), exception.getClass());
77              if (logger.isDebugEnabled())
78              {
79                  logger.debug("Setting error code for: " + connector.getProtocol() + ", " + propName + "="
80                               + code);
81              }
82              message.setOutboundProperty(propName, code);
83          }
84      }
85  
86      @Override
87      public String toString()
88      {
89          return ObjectUtils.toString(this);
90      }
91  }