KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > event > DefaultBaseEvent


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: DefaultBaseEvent.java,v 1.15 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.event;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.enhydra.barracuda.plankton.data.*;
26 import org.enhydra.barracuda.plankton.http.*;
27
28 /**
29  * <p>This is the default implementation for the BaseEvent
30  * interface. It acts as the base class in the event heirarchy.
31  *
32  * <p>While events are capable of carrying state information,
33  * generally you want to keep them as lite as possible, storying
34  * any state either in the dispatch queue or the user's session.
35  *
36  * <p>NOTE: As general good practice, you should try and provide
37  * a no-args public constructor for all event classes. Anything
38  * that implements Polymorphic or Exceptional MUST have a noargs
39  * constructor, or the system will not be able to instantiate it.
40  */

41 public abstract class DefaultBaseEvent implements BaseEvent {
42     //constants
43
public static boolean USE_ID_ALIASES = true; //use id aliases (default setting)?
44

45     //private vars
46
protected Object JavaDoc source = null; //the source of the event
47
protected EventObject sourceEvent = null; //the source event for this event (if applicable)
48
protected String JavaDoc ext = ".event"; //default event extension
49
protected boolean handled = false; //has this event been handled
50
protected List idList = null; //list of IDs this event is specifically targeted towards
51
protected long timestamp = -1; //last time this event was touched
52
protected boolean useIDAliases = USE_ID_ALIASES;
53     protected StateMap statemap = new DefaultStateMap(); //private property map
54
protected Map params = null; //private parameter map //jbh_112202.1
55

56     /**
57      * Default noargs constructor
58      */

59     public DefaultBaseEvent() {super();}
60
61     /**
62      * Public constructor. Automatically sets parameters associated
63      * with the event with a URL string of the form "key1=val1&key2=val2&..."
64      * (the param str may be prefixed by a '?')
65      */

66     public DefaultBaseEvent(String JavaDoc urlParamStr) {
67         Map paramMap = HttpConverter.cvtURLStringToMap(urlParamStr);
68         Iterator it = paramMap.entrySet().iterator();
69         while (it.hasNext()) {
70             Map.Entry me = (Map.Entry) it.next();
71             this.setParam((String JavaDoc) me.getKey(), (String JavaDoc) me.getValue());
72         }
73     }
74     
75     /**
76      * Public constructor. Automatically sets the source parameter.
77      * If you do not use this method you should manually set the
78      * source before dispatching the event.
79      */

80     public DefaultBaseEvent(Object JavaDoc source) {
81         setSource(source);
82     }
83
84     /**
85      * set the source for an event
86      *
87      * @param isource the source for this event
88      */

89     public void setSource(Object JavaDoc isource) {
90         source = isource;
91     }
92     
93     /**
94      * get the event source (may be null)
95      *
96      * @return the source for this event
97      */

98     public Object JavaDoc getSource() {
99         return source;
100     }
101     
102     /**
103      * get the root event source (may be null)
104      *
105      * @return the root source for this event
106      */

107     public BaseEvent getRootEvent() {
108         if (source!=null && source instanceof BaseEvent) return ((BaseEvent) source).getRootEvent();
109         else return this;
110     }
111     
112     /**
113      * set the event extension
114      *
115      * @param iext the target event extension
116      */

117     public void setEventExtension(String JavaDoc iext) {
118         ext = iext;
119     }
120     
121     /**
122      * get the event extension
123      *
124      * @return the target event extension
125      */

126     public String JavaDoc getEventExtension() {
127         return ext;
128     }
129
130     /**
131      * determine whether or not we are using ID aliasing. This
132      * can be used to override the default USE_ID_ALIASES
133      * setting.
134      *
135      * @param val true if we want to use ID aliasing
136      */

137     public void setUseIDAliases(boolean val) {
138         useIDAliases = val;
139     }
140     
141     /**
142      * see whether or not we are using ID aliasing
143      *
144      * @return true if we want to use ID aliasing
145      */

146     public boolean useIDAliases() {
147         return useIDAliases;
148     }
149     
150     /**
151      * mark the event as handled/unhandled
152      *
153      * @param val true if the event is handled
154      */

155     public void setHandled(boolean val) {
156         handled = val;
157     }
158     
159     /**
160      * get the handled status for the event
161      *
162      * @return true if the event is handled
163      */

164     public boolean isHandled() {
165         return handled;
166     }
167
168     /**
169      * Add a specific listener id this event should be delivered to.
170      * Events can be targeted to more than one ID.
171      *
172      * @param id the Listener ID the event should target
173      */

174     public void addListenerID(String JavaDoc id) {
175         if (idList==null) idList = new ArrayList();
176         idList.add(id);
177     }
178
179     /**
180      * Get the list of id's this event is specifically targeted for.
181      * May return null if there are none.
182      *
183      * @return a List of ID's this event is specifically targeting
184      */

185     public List getListenerIDs() {
186         return idList;
187     }
188
189     /**
190      * Get the ID that identifies this event. This will typically be the
191      * class name.
192      *
193      * @return a string that uniquely identifies this event
194      */

195     public String JavaDoc getEventID() {
196         String JavaDoc idStr = this.getClass().getName();
197         if (useIDAliases()) {
198             int spos = idStr.lastIndexOf('.');
199             if (spos>-1) idStr = idStr.substring(spos+1);
200         }
201         return idStr;
202     }
203     
204     /**
205      * Get the ID that identifies this event, along with the event extension
206      *
207      * @deprecated csc010404_1; replaced by {@link #getEventURL}
208      */

209     public String JavaDoc getEventIDWithExtension() {
210         return (this.getEventID()+this.getEventExtension());
211     }
212
213     /**
214      * Get the URL version of the event. This method will also include
215      * any params associated with the event.
216      *
217      * @return the id and extension of this event
218      */

219 //csc_010404_1_start
220
public String JavaDoc getEventURL() {
221         String JavaDoc url = this.getEventID()+this.getEventExtension();
222         if (params!=null) url = url + "?" + HttpConverter.cvtMapToURLString(params);
223         return url;
224 //csc_010404_1_end
225
}
226
227     /**
228      * Get the timestamp
229      *
230      * @return the last time this event was touched
231      */

232     public long getTimestamp() {
233         return timestamp;
234     }
235     
236     /**
237      * Update the timestamp on the event
238      */

239     public void touch() {
240         timestamp = System.currentTimeMillis();
241     }
242
243     /**
244      * Reset the event to it's default state
245      */

246     public void reset() {
247         source = null;
248         sourceEvent = null;
249         ext = ".event";
250         handled = false;
251         idList = null;
252         statemap = new DefaultStateMap();
253         touch();
254     }
255     
256     /**
257      * Find the original event in target event's event chain (if it exists)
258      *
259      * @param e the target event
260      * @return the original BaseEvent that caused the target event
261      */

262     public synchronized static BaseEvent getOriginalEvent(BaseEvent e) {
263         Object JavaDoc source = e.getSource();
264         if (source!=null && source instanceof BaseEvent) return getOriginalEvent((BaseEvent) source);
265         else return e;
266     }
267     
268     /**
269      * Events are generally considered equal if they are of the same class.
270      * If you don't want this to be the case for some events (ie. if you
271      * want to get every instance of an event through your dispatcher) then
272      * you should override this method.
273      *
274      * @param o the object we are checking against for equality
275      * @return true if the objects are equal
276      */

277     public boolean equals(Object JavaDoc o) {
278         if (o==null) return false;
279         return (o==this || o.getClass().equals(this.getClass()));
280     }
281     
282     /**
283      * clone the event
284      *
285      * @return a copy of this event
286      * @throws CloneNotSupportedException
287      */

288     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
289         return super.clone();
290     }
291     
292     /**
293      * Get a class ID for a given class. The USE_ID_ALIASES
294      * parameter is taken into account when generating the ID.
295      *
296      * @param cl the target class
297      * @return the class ID for the target class
298      */

