1 15 package org.apache.hivemind.impl; 16 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.Set ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.apache.hivemind.ShutdownCoordinator; 24 import org.apache.hivemind.events.RegistryShutdownListener; 25 import org.apache.hivemind.util.EventListenerList; 26 27 33 public final class ShutdownCoordinatorImpl implements ShutdownCoordinator 34 { 35 private final Log _log; 36 37 private Set alreadyShutdown; 38 39 public ShutdownCoordinatorImpl() 40 { 41 _log = LogFactory.getLog(ShutdownCoordinator.class); 42 } 43 44 private EventListenerList _listenerList; 45 46 public synchronized void addRegistryShutdownListener( 47 RegistryShutdownListener s) 48 { 49 if (_listenerList == null) 50 _listenerList = new EventListenerList(); 51 52 _listenerList.addListener(s); 53 } 54 55 public synchronized void removeRegistryShutdownListener( 56 RegistryShutdownListener s) 57 { 58 if (_listenerList != null) 59 _listenerList.removeListener(s); 60 } 61 62 public void shutdown() 63 { 64 if (_listenerList == null) 65 return; 66 67 Iterator i = _listenerList.getListeners(); 68 69 _listenerList = null; 70 71 while (i.hasNext()) 72 { 73 RegistryShutdownListener s = (RegistryShutdownListener) i.next(); 74 75 shutdown(s); 76 } 77 78 _listenerList = null; 79 } 80 81 private void shutdown(RegistryShutdownListener s) 82 { 83 if (alreadyShutdown == null) 84 { 85 alreadyShutdown = new HashSet (); 86 } 87 final Long id = new Long (System.identityHashCode(s)); 88 if (!alreadyShutdown.contains(id)) 89 { 90 try 91 { 92 s.registryDidShutdown(); 93 } 94 catch (RuntimeException ex) 95 { 96 _log.error(ImplMessages.shutdownCoordinatorFailure(s, ex), ex); 97 } 98 finally 99 { 100 alreadyShutdown.add(id); 101 } 102 } 103 } 104 105 } 106 | Popular Tags |