下面列出了怎么用org.apache.logging.log4j.spi.ReadOnlyThreadContextMap的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* <em>Consider private, used for testing.</em>
*/
static void init() {
ThreadContextMapFactory.init();
contextMap = null;
final PropertiesUtil managerProps = PropertiesUtil.getProperties();
boolean disableAll = managerProps.getBooleanProperty(DISABLE_ALL);
useStack = !(managerProps.getBooleanProperty(DISABLE_STACK) || disableAll);
boolean useMap = !(managerProps.getBooleanProperty(DISABLE_MAP) || disableAll);
contextStack = new DefaultThreadContextStack(useStack);
if (!useMap) {
contextMap = new NoOpThreadContextMap();
} else {
contextMap = ThreadContextMapFactory.createThreadContextMap();
}
if (contextMap instanceof ReadOnlyThreadContextMap) {
readOnlyContextMap = (ReadOnlyThreadContextMap) contextMap;
} else {
readOnlyContextMap = null;
}
}
@Override
public ReadOnlyStringMap rawContextData() {
final ReadOnlyThreadContextMap map = ThreadContext.getThreadContextMap();
if (map instanceof ReadOnlyStringMap) {
return (ReadOnlyStringMap) map;
}
// note: default ThreadContextMap is null
final Map<String, String> copy = ThreadContext.getImmutableContext();
return copy.isEmpty() ? ContextDataFactory.emptyFrozenContextData() : new JdkMapAdapterStringMap(copy);
}
private static ContextDataInjector createDefaultInjector() {
final ReadOnlyThreadContextMap threadContextMap = ThreadContext.getThreadContextMap();
// note: map may be null (if legacy custom ThreadContextMap was installed by user)
if (threadContextMap instanceof DefaultThreadContextMap || threadContextMap == null) {
return new ThreadContextDataInjector.ForDefaultThreadContextMap(); // for non StringMap-based context maps
}
if (threadContextMap instanceof CopyOnWrite) {
return new ThreadContextDataInjector.ForCopyOnWriteThreadContextMap();
}
return new ThreadContextDataInjector.ForGarbageFreeThreadContextMap();
}
private void testContextDataInjector() {
ReadOnlyThreadContextMap readOnlythreadContextMap = getThreadContextMap();
assertThat("thread context map class name",
(readOnlythreadContextMap == null) ? null : readOnlythreadContextMap.getClass().getName(),
is(equalTo(readOnlythreadContextMapClassName)));
ContextDataInjector contextDataInjector = createInjector();
StringMap stringMap = contextDataInjector.injectContextData(null, new SortedArrayStringMap());
assertThat("thread context map", ThreadContext.getContext(), allOf(hasEntry("foo", "bar"), not(hasKey("baz"))));
assertThat("context map", stringMap.toMap(), allOf(hasEntry("foo", "bar"), not(hasKey("baz"))));
if (!stringMap.isFrozen()) {
stringMap.clear();
assertThat("thread context map", ThreadContext.getContext(), allOf(hasEntry("foo", "bar"), not(hasKey("baz"))));
assertThat("context map", stringMap.toMap().entrySet(), is(empty()));
}
ThreadContext.put("foo", "bum");
ThreadContext.put("baz", "bam");
assertThat("thread context map", ThreadContext.getContext(), allOf(hasEntry("foo", "bum"), hasEntry("baz", "bam")));
if (stringMap.isFrozen()) {
assertThat("context map", stringMap.toMap(), allOf(hasEntry("foo", "bar"), not(hasKey("baz"))));
} else {
assertThat("context map", stringMap.toMap().entrySet(), is(empty()));
}
}
private static String contextMap() {
final ReadOnlyThreadContextMap impl = ThreadContext.getThreadContextMap();
return impl == null ? ContextImpl.WEBAPP.implClassSimpleName() : impl.getClass().getSimpleName();
}
/**
* Returns a read-only view of the internal data structure used to store thread context key-value pairs,
* or {@code null} if the internal data structure does not implement the
* {@code ReadOnlyThreadContextMap} interface.
* <p>
* The {@link DefaultThreadContextMap} implementation does not implement {@code ReadOnlyThreadContextMap}, so by
* default this method returns {@code null}.
* </p>
*
* @return the internal data structure used to store thread context key-value pairs or {@code null}
* @see ThreadContextMapFactory
* @see DefaultThreadContextMap
* @see org.apache.logging.log4j.spi.CopyOnWriteSortedArrayThreadContextMap
* @see org.apache.logging.log4j.spi.GarbageFreeSortedArrayThreadContextMap
* @since 2.8
*/
public static ReadOnlyThreadContextMap getThreadContextMap() {
return readOnlyContextMap;
}