View Javadoc

1   /**
2    * Copyright (c) 2004-2011 QOS.ch
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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   * Base class for Event Data. Event Data contains data to be logged about an
40   * event. Users may extend this class for each EventType they want to log.
41   * 
42   * @author Ralph Goers
43   * 
44   * @deprecated Due to a security vulnerability, this class will be removed without replacement.
45   */
46  public class EventData implements Serializable {
47  
48      private static final long serialVersionUID = 153270778642103985L;
49  
50      private Map<String, Object> eventData = new HashMap<String, Object>();
51      public static final String EVENT_MESSAGE = "EventMessage";
52      public static final String EVENT_TYPE = "EventType";
53      public static final String EVENT_DATETIME = "EventDateTime";
54      public static final String EVENT_ID = "EventId";
55  
56      /**
57       * Default Constructor
58       */
59      public EventData() {
60      }
61  
62      /**
63       * Constructor to create event data from a Map.
64       * 
65       * @param map
66       *          The event data.
67       */
68      public EventData(Map<String, Object> map) {
69          eventData.putAll(map);
70      }
71  
72      /**
73       * Construct from a serialized form of the Map containing the RequestInfo
74       * elements
75       * 
76       * @param xml
77       *          The serialized form of the RequestInfo Map.
78       */
79      @SuppressWarnings("unchecked")
80      public EventData(String xml) {
81          ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
82          try {
83              XMLDecoder decoder = new XMLDecoder(bais);
84              this.eventData = (Map<String, Object>) decoder.readObject();
85          } catch (Exception e) {
86              throw new EventException("Error decoding " + xml, e);
87          }
88      }
89  
90      /**
91       * Serialize all the EventData items into an XML representation.
92       * 
93       * @return an XML String containing all the EventData items.
94       */
95      public String toXML() {
96          return toXML(eventData);
97      }
98  
99      /**
100      * Serialize all the EventData items into an XML representation.
101      * 
102      * @param map the Map to transform
103      * @return an XML String containing all the EventData items.
104      */
105     public static String toXML(Map<String, Object> map) {
106         ByteArrayOutputStream baos = new ByteArrayOutputStream();
107         try {
108             XMLEncoder encoder = new XMLEncoder(baos);
109             encoder.setExceptionListener(new ExceptionListener() {
110                 public void exceptionThrown(Exception exception) {
111                     exception.printStackTrace();
112                 }
113             });
114             encoder.writeObject(map);
115             encoder.close();
116             return baos.toString();
117         } catch (Exception e) {
118             e.printStackTrace();
119             return null;
120         }
121     }
122 
123     /**
124      * Retrieve the event identifier.
125      * 
126      * @return The event identifier
127      */
128     public String getEventId() {
129         return (String) this.eventData.get(EVENT_ID);
130     }
131 
132     /**
133      * Set the event identifier.
134      * 
135      * @param eventId
136      *          The event identifier.
137      */
138     public void setEventId(String eventId) {
139         if (eventId == null) {
140             throw new IllegalArgumentException("eventId cannot be null");
141         }
142         this.eventData.put(EVENT_ID, eventId);
143     }
144 
145     /**
146      * Retrieve the message text associated with this event, if any.
147      * 
148      * @return The message text associated with this event or null if there is
149      *         none.
150      */
151     public String getMessage() {
152         return (String) this.eventData.get(EVENT_MESSAGE);
153     }
154 
155     /**
156      * Set the message text associated with this event.
157      * 
158      * @param message
159      *          The message text.
160      */
161     public void setMessage(String message) {
162         this.eventData.put(EVENT_MESSAGE, message);
163     }
164 
165     /**
166      * Retrieve the date and time the event occurred.
167      * 
168      * @return The Date associated with the event.
169      */
170     public Date getEventDateTime() {
171         return (Date) this.eventData.get(EVENT_DATETIME);
172     }
173 
174     /**
175      * Set the date and time the event occurred in case it is not the same as when
176      * the event was logged.
177      * 
178      * @param eventDateTime
179      *          The event Date.
180      */
181     public void setEventDateTime(Date eventDateTime) {
182         this.eventData.put(EVENT_DATETIME, eventDateTime);
183     }
184 
185     /**
186      * Set the type of event that occurred.
187      * 
188      * @param eventType
189      *          The type of the event.
190      */
191     public void setEventType(String eventType) {
192         this.eventData.put(EVENT_TYPE, eventType);
193     }
194 
195     /**
196      * Retrieve the type of the event.
197      * 
198      * @return The event type.
199      */
200     public String getEventType() {
201         return (String) this.eventData.get(EVENT_TYPE);
202     }
203 
204     /**
205      * Add arbitrary attributes about the event.
206      * 
207      * @param name
208      *          The attribute's key.
209      * @param obj
210      *          The data associated with the key.
211      */
212     public void put(String name, Serializable obj) {
213         this.eventData.put(name, obj);
214     }
215 
216     /**
217      * Retrieve an event attribute.
218      * 
219      * @param name
220      *          The attribute's key.
221      * @return The value associated with the key or null if the key is not
222      *         present.
223      */
224     public Serializable get(String name) {
225         return (Serializable) this.eventData.get(name);
226     }
227 
228     /**
229      * Populate the event data from a Map.
230      * 
231      * @param data
232      *          The Map to copy.
233      */
234     public void putAll(Map<String, Object> data) {
235         this.eventData.putAll(data);
236     }
237 
238     /**
239      * Returns the number of attributes in the EventData.
240      * 
241      * @return the number of attributes in the EventData.
242      */
243     public int getSize() {
244         return this.eventData.size();
245     }
246 
247     /**
248      * Returns an Iterator over all the entries in the EventData.
249      * 
250      * @return an Iterator that can be used to access all the event attributes.
251      */
252     public Iterator<Map.Entry<String, Object>> getEntrySetIterator() {
253         return this.eventData.entrySet().iterator();
254     }
255 
256     /**
257      * Retrieve all the attributes in the EventData as a Map. Changes to this map
258      * will be reflected in the EventData.
259      * 
260      * @return The Map of attributes in this EventData instance.
261      */
262     public Map<String, Object> getEventMap() {
263         return this.eventData;
264     }
265 
266     /**
267      * Convert the EventData to a String.
268      * 
269      * @return The EventData as a String.
270      */
271     @Override
272     public String toString() {
273         return toXML();
274     }
275 
276     /**
277      * Compare two EventData objects for equality.
278      * 
279      * @param o
280      *          The Object to compare.
281      * @return true if the objects are the same instance or contain all the same
282      *         keys and their values.
283      */
284     @SuppressWarnings("unchecked")
285     @Override
286     public boolean equals(Object o) {
287         if (this == o) {
288             return true;
289         }
290         if (!(o instanceof EventData || o instanceof Map)) {
291             return false;
292         }
293         Map<String, Object> map = (o instanceof EventData) ? ((EventData) o).getEventMap() : (Map<String, Object>) o;
294 
295         return this.eventData.equals(map);
296     }
297 
298     /**
299      * Compute the hashCode for this EventData instance.
300      * 
301      * @return The hashcode for this EventData instance.
302      */
303     @Override
304     public int hashCode() {
305         return this.eventData.hashCode();
306     }
307 }