001/**
002 * Copyright (c) 2004-2011 QOS.ch
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.log4j12;
026
027import java.util.Deque;
028import java.util.HashMap;
029import java.util.Map;
030
031import org.apache.log4j.MDCFriend;
032import org.slf4j.helpers.ThreadLocalMapOfStacks;
033import org.slf4j.spi.MDCAdapter;
034
035public class Log4jMDCAdapter implements MDCAdapter {
036
037    private final ThreadLocalMapOfStacks threadLocalMapOfDeques = new ThreadLocalMapOfStacks();
038    
039    static {
040        if (VersionUtil.getJavaMajorVersion() >= 9) {
041            MDCFriend.fixForJava9();
042        }
043    }
044
045    @Override
046    public void clear() {
047        @SuppressWarnings("rawtypes")
048        Map map = org.apache.log4j.MDC.getContext();
049        if (map != null) {
050            map.clear();
051        }
052    }
053
054    @Override
055    public String get(String key) {
056        return (String) org.apache.log4j.MDC.get(key);
057    }
058
059    /**
060     * Put a context value (the <code>val</code> parameter) as identified with
061     * the <code>key</code> parameter into the current thread's context map. The
062     * <code>key</code> parameter cannot be null. Log4j does <em>not</em>
063     * support null for the <code>val</code> parameter.
064     * 
065     * <p>
066     * This method delegates all work to log4j's MDC.
067     * 
068     * @throws IllegalArgumentException
069     *             in case the "key" or <b>"val"</b> parameter is null
070     */
071    @Override
072    public void put(String key, String val) {
073        org.apache.log4j.MDC.put(key, val);
074    }
075
076    @Override
077    public void remove(String key) {
078        org.apache.log4j.MDC.remove(key);
079    }
080
081    @SuppressWarnings({ "rawtypes", "unchecked" })
082    public Map getCopyOfContextMap() {
083        Map old = org.apache.log4j.MDC.getContext();
084        if (old != null) {
085            return new HashMap(old);
086        } else {
087            return null;
088        }
089    }
090
091    @SuppressWarnings({ "rawtypes", "unchecked" })
092    @Override
093    public void setContextMap(Map<String, String> contextMap) {
094        Map old = org.apache.log4j.MDC.getContext();
095        
096        // we must cater for the case where the contextMap argument is null 
097        if (contextMap == null) {
098            if (old != null) {
099                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}