View Javadoc

1   /*
2    * $Id: ExceptionMessage.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.message;
12  
13  import org.mule.RequestContext;
14  import org.mule.api.MuleEventContext;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.endpoint.EndpointURI;
17  
18  import java.io.Serializable;
19  import java.util.Date;
20  import java.util.Iterator;
21  
22  /**
23   * <code>ExceptionMessage</code> is used by the DefaultServiceExceptionStrategy
24   * for wrapping an exception with a message to send via an endpointUri.
25   */
26  public class ExceptionMessage extends BaseMessageDTO
27  {
28      /**
29       * Serial version
30       */
31      private static final long serialVersionUID = -538516243574950621L;
32  
33      private Throwable exception;
34      private String componentName;
35      private String endpointUri;
36      private Date timeStamp;
37  
38      public ExceptionMessage(Serializable message,
39                              Throwable exception,
40                              String componentName,
41                              EndpointURI endpointUri)
42      {
43          super(message);
44          this.exception = exception;
45          timeStamp = new Date();
46          this.componentName = componentName;
47          this.endpointUri = endpointUri.toString();
48  
49          MuleEventContext ctx = RequestContext.getEventContext();
50          if (ctx != null)
51          {
52              MuleMessage msg = ctx.getMessage();
53              for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext();)
54              {
55                  String propertyKey = (String) iterator.next();
56                  setProperty(propertyKey, msg.getProperty(propertyKey));
57              }
58          }
59      }
60  
61      /**
62       * @deprecated Use ExceptionMessage(Serializable message...) instead
63       */
64      public ExceptionMessage(Object message,
65                              Throwable exception,
66                              String componentName,
67                              EndpointURI endpointUri)
68      {
69          super(getAsSerializable(message));
70          this.exception = exception;
71          timeStamp = new Date();
72          this.componentName = componentName;
73          this.endpointUri = endpointUri.toString();
74  
75          MuleEventContext ctx = RequestContext.getEventContext();
76          if (ctx != null)
77          {
78              MuleMessage msg = ctx.getMessage();
79              for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext();)
80              {
81                  String propertyKey = (String) iterator.next();
82                  setProperty(propertyKey, msg.getProperty(propertyKey));
83              }
84          }
85      }
86  
87      protected static Serializable getAsSerializable(Object message)
88      {
89          if (message instanceof Serializable)
90          {
91              return (Serializable) message;
92          }
93          else
94          {
95              return message.toString();
96          }
97      }
98  
99      public String getComponentName()
100     {
101         return componentName;
102     }
103 
104     public String getEndpoint()
105     {
106         return endpointUri;
107     }
108 
109     public Date getTimeStamp()
110     {
111         return timeStamp;
112     }
113 
114     public Throwable getException()
115     {
116         return exception;
117     }
118 
119     public String toString()
120     {
121         return "ExceptionMessage{" + "payload=" + getPayload() + ", context=" + properties + "exception=" + exception
122                 + ", componentName='" + componentName + "'" + ", endpointUri=" + endpointUri + ", timeStamp="
123                 + timeStamp + "}";
124     }
125 }