View Javadoc

1   /*
2    * $Id: ExceptionMessage.java 22439 2011-07-18 19:06:39Z dfeist $
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.api.MuleEvent;
14  import org.mule.api.transport.PropertyScope;
15  
16  import java.io.IOException;
17  import java.io.NotSerializableException;
18  import java.io.ObjectInputStream;
19  import java.io.ObjectOutputStream;
20  import java.io.Serializable;
21  import java.net.URI;
22  import java.util.Date;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * <code>ExceptionMessage</code> is used by the DefaultServiceExceptionStrategy
32   * for wrapping an exception with a message to send via an endpointUri.
33   */
34  public class ExceptionMessage implements Serializable
35  {
36      /**
37       * Serial version
38       */
39      private static final long serialVersionUID = -538516243574950621L;
40  
41      private static final Log logger = LogFactory.getLog(ExceptionMessage.class);
42  
43      // This object uses custom serialization via the writeObject() method
44      private transient Object payload;
45      // This object uses custom serialization via the writeObject() method
46      private transient Throwable exception;
47  
48      protected Map<String, Object> properties;
49      private String componentName;
50      private String endpointUri;
51      private Date timeStamp;
52  
53      public ExceptionMessage(MuleEvent event,
54                              Throwable exception,
55                              String componentName,
56                              URI endpointUri)
57      {
58          this.payload = event.getMessage().getPayload();
59          properties = new HashMap<String, Object>();
60          this.exception = exception;
61          timeStamp = new Date();
62          this.componentName = componentName;
63          if (endpointUri != null)
64          {
65              this.endpointUri = endpointUri.toString();
66          }
67  
68          for (Iterator iterator = event.getMessage().getPropertyNames(PropertyScope.OUTBOUND).iterator(); iterator.hasNext();)
69          {
70              String propertyKey = (String) iterator.next();
71              setProperty(propertyKey, event.getMessage().getProperty(propertyKey, PropertyScope.OUTBOUND));
72          }
73      }
74  
75      private void writeObject(ObjectOutputStream out) throws IOException
76      {
77          out.defaultWriteObject();
78          try
79          {
80              out.writeObject(exception);
81          }
82          catch (NotSerializableException e)
83          {
84              logger.warn("Exception " + exception.getClass().getName() + " is not serializable and will be lost when sending ExceptionMessage over the wire: " + e.getMessage());
85          }
86          try
87          {
88              out.writeObject(payload);
89          }
90          catch (NotSerializableException e)
91          {
92              logger.warn("Payload " + payload.getClass().getName() + " is not serializable and will be lost when sending ExceptionMessage over the wire: " + e.getMessage());
93          }
94      }
95  
96      private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
97      {
98          in.defaultReadObject();
99          try
100         {
101             exception = (Throwable) in.readObject();
102         }
103         catch (Exception e)
104         {
105             // ignore
106         }
107         try
108         {
109             payload = in.readObject();
110         }
111         catch (Exception e)
112         {
113             // ignore
114         }
115     }
116 
117     public void setPayload(Object payload)
118     {
119         this.payload = payload;
120     }
121 
122     /**
123      * @return the current message
124      */
125     public Object getPayload()
126     {
127         return payload;
128     }
129 
130 
131     /**
132      * Adds a map of properties to associated with this message
133      *
134      * @param properties the properties add to this message
135      */
136     public void addProperties(Map<String, Object> properties)
137     {
138         this.properties.putAll(properties);
139     }
140 
141     /**
142      * Removes all properties on this message
143      */
144     public void clearProperties()
145     {
146         properties.clear();
147     }
148 
149     /**
150      * Returns a map of all properties on this message
151      *
152      * @return a map of all properties on this message
153      */
154     public Map getProperties()
155     {
156         return properties;
157     }
158 
159     public void setProperty(String key, Object value)
160     {
161         properties.put(key, value);
162     }
163 
164     public Object getProperty(String key)
165     {
166         return properties.get(key);
167     }
168 
169     public String getComponentName()
170     {
171         return componentName;
172     }
173 
174     public String getEndpoint()
175     {
176         return endpointUri;
177     }
178 
179     public Date getTimeStamp()
180     {
181         return timeStamp;
182     }
183 
184     public Throwable getException()
185     {
186         return exception;
187     }
188 
189     @Override
190     public String toString()
191     {
192         return "ExceptionMessage{" + "payload=" + getPayload() + ", context=" + properties + "exception=" + exception
193                 + ", componentName='" + componentName + "'" + ", endpointUri=" + endpointUri + ", timeStamp="
194                 + timeStamp + "}";
195     }
196 }