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      {
29          this(key, desc, null, null);
30      }
31  
32      public Event(EventType key, String desc, String exception)
33      {
34          this(key, desc, exception, null);
35      }
36  
37      public Event(EventType key, String desc, EventLevel level)
38      {
39          this(key, desc, null, level);
40      }
41  
42      public Event(EventType key, String desc, String exception, EventLevel level)
43      {
44          this.desc = desc;
45          this.exception = exception;
46          this.key = key;
47          this.level = level;
48  
49          attributes = new HashMap<Object, Object>();
50          date = getFormattedDate();
51          progress = -1;
52      }
53      
54      public static String toString(Throwable t) {
55          StringWriter string = new StringWriter();
56          PrintWriter writer = new PrintWriter(string);
57  
58          writer.println(t);
59          t.printStackTrace(writer);
60          while ((t = t.getCause()) != null) {
61              writer.println("Caused by: " + t);
62              t.printStackTrace(writer);
63          }
64          writer.flush();
65  
66          return string.toString();
67      }
68  
69      public void addAttribute(Object key, Object value)
70      {
71          attributes.put(key, value);
72      }
73  
74      public Object getAttribute(Object key)
75      {
76          return attributes.get(key);
77      }
78  
79      public Map getAttributes()
80      {
81          return Collections.unmodifiableMap(attributes);
82      }
83  
84      public String getDate()
85      {
86          return date;
87      }
88  
89      public String getDesc()
90      {
91          return desc;
92      }
93  
94      public String getException()
95      {
96          return exception;
97      }
98  
99      public EventType getKey()
100     {
101         return key;
102     }
103 
104     public EventLevel getLevel()
105     {
106         return level;
107     }
108 
109     public int getProgress()
110     {
111         return progress;
112     }
113 
114     public boolean hasProgress()
115     {
116         return (progress != -1);
117     }
118 
119     public void setDate(String date)
120     {
121         this.date = date;
122     }
123 
124     public void setDesc(String desc)
125     {
126         this.desc = desc;
127     }
128 
129     public void setException(String exception)
130     {
131         this.exception = exception;
132     }
133 
134     public void setKey(EventType name)
135     {
136         this.key = name;
137     }
138 
139     public void setLevel(EventLevel level)
140     {
141         this.level = level;
142     }
143 
144     public void setProgress(int progress)
145     {
146         this.progress = progress;
147     }
148 
149     public boolean equals(Object o)
150     {
151         if (this == o)
152         {
153             return true;
154         }
155         if (!(o instanceof Event))
156         {
157             return false;
158         }
159 
160         Event e = (Event) o;
161         return ObjectUtils.equals(getDate(), e.getDate()) &&
162                 ObjectUtils.equals(getDesc(), e.getDesc()) &&
163                 ObjectUtils.equals(getException(), e.getException()) &&
164                 ObjectUtils.equals(getKey(), e.getKey()) &&
165                 ObjectUtils.equals(getLevel(), e.getLevel());
166     }
167 
168     public int hashCode()
169     {
170         int result = 7;
171         result = 31 * result + ObjectUtils.hashCode(getKey());
172         result = 31 * result + ObjectUtils.hashCode(getDesc());
173         result = 31 * result + ObjectUtils.hashCode(getException());
174         result = 31 * result + ObjectUtils.hashCode(getLevel());
175         result = 31 * result + ObjectUtils.hashCode(getDate());
176         return result;
177     }
178 
179     public String toString()
180     {
181         return "(Event: Level = " + (getLevel() == null ? "" : getLevel() + " ") +
182                 ", Key = " + (getKey() == null ? "" : getKey() + " ") +
183                 ", Desc = " + (getDesc() == null ? "" : getDesc() + " ") +
184                 ", Exception = " + (getException() == null ? "" : getException() +
185                 ")");
186     }
187 
188     private static String getFormattedDate()
189     {
190         return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
191     }
192 }