1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.slf4j.ext;
24
25 import java.io.Serializable;
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Map;
32 import java.beans.XMLDecoder;
33 import java.beans.XMLEncoder;
34 import java.beans.ExceptionListener;
35
36
37
38
39
40
41
42 public class EventData implements Serializable {
43
44 private static final long serialVersionUID = 153270778642103985L;
45
46 private Map<String, Object> eventData = new HashMap<String, Object>();
47 public static final String EVENT_MESSAGE = "EventMessage";
48 public static final String EVENT_TYPE = "EventType";
49 public static final String EVENT_DATETIME = "EventDateTime";
50 public static final String EVENT_ID = "EventId";
51
52
53
54
55 public EventData() {
56 }
57
58
59
60
61
62
63
64 public EventData(Map<String, Object> map) {
65 eventData.putAll(map);
66 }
67
68
69
70
71
72
73
74
75 @SuppressWarnings("unchecked")
76 public EventData(String xml) {
77 ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
78 try {
79 XMLDecoder decoder = new XMLDecoder(bais);
80 this.eventData = (Map<String, Object>) decoder.readObject();
81 } catch (Exception e) {
82 throw new EventException("Error decoding " + xml, e);
83 }
84 }
85
86
87
88
89
90
91 public String toXML() {
92 return toXML(eventData);
93 }
94
95
96
97
98
99
100 public static String toXML(Map<String, Object> map) {
101 ByteArrayOutputStream baos = new ByteArrayOutputStream();
102 try {
103 XMLEncoder encoder = new XMLEncoder(baos);
104 encoder.setExceptionListener(new ExceptionListener() {
105 public void exceptionThrown(Exception exception) {
106 exception.printStackTrace();
107 }
108 });
109 encoder.writeObject(map);
110 encoder.close();
111 return baos.toString();
112 } catch (Exception e) {
113 e.printStackTrace();
114 return null;
115 }
116 }
117
118
119
120
121
122
123 public String getEventId() {
124 return (String) this.eventData.get(EVENT_ID);
125 }
126
127
128
129
130
131
132
133 public void setEventId(String eventId) {
134 if (eventId == null) {
135 throw new IllegalArgumentException("eventId cannot be null");
136 }
137 this.eventData.put(EVENT_ID, eventId);
138 }
139
140
141
142
143
144
145
146 public String getMessage() {
147 return (String) this.eventData.get(EVENT_MESSAGE);
148 }
149
150
151
152
153
154
155
156 public void setMessage(String message) {
157 this.eventData.put(EVENT_MESSAGE, message);
158 }
159
160
161
162
163
164
165 public Date getEventDateTime() {
166 return (Date) this.eventData.get(EVENT_DATETIME);
167 }
168
169
170
171
172
173
174
175
176 public void setEventDateTime(Date eventDateTime) {
177 this.eventData.put(EVENT_DATETIME, eventDateTime);
178 }
179
180
181
182
183
184
185
186 public void setEventType(String eventType) {
187 this.eventData.put(EVENT_TYPE, eventType);
188 }
189
190
191
192
193
194
195 public String getEventType() {
196 return (String) this.eventData.get(EVENT_TYPE);
197 }
198
199
200
201
202
203
204
205
206
207 public void put(String name, Serializable obj) {
208 this.eventData.put(name, obj);
209 }
210
211
212
213
214
215
216
217
218
219 public Serializable get(String name) {
220 return (Serializable) this.eventData.get(name);
221 }
222
223
224
225
226
227
228
229 public void putAll(Map<String, Object> data) {
230 this.eventData.putAll(data);
231 }
232
233
234
235
236
237
238 public int getSize() {
239 return this.eventData.size();
240 }
241
242
243
244
245
246
247 public Iterator<Map.Entry<String, Object>> getEntrySetIterator() {
248 return this.eventData.entrySet().iterator();
249 }
250
251
252
253
254
255
256
257 public Map<String, Object> getEventMap() {
258 return this.eventData;
259 }
260
261
262
263
264
265
266 @Override
267 public String toString() {
268 return toXML();
269 }
270
271
272
273
274
275
276
277
278
279 @SuppressWarnings("unchecked")
280 @Override
281 public boolean equals(Object o) {
282 if (this == o) {
283 return true;
284 }
285 if (!(o instanceof EventData || o instanceof Map)) {
286 return false;
287 }
288 Map<String, Object> map = (o instanceof EventData) ? ((EventData) o)
289 .getEventMap() : (Map<String, Object>) o;
290
291 return this.eventData.equals(map);
292 }
293
294
295
296
297
298
299 @Override
300 public int hashCode() {
301 return this.eventData.hashCode();
302 }
303 }