@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); } }
/** * 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()); } } }
/** * 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); } } }
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(); }
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); }
@BeforeClass public static void setupClass() { System.setProperty(DefaultThreadContextMap.INHERITABLE_MAP, "true"); ThreadContext.init(); }
@AfterClass public static void tearDownClass() { System.clearProperty(DefaultThreadContextMap.INHERITABLE_MAP); ThreadContext.init(); }