-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[92] Allow the ThreadContextMDCMap to work with both the JBoss Log Ma…
…nager 3.0 and 2.1. Signed-off-by: James R. Perkins <[email protected]>
- Loading branch information
Showing
1 changed file
with
25 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,9 @@ | |
|
||
package org.jboss.logmanager.log4j; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodType; | ||
import java.util.Map; | ||
|
||
import org.apache.logging.log4j.spi.ThreadContextMap; | ||
|
@@ -30,6 +33,19 @@ | |
* @author <a href="mailto:[email protected]">James R. Perkins</a> | ||
*/ | ||
public class ThreadContextMDCMap implements ThreadContextMap { | ||
private static final MethodHandle IS_EMPTY; | ||
|
||
static { | ||
// Use MDC.isEmpty() if it's available | ||
MethodHandle handle = null; | ||
try { | ||
handle = MethodHandles.publicLookup() | ||
.findStatic(MDC.class, "isEmpty", MethodType.methodType(boolean.class)); | ||
} catch (NoSuchMethodException | IllegalAccessException ignore) { | ||
} | ||
IS_EMPTY = handle; | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
MDC.clear(); | ||
|
@@ -58,7 +74,15 @@ public Map<String, String> getImmutableMapOrNull() { | |
|
||
@Override | ||
public boolean isEmpty() { | ||
return MDC.isEmpty(); | ||
if (IS_EMPTY != null) { | ||
try { | ||
return (boolean) IS_EMPTY.invoke(); | ||
} catch (Throwable ignore) { | ||
// Ignore and fall through to the fallback | ||
} | ||
} | ||
// Fallback to a simple copy/isEmpty. This will not perform well, but also is likely not used much | ||
return MDC.copy().isEmpty(); | ||
} | ||
|
||
@Override | ||
|