299     public static String JavaDoc getClassID(Class JavaDoc cl) {
300         //eliminate the obvious
301
if (cl==null) return null;
302
303         String JavaDoc idStr = cl.getName();
304         if (USE_ID_ALIASES) {
305             int spos = idStr.lastIndexOf('.');
306             if (spos>-1) idStr = idStr.substring(spos+1);
307         }
308         return idStr;
309     }
310     
311     /**
312      * get the RootEvent that caused this event (if any). Will look at
313      * the source object to see if it happens to be an instance of a
314      * BaseEvent, and will recursively work deeper until it finds
315      * the event which caused it all
316      *
317      * @param be a BaseEvent for which we wish to find the root event
318      * @return the root BaseEvent in an event chain (may return null)
319      */

320     public synchronized static BaseEvent getRootEvent (BaseEvent be) {
321         Object JavaDoc source = be.getSource();
322         if (source!=null && source instanceof BaseEvent) return getRootEvent ((BaseEvent) source);
323         return be;
324     }
325     
326     //-------------------- StateMap ------------------------------
327
/**
328      * set a property in this StateMap
329      *
330      * @param key the state key object
331      * @param val the state value object
332      */

333     public void putState(Object JavaDoc key, Object JavaDoc val) {
334         statemap.putState(key,val);
335     }
336     
337     /**
338      * get a property in this StateMap
339      *
340      * @param key the state key object
341      * @return the value for the given key
342      */

343     public Object JavaDoc getState(Object JavaDoc key) {
344         return statemap.getState(key);
345     }
346     
347     /**
348      * remove a property in this StateMap
349      *
350      * @param key the key object
351      * @return the object which was removed
352      */

353     public Object JavaDoc removeState(Object JavaDoc key) {
354         return statemap.removeState(key);
355     }
356     
357     /**
358      * get a list of the keys for this StateMap
359      *
360      * @return a list the keys for this StateMap
361      */

362     public List getStateKeys() {
363         return statemap.getStateKeys();
364     }
365     
366     /**
367      * get a copy of the underlying Map
368      *
369      * @return a copy of the underlying state Map
370      */

371     public Map getStateValues() {
372         return statemap.getStateValues();
373     }
374     
375     //csc_052803_2 - added
376
/**
377      * clear all state information
378      */

379     public void clearState() {
380         statemap.clearState();
381     }
382     
383
384     
385     //jbh_112202.1_start
386
//-------------------- Params --------------------------------
387
/**
388      * Set any associated params
389      */

390     public void setParam(String JavaDoc key, String JavaDoc val) {
391         if (params==null) params = new TreeMap();
392         params.put(key, val);
393     }
394
395     /**
396      * Set any associated params
397      */

398     public void setParam(String JavaDoc key, String JavaDoc[] val) {
399         if (params==null) params = new TreeMap();
400         params.put(key, val);
401     }
402
403     /**
404      * Get any associated params
405      */

406     public Map getParams() {
407         return params;
408     }
409     //jbh_112202.1_end
410

411 }
412
Popular Tags