Java 类org.apache.logging.log4j.spi.DefaultThreadContextMap 实例源码

项目:logging-log4j2    文件:ThreadContextInheritanceTest.java   
@Test
public void testInheritanceSwitchedOn() throws Exception {
    System.setProperty(DefaultThreadContextMap.INHERITABLE_MAP, "true");
    ThreadContext.init();
    try {
        ThreadContext.clearMap();
        ThreadContext.put("Greeting", "Hello");
        StringBuilder sb = new StringBuilder();
        TestThread thread = new TestThread(sb);
        thread.start();
        thread.join();
        String str = sb.toString();
        assertTrue("Unexpected ThreadContext value. Expected Hello. Actual "
                + str, "Hello".equals(str));
        sb = new StringBuilder();
        thread = new TestThread(sb);
        thread.start();
        thread.join();
        str = sb.toString();
        assertTrue("Unexpected ThreadContext value. Expected Hello. Actual "
                + str, "Hello".equals(str));
    } finally {
        System.clearProperty(DefaultThreadContextMap.INHERITABLE_MAP);
    }
}
项目:logging-log4j2    文件:ThreadContext.java   
/**
 * Puts all given context map entries into the current thread's
 * context map.
 *
 * <p>If the current thread does not have a context map it is
 * created as a side effect.</p>
 * @param m The map.
 * @since 2.7
 */
public static void putAll(final Map<String, String> m) {
    if (contextMap instanceof ThreadContextMap2) {
        ((ThreadContextMap2) contextMap).putAll(m);
    } else if (contextMap instanceof DefaultThreadContextMap) {
        ((DefaultThreadContextMap) contextMap).putAll(m);
    } else {
        for (final Map.Entry<String, String> entry: m.entrySet()) {
            contextMap.put(entry.getKey(), entry.getValue());
        }
    }
}
项目:logging-log4j2    文件:ThreadContext.java   
/**
 * Removes the context values identified by the <code>keys</code> parameter.
 *
 * @param keys The keys to remove.
 *
 * @since 2.8
 */
public static void removeAll(final Iterable<String> keys) {
    if (contextMap instanceof CleanableThreadContextMap) {
        ((CleanableThreadContextMap) contextMap).removeAll(keys);
    } else if (contextMap instanceof DefaultThreadContextMap) {
        ((DefaultThreadContextMap) contextMap).removeAll(keys);
    } else {
        for (final String key : keys) {
            contextMap.remove(key);
        }
    }
}
项目:logging-log4j2    文件:ContextDataInjectorFactory.java   
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();
}
项目:logging-log4j2    文件:AbstractAsyncThreadContextTestBase.java   
public String implClassSimpleName() {
    switch (this) {
        case WEBAPP:
            return DefaultThreadContextMap.class.getSimpleName();
        case GARBAGE_FREE:
            return "GarbageFreeSortedArrayThreadContextMap";
        case COPY_ON_WRITE:
            return "CopyOnWriteSortedArrayThreadContextMap";
    }
    throw new IllegalStateException("Unknown state " + this);
}
项目:logging-log4j2    文件:ThreadContextInheritanceTest.java   
@BeforeClass
public static void setupClass() {
    System.setProperty(DefaultThreadContextMap.INHERITABLE_MAP, "true");
    ThreadContext.init();
}
项目:logging-log4j2    文件:ThreadContextInheritanceTest.java   
@AfterClass
public static void tearDownClass() {
    System.clearProperty(DefaultThreadContextMap.INHERITABLE_MAP);
    ThreadContext.init();
}