
Author: ceki Date: Thu Dec 18 14:22:04 2008 New Revision: 2083 Modified: logback/trunk/logback-classic/src/main/java/org/slf4j/impl/LogbackMDCAdapter.java logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/MDCTest.java Log: Fixed LBCLASSIC-98 (NPE thrown when calling setContextMap on a fresh MDC) Modified: logback/trunk/logback-classic/src/main/java/org/slf4j/impl/LogbackMDCAdapter.java ============================================================================== --- logback/trunk/logback-classic/src/main/java/org/slf4j/impl/LogbackMDCAdapter.java (original) +++ logback/trunk/logback-classic/src/main/java/org/slf4j/impl/LogbackMDCAdapter.java Thu Dec 18 14:22:04 2008 @@ -10,12 +10,9 @@ * A <em>Mapped Diagnostic Context</em>, or MDC in short, is an instrument * for distinguishing interleaved log output from different sources. Log output * is typically interleaved when a server handles multiple clients - * near-simultaneously. - * <p> - * <b><em>The MDC is managed on a per thread basis</em></b>. A child thread - * automatically inherits a <em>copy</em> of the mapped diagnostic context of - * its parent. - * <p> + * near-simultaneously. <p> <b><em>The MDC is managed on a per thread basis</em></b>. + * A child thread automatically inherits a <em>copy</em> of the mapped + * diagnostic context of its parent. <p> * * For more information about MDC, please refer to the online manual at * http://logback.qos.ch/manual/mdc.html @@ -24,7 +21,8 @@ */ public class LogbackMDCAdapter implements MDCAdapter { - //final CopyOnInheritThreadLocal copyOnInheritThreadLocal = new CopyOnInheritThreadLocal(); + // final CopyOnInheritThreadLocal copyOnInheritThreadLocal = new + // CopyOnInheritThreadLocal(); final CopyOnInheritThreadLocal copyOnInheritThreadLocal = new CopyOnInheritThreadLocal(); @@ -36,13 +34,11 @@ * the <code>key</code> parameter into the current thread's context map. * Note that contrary to log4j, the <code>val</code> parameter can be null. * - * <p> - * If the current thread does not have a context map it is created as a side - * effect of this call. + * <p> If the current thread does not have a context map it is created as a + * side effect of this call. * - * <p> - * Each time a value is added, a new instance of the map is created. This is - * to be certain that the serialization process will operate on the updated + * <p> Each time a value is added, a new instance of the map is created. This + * is to be certain that the serialization process will operate on the updated * map and not send a reference to the old map, thus not allowing the remote * logback component to see the latest changes. * @@ -68,8 +64,7 @@ /** * Get the context identified by the <code>key</code> parameter. * - * <p> - * This method has no side effects. + * <p> This method has no side effects. */ public String get(String key) { HashMap<String, String> hashMap = copyOnInheritThreadLocal.get(); @@ -84,11 +79,10 @@ /** * Remove the the context identified by the <code>key</code> parameter. * - * <p> - * Each time a value is removed, a new instance of the map is created. This is - * to be certain that the serialization process will operate on the updated - * map and not send a reference to the old map, thus not allowing the remote - * logback component to see the latest changes. + * <p> Each time a value is removed, a new instance of the map is created. + * This is to be certain that the serialization process will operate on the + * updated map and not send a reference to the old map, thus not allowing the + * remote logback component to see the latest changes. */ public void remove(String key) { HashMap<String, String> oldMap = copyOnInheritThreadLocal.get(); @@ -160,7 +154,9 @@ copyOnInheritThreadLocal.set(newMap); // hints for the garbage collector - oldMap.clear(); - oldMap = null; + if (oldMap != null) { + oldMap.clear(); + oldMap = null; + } } } Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/MDCTest.java ============================================================================== --- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/MDCTest.java (original) +++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/MDCTest.java Thu Dec 18 14:22:04 2008 @@ -12,7 +12,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import java.util.HashMap; + import org.junit.Test; +import org.slf4j.MDC; public class MDCTest { @@ -37,4 +40,9 @@ } + @Test + public void testLBCLASSIC_98() { + MDC.setContextMap(new HashMap<String, String>()); + } + }