diff --git a/slf4j-ext/src/main/java/org/slf4j/NDC.java b/slf4j-ext/src/main/java/org/slf4j/NDC.java index 623e96d3e..b8afd32fc 100644 --- a/slf4j-ext/src/main/java/org/slf4j/NDC.java +++ b/slf4j-ext/src/main/java/org/slf4j/NDC.java @@ -1,7 +1,7 @@ /** * Copyright (c) 2004-2011 QOS.ch * All rights reserved. - * + *

* Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including @@ -9,10 +9,10 @@ * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: - * + *

* The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. - * + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND @@ -20,7 +20,6 @@ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * */ package org.slf4j; @@ -29,9 +28,9 @@ public class NDC { private static int size() { int i = 0; - while (true) { + while(true) { String val = MDC.get(PREFIX + i); - if (val != null) { + if(val != null) { i++; } else { break; @@ -40,14 +39,28 @@ private static int size() { return i; } + /** + * Adds a new value to the current thread's nested diagnostic context. The + * context parameter cannot be null. + * + * @param val a non-null value to add to the nested diagnostic context + * @throws IllegalArgumentException in case the val parameter is null + */ public static void push(String val) { int next = size(); MDC.put(PREFIX + next, val); } + /** + * Removes the last added value from the current thread's nested diagnostic context and returns it. + * If the nested diagnostic context is empty, an empty String will be returned. + * + * @return the nested diagnostic context value removed, or an empty String if the nested diagnostic context is + * empty. + */ public static String pop() { int next = size(); - if (next == 0) { + if(next == 0) { return ""; } int last = next - 1; @@ -57,4 +70,28 @@ public static String pop() { return val; } + /** + * Returns whether or not the provided context value is already included in the current thread's + * nested diagnostic context. A null value will always return false. + * + * @param context the context to check for in the nested diagnostic context. + * @return true if the provided context exists within the nested diagnostic context, + * false if it does not exist or the provided context value was null. + */ + public static boolean contains(String context) { + if(context == null) { + return false; + } + + int i = 0; + String val; + while((val = MDC.get(PREFIX + i)) != null) { + if(val.equals(context)) { + return true; + } + i++; + } + return false; + } + } diff --git a/slf4j-ext/src/test/java/org/slf4j/NDCTest.java b/slf4j-ext/src/test/java/org/slf4j/NDCTest.java index e25164ee6..1f04a3aff 100644 --- a/slf4j-ext/src/test/java/org/slf4j/NDCTest.java +++ b/slf4j-ext/src/test/java/org/slf4j/NDCTest.java @@ -24,11 +24,11 @@ */ package org.slf4j; -import static org.junit.Assert.assertEquals; - import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.*; + public class NDCTest { @Before @@ -57,4 +57,29 @@ public void testSmoke2() { assertEquals("b", result1); assertEquals("a", result0); } + + @Test + public void testContains() { + assertFalse("nested context does not contain null value", NDC.contains(null)); + assertFalse("default nested context does not contain \"a\" value", NDC.contains("a")); + + NDC.push("a"); + + assertTrue("nested context contains \"a\" after being pushed", NDC.contains("a")); + assertFalse("nested context does not contain \"b\" before being pushed", NDC.contains("b")); + + NDC.push("b"); + + assertTrue("nested context still contains \"a\" after \"b\" is pushed", NDC.contains("a")); + assertTrue("nested context contains \"b\" after being pushed", NDC.contains("b")); + + NDC.pop(); + + assertFalse("nested context no longer contains \"b\" after being popped", NDC.contains("b")); + assertTrue("nested context still contains \"a\" after \"b\" is popped", NDC.contains("a")); + + NDC.pop(); + + assertFalse("nested context no longer contains \"a\" after being popped", NDC.contains("a")); + } }