1
2
3
4
5
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
28
29
30 public class ExceptionMessage implements Serializable
31 {
32
33
34
35 private static final long serialVersionUID = -538516243574950621L;
36
37 private static final Log logger = LogFactory.getLog(ExceptionMessage.class);
38
39
40 private transient Object payload;
41
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
102 }
103 try
104 {
105 payload = in.readObject();
106 }
107 catch (Exception e)
108 {
109
110 }
111 }
112
113 public void setPayload(Object payload)
114 {
115 this.payload = payload;
116 }
117
118
119
120
121 public Object getPayload()
122 {
123 return payload;
124 }
125
126
127
128
129
130
131
132 public void addProperties(Map<String, Object> properties)
133 {
134 this.properties.putAll(properties);
135 }
136
137
138
139
140 public void clearProperties()
141 {
142 properties.clear();
143 }
144
145
146
147
148
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 }