Skip to content

Commit

Permalink
Merge pull request #3396 from jamezp/RESTEASY-3280-5.x
Browse files Browse the repository at this point in the history
[RESTEASY-3280] Do not load the ThreadContext's from a service loader…
  • Loading branch information
jamezp committed Jan 24, 2023
2 parents abd781d + b6cd339 commit b2f6374
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
Expand Up @@ -24,7 +24,6 @@
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -323,20 +322,13 @@ private static Runnable runnable(final Map<ThreadContext<Object>, Object> contex
@SuppressWarnings("unchecked")
private static Map<ThreadContext<Object>, Object> getContexts() {
final Map<ThreadContext<Object>, Object> contexts = new LinkedHashMap<>();
if (System.getSecurityManager() == null) {
ServiceLoader.load(ThreadContext.class).forEach(context -> contexts.put(context, context.capture()));
} else {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
ServiceLoader.load(ThreadContext.class).forEach(context -> contexts.put(context, context.capture()));
return null;
});
}
// Load any registered providers
final ThreadContexts threadContexts = ResteasyProviderFactory.getInstance().getContextData(ThreadContexts.class);
if (threadContexts != null) {
for (ThreadContext<Object> context : threadContexts.getThreadContexts()) {
contexts.put(context, context.capture());
}
ThreadContexts threadContexts = ResteasyProviderFactory.getInstance().getContextData(ThreadContexts.class);
if (threadContexts == null) {
threadContexts = new ThreadContexts();
}
for (ThreadContext<Object> context : threadContexts.getThreadContexts()) {
contexts.put(context, context.capture());
}
return contexts;
}
Expand Down
Expand Up @@ -19,10 +19,13 @@

package org.jboss.resteasy.spi.concurrent;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.ServiceLoader;

import org.jboss.resteasy.spi.PriorityComparator;

Expand All @@ -39,7 +42,7 @@ public class ThreadContexts {
* Creates a new collection instance.
*/
public ThreadContexts() {
contexts = new ArrayList<>();
contexts = createContexts();
}

/**
Expand Down Expand Up @@ -85,4 +88,17 @@ public ThreadContexts clear() {
}
return this;
}

private static List<ThreadContext<Object>> createContexts() {
final List<ThreadContext<Object>> contexts = new ArrayList<>();
if (System.getSecurityManager() == null) {
ServiceLoader.load(ThreadContext.class).forEach(contexts::add);
} else {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
ServiceLoader.load(ThreadContext.class).forEach(contexts::add);
return null;
});
}
return contexts;
}
}

0 comments on commit b2f6374

Please sign in to comment.