/
ObservableMdcAdapter.java
171 lines (133 loc) · 4.65 KB
/
ObservableMdcAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.slf4j.impl;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import javax.annotation.concurrent.NotThreadSafe;
import org.slf4j.helpers.BasicMDCAdapter;
import org.slf4j.spi.MDCAdapter;
/**
* This MDCAdapter delegates all of its methods to a {@link BasicMDCAdapter}.
* The additional functionality of this class is to offer the possibility to observe the call of its methods.
* This comes handy for unit testing as the MDC wouldn't be testable otherwise.
*
* @since 1.3.0
*/
@NotThreadSafe
public final class ObservableMdcAdapter implements MDCAdapter {
private static final Map<String, MdcAdapterObserver> OBSERVERS = new HashMap<>(3);
private final BasicMDCAdapter basicMdcAdapter;
private ObservableMdcAdapter() {
basicMdcAdapter = new BasicMDCAdapter();
}
public static ObservableMdcAdapter getInstance() {
return new ObservableMdcAdapter();
}
public static void registerObserver(final String observerName, final MdcAdapterObserver observer) {
OBSERVERS.put(observerName, observer);
}
public static void deregisterObserver(final String observerName) {
OBSERVERS.remove(observerName);
}
public static void clearObservers() {
OBSERVERS.clear();
}
@Override
public void put(final String key, final String val) {
notifyAllObservers(observer -> observer.onPut(key, val));
basicMdcAdapter.put(key, val);
}
private static void notifyAllObservers(final Consumer<MdcAdapterObserver> observerConsumer) {
OBSERVERS.values().forEach(observerConsumer);
}
@Override
public String get(final String key) {
notifyAllObservers(observer -> observer.onGet(key));
return basicMdcAdapter.get(key);
}
@Override
public void remove(final String key) {
notifyAllObservers(observer -> observer.onRemove(key));
basicMdcAdapter.remove(key);
}
@Override
public void clear() {
notifyAllObservers(MdcAdapterObserver::onClear);
basicMdcAdapter.clear();
}
public Set<String> getKeys() {
notifyAllObservers(MdcAdapterObserver::onGetKeys);
return basicMdcAdapter.getKeys();
}
@Override
public Map<String, String> getCopyOfContextMap() {
notifyAllObservers(MdcAdapterObserver::onGetCopyOfContextMap);
return basicMdcAdapter.getCopyOfContextMap();
}
@Override
public void setContextMap(final Map<String, String> contextMap) {
notifyAllObservers(observer -> observer.onSetContextMap(contextMap));
basicMdcAdapter.setContextMap(contextMap);
}
/**
* This call-back will be notified for each call of a {@link ObservableMdcAdapter}s method.
*/
@NotThreadSafe
public interface MdcAdapterObserver {
void onPut(String key, String value);
void onGet(String key);
void onRemove(String key);
void onClear();
void onGetKeys();
void onGetCopyOfContextMap();
void onSetContextMap(Map<String, String> contextMap);
}
/**
* Basic implementation of {@link MdcAdapterObserver}.
* By default, the method of this class do nothing.
* It can be sub-classed to implement only particular observer methods and not to have to implement all of them.
*/
@NotThreadSafe
public abstract static class AbstractMdcAdapterObserver implements MdcAdapterObserver {
@Override
public void onPut(final String key, final String value) {
// Does nothing by default.
}
@Override
public void onGet(final String key) {
// Does nothing by default.
}
@Override
public void onRemove(final String key) {
// Does nothing by default.
}
@Override
public void onClear() {
// Does nothing by default.
}
@Override
public void onGetKeys() {
// Does nothing by default.
}
@Override
public void onGetCopyOfContextMap() {
// Does nothing by default.
}
@Override
public void onSetContextMap(final Map<String, String> contextMap) {
// Does nothing by default.
}
}
}