1 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 41 public abstract class DefaultBaseEvent implements BaseEvent { 42 public static boolean USE_ID_ALIASES = true; 45 protected Object source = null; protected EventObject sourceEvent = null; protected String ext = ".event"; protected boolean handled = false; protected List idList = null; protected long timestamp = -1; protected boolean useIDAliases = USE_ID_ALIASES; 53 protected StateMap statemap = new DefaultStateMap(); protected Map params = null; 56 59 public DefaultBaseEvent() {super();} 60 61 66 public DefaultBaseEvent(String 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 ) me.getKey(), (String ) me.getValue()); 72 } 73 } 74 75 80 public DefaultBaseEvent(Object source) { 81 setSource(source); 82 } 83 84 89 public void setSource(Object isource) { 90 source = isource; 91 } 92 93 98 public Object getSource() { 99 return source; 100 } 101 102 107 public BaseEvent getRootEvent() { 108 if (source!=null && source instanceof BaseEvent) return ((BaseEvent) source).getRootEvent(); 109 else return this; 110 } 111 112 117 public void setEventExtension(String iext) { 118 ext = iext; 119 } 120 121 126 public String getEventExtension() { 127 return ext; 128 } 129 130 137 public void setUseIDAliases(boolean val) { 138 useIDAliases = val; 139 } 140 141 146 public boolean useIDAliases() { 147 return useIDAliases; 148 } 149 150 155 public void setHandled(boolean val) { 156 handled = val; 157 } 158 159 164 public boolean isHandled() { 165 return handled; 166 } 167 168 174 public void addListenerID(String id) { 175 if (idList==null) idList = new ArrayList(); 176 idList.add(id); 177 } 178 179 185 public List getListenerIDs() { 186 return idList; 187 } 188 189 195 public String getEventID() { 196 String 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 209 public String getEventIDWithExtension() { 210 return (this.getEventID()+this.getEventExtension()); 211 } 212 213 219 public String getEventURL() { 221 String url = this.getEventID()+this.getEventExtension(); 222 if (params!=null) url = url + "?" + HttpConverter.cvtMapToURLString(params); 223 return url; 224 } 226 227 232 public long getTimestamp() { 233 return timestamp; 234 } 235 236 239 public void touch() { 240 timestamp = System.currentTimeMillis(); 241 } 242 243 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 262 public synchronized static BaseEvent getOriginalEvent(BaseEvent e) { 263 Object source = e.getSource(); 264 if (source!=null && source instanceof BaseEvent) return getOriginalEvent((BaseEvent) source); 265 else return e; 266 } 267 268 277 public boolean equals(Object o) { 278 if (o==null) return false; 279 return (o==this || o.getClass().equals(this.getClass())); 280 } 281 282 288 public Object clone() throws CloneNotSupportedException { 289 return super.clone(); 290 } 291 292 299 public static String getClassID(Class cl) { 300 if (cl==null) return null; 302 303 String 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 320 public synchronized static BaseEvent getRootEvent (BaseEvent be) { 321 Object source = be.getSource(); 322 if (source!=null && source instanceof BaseEvent) return getRootEvent ((BaseEvent) source); 323 return be; 324 } 325 326 333 public void putState(Object key, Object val) { 334 statemap.putState(key,val); 335 } 336 337 343 public Object getState(Object key) { 344 return statemap.getState(key); 345 } 346 347 353 public Object removeState(Object key) { 354 return statemap.removeState(key); 355 } 356 357 362 public List getStateKeys() { 363 return statemap.getStateKeys(); 364 } 365 366 371 public Map getStateValues() { 372 return statemap.getStateValues(); 373 } 374 375 379 public void clearState() { 380 statemap.clearState(); 381 } 382 383 384 385 390 public void setParam(String key, String val) { 391 if (params==null) params = new TreeMap(); 392 params.put(key, val); 393 } 394 395 398 public void setParam(String key, String [] val) { 399 if (params==null) params = new TreeMap(); 400 params.put(key, val); 401 } 402 403 406 public Map getParams() { 407 return params; 408 } 409 411 } 412 | Popular Tags |