-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
190 additions
and
48 deletions.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.jboss.logmanager; | ||
|
||
import java.util.Map; | ||
|
||
public interface MDCProvider { | ||
|
||
/** | ||
* Get the value for a key, or {@code null} if there is no mapping. | ||
* | ||
* @param key the key | ||
* @return the value | ||
*/ | ||
String get(String key); | ||
|
||
/** | ||
* Get the value for a key, or {@code null} if there is no mapping. | ||
* | ||
* @param key the key | ||
* @return the value | ||
*/ | ||
Object getObject(String key); | ||
|
||
/** | ||
* Set the value of a key, returning the old value (if any) or {@code null} if there was none. | ||
* | ||
* @param key the key | ||
* @param value the new value | ||
* @return the old value or {@code null} if there was none | ||
*/ | ||
String put(String key, String value); | ||
|
||
/** | ||
* Set the value of a key, returning the old value (if any) or {@code null} if there was none. | ||
* | ||
* @param key the key | ||
* @param value the new value | ||
* @return the old value or {@code null} if there was none | ||
*/ | ||
Object putObject(String key, Object value); | ||
|
||
/** | ||
* Remove a key. | ||
* | ||
* @param key the key | ||
* @return the old value or {@code null} if there was none | ||
*/ | ||
String remove(String key); | ||
|
||
/** | ||
* Remove a key. | ||
* | ||
* @param key the key | ||
* @return the old value or {@code null} if there was none | ||
*/ | ||
Object removeObject(String key); | ||
|
||
/** | ||
* Get a copy of the MDC map. This is a relatively expensive operation. | ||
* | ||
* @return a copy of the map | ||
*/ | ||
Map<String, String> copy(); | ||
|
||
/** | ||
* Get a copy of the MDC map. This is a relatively expensive operation. | ||
* | ||
* @return a copy of the map | ||
*/ | ||
Map<String, Object> copyObject(); | ||
|
||
/** | ||
* Clear the current MDC map. | ||
*/ | ||
void clear(); | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
core/src/main/java/org/jboss/logmanager/ThreadLocalMDC.java
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.jboss.logmanager; | ||
|
||
import java.util.Map; | ||
|
||
final class ThreadLocalMDC implements MDCProvider { | ||
private static final Holder mdc = new Holder(); | ||
|
||
@Override | ||
public String get(String key) { | ||
final Object value = getObject(key); | ||
return value == null ? null : value.toString(); | ||
} | ||
|
||
@Override | ||
public Object getObject(String key) { | ||
return mdc.get().get(key); | ||
} | ||
|
||
@Override | ||
public String put(String key, String value) { | ||
final Object oldValue = putObject(key, value); | ||
return oldValue == null ? null : oldValue.toString(); | ||
} | ||
|
||
@Override | ||
public Object putObject(String key, Object value) { | ||
if (key == null) { | ||
throw new NullPointerException("key is null"); | ||
} | ||
if (value == null) { | ||
throw new NullPointerException("value is null"); | ||
} | ||
return mdc.get().put(key, value); | ||
} | ||
|
||
@Override | ||
public String remove(String key) { | ||
final Object oldValue = removeObject(key); | ||
return oldValue == null ? null : oldValue.toString(); | ||
} | ||
|
||
@Override | ||
public Object removeObject(String key) { | ||
return mdc.get().remove(key); | ||
} | ||
|
||
@Override | ||
public Map<String, String> copy() { | ||
final FastCopyHashMap<String, String> result = new FastCopyHashMap<>(); | ||
for (Map.Entry<String, Object> entry : mdc.get().entrySet()) { | ||
result.put(entry.getKey(), entry.getValue().toString()); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> copyObject() { | ||
return mdc.get().clone(); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
mdc.get().clear(); | ||
} | ||
|
||
private static final class Holder extends InheritableThreadLocal<FastCopyHashMap<String, Object>> { | ||
|
||
@Override | ||
protected FastCopyHashMap<String, Object> childValue(final FastCopyHashMap<String, Object> parentValue) { | ||
return new FastCopyHashMap<>(parentValue); | ||
} | ||
|
||
@Override | ||
protected FastCopyHashMap<String, Object> initialValue() { | ||
return new FastCopyHashMap<>(); | ||
} | ||
} | ||
} |