org.apache.logging.log4j.spi.ReadOnlyThreadContextMap Java Examples

The following examples show how to use org.apache.logging.log4j.spi.ReadOnlyThreadContextMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ThreadContext.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * <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;
    }
}
 
Example #2
Source File: ThreadContextDataInjector.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #3
Source File: ContextDataInjectorFactory.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #4
Source File: ThreadContextDataInjectorTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
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()));
    }
}
 
Example #5
Source File: AbstractAsyncThreadContextTestBase.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
private static String contextMap() {
    final ReadOnlyThreadContextMap impl = ThreadContext.getThreadContextMap();
    return impl == null ? ContextImpl.WEBAPP.implClassSimpleName() : impl.getClass().getSimpleName();
}
 
Example #6
Source File: ThreadContext.java    From logging-log4j2 with Apache License 2.0 2 votes vote down vote up
/**
 * 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;
}