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 public static String toXML(Map<String, Object> map) {
103 ByteArrayOutputStream baos = new ByteArrayOutputStream();
104 try {
105 XMLEncoder encoder = new XMLEncoder(baos);
106 encoder.setExceptionListener(new ExceptionListener() {
107 public void exceptionThrown(Exception exception) {
108 exception.printStackTrace();
109 }
110 });
111 encoder.writeObject(map);
112 encoder.close();
113 return baos.toString();
114 } catch (Exception e) {
115 e.printStackTrace();
116 return null;
117 }
118 }
119
120
121
122
123
124
125 public String getEventId() {
126 return (String) this.eventData.get(EVENT_ID);
127 }
128
129
130
131
132
133
134
135 public void setEventId(String eventId) {
136 if (eventId == null) {
137 throw new IllegalArgumentException("eventId cannot be null");
138 }
139 this.eventData.put(EVENT_ID, eventId);
140 }
141
142
143
144
145
146
147
148 public String getMessage() {
149 return (String) this.eventData.get(EVENT_MESSAGE);
150 }
151
152
153
154
155
156
157
158 public void setMessage(String message) {
159 this.eventData.put(EVENT_MESSAGE, message);
160 }
161
162
163
164
165
166
167 public Date getEventDateTime() {
168 return (Date) this.eventData.get(EVENT_DATETIME);
169 }
170
171
172
173
174
175
176
177
178 public void setEventDateTime(Date eventDateTime) {
179 this.eventData.put(EVENT_DATETIME, eventDateTime);
180 }
181
182
183
184
185
186
187
188 public void setEventType(String eventType) {
189 this.eventData.put(EVENT_TYPE, eventType);
190 }
191
192
193
194
195
196
197 public String getEventType() {
198 return (String) this.eventData.get(EVENT_TYPE);
199 }
200
201
202
203
204
205
206
207
208
209 public void put(String name, Serializable obj) {
210 this.eventData.put(name, obj);
211 }
212
213
214
215
216
217
218
219
220
221 public Serializable get(String name) {
222 return (Serializable) this.eventData.get(name);
223 }
224
225
226
227
228
229
230
231 public void putAll(Map<String, Object> data) {
232 this.eventData.putAll(data);
233 }
234
235
236
237
238
239
240 public int getSize() {
241 return this.eventData.size();
242 }
243
244
245
246
247
248
249 public Iterator<Map.Entry<String, Object>> getEntrySetIterator() {
250 return this.eventData.entrySet().iterator();
251 }
252
253
254
255
256
257
258
259 public Map<String, Object> getEventMap() {
260 return this.eventData;
261 }
262
263
264
265
266
267
268 @Override
269 public String toString() {
270 return toXML();
271 }
272
273
274
275
276
277
278
279
280
281 @SuppressWarnings("unchecked")
282 @Override
283 public boolean equals(Object o) {
284 if (this == o) {
285 return true;
286 }
287 if (!(o instanceof EventData || o instanceof Map)) {
288 return false;
289 }
290 Map<String, Object> map = (o instanceof EventData) ? ((EventData) o)
291 .getEventMap() : (Map<String, Object>) o;
292
293 return this.eventData.equals(map);
294 }
295
296
297
298
299
300
301 @Override
302 public int hashCode() {
303 return this.eventData.hashCode();
304 }
305 }