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