KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > ShutdownCoordinatorImpl


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.impl;
16
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Set JavaDoc;
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 /**
28  * Manages a list of objects that implement the
29  * {@link org.apache.hivemind.events.RegistryShutdownListener} interface.
30  *
31  * @author Howard Lewis Ship
32  */

33 public final class ShutdownCoordinatorImpl implements ShutdownCoordinator
34 {
35     private final Log _log;
36
37     private Set JavaDoc 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 JavaDoc 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 JavaDoc();
86         }
87         final Long JavaDoc id = new Long JavaDoc(System.identityHashCode(s));
88         if (!alreadyShutdown.contains(id))
89         {
90             try
91             {
92                 s.registryDidShutdown();
93             }
94             catch (RuntimeException JavaDoc 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