01: // Copyright 2006, 2007 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.newList;
18:
19: import java.util.List;
20:
21: import org.apache.commons.logging.Log;
22: import org.apache.tapestry.ioc.services.ThreadCleanupHub;
23: import org.apache.tapestry.ioc.services.ThreadCleanupListener;
24:
25: public class ThreadCleanupHubImpl implements ThreadCleanupHub {
26: private static class ListHolder extends
27: ThreadLocal<List<ThreadCleanupListener>> {
28: @Override
29: protected List<ThreadCleanupListener> initialValue() {
30: return newList();
31: }
32: }
33:
34: private final Log _log;
35:
36: private final ListHolder _holder = new ListHolder();
37:
38: public ThreadCleanupHubImpl(Log log) {
39: _log = log;
40: }
41:
42: public void addThreadCleanupListener(ThreadCleanupListener listener) {
43: _holder.get().add(listener);
44: }
45:
46: /**
47: * Instructs the hub to notify all its listeners (for the current thread). It also discards its
48: * list of listeners.
49: */
50: public void cleanup() {
51: List<ThreadCleanupListener> listeners = _holder.get();
52:
53: // Discard the listeners. In a perfect world, we would set a per-thread flag that prevented
54: // more listeners from being added, until a new thread begins. But we don't have a concept
55: // of thread start, just thread complete.
56:
57: _holder.remove();
58:
59: for (ThreadCleanupListener listener : listeners) {
60: try {
61: listener.threadDidCleanup();
62: } catch (Exception ex) {
63: _log.warn(ServiceMessages.threadCleanupError(listener,
64: ex), ex);
65: }
66: }
67:
68: }
69:
70: }
|