KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > async > api > AbstractEventHandler


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13 /**
14  * Simple superclass for event handlers that does the iterating over events in the array
15  *
16  * @author steve
17  */

18 public abstract class AbstractEventHandler implements EventHandler {
19
20   private ConfigurationContext configContext;
21   private TCLogger logger;
22   private final ThreadLocal JavaDoc start = new ThreadLocal JavaDoc();
23   private boolean initialized = false;
24
25   public abstract void handleEvent(EventContext context) throws EventHandlerException;
26
27   public void handleEvents(Collection JavaDoc contexts) throws EventHandlerException {
28     for (Iterator JavaDoc 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   /**
55    * @return the ConfigurationContext object that was passed to the <code>initialize</code> method.
56    */

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 JavaDoc(System.currentTimeMillis()));
90   }
91
92   private long getExecutionTime() {
93     return System.currentTimeMillis() - ((Long JavaDoc) start.get()).longValue();
94   }
95 }
Popular Tags