1 4 package com.tc.async.api; 5 6 import com.tc.logging.TCLogger; 7 import com.tc.logging.TCLogging; 8 import com.tc.util.Assert; 9 10 import java.util.Collection ; 11 import java.util.Iterator ; 12 13 18 public abstract class AbstractEventHandler implements EventHandler { 19 20 private ConfigurationContext configContext; 21 private TCLogger logger; 22 private final ThreadLocal start = new ThreadLocal (); 23 private boolean initialized = false; 24 25 public abstract void handleEvent(EventContext context) throws EventHandlerException; 26 27 public void handleEvents(Collection contexts) throws EventHandlerException { 28 for (Iterator i = contexts.iterator(); i.hasNext();) { 29 EventContext eh = (EventContext) i.next(); 30 handleEvent(eh); 31 } 32 } 33 34 public synchronized void initialize(ConfigurationContext context) { 35 if (context == null) { 36 this.logger = TCLogging.getLogger(this.getClass()); 37 logger.warn("Setting config context to null. This is highly unusual"); 38 } else { 39 this.logger = context.getLogger(this.getClass()); 40 } 41 this.configContext = context; 42 this.initialized = true; 43 } 44 45 public TCLogger getLogger() { 46 return logger; 47 } 48 49 public synchronized void destroy() { 50 configContext = null; 51 this.initialized = false; 52 } 53 54 57 protected synchronized ConfigurationContext getConfigurationContext() { 58 return configContext; 59 } 60 61 public void logOnEnter(EventContext eventContext) { 62 logOnEnter(eventContext, getLogger()); 63 } 64 65 public void logOnEnter(EventContext eventContext, TCLogger localLogger) { 66 Assert.eval("This usually happens because someone is overriding the initialize method without calling super", 67 initialized); 68 if (localLogger.isDebugEnabled()) { 69 markStart(); 70 localLogger.debug("handler.handleEvent() start: <event-handler>" + this + "</event-handler><event-context>" 71 + eventContext + "</event-context>"); 72 } 73 } 74 75 public void logOnExit(EventContext eventContext) { 76 logOnExit(eventContext, logger); 77 } 78 79 public void logOnExit(EventContext eventContext, TCLogger localLogger) { 80 Assert.eval(initialized); 81 if (localLogger.isDebugEnabled()) { 82 localLogger.debug("handler.handleEvent() stop: <elapsed-time>" + getExecutionTime() 83 + "</elapsed-time><event-handler>" + this + "</event-handler><event-context>" + eventContext 84 + "</event-context>"); 85 } 86 } 87 88 private void markStart() { 89 start.set(new Long (System.currentTimeMillis())); 90 } 91 92 private long getExecutionTime() { 93 return System.currentTimeMillis() - ((Long ) start.get()).longValue(); 94 } 95 } | Popular Tags |