KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > CoreEvent


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.core;
21
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Calendar JavaDoc;
24 import java.util.EventObject JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.TimeZone JavaDoc;
28 import java.util.TreeMap JavaDoc;
29
30 import com.sslexplorer.boot.Util;
31 import com.sslexplorer.security.SessionInfo;
32
33 /**
34  * Superclass of all events that may be fired during the life of an
35  * SSL-Explorer.
36  * <p>
37  * All events have the following attributes in common :-
38  * <ul>
39  * <li>Event code. This is an <code>int</code> and must be unique across the
40  * whole of SSL-Explorer and all of its plugins.</li>
41  * <li>Source. An arbitrary object pointing to the source of the event. </li>
42  * <li>Parameter. An arbitrary parameter object appropriate for the type of
43  * event</li>
44  * <li>Session. The session that caused the event, or <code>null</code> if
45  * this is a system event.</li>
46  * <li>State. An integer specifying whether the event is the result of a
47  * successful operation or a failed one.</li>
48  * </ul>
49  * <p>
50  * All events may also contain a map of arbitrary name / value pair attributes
51  * to store any other information that might be of interest to listeners of the
52  * type of event being fired. For example, event event with the code
53  * {@link com.sslexplorer.core.CoreEventConstants#USER_CREATED} would also add
54  * an attribute with the key
55  * {@link com.sslexplorer.core.CoreAttributeConstants#EVENT_ATTR_PRINCIPAL_ID}
56  * and a value of the username being created.
57  * <p>
58  * All events also have a time attribute that is set when the object is
59  * instantiated.
60  *
61  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
62  * @see com.sslexplorer.core.CoreListener
63  * @see com.sslexplorer.core.CoreServlet
64  * @see com.sslexplorer.core.CoreEventConstants
65  * @see com.sslexplorer.core.CoreAttributeConstants
66  */

67 public class CoreEvent extends EventObject JavaDoc {
68
69     /**
70      * Successful event
71      */

72     public static final int STATE_SUCCESSFUL = 0;
73     
74     /**
75      * Failed event
76      */

77     public static final int STATE_UNSUCCESSFUL = 1;
78
79     // Private instance variables
80

81     private int id;
82     private Object JavaDoc parameter;
83     private SessionInfo session;
84     private long time;
85     private int state;
86     private TreeMap JavaDoc eventAttributes;
87
88     /**
89      * Constructor.
90      *
91      * @param source source of event
92      * @param id event code
93      * @param parameter arbitrary parameter
94      * @param session session that caused event or <code>null</code> for system event
95      * @param state state. May be one of {@link #STATE_SUCCESSFUL} or {@link #STATE_UNSUCCESSFUL}.
96      */

97     public CoreEvent(Object JavaDoc source, int id, Object JavaDoc 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 JavaDoc();
105     }
106
107     /**
108      * Constructor for {@link #STATE_UNSUCCESSFUL}.
109      *
110      * @param source source of event
111      * @param id event code
112      * @param parameter arbitrary parameter
113      * @param session session that caused event or <code>null</code> for system event
114      * @param exception exception
115      *
116      */

117     public CoreEvent(Object JavaDoc source, int id, Object JavaDoc parameter, SessionInfo session, Throwable JavaDoc exception) {
118         this(source, id, parameter, session, STATE_UNSUCCESSFUL);
119         addAttribute(CoreAttributeConstants.EVENT_ATTR_EXCEPTION_MESSAGE,
120             Util.getExceptionMessageChain(exception));
121     }
122
123     /**
124      * Constructor for a successful event.
125      *
126      * @param source source of event
127      * @param id event code
128      * @param parameter arbitrary parameter
129      * @param session session that caused event or <code>null</code> for system event
130      */

131     public CoreEvent(Object JavaDoc source, int id, Object JavaDoc parameter, SessionInfo session) {
132         this(source, id, parameter, session, STATE_SUCCESSFUL);
133     }
134
135     /* (non-Javadoc)
136      * @see java.lang.Object#toString()
137      */

138     public String JavaDoc toString() {
139         /*
140          * Set up variables required for the use of SimpleDateFormat.
141          */

142         Calendar JavaDoc cal = Calendar.getInstance(TimeZone.getDefault());
143         String JavaDoc DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
144         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(DATE_FORMAT);
145         sdf.setTimeZone(TimeZone.getDefault());
146
147         /*
148          * Set up a StringBuffer to hold the event message.
149          */

150         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
151
152         /*
153          * Build the event message.
154          */

155         buff.append("Date : ");
156         buff.append(sdf.format(cal.getTime()));
157         buff.append(" Time : ");
158         buff.append(getTime());
159         buff.append(" User : ");
160         /*
161          * SessionInfo can sometimes be null. If this is the case a default
162          * value of "System" is always used.
163          */

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 JavaDoc it = eventAttributes.keySet().iterator(); it.hasNext();) {
181             String JavaDoc parameter = (String JavaDoc) it.next();
182             String JavaDoc value = (String JavaDoc) 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     /**
193      * Add an attribute to the event
194      *
195      * @param key key of attribute
196      * @param value value of attribute
197      * @return this object to allow event attribute chains
198      */

199     public CoreEvent addAttribute(String JavaDoc key, String JavaDoc value) {
200         eventAttributes.put(key, value);
201         return this;
202     }
203
204     /**
205      * Remove an attribute given its key
206      *
207      * @param key key of attribute
208      */

209     public void removeAttribute(String JavaDoc key) {
210         eventAttributes.remove(key);
211     }
212
213     /**
214      * Get a {@link Set} of all the event attribute keys
215      *
216      * @return set of attribute keys
217      */

218     public Set JavaDoc keySet() {
219         return eventAttributes.keySet();
220     }
221
222     /**
223      * Get the value of an event attribute or return the supplied if no
224      * such attribute exists.
225      *
226      * @param key key of event
227      * @param defaultValue default value
228      * @return value
229      */

230     public String JavaDoc getAttribute(String JavaDoc key, String JavaDoc defaultValue) {
231         String JavaDoc val = (String JavaDoc)eventAttributes.get(key);
232         return val == null ? defaultValue : val;
233     }
234
235     /**
236      * Get the session that caused this event or <code>null</code> if
237      * this is a system event.
238      *
239      * @return session that cause event or <code>null</code> if system event
240      */

241     public SessionInfo getSessionInfo() {
242         return session;
243     }
244
245     /**
246      * Get the arbitrary parameter object
247      *
248      * @return arbitrary parameter object
249      */

250     public Object JavaDoc getParameter() {
251         return parameter;
252     }
253
254     /**
255      * Get the unique event id
256      *
257      * @return unique event id
258      */

259     public int getId() {
260         return id;
261     }
262
263     /**
264      * Get the time this event object was created
265      *
266      * @return time this event object was created
267      */

268     public long getTime() {
269         return time;
270     }
271
272     /**
273      * Get the event state. May be one of {@link #STATE_SUCCESSFUL}
274      * or {@link #STATE_UNSUCCESSFUL}.
275      *
276      * @return event state
277      */

278     public int getState() {
279         return state;
280     }
281
282     /**
283      * Get the number of attributes this event has
284      *
285      * @return attribute count
286      */

287     public int getAttributeCount() {
288         return eventAttributes.size();
289     }
290
291 }
292
Popular Tags