001/**
002 * Copyright (c) 2004-2022 QOS.ch Sarl (Switzerland)
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j.reload4j;
026
027import java.util.Deque;
028import java.util.HashMap;
029import java.util.Map;
030
031import org.slf4j.helpers.ThreadLocalMapOfStacks;
032import org.slf4j.spi.MDCAdapter;
033
034public class Reload4jMDCAdapter implements MDCAdapter {
035
036    private final ThreadLocalMapOfStacks threadLocalMapOfDeques = new ThreadLocalMapOfStacks();
037    
038    @Override
039    public void clear() {
040        @SuppressWarnings("rawtypes")
041        Map map = org.apache.log4j.MDC.getContext();
042        if (map != null) {
043            map.clear();
044        }
045    }
046
047    @Override
048    public String get(String key) {
049        return (String) org.apache.log4j.MDC.get(key);
050    }
051
052    /**
053     * Put a context value (the <code>val</code> parameter) as identified with
054     * the <code>key</code> parameter into the current thread's context map. The
055     * <code>key</code> parameter cannot be null. Log4j does <em>not</em>
056     * support null for the <code>val</code> parameter.
057     * 
058     * <p>
059     * This method delegates all work to log4j's MDC.
060     * 
061     * @throws IllegalArgumentException
062     *             in case the "key" or <b>"val"</b> parameter is null
063     */
064    @Override
065    public void put(String key, String val) {
066        org.apache.log4j.MDC.put(key, val);
067    }
068
069    @Override
070    public void remove(String key) {
071        org.apache.log4j.MDC.remove(key);
072    }
073
074    @SuppressWarnings({ "rawtypes", "unchecked" })
075    public Map getCopyOfContextMap() {
076        Map old = org.apache.log4j.MDC.getContext();
077        if (old != null) {
078            return new HashMap(old);
079        } else {
080            return null;
081        }
082    }
083
084    @SuppressWarnings({ "rawtypes", "unchecked" })
085    @Override
086    public void setContextMap(Map<String, String> contextMap) {
087        Map old = org.apache.log4j.MDC.getContext();
088        
089        // we must cater for the case where the contextMap argument is null 
090        if (contextMap == null) {
091            if (old != null) {
092                old.clear();
093            }
094            return;
095        }
096        
097        if (old == null) {
098            for (Map.Entry<String, String> mapEntry : contextMap.entrySet()) {
099                org.apache.log4j.MDC.put(mapEntry.getKey(), mapEntry.getValue());
100            }
101        } else {
102            old.clear();
103            old.putAll(contextMap);
104        }
105    }
106
107    @Override
108    public void pushByKey(String key, String value) {
109        threadLocalMapOfDeques.pushByKey(key, value);
110    }
111
112    @Override
113    public String popByKey(String key) {
114        return threadLocalMapOfDeques.popByKey(key);    
115     }
116
117    @Override
118    public Deque<String> getCopyOfDequeByKey(String key) {
119        return threadLocalMapOfDeques.getCopyOfDequeByKey(key);
120    }
121
122    @Override
123    public void clearDequeByKey(String key) {
124        threadLocalMapOfDeques.clearDequeByKey(key);
125    }
126}