1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache license, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the license for the specific language governing permissions and
15  * limitations under the license.
16  */

17 package org.apache.logging.slf4j;
18
19 import java.util.Map;
20 import java.util.Map.Entry;
21
22 import org.apache.logging.log4j.spi.CleanableThreadContextMap;
23 import org.apache.logging.log4j.util.SortedArrayStringMap;
24 import org.apache.logging.log4j.util.StringMap;
25 import org.slf4j.MDC;
26
27 /**
28  * Bind the ThreadContextMap to the SLF4J MDC.
29  */

30 public class MDCContextMap implements CleanableThreadContextMap {
31
32     private static final StringMap EMPTY_CONTEXT_DATA = new SortedArrayStringMap(1);
33     static {
34         EMPTY_CONTEXT_DATA.freeze();
35     }
36
37     @Override
38     public void put(final String key, final String value) {
39         MDC.put(key, value);
40     }
41
42     @Override
43     public void putAll(final Map<String, String> m) {
44         for (final Entry<String, String> entry : m.entrySet()) {
45             MDC.put(entry.getKey(), entry.getValue());
46         }
47     }
48
49     @Override
50     public String get(final String key) {
51         return MDC.get(key);
52     }
53
54     @Override
55     public void remove(final String key) {
56         MDC.remove(key);
57     }
58
59
60     @Override
61     public void removeAll(final Iterable<String> keys) {
62         for (final String key : keys) {
63             MDC.remove(key);
64         }
65     }
66
67     @Override
68     public void clear() {
69         MDC.clear();
70     }
71
72     @Override
73     public boolean containsKey(final String key) {
74         Map<String, String> map = MDC.getCopyOfContextMap();
75         return map != null && map.containsKey(key);
76     }
77
78     @Override
79     @SuppressWarnings("unchecked"// nothing we can do about this, restricted by SLF4J API
80     public Map<String, String> getCopy() {
81         return MDC.getCopyOfContextMap();
82     }
83
84     @Override
85     @SuppressWarnings("unchecked"// nothing we can do about this, restricted by SLF4J API
86     public Map<String, String> getImmutableMapOrNull() {
87         return MDC.getCopyOfContextMap();
88     }
89
90     @Override
91     public boolean isEmpty() {
92         Map<String, String> map = MDC.getCopyOfContextMap();
93         return map == null || map.isEmpty();
94     }
95
96     @Override
97     public StringMap getReadOnlyContextData() {
98         final Map<String, String> copy = getCopy();
99         if (copy.isEmpty()) {
100             return EMPTY_CONTEXT_DATA;
101         }
102         final StringMap result = new SortedArrayStringMap();
103         for (final Entry<String, String> entry : copy.entrySet()) {
104             result.putValue(entry.getKey(), entry.getValue());
105         }
106         return result;
107     }
108 }
109