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