1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
33 | |
|
34 | |
|
35 | |
public class ExceptionMessage implements Serializable |
36 | |
{ |
37 | |
|
38 | |
|
39 | |
|
40 | |
private static final long serialVersionUID = -538516243574950621L; |
41 | |
|
42 | 0 | private static final Log logger = LogFactory.getLog(ExceptionMessage.class); |
43 | |
|
44 | |
|
45 | |
private transient Object payload; |
46 | |
|
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 | 0 | { |
59 | 0 | this.payload = payload; |
60 | 0 | properties = new HashMap<String, Object>(); |
61 | 0 | this.exception = exception; |
62 | 0 | timeStamp = new Date(); |
63 | 0 | this.componentName = componentName; |
64 | 0 | if (endpointUri != null) |
65 | |
{ |
66 | 0 | this.endpointUri = endpointUri.toString(); |
67 | |
} |
68 | |
|
69 | 0 | MuleEventContext ctx = RequestContext.getEventContext(); |
70 | 0 | if (ctx != null) |
71 | |
{ |
72 | 0 | MuleMessage msg = ctx.getMessage(); |
73 | 0 | for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext();) |
74 | |
{ |
75 | 0 | String propertyKey = (String) iterator.next(); |
76 | 0 | setProperty(propertyKey, msg.getProperty(propertyKey)); |
77 | 0 | } |
78 | |
} |
79 | 0 | } |
80 | |
|
81 | |
private void writeObject(ObjectOutputStream out) throws IOException |
82 | |
{ |
83 | 0 | out.defaultWriteObject(); |
84 | |
try |
85 | |
{ |
86 | 0 | out.writeObject(exception); |
87 | |
} |
88 | 0 | catch (NotSerializableException e) |
89 | |
{ |
90 | 0 | logger.warn("Exception " + exception.getClass().getName() + " is not serializable and will be lost when sending ExceptionMessage over the wire: " + e.getMessage()); |
91 | 0 | } |
92 | |
try |
93 | |
{ |
94 | 0 | out.writeObject(payload); |
95 | |
} |
96 | 0 | catch (NotSerializableException e) |
97 | |
{ |
98 | 0 | logger.warn("Payload " + payload.getClass().getName() + " is not serializable and will be lost when sending ExceptionMessage over the wire: " + e.getMessage()); |
99 | 0 | } |
100 | 0 | } |
101 | |
|
102 | |
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException |
103 | |
{ |
104 | 0 | in.defaultReadObject(); |
105 | |
try |
106 | |
{ |
107 | 0 | exception = (Throwable) in.readObject(); |
108 | |
} |
109 | 0 | catch (Exception e) |
110 | |
{ |
111 | |
|
112 | 0 | } |
113 | |
try |
114 | |
{ |
115 | 0 | payload = in.readObject(); |
116 | |
} |
117 | 0 | catch (Exception e) |
118 | |
{ |
119 | |
|
120 | 0 | } |
121 | 0 | } |
122 | |
|
123 | |
public void setPayload(Object payload) |
124 | |
{ |
125 | 0 | this.payload = payload; |
126 | 0 | } |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public Object getPayload() |
132 | |
{ |
133 | 0 | return payload; |
134 | |
} |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
public void addProperties(Map<String, Object> properties) |
143 | |
{ |
144 | 0 | this.properties.putAll(properties); |
145 | 0 | } |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
public void clearProperties() |
151 | |
{ |
152 | 0 | properties.clear(); |
153 | 0 | } |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public Map getProperties() |
161 | |
{ |
162 | 0 | return properties; |
163 | |
} |
164 | |
|
165 | |
public void setProperty(String key, Object value) |
166 | |
{ |
167 | 0 | properties.put(key, value); |
168 | 0 | } |
169 | |
|
170 | |
public Object getProperty(String key) |
171 | |
{ |
172 | 0 | return properties.get(key); |
173 | |
} |
174 | |
|
175 | |
public String getComponentName() |
176 | |
{ |
177 | 0 | return componentName; |
178 | |
} |
179 | |
|
180 | |
public String getEndpoint() |
181 | |
{ |
182 | 0 | return endpointUri; |
183 | |
} |
184 | |
|
185 | |
public Date getTimeStamp() |
186 | |
{ |
187 | 0 | return timeStamp; |
188 | |
} |
189 | |
|
190 | |
public Throwable getException() |
191 | |
{ |
192 | 0 | return exception; |
193 | |
} |
194 | |
|
195 | |
@Override |
196 | |
public String toString() |
197 | |
{ |
198 | 0 | return "ExceptionMessage{" + "payload=" + getPayload() + ", context=" + properties + "exception=" + exception |
199 | |
+ ", componentName='" + componentName + "'" + ", endpointUri=" + endpointUri + ", timeStamp=" |
200 | |
+ timeStamp + "}"; |
201 | |
} |
202 | |
} |