<commit_before>package se.callista.microservices.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.util.Map;
import java.util.concurrent.Callable;

/**
 * Created by magnus on 02/05/15.
 */
public class MDCHystrixContextCallable<K> implements Callable {

    private static final Logger LOG = LoggerFactory.getLogger(MDCHystrixContextCallable.class);

    private final Callable<K> actual;
    private final Map parentMDC;

    public MDCHystrixContextCallable(Callable<K> actual) {
        LOG.debug("Init MDCHystrixContextCallable...");
        this.actual = actual;
        this.parentMDC = MDC.getCopyOfContextMap();
    }

    @Override
    public K call() throws Exception {
        LOG.debug("Call using MDCHystrixContextCallable...");
        Map childMDC = MDC.getCopyOfContextMap();

        try {
            MDC.setContextMap(parentMDC);
            return actual.call();
        } finally {
            MDC.setContextMap(childMDC);
        }
    }
}<commit_msg>Corrected a bug in Hystrix Context switch handling that caused a NPE due to a null MDC object.<commit_after>package se.callista.microservices.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.util.Map;
import java.util.concurrent.Callable;

/**
 * Created by magnus on 02/05/15.
 */
public class MDCHystrixContextCallable<K> implements Callable {

    private static final Logger LOG = LoggerFactory.getLogger(MDCHystrixContextCallable.class);

    private final Callable<K> actual;
    private final Map parentMDC;

    public MDCHystrixContextCallable(Callable<K> actual) {
        LOG.debug("Init MDCHystrixContextCallable...");
        this.actual = actual;
        this.parentMDC = MDC.getCopyOfContextMap();
    }

    @Override
    public K call() throws Exception {
        LOG.debug("Call using MDCHystrixContextCallable...");
        Map childMDC = MDC.getCopyOfContextMap();

        try {
            MDC.setContextMap(parentMDC);
            return actual.call();
        } finally {
            if (childMDC == null) {
                LOG.debug("Call done. ChildMDC is null so we clear the MDC.");
                MDC.clear();
            } else {
                LOG.debug("Call done. Reset MDC to the ChildMDC.");
                MDC.setContextMap(childMDC);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.10050462573591253, 'fraction_numerical': 0.007569386038687973, 'mean_word_length': 2.6320610687022903, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2657083', 'n_tokens_mistral': 739, 'n_tokens_neox': 712, 'n_words': 165}