1 20 package org.enhydra.barracuda.core.event; 21 22 import java.io.*; 23 import java.util.*; 24 25 import org.apache.log4j.Logger; 26 27 import org.enhydra.barracuda.plankton.data.*; 28 29 35 public class DefaultEventGateway implements EventGateway { 36 37 private static final Logger logger = Logger.getLogger(DefaultEventGateway.class.getName()); 39 40 private static byte[] sep = System.getProperty("line.separator").getBytes(); 41 42 45 private EventGateway parent = null; 46 private List gateways = new ArrayList(); 47 private List interests = null; 48 private List aliases = null; 49 50 55 public final void setParent(EventGateway eg) { 56 parent = eg; 57 } 58 59 64 public final EventGateway getParent() { 65 return parent; 66 } 67 68 73 public final void add(EventGateway eg) { 74 if (eg==null) return; 75 if (logger.isInfoEnabled()) logger.info(this+" adding "+eg); 76 gateways.add(eg); 77 eg.setParent(this); 78 } 79 80 85 public final void remove(EventGateway eg) { 86 if (eg==null) return; 87 if (logger.isInfoEnabled()) logger.info(this+" removing "+eg); 88 gateways.remove(eg); 89 eg.setParent(null); 90 } 91 92 98 public List getChildren() { 99 return new ArrayList(gateways); 100 } 101 102 109 public final void register(EventBroker eb) { 110 if (logger.isInfoEnabled()) logger.info(this+" registering with EventBroker"); 111 112 if (interests!=null) { 114 Iterator it = interests.iterator(); 115 while (it.hasNext()) { 116 Interest interest = (Interest) it.next(); 117 try { 118 if (interest.eventClass!=null) eb.addEventListener(interest.factory, interest.eventClass); 119 else eb.addEventListener(interest.factory); 120 } catch (org.enhydra.barracuda.core.event.InvalidClassException e) {} 121 } 122 } 123 124 if (aliases!=null) { 126 Iterator it = aliases.iterator(); 127 while (it.hasNext()) { 128 Class event = (Class ) it.next(); 129 try {eb.addEventAlias(event);} 130 catch (org.enhydra.barracuda.core.event.InvalidClassException e) {} 131 } 132 aliases = null; 133 } 134 135 if (logger.isDebugEnabled()) logger.debug(this+"...registering local interests "); 137 registerLocalEventInterests(eb); 138 139 if (logger.isDebugEnabled()) logger.debug(this+"...registering local aliases"); 141 registerLocalEventAliases(eb); 142 143 if (logger.isDebugEnabled()) logger.debug(this+"...registering children "); 145 Iterator it = gateways.iterator(); 146 while (it.hasNext()) { 147 EventGateway eg = (EventGateway) it.next(); 148 if (logger.isDebugEnabled()) logger.debug(this+"...found child gateway:"+eg); 149 eg.register(eb); 150 } 151 152 if (logger.isInfoEnabled()) logger.info("Registration complete!"); 153 } 154 155 161 public void registerLocalEventInterests(EventBroker eb) { 162 } 164 165 171 public void registerLocalEventAliases(EventBroker eb) { 172 } 174 175 193 public final void specifyLocalEventInterests(ListenerFactory factory) { 194 specifyLocalEventInterests(factory, null); 195 } 196 197 214 public final void specifyLocalEventInterests(ListenerFactory factory, Class event) { 215 if (logger.isDebugEnabled()) logger.debug(this+" specifying event interest: "+factory+"-->"+(event!=null ? event.getName() : "null")); 216 if (interests==null) interests = new ArrayList(); 217 interests.add(new Interest(factory, event)); 218 } 219 220 235 public final void specifyLocalEventAliases(Class event) { 236 if (logger.isDebugEnabled()) logger.debug(this+" specifying event alias: "+event.getName()); 237 if (aliases==null) aliases = new ArrayList(); 238 aliases.add(event); 239 } 240 241 248 public final void deregister(EventBroker eb) { 249 if (logger.isInfoEnabled()) logger.info(this+" deregistering with EventBroker"); 250 251 if (interests!=null) { 253 Iterator it = interests.iterator(); 254 while (it.hasNext()) { 255 Interest interest = (Interest) it.next(); 256 try { 257 if (interest.eventClass!=null) eb.removeEventListener(interest.factory, interest.eventClass); 258 else eb.removeEventListener(interest.factory); 259 } catch (org.enhydra.barracuda.core.event.InvalidClassException e) {} 260 } 261 interests = null; 262 } 263 264 if (logger.isDebugEnabled()) logger.debug(this+"...deregistering local "); 266 deregisterLocalEventInterests(eb); 267 268 if (logger.isDebugEnabled()) logger.debug(this+"...deregistering children "); 270 Iterator it = gateways.iterator(); 271 while (it.hasNext()) { 272 EventGateway eg = (EventGateway) it.next(); 273 if (logger.isDebugEnabled()) logger.debug(this+"...found child gateway:"+eg); 274 eg.deregister(eb); 275 } 276 277 if (logger.isInfoEnabled()) logger.info("Deregistration complete!"); 278 } 279 280 286 public void deregisterLocalEventInterests(EventBroker eb) { 287 } 289 290 class Interest { 291 ListenerFactory factory = null; 292 Class eventClass = null; 293 294 public Interest(ListenerFactory ifactory, Class ieventClass) { 295 factory = ifactory; 296 eventClass = ieventClass; 297 } 298 299 public String toString() { 300 return "Interest {factory:"+factory+" event:"+eventClass+"}"; 301 } 302 } 303 304 308 public void printStackTrace(int depth, Logger extLogger) { 309 printStackTrace(depth, extLogger, null); 310 } 311 312 316 public void printStackTrace(int depth, OutputStream out) { 317 printStackTrace(depth, null, out); 318 } 319 320 325 private void printStackTrace(int depth, Logger extLogger, OutputStream out) { 326 if (depth<0) depth = 0; 327 if (depth>25) depth = 25; 328 String spaces = " "; 329 String inset = spaces.substring(0,depth*3); 330 331 print(extLogger, out, inset+this.getClass().getName() + "@" + Integer.toHexString(this.hashCode())); 332 333 print(extLogger, out, inset+" interests: "+(interests==null ? "null" : "")); 335 if (interests!=null) CollectionsUtil.printStackTrace(interests, depth+2, extLogger, out); 336 print(extLogger, out, inset+" /end interests"); 337 338 print(extLogger, out, inset+" aliases: "+(aliases==null ? "null" : "")); 340 if (aliases!=null) CollectionsUtil.printStackTrace(aliases, depth+2, extLogger, out); 341 print(extLogger, out, inset+" /end aliases"); 342 343 print(extLogger, out, inset+" gateways: "+(gateways==null ? "null" : "")); 345 int cntr = -1; 346 if (gateways!=null) { 347 Iterator it = gateways.iterator(); 348 while (it.hasNext()) { 349 EventGateway eg = (EventGateway) it.next(); 350 if (eg instanceof DefaultEventGateway) { 351 print(extLogger, out, inset+" ["+(++cntr)+"]:"); 352 ((DefaultEventGateway) eg).printStackTrace(depth+2, extLogger, out); 353 } else { 354 print(extLogger, out, inset+" ["+(++cntr)+"]"+eg.getClass().getName()+" (details unknown)"); 355 } 356 } 357 } 358 print(extLogger, out, inset+" /end gateways"); 359 360 print(extLogger, out, inset+"/end @" + Integer.toHexString(this.hashCode())); 361 } 362 363 protected static void print(Logger extLogger, OutputStream out, String s) { 364 if (extLogger!=null) { 365 if (extLogger.isDebugEnabled()) extLogger.debug(s); 366 } else if (out!=null) { 367 try { 368 out.write(s.getBytes()); 369 out.write(sep); 370 } catch (IOException ioe) {} 371 } 372 } 373 } 374 | Popular Tags |