1 15 package org.apache.hivemind.service.impl; 16 17 import java.util.Iterator ; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.hivemind.service.ThreadCleanupListener; 22 import org.apache.hivemind.service.ThreadEventNotifier; 23 import org.apache.hivemind.util.Defense; 24 import org.apache.hivemind.util.EventListenerList; 25 26 32 public class ThreadEventNotifierImpl implements ThreadEventNotifier 33 { 34 private static final Log DEFAULT_LOG = LogFactory.getLog(ThreadEventNotifier.class); 35 36 private final Log _log; 37 38 private final ThreadLocal _storage = new ThreadLocal (); 39 40 public ThreadEventNotifierImpl() 41 { 42 this(DEFAULT_LOG); 43 } 44 45 public ThreadEventNotifierImpl(Log log) 46 { 47 Defense.notNull(log, "log"); 48 49 _log = log; 50 } 51 52 public void addThreadCleanupListener(ThreadCleanupListener listener) 53 { 54 EventListenerList list = (EventListenerList) _storage.get(); 55 56 if (list == null) 57 { 58 list = new EventListenerList(); 59 _storage.set(list); 60 } 61 62 list.addListener(listener); 63 } 64 65 public void removeThreadCleanupListener(ThreadCleanupListener listener) 66 { 67 EventListenerList list = (EventListenerList) _storage.get(); 68 69 if (list != null) 70 list.removeListener(listener); 71 } 72 73 public void fireThreadCleanup() 74 { 75 79 EventListenerList list = (EventListenerList) _storage.get(); 80 81 if (list == null) 82 return; 83 84 Iterator i = list.getListeners(); 85 86 89 _storage.set(null); 90 91 while (i.hasNext()) 92 { 93 ThreadCleanupListener listener = (ThreadCleanupListener) i.next(); 94 95 99 try 100 { 101 listener.threadDidCleanup(); 102 } 103 catch (RuntimeException ex) 104 { 105 _log.warn(ServiceMessages.threadCleanupException(ex), ex); 106 } 107 } 108 } 109 110 } 111 | Popular Tags |