View Javadoc
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          return MDC.getCopyOfContextMap().containsKey(key);
75      }
76  
77      @Override
78      @SuppressWarnings("unchecked") // nothing we can do about this, restricted by SLF4J API
79      public Map<String, String> getCopy() {
80          return MDC.getCopyOfContextMap();
81      }
82  
83      @Override
84      @SuppressWarnings("unchecked") // nothing we can do about this, restricted by SLF4J API
85      public Map<String, String> getImmutableMapOrNull() {
86          return MDC.getCopyOfContextMap();
87      }
88  
89      @Override
90      public boolean isEmpty() {
91          return MDC.getCopyOfContextMap().isEmpty();
92      }
93  
94      @Override
95      public StringMap getReadOnlyContextData() {
96          final Map<String, String> copy = getCopy();
97          if (copy.isEmpty()) {
98              return EMPTY_CONTEXT_DATA;
99          }
100         final StringMap result = new SortedArrayStringMap();
101         for (final Entry<String, String> entry : copy.entrySet()) {
102             result.putValue(entry.getKey(), entry.getValue());
103         }
104         return result;
105     }
106 }