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