01: // Copyright 2006 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.tapestry.ioc.internal.services;
16:
17: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newThreadSafeList;
18:
19: import java.util.List;
20:
21: import org.apache.commons.logging.Log;
22: import org.apache.tapestry.ioc.internal.util.OneShotLock;
23: import org.apache.tapestry.ioc.services.RegistryShutdownHub;
24: import org.apache.tapestry.ioc.services.RegistryShutdownListener;
25:
26: public class RegistryShutdownHubImpl implements RegistryShutdownHub {
27: private final OneShotLock _lock = new OneShotLock();
28:
29: private final Log _log;
30:
31: private final List<RegistryShutdownListener> _listeners = newThreadSafeList();
32:
33: public RegistryShutdownHubImpl(Log log) {
34: _log = log;
35: }
36:
37: public void addRegistryShutdownListener(
38: RegistryShutdownListener listener) {
39: _lock.check();
40:
41: _listeners.add(listener);
42: }
43:
44: /**
45: * Fires the {@link RegistryShutdownListener#registryDidShutdown()} method on each listener. At
46: * the end, all the listeners are discarded.
47: *
48: * @param log
49: * used if any listener throws an exception
50: */
51: public void fireRegistryDidShutdown() {
52: _lock.lock();
53:
54: for (RegistryShutdownListener l : _listeners) {
55: try {
56: l.registryDidShutdown();
57: } catch (Exception ex) {
58: _log.error(
59: ServiceMessages.shutdownListenerError(l, ex),
60: ex);
61: }
62: }
63:
64: _listeners.clear();
65: }
66:
67: }
|