|
21 | 21 | */
|
22 | 22 | package org.exist.storage;
|
23 | 23 |
|
| 24 | +import net.jcip.annotations.ThreadSafe; |
24 | 25 | import org.apache.logging.log4j.LogManager;
|
25 | 26 | import org.apache.logging.log4j.Logger;
|
26 | 27 | import org.exist.dom.persistent.DocumentImpl;
|
27 | 28 | import org.exist.numbering.NodeId;
|
28 | 29 |
|
29 | 30 | import java.util.IdentityHashMap;
|
| 31 | +import java.util.Map; |
| 32 | + |
30 | 33 | import org.exist.dom.persistent.IStoredNode;
|
31 | 34 |
|
32 | 35 | /**
|
33 | 36 | * Global notification service for document updates. Other classes
|
34 | 37 | * can subscribe to this service to be notified of document modifications,
|
35 | 38 | * removals or additions.
|
36 |
| - * |
37 |
| - * @author wolf |
38 | 39 | *
|
| 40 | + * @author wolf |
39 | 41 | */
|
40 |
| -public class NotificationService extends IdentityHashMap<UpdateListener, Object> implements BrokerPoolService { |
| 42 | +@ThreadSafe |
| 43 | +public class NotificationService implements BrokerPoolService { |
| 44 | + |
| 45 | + private static final long serialVersionUID = -3629584664969740903L; |
| 46 | + private static final Logger LOG = LogManager.getLogger(NotificationService.class); |
| 47 | + |
| 48 | + private final Map<UpdateListener, Object> listeners = new IdentityHashMap<>(); |
| 49 | + |
| 50 | + public NotificationService() { |
| 51 | + super(); |
| 52 | + } |
41 | 53 |
|
42 |
| - private static final long serialVersionUID = -3629584664969740903L; |
| 54 | + /** |
| 55 | + * Subscribe an {@link UpdateListener} to receive notifications. |
| 56 | + * |
| 57 | + * @param listener |
| 58 | + */ |
| 59 | + public synchronized void subscribe(final UpdateListener listener) { |
| 60 | + listeners.put(listener, new Object()); |
| 61 | + } |
43 | 62 |
|
44 |
| - private final static Logger LOG = LogManager.getLogger(NotificationService.class); |
45 |
| - |
46 |
| - public NotificationService() { |
47 |
| - super(); |
48 |
| - } |
49 |
| - |
50 |
| - /** |
51 |
| - * Subscribe an {@link UpdateListener} to receive notifications. |
52 |
| - * |
53 |
| - * @param listener |
54 |
| - */ |
55 |
| - public synchronized void subscribe(UpdateListener listener) { |
56 |
| - put(listener, new Object()); |
57 |
| - } |
58 |
| - |
59 |
| - /** |
60 |
| - * Unsubscribe an {@link UpdateListener}. |
61 |
| - * |
62 |
| - * @param listener |
63 |
| - */ |
64 |
| - public synchronized void unsubscribe(UpdateListener listener) { |
65 |
| - final Object i = remove(listener); |
66 |
| - if (i == null) |
67 |
| - {throw new RuntimeException(hashCode() + " listener not found: " + listener.hashCode());} |
| 63 | + /** |
| 64 | + * Unsubscribe an {@link UpdateListener}. |
| 65 | + * |
| 66 | + * @param listener |
| 67 | + */ |
| 68 | + public synchronized void unsubscribe(final UpdateListener listener) { |
| 69 | + final Object i = listeners.remove(listener); |
| 70 | + if (i == null) { |
| 71 | + throw new RuntimeException(hashCode() + " listener not found: " + listener.hashCode()); |
| 72 | + } |
68 | 73 | listener.unsubscribe();
|
69 | 74 | }
|
70 | 75 |
|
71 |
| - /** |
72 |
| - * Notify all subscribers that a document has been updated/removed or |
73 |
| - * a new document has been added. |
74 |
| - * |
75 |
| - * @param document |
76 |
| - * @param event |
77 |
| - */ |
78 |
| - public synchronized void notifyUpdate(DocumentImpl document, int event) { |
79 |
| - for (final UpdateListener listener : keySet()) { |
80 |
| - listener.documentUpdated(document, event); |
81 |
| - } |
82 |
| - } |
| 76 | + /** |
| 77 | + * Notify all subscribers that a document has been updated/removed or |
| 78 | + * a new document has been added. |
| 79 | + * |
| 80 | + * @param document |
| 81 | + * @param event |
| 82 | + */ |
| 83 | + public synchronized void notifyUpdate(final DocumentImpl document, final int event) { |
| 84 | + listeners.keySet().forEach(listener -> listener.documentUpdated(document, event)); |
| 85 | + } |
83 | 86 |
|
84 | 87 | /**
|
85 |
| - * Notify all subscribers that a node has been moved. Nodes may be moved during a |
| 88 | + * Notify all subscribers that a node has been moved. Nodes may be moved during a |
86 | 89 | * defragmentation run.
|
87 |
| - */ |
88 |
| - public synchronized void notifyMove(NodeId oldNodeId, IStoredNode newNode) { |
89 |
| - for (final UpdateListener listener : keySet()) { |
90 |
| - listener.nodeMoved(oldNodeId, newNode); |
91 |
| - } |
92 |
| - } |
| 90 | + */ |
| 91 | + public synchronized void notifyMove(final NodeId oldNodeId, final IStoredNode newNode) { |
| 92 | + listeners.keySet().forEach(listener -> listener.nodeMoved(oldNodeId, newNode)); |
| 93 | + } |
93 | 94 |
|
94 |
| - public void debug() { |
95 |
| - LOG.debug("Registered UpdateListeners:"); |
96 |
| - for (final UpdateListener listener : keySet()) { |
97 |
| - listener.debug(); |
98 |
| - } |
99 |
| - } |
| 95 | + public synchronized void debug() { |
| 96 | + if (LOG.isDebugEnabled()) { |
| 97 | + LOG.debug("Registered UpdateListeners:"); |
| 98 | + } |
| 99 | + listeners.keySet().forEach(UpdateListener::debug); |
| 100 | + } |
100 | 101 | }
|
0 commit comments