View Javadoc

1   package com.atlassian.johnson.event;
2   
3   import org.apache.commons.lang.ObjectUtils;
4   
5   import java.io.PrintWriter;
6   import java.io.StringWriter;
7   import java.text.SimpleDateFormat;
8   import java.util.Date;
9   import java.util.Map;
10  import java.util.HashMap;
11  import java.util.Collections;
12  
13  /**
14   * This class represents an ApplicationEvent
15   */
16  public class Event {
17  
18      private final Map<Object, Object> attributes;
19  
20      private String date;
21      private String desc;
22      private String exception;
23      private EventType key;
24      private EventLevel level;
25      private int progress;
26  
27      public Event(EventType key, String desc) {
28          this(key, desc, null, null);
29      }
30  
31      public Event(EventType key, String desc, String exception) {
32          this(key, desc, exception, null);
33      }
34  
35      public Event(EventType key, String desc, EventLevel level) {
36          this(key, desc, null, level);
37      }
38  
39      public Event(EventType key, String desc, String exception, EventLevel level) {
40          this.desc = desc;
41          this.exception = exception;
42          this.key = key;
43          this.level = level;
44  
45          attributes = new HashMap<Object, Object>();
46          date = getFormattedDate();
47          progress = -1;
48      }
49  
50      public static String toString(Throwable t) {
51          StringWriter string = new StringWriter();
52          PrintWriter writer = new PrintWriter(string);
53  
54          writer.println(t);
55          t.printStackTrace(writer);
56          while ((t = t.getCause()) != null) {
57              writer.println("Caused by: " + t);
58              t.printStackTrace(writer);
59          }
60          writer.flush();
61  
62          return string.toString();
63      }
64  
65      public void addAttribute(Object key, Object value) {
66          attributes.put(key, value);
67      }
68  
69      public Object getAttribute(Object key) {
70          return attributes.get(key);
71      }
72  
73      public Map getAttributes() {
74          return Collections.unmodifiableMap(attributes);
75      }
76  
77      public String getDate() {
78          return date;
79      }
80  
81      public String getDesc() {
82          return desc;
83      }
84  
85      public String getException() {
86          return exception;
87      }
88  
89      public EventType getKey() {
90          return key;
91      }
92  
93      public EventLevel getLevel() {
94          return level;
95      }
96  
97      public int getProgress() {
98          return progress;
99      }
100 
101     public boolean hasProgress() {
102         return (progress != -1);
103     }
104 
105     public void setDate(String date) {
106         this.date = date;
107     }
108 
109     public void setDesc(String desc) {
110         this.desc = desc;
111     }
112 
113     public void setException(String exception) {
114         this.exception = exception;
115     }
116 
117     public void setKey(EventType name) {
118         this.key = name;
119     }
120 
121     public void setLevel(EventLevel level) {
122         this.level = level;
123     }
124 
125     public void setProgress(int progress) {
126         this.progress = progress;
127     }
128 
129     public boolean equals(Object o) {
130         if (this == o) {
131             return true;
132         }
133         if (!(o instanceof Event)) {
134             return false;
135         }
136 
137         Event e = (Event) o;
138         return ObjectUtils.equals(getDate(), e.getDate()) &&
139                 ObjectUtils.equals(getDesc(), e.getDesc()) &&
140                 ObjectUtils.equals(getException(), e.getException()) &&
141                 ObjectUtils.equals(getKey(), e.getKey()) &&
142                 ObjectUtils.equals(getLevel(), e.getLevel());
143     }
144 
145     public int hashCode() {
146         int result = 7;
147         result = 31 * result + ObjectUtils.hashCode(getKey());
148         result = 31 * result + ObjectUtils.hashCode(getDesc());
149         result = 31 * result + ObjectUtils.hashCode(getException());
150         result = 31 * result + ObjectUtils.hashCode(getLevel());
151         result = 31 * result + ObjectUtils.hashCode(getDate());
152         return result;
153     }
154 
155     public String toString() {
156         return "(Event: Level = " + (getLevel() == null ? "" : getLevel() + " ") +
157                 ", Key = " + (getKey() == null ? "" : getKey() + " ") +
158                 ", Desc = " + (getDesc() == null ? "" : getDesc() + " ") +
159                 ", Exception = " + (getException() == null ? "" : getException() +
160                 ")");
161     }
162 
163     private static String getFormattedDate() {
164         return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
165     }
166 }