001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.hivemind.impl;
016    
017    import java.util.HashSet;
018    import java.util.Iterator;
019    import java.util.Set;
020    
021    import org.apache.commons.logging.Log;
022    import org.apache.commons.logging.LogFactory;
023    import org.apache.hivemind.ShutdownCoordinator;
024    import org.apache.hivemind.events.RegistryShutdownListener;
025    import org.apache.hivemind.util.EventListenerList;
026    
027    /**
028     * Manages a list of objects that implement the
029     * {@link org.apache.hivemind.events.RegistryShutdownListener} interface.
030     * 
031     * @author Howard Lewis Ship
032     */
033    public final class ShutdownCoordinatorImpl implements ShutdownCoordinator
034    {
035        private final Log _log;
036    
037        private Set alreadyShutdown;
038    
039        public ShutdownCoordinatorImpl()
040        {
041            _log = LogFactory.getLog(ShutdownCoordinator.class);
042        }
043    
044        private EventListenerList _listenerList;
045    
046        public synchronized void addRegistryShutdownListener(
047                RegistryShutdownListener s)
048        {
049            if (_listenerList == null)
050                _listenerList = new EventListenerList();
051    
052            _listenerList.addListener(s);
053        }
054    
055        public synchronized void removeRegistryShutdownListener(
056                RegistryShutdownListener s)
057        {
058            if (_listenerList != null)
059                _listenerList.removeListener(s);
060        }
061    
062        public void shutdown()
063        {
064            if (_listenerList == null)
065                return;
066    
067            Iterator i = _listenerList.getListeners();
068    
069            _listenerList = null;
070    
071            while (i.hasNext())
072            {
073                RegistryShutdownListener s = (RegistryShutdownListener) i.next();
074    
075                shutdown(s);
076            }
077    
078            _listenerList = null;
079        }
080    
081        private void shutdown(RegistryShutdownListener s)
082        {
083            if (alreadyShutdown == null)
084            {
085                alreadyShutdown = new HashSet();
086            }
087            final Long id = new Long(System.identityHashCode(s));
088            if (!alreadyShutdown.contains(id))
089            {
090                try
091                {
092                    s.registryDidShutdown();
093                }
094                catch (RuntimeException ex)
095                {
096                    _log.error(ImplMessages.shutdownCoordinatorFailure(s, ex), ex);
097                }
098                finally
099                {
100                    alreadyShutdown.add(id);
101                }
102            }
103        }
104    
105    }