|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ThreadEventNotifierImpl.java | 87.5% | 100% | 100% | 96.2% |
|
1 |
// Copyright 2004 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.service.impl;
|
|
16 |
|
|
17 |
import java.util.Iterator;
|
|
18 |
|
|
19 |
import org.apache.hivemind.service.ThreadCleanupListener;
|
|
20 |
import org.apache.hivemind.service.ThreadEventNotifier;
|
|
21 |
import org.apache.hivemind.util.EventListenerList;
|
|
22 |
|
|
23 |
/**
|
|
24 |
* Implementation of {@link org.apache.hivemind.service.ThreadEventNotifier},
|
|
25 |
* available as service <code>hivemind.ThreadEventNotifier</code>.
|
|
26 |
*
|
|
27 |
* @author Howard Lewis Ship
|
|
28 |
*/
|
|
29 |
public class ThreadEventNotifierImpl implements ThreadEventNotifier |
|
30 |
{ |
|
31 |
private ThreadLocal _storage = new ThreadLocal(); |
|
32 |
|
|
33 | 23 |
public void addThreadCleanupListener(ThreadCleanupListener listener) |
34 |
{ |
|
35 | 23 |
EventListenerList list = (EventListenerList) _storage.get(); |
36 |
|
|
37 | 23 |
if (list == null) |
38 |
{ |
|
39 | 17 |
list = new EventListenerList();
|
40 | 17 |
_storage.set(list); |
41 |
} |
|
42 |
|
|
43 | 23 |
list.addListener(listener); |
44 |
} |
|
45 |
|
|
46 | 6 |
public void removeThreadCleanupListener(ThreadCleanupListener listener) |
47 |
{ |
|
48 | 6 |
EventListenerList list = (EventListenerList) _storage.get(); |
49 |
|
|
50 | 6 |
if (list != null) |
51 | 6 |
list.removeListener(listener); |
52 |
} |
|
53 |
|
|
54 | 13 |
public void fireThreadCleanup() |
55 |
{ |
|
56 |
// Here's where we need the CursorableLinkedList since listeners
|
|
57 |
// are free to unregister as listeners from threadDidCleanup() and
|
|
58 |
// we need to avoid concurrent modification errors.
|
|
59 |
|
|
60 | 13 |
EventListenerList list = (EventListenerList) _storage.get(); |
61 |
|
|
62 | 13 |
if (list == null) |
63 | 1 |
return;
|
64 |
|
|
65 | 12 |
Iterator i = list.getListeners(); |
66 |
|
|
67 | 12 |
while (i.hasNext())
|
68 |
{ |
|
69 | 15 |
ThreadCleanupListener listener = (ThreadCleanupListener) i.next(); |
70 |
|
|
71 |
// Each listener may decide to remove itself; that's OK,
|
|
72 |
// EventListenerList handles that kind of concurrent modification
|
|
73 |
// well.
|
|
74 |
|
|
75 | 15 |
listener.threadDidCleanup(); |
76 |
} |
|
77 |
|
|
78 |
} |
|
79 |
|
|
80 |
} |
|
81 |
|
|