1 /**
2 * Copyright (c) 2004-2011 QOS.ch
3 * All rights reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 */
25 package org.slf4j.log4j12;
26
27 import java.util.Deque;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import org.apache.log4j.MDCFriend;
32 import org.slf4j.helpers.ThreadLocalMapOfStacks;
33 import org.slf4j.spi.MDCAdapter;
34
35 public class Log4jMDCAdapter implements MDCAdapter {
36
37 private final ThreadLocalMapOfStacks threadLocalMapOfDeques = new ThreadLocalMapOfStacks();
38
39 static {
40 if (VersionUtil.getJavaMajorVersion() >= 9) {
41 MDCFriend.fixForJava9();
42 }
43 }
44
45 @Override
46 public void clear() {
47 @SuppressWarnings("rawtypes")
48 Map map = org.apache.log4j.MDC.getContext();
49 if (map != null) {
50 map.clear();
51 }
52 }
53
54 @Override
55 public String get(String key) {
56 return (String) org.apache.log4j.MDC.get(key);
57 }
58
59 /**
60 * Put a context value (the <code>val</code> parameter) as identified with
61 * the <code>key</code> parameter into the current thread's context map. The
62 * <code>key</code> parameter cannot be null. Log4j does <em>not</em>
63 * support null for the <code>val</code> parameter.
64 *
65 * <p>
66 * This method delegates all work to log4j's MDC.
67 *
68 * @throws IllegalArgumentException
69 * in case the "key" or <b>"val"</b> parameter is null
70 */
71 @Override
72 public void put(String key, String val) {
73 org.apache.log4j.MDC.put(key, val);
74 }
75
76 @Override
77 public void remove(String key) {
78 org.apache.log4j.MDC.remove(key);
79 }
80
81 @SuppressWarnings({ "rawtypes", "unchecked" })
82 public Map getCopyOfContextMap() {
83 Map old = org.apache.log4j.MDC.getContext();
84 if (old != null) {
85 return new HashMap(old);
86 } else {
87 return null;
88 }
89 }
90
91 @SuppressWarnings({ "rawtypes", "unchecked" })
92 @Override
93 public void setContextMap(Map<String, String> contextMap) {
94 Map old = org.apache.log4j.MDC.getContext();
95
96 // we must cater for the case where the contextMap argument is null
97 if (contextMap == null) {
98 if (old != null) {
99 old.clear();
100 }
101 return;
102 }
103
104 if (old == null) {
105 for (Map.Entry<String, String> mapEntry : contextMap.entrySet()) {
106 org.apache.log4j.MDC.put(mapEntry.getKey(), mapEntry);
107 }
108 } else {
109 old.clear();
110 old.putAll(contextMap);
111 }
112 }
113
114 @Override
115 public void pushByKey(String key, String value) {
116 threadLocalMapOfDeques.pushByKey(key, value);
117 }
118
119 @Override
120 public String popByKey(String key) {
121 return threadLocalMapOfDeques.popByKey(key);
122 }
123
124 @Override
125 public Deque<String> getCopyOfDequeByKey(String key) {
126 return threadLocalMapOfDeques.getCopyOfDequeByKey(key);
127 }
128
129 @Override
130 public void clearDequeByKey(String key) {
131 threadLocalMapOfDeques.clearDequeByKey(key);
132 }
133 }