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.util.StringMap;
23  import org.apache.logging.log4j.spi.ThreadContextMap2;
24  import org.apache.logging.log4j.util.SortedArrayStringMap;
25  import org.slf4j.MDC;
26  
27  /**
28   * Bind the ThreadContextMap to the SLF4J MDC.
29   */
30  public class MDCContextMap implements ThreadContextMap2 {
31  
32      private static final StringMap EMPTY_CONTEXT_DATA = new SortedArrayStringMap();
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      @Override
60      public void clear() {
61          MDC.clear();
62      }
63  
64      @Override
65      public boolean containsKey(final String key) {
66          return MDC.getCopyOfContextMap().containsKey(key);
67      }
68  
69      @Override
70      @SuppressWarnings("unchecked") // nothing we can do about this, restricted by SLF4J API
71      public Map<String, String> getCopy() {
72          return MDC.getCopyOfContextMap();
73      }
74  
75      @Override
76      @SuppressWarnings("unchecked") // nothing we can do about this, restricted by SLF4J API
77      public Map<String, String> getImmutableMapOrNull() {
78          return MDC.getCopyOfContextMap();
79      }
80  
81      @Override
82      public boolean isEmpty() {
83          return MDC.getCopyOfContextMap().isEmpty();
84      }
85  
86      @Override
87      public StringMap getReadOnlyContextData() {
88          final Map<String, String> copy = getCopy();
89          if (copy.isEmpty()) {
90              return EMPTY_CONTEXT_DATA;
91          }
92          final StringMap result = new SortedArrayStringMap();
93          for (Entry<String, String> entry : copy.entrySet()) {
94              result.putValue(entry.getKey(), entry.getValue());
95          }
96          return result;
97      }
98  }