From 48be0340d71d4629f4611fa9123c3d50121c0523 Mon Sep 17 00:00:00 2001 From: Sjoerd Talsma Date: Sat, 30 Nov 2024 19:24:14 +0100 Subject: [PATCH] Remove more ContextManagers references. Signed-off-by: Sjoerd Talsma --- .../nl/talsmasoftware/context/api/ContextManager.java | 2 +- .../context/clearable/ClearableContextManagerTest.java | 5 ++--- .../context/core/NoContextManagersTest.java | 6 +++--- .../core/concurrent/ContextAwareExecutorServiceTest.java | 4 ++-- .../managers/locale/CurrentLocaleManagerTest.java | 4 ++-- managers/context-manager-log4j2/README.md | 4 ++-- .../threadcontext/Log4j2ThreadContextManagerTest.java | 4 ++-- managers/context-manager-opentracing/README.md | 2 +- .../managers/opentracing/ContextScopeManagerTest.java | 4 ++-- .../context/managers/opentracing/Issue30Test.java | 4 +++- .../context/managers/opentracing/SpanManagerTest.java | 4 ++-- managers/context-manager-servletrequest/README.md | 9 +++++---- .../servletrequest/ServletRequestContextManagerTest.java | 4 ++-- managers/context-manager-slf4j/README.md | 2 +- .../context/managers/slf4j/mdc/Slf4jMdcManager.java | 4 ++-- .../context/managers/slf4j/mdc/Slf4jMdcManagerTest.java | 4 ++-- managers/context-manager-spring-security/README.md | 2 +- .../security/SpringSecurityContextManagerTest.java | 4 ++-- 18 files changed, 37 insertions(+), 35 deletions(-) diff --git a/context-propagation-api/src/main/java/nl/talsmasoftware/context/api/ContextManager.java b/context-propagation-api/src/main/java/nl/talsmasoftware/context/api/ContextManager.java index f5ed1368..ecd55a6c 100644 --- a/context-propagation-api/src/main/java/nl/talsmasoftware/context/api/ContextManager.java +++ b/context-propagation-api/src/main/java/nl/talsmasoftware/context/api/ContextManager.java @@ -62,7 +62,7 @@ public interface ContextManager { * to clear all contexts before returning threads to the pool. * *

- * This method normally should only get called by {@code ContextManagers.clearActiveContexts()}. + * This method normally should only get called by {@linkplain #clearAll()}. * * @since 2.0.0 */ diff --git a/context-propagation-core/src/test/java/nl/talsmasoftware/context/clearable/ClearableContextManagerTest.java b/context-propagation-core/src/test/java/nl/talsmasoftware/context/clearable/ClearableContextManagerTest.java index 5f335690..5a56fdc1 100644 --- a/context-propagation-core/src/test/java/nl/talsmasoftware/context/clearable/ClearableContextManagerTest.java +++ b/context-propagation-core/src/test/java/nl/talsmasoftware/context/clearable/ClearableContextManagerTest.java @@ -16,7 +16,6 @@ package nl.talsmasoftware.context.clearable; import nl.talsmasoftware.context.api.ContextManager; -import nl.talsmasoftware.context.core.ContextManagers; import nl.talsmasoftware.context.dummy.ThrowingContextManager; import org.junit.jupiter.api.Test; @@ -42,7 +41,7 @@ public void testClearActiveContexts_byManager() { CLEARABLE.initializeNewContext("Third value"); assertThat(CLEARABLE.getActiveContextValue(), is("Third value")); - ContextManagers.clearActiveContexts(); + ContextManager.clearAll(); assertThat(CLEARABLE.getActiveContextValue(), is(nullValue())); } @@ -50,7 +49,7 @@ public void testClearActiveContexts_byManager() { public void testClearActiveContexts_exception() { ThrowingContextManager.onGet = new IllegalStateException("Cannot get the current active context!"); - ContextManagers.clearActiveContexts(); + ContextManager.clearAll(); // Mustn't throw exceptions. } } diff --git a/context-propagation-core/src/test/java/nl/talsmasoftware/context/core/NoContextManagersTest.java b/context-propagation-core/src/test/java/nl/talsmasoftware/context/core/NoContextManagersTest.java index ba4194e0..399b591f 100644 --- a/context-propagation-core/src/test/java/nl/talsmasoftware/context/core/NoContextManagersTest.java +++ b/context-propagation-core/src/test/java/nl/talsmasoftware/context/core/NoContextManagersTest.java @@ -36,14 +36,14 @@ public class NoContextManagersTest { @BeforeEach public void avoidContextManagersCache() { - ContextManagers.useClassLoader(new ClassLoader(Thread.currentThread().getContextClassLoader()) { + ContextManager.useClassLoader(new ClassLoader(Thread.currentThread().getContextClassLoader()) { }); assertThat("Move service file", SERVICE_FILE.renameTo(TMP_SERVICE_FILE), is(true)); } @AfterEach public void resetDefaultClassLoader() { - ContextManagers.useClassLoader(null); + ContextManager.useClassLoader(null); assertThat("Restore service file!", TMP_SERVICE_FILE.renameTo(SERVICE_FILE), is(true)); } @@ -69,7 +69,7 @@ public void testCreateSnapshot_withoutContextManagers() { @Test public void testClearManagedContexts_withoutContextManagers() { - ContextManagers.clearActiveContexts(); // there should be no exception + ContextManager.clearAll(); // there should be no exception } } diff --git a/context-propagation-core/src/test/java/nl/talsmasoftware/context/core/concurrent/ContextAwareExecutorServiceTest.java b/context-propagation-core/src/test/java/nl/talsmasoftware/context/core/concurrent/ContextAwareExecutorServiceTest.java index cf15f253..1042fe6f 100644 --- a/context-propagation-core/src/test/java/nl/talsmasoftware/context/core/concurrent/ContextAwareExecutorServiceTest.java +++ b/context-propagation-core/src/test/java/nl/talsmasoftware/context/core/concurrent/ContextAwareExecutorServiceTest.java @@ -15,7 +15,7 @@ */ package nl.talsmasoftware.context.core.concurrent; -import nl.talsmasoftware.context.core.ContextManagers; +import nl.talsmasoftware.context.api.ContextManager; import nl.talsmasoftware.context.dummy.DummyContextManager; import nl.talsmasoftware.context.dummy.ThrowingContextManager; import org.junit.jupiter.api.AfterEach; @@ -58,7 +58,7 @@ public void tearDownExecutor() throws InterruptedException { @BeforeEach @AfterEach public void clearActiveContexts() { - ContextManagers.clearActiveContexts(); + ContextManager.clearAll(); } @Test diff --git a/managers/context-manager-locale/src/test/java/nl/talsmasoftware/context/managers/locale/CurrentLocaleManagerTest.java b/managers/context-manager-locale/src/test/java/nl/talsmasoftware/context/managers/locale/CurrentLocaleManagerTest.java index 1c4fa945..d1a857c8 100644 --- a/managers/context-manager-locale/src/test/java/nl/talsmasoftware/context/managers/locale/CurrentLocaleManagerTest.java +++ b/managers/context-manager-locale/src/test/java/nl/talsmasoftware/context/managers/locale/CurrentLocaleManagerTest.java @@ -16,7 +16,7 @@ package nl.talsmasoftware.context.managers.locale; import nl.talsmasoftware.context.api.Context; -import nl.talsmasoftware.context.core.ContextManagers; +import nl.talsmasoftware.context.api.ContextManager; import nl.talsmasoftware.context.core.concurrent.ContextAwareExecutorService; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -128,7 +128,7 @@ public void testClearActiveContexts() { Context dutchCtx = MANAGER.initializeNewContext(DUTCH); Context englishCtx = MANAGER.initializeNewContext(ENGLISH); - ContextManagers.clearActiveContexts(); + ContextManager.clearAll(); assertThat(MANAGER.getActiveContextValue(), is(nullValue())); assertThat(CurrentLocaleHolder.getOrDefault(), is(DEFAULT_LOCALE)); diff --git a/managers/context-manager-log4j2/README.md b/managers/context-manager-log4j2/README.md index 25c69b92..2c7a7648 100644 --- a/managers/context-manager-log4j2/README.md +++ b/managers/context-manager-log4j2/README.md @@ -21,7 +21,7 @@ Add it to your classpath. Done! Now the data of the Log4j 2 Thread Context is copied into each snapshot -from the `ContextManagers.createSnapshot()` method +from the `ContextSnapshot.capture()` method to be reactivated by the `Contextsnapshot.reactivate()` call. The `ContextAwareExecutorService` automatically propagates the full Thread Context data into all executed tasks this way. @@ -31,7 +31,7 @@ data, if any: Thread Context stack values are pushed on top of the existing stack; map entries are added to the existing map, only replacing existing ones in case of a map key conflict. -Calling `ContextManagers.clearActiveContexts()` will clear the Thread Context +Calling `ContextManager.clearAll()` will clear the Thread Context data of the current thread. [maven-img]: https://img.shields.io/maven-central/v/nl.talsmasoftware.context/log4j2-propagation diff --git a/managers/context-manager-log4j2/src/test/java/nl/talsmasoftware/context/managers/log4j2/threadcontext/Log4j2ThreadContextManagerTest.java b/managers/context-manager-log4j2/src/test/java/nl/talsmasoftware/context/managers/log4j2/threadcontext/Log4j2ThreadContextManagerTest.java index 17f15716..f13bc858 100644 --- a/managers/context-manager-log4j2/src/test/java/nl/talsmasoftware/context/managers/log4j2/threadcontext/Log4j2ThreadContextManagerTest.java +++ b/managers/context-manager-log4j2/src/test/java/nl/talsmasoftware/context/managers/log4j2/threadcontext/Log4j2ThreadContextManagerTest.java @@ -16,8 +16,8 @@ package nl.talsmasoftware.context.managers.log4j2.threadcontext; import nl.talsmasoftware.context.api.Context; +import nl.talsmasoftware.context.api.ContextManager; import nl.talsmasoftware.context.api.ContextSnapshot; -import nl.talsmasoftware.context.core.ContextManagers; import nl.talsmasoftware.context.core.concurrent.ContextAwareExecutorService; import org.apache.logging.log4j.ThreadContext; import org.junit.jupiter.api.AfterEach; @@ -235,7 +235,7 @@ void testClearActiveContexts() { ThreadContext.put("map1", "value1"); ThreadContext.push("stack1"); - ContextManagers.clearActiveContexts(); + ContextManager.clearAll(); assertThat(ThreadContext.isEmpty(), is(true)); assertThat(ThreadContext.getDepth(), is(0)); diff --git a/managers/context-manager-opentracing/README.md b/managers/context-manager-opentracing/README.md index 855e1419..3833a6ac 100644 --- a/managers/context-manager-opentracing/README.md +++ b/managers/context-manager-opentracing/README.md @@ -21,7 +21,7 @@ Add it to your classpath. Done! Now the `GlobalTracer.get().activeSpan()` is included in each -snapshot created by the `ContextManagers.createSnapshot()` method +snapshot created by the `ContextSnapshot.capture()` method and reactivated with it. This includes all usages of the `ContextAwareExecutorService`. diff --git a/managers/context-manager-opentracing/src/test/java/nl/talsmasoftware/context/managers/opentracing/ContextScopeManagerTest.java b/managers/context-manager-opentracing/src/test/java/nl/talsmasoftware/context/managers/opentracing/ContextScopeManagerTest.java index 587d1ecb..a8edb360 100644 --- a/managers/context-manager-opentracing/src/test/java/nl/talsmasoftware/context/managers/opentracing/ContextScopeManagerTest.java +++ b/managers/context-manager-opentracing/src/test/java/nl/talsmasoftware/context/managers/opentracing/ContextScopeManagerTest.java @@ -21,7 +21,7 @@ import io.opentracing.util.GlobalTracer; import io.opentracing.util.GlobalTracerTestUtil; import nl.talsmasoftware.context.api.Context; -import nl.talsmasoftware.context.core.ContextManagers; +import nl.talsmasoftware.context.api.ContextManager; import nl.talsmasoftware.context.core.concurrent.ContextAwareExecutorService; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -59,7 +59,7 @@ public void registerMockGlobalTracer() { @AfterEach public void cleanup() { threadpool.shutdown(); - ContextManagers.clearActiveContexts(); + ContextManager.clearAll(); GlobalTracerTestUtil.resetGlobalTracer(); } diff --git a/managers/context-manager-opentracing/src/test/java/nl/talsmasoftware/context/managers/opentracing/Issue30Test.java b/managers/context-manager-opentracing/src/test/java/nl/talsmasoftware/context/managers/opentracing/Issue30Test.java index 66a7533f..711bcb03 100644 --- a/managers/context-manager-opentracing/src/test/java/nl/talsmasoftware/context/managers/opentracing/Issue30Test.java +++ b/managers/context-manager-opentracing/src/test/java/nl/talsmasoftware/context/managers/opentracing/Issue30Test.java @@ -38,10 +38,12 @@ * ContextAware* when there is no active span.
* The problem is that {@code ContextSnapshot.capture()} only stores * {@code activeContext.getValue()} which is {@code null}
- * {@code ContextManagers.reactivate()} then retreives {@code null} from the snapshot and + * {@code ContextSnapshot.reactivate()} then retreives {@code null} from the snapshot and * calls {@code OpentracingSpanManger.initializeNewContext(null)} + * *

* The test in ScopeContext.close only checks that closed is false before calling span.close. + * *

* The fix could be to set closed to true in initializeNewContext() if span is null, * or add a nullcheck in SpanContext.close. diff --git a/managers/context-manager-opentracing/src/test/java/nl/talsmasoftware/context/managers/opentracing/SpanManagerTest.java b/managers/context-manager-opentracing/src/test/java/nl/talsmasoftware/context/managers/opentracing/SpanManagerTest.java index e64ea9ee..8ce1e297 100644 --- a/managers/context-manager-opentracing/src/test/java/nl/talsmasoftware/context/managers/opentracing/SpanManagerTest.java +++ b/managers/context-manager-opentracing/src/test/java/nl/talsmasoftware/context/managers/opentracing/SpanManagerTest.java @@ -23,7 +23,7 @@ import io.opentracing.util.GlobalTracer; import io.opentracing.util.GlobalTracerTestUtil; import io.opentracing.util.ThreadLocalScopeManager; -import nl.talsmasoftware.context.core.ContextManagers; +import nl.talsmasoftware.context.api.ContextManager; import nl.talsmasoftware.context.core.concurrent.ContextAwareExecutorService; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -212,7 +212,7 @@ public void testClearingAllContexts() { Scope scope = mockTracer.scopeManager().activate(span); assertThat(SpanManager.provider().getActiveContextValue(), is(sameInstance(span))); - ContextManagers.clearActiveContexts(); + ContextManager.clearAll(); // TODO Test after this is merged: https://github.com/opentracing/opentracing-java/pull/313 // assertThat(SpanManager.provider().getActiveContextValue(), is(nullValue())); } diff --git a/managers/context-manager-servletrequest/README.md b/managers/context-manager-servletrequest/README.md index e0efdc4c..e19899cf 100644 --- a/managers/context-manager-servletrequest/README.md +++ b/managers/context-manager-servletrequest/README.md @@ -1,4 +1,4 @@ -[![Maven Version][maven-img]][maven] +[![Maven Version][maven-img]][maven] # ServletRequest propagation library @@ -23,9 +23,10 @@ if the `ServletRequestContextFilter` was applied to the inbound request. Done! Now the `ServletRequestContextManager.currentServletRequest()` is propagated into each -snapshot created by the `ContextManagers.createSnapshot()` method. +snapshot created by the `ContextSnapshot.capture()` method. This includes all usages of the `ContextAwareExecutorService`. - [maven-img]: https://img.shields.io/maven-central/v/nl.talsmasoftware.context/servletrequest-propagation - [maven]: https://search.maven.org/artifact/nl.talsmasoftware.context/servletrequest-propagation +[maven-img]: https://img.shields.io/maven-central/v/nl.talsmasoftware.context/servletrequest-propagation + +[maven]: https://search.maven.org/artifact/nl.talsmasoftware.context/servletrequest-propagation diff --git a/managers/context-manager-servletrequest/src/test/java/nl/talsmasoftware/context/managers/servletrequest/ServletRequestContextManagerTest.java b/managers/context-manager-servletrequest/src/test/java/nl/talsmasoftware/context/managers/servletrequest/ServletRequestContextManagerTest.java index 8d0bdfd7..b20c3759 100644 --- a/managers/context-manager-servletrequest/src/test/java/nl/talsmasoftware/context/managers/servletrequest/ServletRequestContextManagerTest.java +++ b/managers/context-manager-servletrequest/src/test/java/nl/talsmasoftware/context/managers/servletrequest/ServletRequestContextManagerTest.java @@ -16,7 +16,7 @@ package nl.talsmasoftware.context.managers.servletrequest; import nl.talsmasoftware.context.api.Context; -import nl.talsmasoftware.context.core.ContextManagers; +import nl.talsmasoftware.context.api.ContextManager; import nl.talsmasoftware.context.core.concurrent.ContextAwareExecutorService; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -91,7 +91,7 @@ public void testClearableImplementation() { assertThat(ctx.getValue(), is(sameInstance(request))); assertThat(ServletRequestContextManager.provider().getActiveContextValue(), is(sameInstance(request))); - ContextManagers.clearActiveContexts(); + ContextManager.clearAll(); assertThat(ctx.getValue(), is(nullValue())); // must have been closed assertThat(ServletRequestContextManager.provider().getActiveContextValue(), is(nullValue())); } diff --git a/managers/context-manager-slf4j/README.md b/managers/context-manager-slf4j/README.md index a78c6a11..aa3c2d5e 100644 --- a/managers/context-manager-slf4j/README.md +++ b/managers/context-manager-slf4j/README.md @@ -21,7 +21,7 @@ Add it to your classpath. Done! Now the `MDC.getCopyOfContextMap()` is copied into each snapshot -from the `ContextManagers.createSnapshot()` method +from the `ContextSnapshot.capture()` method to be reactivated by the `Contextsnapshot.reactivate()` call. The `ContextAwareExecutorService` automatically propagates the full [MDC] content into all executed tasks this way. diff --git a/managers/context-manager-slf4j/src/main/java/nl/talsmasoftware/context/managers/slf4j/mdc/Slf4jMdcManager.java b/managers/context-manager-slf4j/src/main/java/nl/talsmasoftware/context/managers/slf4j/mdc/Slf4jMdcManager.java index b69d1ed1..9bcd1e73 100644 --- a/managers/context-manager-slf4j/src/main/java/nl/talsmasoftware/context/managers/slf4j/mdc/Slf4jMdcManager.java +++ b/managers/context-manager-slf4j/src/main/java/nl/talsmasoftware/context/managers/slf4j/mdc/Slf4jMdcManager.java @@ -38,7 +38,7 @@ * *

* This manager does not implement the optional {@link #clear()} method. - * {@code ContextManagers.clearActiveContexts()} will therefore not clear the {@linkplain MDC}. + * {@linkplain ContextManager#clearAll()} will therefore not clear the {@linkplain MDC}. * Please use {@linkplain MDC#clear()} explicitly to do that. * * @author Sjoerd Talsma @@ -103,7 +103,7 @@ public Map getActiveContextValue() { * This manager does not support clearing the MDC. * *

- * Calling {@code ContextManagers.clearActiveContexts()} will therefore not clear the + * Calling {@linkplain ContextManager#clearAll()} will therefore not clear the * MDC by default. This can be achieved by calling {@link MDC#clear()} explicitly. * * @see MDC#clear() diff --git a/managers/context-manager-slf4j/src/test/java/nl/talsmasoftware/context/managers/slf4j/mdc/Slf4jMdcManagerTest.java b/managers/context-manager-slf4j/src/test/java/nl/talsmasoftware/context/managers/slf4j/mdc/Slf4jMdcManagerTest.java index 62c1d24d..3b1056c1 100644 --- a/managers/context-manager-slf4j/src/test/java/nl/talsmasoftware/context/managers/slf4j/mdc/Slf4jMdcManagerTest.java +++ b/managers/context-manager-slf4j/src/test/java/nl/talsmasoftware/context/managers/slf4j/mdc/Slf4jMdcManagerTest.java @@ -16,8 +16,8 @@ package nl.talsmasoftware.context.managers.slf4j.mdc; import nl.talsmasoftware.context.api.Context; +import nl.talsmasoftware.context.api.ContextManager; import nl.talsmasoftware.context.api.ContextSnapshot; -import nl.talsmasoftware.context.core.ContextManagers; import nl.talsmasoftware.context.core.concurrent.ContextAwareExecutorService; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -113,7 +113,7 @@ public void testSlf4jMdcContextToString() { public void testClearActiveContexts() { MDC.put("dummy", "value"); // Test no-op for MdcManager - ContextManagers.clearActiveContexts(); + ContextManager.clearAll(); assertThat(MDC.get("dummy"), is("value")); } } diff --git a/managers/context-manager-spring-security/README.md b/managers/context-manager-spring-security/README.md index 587653df..876a254c 100644 --- a/managers/context-manager-spring-security/README.md +++ b/managers/context-manager-spring-security/README.md @@ -22,7 +22,7 @@ Add it to your classpath. Done! Now the `SecurityContextHolder.getContext()` is copied into each snapshot -from the `ContextManagers.createSnapshot()` method +from the `ContextSnapshot.capture()` method to be reactivated by the `Contextsnapshot.reactivate()` call. The `ContextAwareExecutorService` automatically propagates the active [spring security] `Authentication` into all executed tasks this way. diff --git a/managers/context-manager-spring-security/src/test/java/nl/talsmasoftware/context/managers/spring/security/SpringSecurityContextManagerTest.java b/managers/context-manager-spring-security/src/test/java/nl/talsmasoftware/context/managers/spring/security/SpringSecurityContextManagerTest.java index b9ca0131..8ed175c5 100644 --- a/managers/context-manager-spring-security/src/test/java/nl/talsmasoftware/context/managers/spring/security/SpringSecurityContextManagerTest.java +++ b/managers/context-manager-spring-security/src/test/java/nl/talsmasoftware/context/managers/spring/security/SpringSecurityContextManagerTest.java @@ -15,8 +15,8 @@ */ package nl.talsmasoftware.context.managers.spring.security; +import nl.talsmasoftware.context.api.ContextManager; import nl.talsmasoftware.context.api.ContextSnapshot; -import nl.talsmasoftware.context.core.ContextManagers; import nl.talsmasoftware.context.core.concurrent.ContextAwareExecutorService; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -117,7 +117,7 @@ public void testClearableImplementation() { setAuthentication("Vincent Vega"); assertThat(SpringSecurityContextManager.provider().getActiveContextValue().getName(), is("Vincent Vega")); - ContextManagers.clearActiveContexts(); + ContextManager.clearAll(); assertThat(SpringSecurityContextManager.provider().getActiveContextValue(), is(nullValue())); }