View Javadoc

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