1 20 package org.enhydra.barracuda.core.event; 21 22 import java.io.*; 23 import java.util.*; 24 import javax.servlet.http.*; 25 26 import org.apache.log4j.*; 27 28 import org.enhydra.barracuda.core.event.events.*; 29 import org.enhydra.barracuda.core.helper.servlet.*; 30 import org.enhydra.barracuda.plankton.data.*; 31 import org.enhydra.barracuda.plankton.http.*; 32 33 72 public class DefaultEventBroker implements EventBroker { 73 74 protected static final Logger logger = Logger.getLogger(DefaultEventBroker.class.getName()); 76 77 protected Map idMap = new HashMap(); 79 protected Map eventMap = new HashMap(); 80 protected Map idXref = new HashMap(); 81 protected Map eventXref = new HashMap(); 82 protected String extension = null; 87 protected DispatcherFactory dispatcherFactory = null; 88 89 97 public DefaultEventBroker(DispatcherFactory idispatcherFactory, String iextension) { 98 dispatcherFactory = idispatcherFactory; 99 if (dispatcherFactory==null) dispatcherFactory = new DefaultDispatcherFactory(); 100 extension = iextension; 101 } 102 103 108 public String getEventExtension() { 109 return extension; 110 } 111 112 118 public void addEventListener(ListenerFactory factory) { 119 if (factory==null) return; 121 String lid = factory.getListenerID(); 122 123 if (logger.isInfoEnabled()) logger.info("Adding listener id:"+lid); 125 idMap.put(lid, factory); 126 127 List lAliases = getAliases(lid); 129 addAliases(lid, lAliases, idXref); 130 } 131 132 142 public void addEventListener(ListenerFactory factory, Class event) throws InvalidClassException { 143 if (factory==null || event==null) return; 145 if (!((BaseEvent.class).isAssignableFrom(event))) throw new InvalidClassException ("Class "+event.getName()+" is not a BaseEvent"); 146 if (logger.isInfoEnabled()) logger.info("Adding listener:"+factory+" for class:"+event); 147 148 List eventList = (List) eventMap.get(event); 150 if (eventList==null) { 151 eventList = new ArrayList(); 152 eventMap.put(event, eventList); 153 } 154 eventList.add(factory); 155 156 String eid = event.getName(); 158 List eAliases = getAliases(eid); 159 addAliases(eid, eAliases, eventXref); 160 161 String lid = factory.getListenerID(); 163 if (idMap.get(lid)==null) idMap.put(lid, factory); 164 165 List lAliases = getAliases(lid); 167 addAliases(lid, lAliases, idXref); 168 } 169 170 175 public void removeEventListener(ListenerFactory factory) { 176 if (factory==null) return; 178 String lid = factory.getListenerID(); 179 180 if (logger.isInfoEnabled()) logger.info("Removing listener id:"+lid); 182 idMap.remove(lid); 183 } 184 185 196 public void removeEventListener(ListenerFactory factory, Class event) throws InvalidClassException { 197 if (factory==null || event==null) return; 199 if (!((BaseEvent.class).isAssignableFrom(event))) throw new InvalidClassException ("Class "+event.getName()+" is not a BaseEvent"); 200 if (logger.isInfoEnabled()) logger.info("Removing listener:"+factory+" for class:"+event); 201 202 List eventList = (List) eventMap.get(event); 204 if (eventList!=null) { 205 while (eventList.contains(factory)) { 206 eventList.remove(factory); 207 } 208 if (eventList.size()<1) eventMap.remove(event); 209 } 210 211 String lid = factory.getListenerID(); 213 idMap.remove(lid); 214 } 215 216 222 public void purgeEventListener(ListenerFactory factory) { 223 if (factory==null) return; 225 if (logger.isInfoEnabled()) logger.info("Purging listener:"+factory); 226 227 String lid = factory.getListenerID(); 229 idMap.remove(lid); 230 231 Iterator it = eventMap.keySet().iterator(); 233 while (it.hasNext()) { 234 Object eventKey = it.next(); 235 List eventList = (List) eventMap.get(eventKey); 236 while (eventList.contains(factory)) { 237 eventList.remove(factory); 238 } 239 if (eventList.size()<1) eventMap.remove(eventKey); 240 } 241 } 242 243 249 public ListenerFactory getEventListener(Object id) { 250 if (id==null) return null; 252 if (logger.isInfoEnabled()) logger.info("Getting listener by id:"+id); 253 254 return (ListenerFactory) idMap.get(id); 256 } 257 258 268 public List getEventListeners(Class event) throws InvalidClassException { 269 if (event==null) return null; 271 if (!((BaseEvent.class).isAssignableFrom(event))) throw new InvalidClassException ("Class "+event.getName()+" is not a BaseEvent"); 272 if (logger.isInfoEnabled()) logger.info("Getting listeners for Class:"+event); 273 274 return (List) eventMap.get(event); 276 } 277 278 289 public String matchEventClass(String eventStr) throws InvalidClassException { 290 if (eventStr==null) throw new InvalidClassException(); Object result = eventXref.get(eventStr.toLowerCase()); if (result==null || result instanceof List) throw new InvalidClassException(); 294 else return (String ) result; 295 } 296 297 308 public String matchListenerID(String idStr) throws InvalidClassException { 309 if (idStr==null) throw new InvalidClassException(); Object result = idXref.get(idStr.toLowerCase()); if (result==null || result instanceof List) throw new InvalidClassException(); 313 else return (String ) result; 314 } 315 316 327 protected List getAliases(String className) { 328 List list = new ArrayList(); 329 330 list.add(className); 332 333 int spos = 0; 335 int epos = className.length(); 336 while ((spos=className.indexOf('.',spos))>-1) { 337 String alias = className.substring(spos+1); 338 list.add(alias); 339 spos+=1; 340 } 341 342 return list; 343 } 344 345 353 public void addEventAlias(Class event) throws InvalidClassException { 354 if (event==null) return; 355 if (!((BaseEvent.class).isAssignableFrom(event))) throw new InvalidClassException ("Class "+event.getName()+" is not a BaseEvent"); 356 357 addEventAlias(event, event.getName()); 359 360 try { 362 BaseEvent be = (BaseEvent) event.newInstance(); 363 if (!be.getEventID().equals(event.getName())) addEventAlias(event, be.getEventID()); 364 } catch (Exception e) {} 365 } 366 367 376 public void addEventAlias(Class event, String alias) throws InvalidClassException { 377 if (event==null || alias==null) return; 378 if (!((BaseEvent.class).isAssignableFrom(event))) throw new InvalidClassException ("Class "+event.getName()+" is not a BaseEvent"); 379 List lAliases = getAliases(alias); 380 addAliases(event.getName(), lAliases, eventXref); 381 } 382 383 387 protected void addAliases(String id, List aliases, Map xref) { 388 Iterator it = aliases.iterator(); 389 while (it.hasNext()) { 390 String alias = ((String ) it.next()).toLowerCase(); Object ref = xref.get(alias); 394 395 if (ref==null) { 397 xref.put(alias, id); 398 399 } else { 401 List list = null; 403 if (ref instanceof List) { 404 list = (List) ref; 405 } else { 406 if (id.equals(ref)) continue; 408 409 list = new ArrayList(); 411 list.add(ref); 412 xref.put(alias, list); 413 } 414 415 if (!list.contains(id)) list.add(id); 418 } 419 } 420 } 421 422 441 public void dispatchEvent(EventContext context) throws EventException { 442 if (context==null) return; 444 445 if (logger.isInfoEnabled()) logger.info("Dispatching event:"+context); 447 dispatcherFactory.getInstance().dispatchEvent(this, context); 448 if (logger.isInfoEnabled()) logger.info("Dispatch complete!"); 449 } 450 451 452 453 454 455 456 457 458 459 460 461 public static void main (String [] args) { 462 DefaultEventBroker eb = new DefaultEventBroker(null, ".event"); 463 eb.runTest(); 464 } 465 466 public void runTest() { 467 if (true) { 469 String lid = "foo.blah.event.Test"; 470 List lAliases = getAliases(lid); 471 addAliases(lid, lAliases, idXref); 472 } 473 if (true) { 475 String lid = "foo.blah.event.Blarney"; 476 List lAliases = getAliases(lid); 477 addAliases(lid, lAliases, idXref); 478 } 479 if (true) { 481 String lid = "foo.blah.event2.Test"; 482 List lAliases = getAliases(lid); 483 addAliases(lid, lAliases, idXref); 484 addAliases(lid, lAliases, idXref); 485 addAliases(lid, lAliases, idXref); 486 } 487 488 System.out.println (""); 490 System.out.println ("Check @ point 1:"); 491 CollectionsUtil.printStackTrace(idXref, System.out); 492 493 494 String id = null; 496 try { 497 id = "Blarney"; String result = matchListenerID(id); 499 System.out.println ("id:"+id+" result:"+result); 500 } catch (InvalidClassException e) {System.out.println ("Whoops..."+id+" didn't work!");} 501 try { 502 id = "event.Test"; String result = matchListenerID(id); 504 System.out.println ("id:"+id+" result:"+result); 505 } catch (InvalidClassException e) {System.out.println ("Whoops..."+id+" didn't work!");} 506 try { 507 id = "Test"; String result = matchListenerID(id); 509 System.out.println ("Whoops...didn't work!"); 510 } catch (InvalidClassException e) {System.out.println ("Got error for "+id+" as expected");} 511 } 512 513 } 514 | Popular Tags |