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