1 19 20 package com.sslexplorer.core; 21 22 import java.text.SimpleDateFormat ; 23 import java.util.Calendar ; 24 import java.util.EventObject ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 import java.util.TimeZone ; 28 import java.util.TreeMap ; 29 30 import com.sslexplorer.boot.Util; 31 import com.sslexplorer.security.SessionInfo; 32 33 67 public class CoreEvent extends EventObject { 68 69 72 public static final int STATE_SUCCESSFUL = 0; 73 74 77 public static final int STATE_UNSUCCESSFUL = 1; 78 79 81 private int id; 82 private Object parameter; 83 private SessionInfo session; 84 private long time; 85 private int state; 86 private TreeMap eventAttributes; 87 88 97 public CoreEvent(Object source, int id, Object parameter, SessionInfo session, int state) { 98 super(source); 99 this.id = id; 100 this.parameter = parameter; 101 this.session = session; 102 this.state = state; 103 time = System.currentTimeMillis(); 104 eventAttributes = new TreeMap (); 105 } 106 107 117 public CoreEvent(Object source, int id, Object parameter, SessionInfo session, Throwable exception) { 118 this(source, id, parameter, session, STATE_UNSUCCESSFUL); 119 addAttribute(CoreAttributeConstants.EVENT_ATTR_EXCEPTION_MESSAGE, 120 Util.getExceptionMessageChain(exception)); 121 } 122 123 131 public CoreEvent(Object source, int id, Object parameter, SessionInfo session) { 132 this(source, id, parameter, session, STATE_SUCCESSFUL); 133 } 134 135 138 public String toString() { 139 142 Calendar cal = Calendar.getInstance(TimeZone.getDefault()); 143 String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; 144 SimpleDateFormat sdf = new SimpleDateFormat (DATE_FORMAT); 145 sdf.setTimeZone(TimeZone.getDefault()); 146 147 150 StringBuffer buff = new StringBuffer (); 151 152 155 buff.append("Date : "); 156 buff.append(sdf.format(cal.getTime())); 157 buff.append(" Time : "); 158 buff.append(getTime()); 159 buff.append(" User : "); 160 164 if (getSessionInfo() != null) { 165 buff.append(session.getUser().getPrincipalName()); 166 } else { 167 buff.append("System"); 168 } 169 buff.append(" Event ID: "); 170 buff.append(getId()); 171 buff.append(" State : "); 172 buff.append(getState()); 173 buff.append(" Source : "); 174 if (getSource() != null) { 175 buff.append(getSource()); 176 } else { 177 buff.append("No Source Details"); 178 } 179 180 for (Iterator it = eventAttributes.keySet().iterator(); it.hasNext();) { 181 String parameter = (String ) it.next(); 182 String value = (String ) eventAttributes.get(parameter); 183 buff.append(" Key : "); 184 buff.append(parameter); 185 buff.append(" Value : "); 186 buff.append(value); 187 } 188 189 return buff.toString(); 190 } 191 192 199 public CoreEvent addAttribute(String key, String value) { 200 eventAttributes.put(key, value); 201 return this; 202 } 203 204 209 public void removeAttribute(String key) { 210 eventAttributes.remove(key); 211 } 212 213 218 public Set keySet() { 219 return eventAttributes.keySet(); 220 } 221 222 230 public String getAttribute(String key, String defaultValue) { 231 String val = (String )eventAttributes.get(key); 232 return val == null ? defaultValue : val; 233 } 234 235 241 public SessionInfo getSessionInfo() { 242 return session; 243 } 244 245 250 public Object getParameter() { 251 return parameter; 252 } 253 254 259 public int getId() { 260 return id; 261 } 262 263 268 public long getTime() { 269 return time; 270 } 271 272 278 public int getState() { 279 return state; 280 } 281 282 287 public int getAttributeCount() { 288 return eventAttributes.size(); 289 } 290 291 } 292 | Popular Tags |