Skip to content

Commit

Permalink
Remove more ContextManagers references.
Browse files Browse the repository at this point in the history
Signed-off-by: Sjoerd Talsma <[email protected]>
  • Loading branch information
sjoerdtalsma committed Nov 30, 2024
1 parent 977984e commit 48be034
Show file tree
Hide file tree
Showing 18 changed files with 37 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public interface ContextManager<T> {
* to clear all contexts before returning threads to the pool.
*
* <p>
* 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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -42,15 +41,15 @@ public void testClearActiveContexts_byManager() {
CLEARABLE.initializeNewContext("Third value");
assertThat(CLEARABLE.getActiveContextValue(), is("Third value"));

ContextManagers.clearActiveContexts();
ContextManager.clearAll();
assertThat(CLEARABLE.getActiveContextValue(), is(nullValue()));
}

@Test
public void testClearActiveContexts_exception() {
ThrowingContextManager.onGet = new IllegalStateException("Cannot get the current active context!");

ContextManagers.clearActiveContexts();
ContextManager.clearAll();
// Mustn't throw exceptions.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,7 +58,7 @@ public void tearDownExecutor() throws InterruptedException {
@BeforeEach
@AfterEach
public void clearActiveContexts() {
ContextManagers.clearActiveContexts();
ContextManager.clearAll();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -128,7 +128,7 @@ public void testClearActiveContexts() {
Context<Locale> dutchCtx = MANAGER.initializeNewContext(DUTCH);
Context<Locale> englishCtx = MANAGER.initializeNewContext(ENGLISH);

ContextManagers.clearActiveContexts();
ContextManager.clearAll();
assertThat(MANAGER.getActiveContextValue(), is(nullValue()));
assertThat(CurrentLocaleHolder.getOrDefault(), is(DEFAULT_LOCALE));

Expand Down
4 changes: 2 additions & 2 deletions managers/context-manager-log4j2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion managers/context-manager-opentracing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,7 +59,7 @@ public void registerMockGlobalTracer() {
@AfterEach
public void cleanup() {
threadpool.shutdown();
ContextManagers.clearActiveContexts();
ContextManager.clearAll();
GlobalTracerTestUtil.resetGlobalTracer();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
* ContextAware* when there is no active span.<br>
* The problem is that {@code ContextSnapshot.capture()} only stores
* {@code activeContext.getValue()} which is {@code null}<br>
* {@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)}
*
* <p>
* The test in ScopeContext.close only checks that closed is false before calling span.close.
*
* <p>
* The fix could be to set closed to true in initializeNewContext() if span is null,
* or add a nullcheck in SpanContext.close.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}
Expand Down
9 changes: 5 additions & 4 deletions managers/context-manager-servletrequest/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Maven Version][maven-img]][maven]
[![Maven Version][maven-img]][maven]

# ServletRequest propagation library

Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}
Expand Down
2 changes: 1 addition & 1 deletion managers/context-manager-slf4j/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*
* <p>
* This manager does not implement the optional {@link #clear()} method.
* {@code ContextManagers.clearActiveContexts()} will therefore <strong>not</strong> clear the {@linkplain MDC}.
* {@linkplain ContextManager#clearAll()} will therefore <strong>not</strong> clear the {@linkplain MDC}.
* Please use {@linkplain MDC#clear()} explicitly to do that.
*
* @author Sjoerd Talsma
Expand Down Expand Up @@ -103,7 +103,7 @@ public Map<String, String> getActiveContextValue() {
* This manager does not support clearing the MDC.
*
* <p>
* Calling {@code ContextManagers.clearActiveContexts()} will therefore <strong>not</strong> clear the
* Calling {@linkplain ContextManager#clearAll()} will therefore <strong>not</strong> clear the
* MDC by default. This can be achieved by calling {@link MDC#clear()} explicitly.
*
* @see MDC#clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}
}
2 changes: 1 addition & 1 deletion managers/context-manager-spring-security/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}

Expand Down

0 comments on commit 48be034

Please sign in to comment.