1 /** 2 * Copyright (c) 2004-2022 QOS.ch Sarl (Switzerland) 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.reload4j; 26 27 import java.util.Deque; 28 import java.util.HashMap; 29 import java.util.Map; 30 31 import org.slf4j.helpers.ThreadLocalMapOfStacks; 32 import org.slf4j.spi.MDCAdapter; 33 34 public class Reload4jMDCAdapter implements MDCAdapter { 35 36 private final ThreadLocalMapOfStacks threadLocalMapOfDeques = new ThreadLocalMapOfStacks(); 37 38 @Override clear()39 public void clear() { 40 @SuppressWarnings("rawtypes") 41 Map map = org.apache.log4j.MDC.getContext(); 42 if (map != null) { 43 map.clear(); 44 } 45 } 46 47 @Override get(String key)48 public String get(String key) { 49 return (String) org.apache.log4j.MDC.get(key); 50 } 51 52 /** 53 * Put a context value (the <code>val</code> parameter) as identified with 54 * the <code>key</code> parameter into the current thread's context map. The 55 * <code>key</code> parameter cannot be null. Log4j does <em>not</em> 56 * support null for the <code>val</code> parameter. 57 * 58 * <p> 59 * This method delegates all work to log4j's MDC. 60 * 61 * @throws IllegalArgumentException 62 * in case the "key" or <b>"val"</b> parameter is null 63 */ 64 @Override put(String key, String val)65 public void put(String key, String val) { 66 org.apache.log4j.MDC.put(key, val); 67 } 68 69 @Override remove(String key)70 public void remove(String key) { 71 org.apache.log4j.MDC.remove(key); 72 } 73 74 @SuppressWarnings({ "rawtypes", "unchecked" }) getCopyOfContextMap()75 public Map getCopyOfContextMap() { 76 Map old = org.apache.log4j.MDC.getContext(); 77 if (old != null) { 78 return new HashMap(old); 79 } else { 80 return null; 81 } 82 } 83 84 @SuppressWarnings({ "rawtypes", "unchecked" }) 85 @Override setContextMap(Map<String, String> contextMap)86 public void setContextMap(Map<String, String> contextMap) { 87 Map old = org.apache.log4j.MDC.getContext(); 88 89 // we must cater for the case where the contextMap argument is null 90 if (contextMap == null) { 91 if (old != null) { 92 old.clear(); 93 } 94 return; 95 } 96 97 if (old == null) { 98 for (Map.Entry<String, String> mapEntry : contextMap.entrySet()) { 99 org.apache.log4j.MDC.put(mapEntry.getKey(), mapEntry.getValue()); 100 } 101 } else { 102 old.clear(); 103 old.putAll(contextMap); 104 } 105 } 106 107 @Override pushByKey(String key, String value)108 public void pushByKey(String key, String value) { 109 threadLocalMapOfDeques.pushByKey(key, value); 110 } 111 112 @Override popByKey(String key)113 public String popByKey(String key) { 114 return threadLocalMapOfDeques.popByKey(key); 115 } 116 117 @Override getCopyOfDequeByKey(String key)118 public Deque<String> getCopyOfDequeByKey(String key) { 119 return threadLocalMapOfDeques.getCopyOfDequeByKey(key); 120 } 121 122 @Override clearDequeByKey(String key)123 public void clearDequeByKey(String key) { 124 threadLocalMapOfDeques.clearDequeByKey(key); 125 } 126 } 127