Skip to content

Commit f5e7103

Browse files
committed
Remove built-in ThreadLocalAccessor
The ThreadLocalAccessor in 1.0 was intended as a temporary solution that is now replaced by a similar contract in the `io.micrometer:context-propagation` library. Unfortunately, there is no way to adapt one to the other, but it is trivial to implement the new contract and register it either with `ContextRegistry#getInstance` on startup or through the `ServiceLoader` mechanism. See gh-459
1 parent f680892 commit f5e7103

File tree

7 files changed

+1
-249
lines changed

7 files changed

+1
-249
lines changed

spring-graphql/src/main/java/org/springframework/graphql/execution/CompositeThreadLocalAccessor.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

spring-graphql/src/main/java/org/springframework/graphql/execution/SecurityContextThreadLocalAccessor.java

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.graphql.execution;
1717

18-
import java.util.Map;
19-
2018
import io.micrometer.context.ThreadLocalAccessor;
2119

2220
import org.springframework.security.core.context.SecurityContext;
@@ -33,9 +31,7 @@
3331
* @author Rossen Stoyanchev
3432
* @since 1.0.0
3533
*/
36-
@SuppressWarnings("deprecation")
37-
public class SecurityContextThreadLocalAccessor implements ThreadLocalAccessor<Object>,
38-
org.springframework.graphql.execution.ThreadLocalAccessor {
34+
public class SecurityContextThreadLocalAccessor implements ThreadLocalAccessor<Object> {
3935

4036
private final static boolean springSecurityPresent = ClassUtils.isPresent(
4137
"org.springframework.security.core.context.SecurityContext",
@@ -81,27 +77,6 @@ public void reset() {
8177
}
8278

8379

84-
// Temporary implementation of deprecated ThreadLocalAccessor while it is still used
85-
// in the Boot starter. If registered as such, it is ignored.
86-
87-
@Override
88-
public void extractValues(Map<String, Object> container) {
89-
container.put((String) key(), SecurityContextHolder.getContext());
90-
}
91-
92-
@Override
93-
public void restoreValues(Map<String, Object> values) {
94-
if (values.containsKey((String) key())) {
95-
SecurityContextHolder.setContext((SecurityContext) values.get((String) key()));
96-
}
97-
}
98-
99-
@Override
100-
public void resetValues(Map<String, Object> values) {
101-
SecurityContextHolder.clearContext();
102-
}
103-
104-
10580
private static class DelegateAccessor implements ThreadLocalAccessor<Object> {
10681

10782
@Override

spring-graphql/src/main/java/org/springframework/graphql/execution/ThreadLocalAccessor.java

Lines changed: 0 additions & 80 deletions
This file was deleted.

spring-graphql/src/main/java/org/springframework/graphql/server/DefaultWebGraphQlHandlerBuilder.java

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,19 @@
2121
import java.util.List;
2222

2323
import io.micrometer.context.ContextSnapshot;
24-
25-
import org.springframework.graphql.execution.SecurityContextThreadLocalAccessor;
26-
import org.springframework.graphql.execution.ThreadLocalAccessor;
2724
import reactor.core.publisher.Mono;
2825

2926
import org.springframework.graphql.ExecutionGraphQlService;
3027
import org.springframework.graphql.server.WebGraphQlInterceptor.Chain;
3128
import org.springframework.lang.Nullable;
3229
import org.springframework.util.Assert;
33-
import org.springframework.util.CollectionUtils;
3430

3531

3632
/**
3733
* Default implementation of {@link WebGraphQlHandler.Builder}.
3834
*
3935
* @author Rossen Stoyanchev
4036
*/
41-
@SuppressWarnings("deprecation")
4237
class DefaultWebGraphQlHandlerBuilder implements WebGraphQlHandler.Builder {
4338

4439
private final ExecutionGraphQlService service;
@@ -48,9 +43,6 @@ class DefaultWebGraphQlHandlerBuilder implements WebGraphQlHandler.Builder {
4843
@Nullable
4944
private WebSocketGraphQlInterceptor webSocketInterceptor;
5045

51-
@Nullable
52-
private List<ThreadLocalAccessor> accessors;
53-
5446

5547
DefaultWebGraphQlHandlerBuilder(ExecutionGraphQlService service) {
5648
Assert.notNull(service, "GraphQlService is required");
@@ -75,30 +67,6 @@ public WebGraphQlHandler.Builder interceptors(List<WebGraphQlInterceptor> interc
7567
return this;
7668
}
7769

78-
@SuppressWarnings("deprecation")
79-
@Override
80-
public WebGraphQlHandler.Builder threadLocalAccessor(ThreadLocalAccessor... accessors) {
81-
return threadLocalAccessors(Arrays.asList(accessors));
82-
}
83-
84-
@SuppressWarnings("deprecation")
85-
@Override
86-
public WebGraphQlHandler.Builder threadLocalAccessors(List<ThreadLocalAccessor> accessors) {
87-
88-
// Filter out SecurityContextThreadLocalAccessor which is registered as a ThreadLocalAccessor
89-
// from the micrometer-metrics/context-propagatation library. This code can be removed when
90-
// SecurityContextThreadLocalAccessor no longer implements the deprecated ThreadLocalAccessor.
91-
accessors = accessors.stream()
92-
.filter(a -> !(a instanceof SecurityContextThreadLocalAccessor))
93-
.toList();
94-
95-
if (!CollectionUtils.isEmpty(accessors)) {
96-
this.accessors = (this.accessors != null) ? this.accessors : new ArrayList<>();
97-
this.accessors.addAll(accessors);
98-
}
99-
return this;
100-
}
101-
10270
@Override
10371
public WebGraphQlHandler build() {
10472

@@ -117,12 +85,6 @@ public WebSocketGraphQlInterceptor getWebSocketInterceptor() {
11785
webSocketInterceptor : new WebSocketGraphQlInterceptor() {});
11886
}
11987

120-
@Nullable
121-
@Override
122-
public ThreadLocalAccessor getThreadLocalAccessor() {
123-
return (CollectionUtils.isEmpty(accessors) ? null : ThreadLocalAccessor.composite(accessors));
124-
}
125-
12688
@Override
12789
public Mono<WebGraphQlResponse> handleRequest(WebGraphQlRequest request) {
12890
ContextSnapshot snapshot = ContextSnapshot.capture();

spring-graphql/src/main/java/org/springframework/graphql/server/WebGraphQlHandler.java

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import reactor.core.publisher.Mono;
2222

2323
import org.springframework.graphql.ExecutionGraphQlService;
24-
import org.springframework.graphql.execution.ThreadLocalAccessor;
25-
import org.springframework.lang.Nullable;
2624

2725

2826
/**
@@ -41,15 +39,6 @@ public interface WebGraphQlHandler {
4139
*/
4240
WebSocketGraphQlInterceptor getWebSocketInterceptor();
4341

44-
/**
45-
* Return the composite {@link ThreadLocalAccessor} that the handler is
46-
* configured with.
47-
* @deprecated as of 1.1.0, together with {@link ThreadLocalAccessor}.
48-
*/
49-
@Deprecated
50-
@Nullable
51-
ThreadLocalAccessor getThreadLocalAccessor();
52-
5342
/**
5443
* Execute the given request and return the response.
5544
* @param request the request to execute
@@ -97,28 +86,6 @@ interface Builder {
9786
*/
9887
Builder interceptors(List<WebGraphQlInterceptor> interceptors);
9988

100-
/**
101-
* Configure accessors for ThreadLocal variables to use to extract
102-
* ThreadLocal values at the start of GraphQL execution in the web layer,
103-
* and have those saved, and restored around the invocation of data
104-
* fetchers and exception resolvers.
105-
* @param accessors the accessors to add
106-
* @return this builder
107-
* @deprecated as of 1.1.0 together with {@link ThreadLocalAccessor}.
108-
*/
109-
@Deprecated
110-
Builder threadLocalAccessor(ThreadLocalAccessor... accessors);
111-
112-
/**
113-
* Alternative to {@link #threadLocalAccessor(ThreadLocalAccessor...)} with a
114-
* List.
115-
* @param accessors the list of accessors to add
116-
* @return this builder
117-
* @deprecated as of 1.1.0 together with {@link ThreadLocalAccessor}.
118-
*/
119-
@Deprecated
120-
Builder threadLocalAccessors(List<ThreadLocalAccessor> accessors);
121-
12289
/**
12390
* Build the {@link WebGraphQlHandler} instance.
12491
* @return the built WebGraphQlHandler

spring-graphql/src/testFixtures/java/org/springframework/graphql/GraphQlSetup.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@
3535
import org.springframework.graphql.execution.GraphQlSource;
3636
import org.springframework.graphql.execution.RuntimeWiringConfigurer;
3737
import org.springframework.graphql.execution.SubscriptionExceptionResolver;
38-
import org.springframework.graphql.execution.ThreadLocalAccessor;
3938
import org.springframework.graphql.server.WebGraphQlHandler;
4039
import org.springframework.graphql.server.WebGraphQlInterceptor;
4140
import org.springframework.graphql.server.WebGraphQlSetup;
4241
import org.springframework.graphql.server.webflux.GraphQlHttpHandler;
43-
import org.springframework.lang.Nullable;
4442

4543
/**
4644
* Workflow for GraphQL tests setup that starts with {@link GraphQlSource.Builder}
@@ -58,8 +56,6 @@ public class GraphQlSetup implements GraphQlServiceSetup {
5856

5957
private final List<WebGraphQlInterceptor> interceptors = new ArrayList<>();
6058

61-
private final List<ThreadLocalAccessor> accessors = new ArrayList<>();
62-
6359

6460
private GraphQlSetup(Resource... schemaResources) {
6561
this.graphQlSourceBuilder = GraphQlSource.schemaResourceBuilder().schemaResources(schemaResources);
@@ -147,19 +143,10 @@ public WebGraphQlSetup interceptor(WebGraphQlInterceptor... interceptors) {
147143
return this;
148144
}
149145

150-
@Override
151-
public WebGraphQlSetup threadLocalAccessor(@Nullable ThreadLocalAccessor accessor) {
152-
if (accessor != null) {
153-
this.accessors.add(accessor);
154-
}
155-
return this;
156-
}
157-
158146
public WebGraphQlHandler toWebGraphQlHandler() {
159147
ExecutionGraphQlService service = toGraphQlService();
160148
return WebGraphQlHandler.builder(service)
161149
.interceptors(this.interceptors)
162-
.threadLocalAccessors(this.accessors)
163150
.build();
164151
}
165152

spring-graphql/src/testFixtures/java/org/springframework/graphql/server/WebGraphQlSetup.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616
package org.springframework.graphql.server;
1717

18-
import org.springframework.graphql.execution.ThreadLocalAccessor;
1918
import org.springframework.graphql.server.webflux.GraphQlHttpHandler;
20-
import org.springframework.lang.Nullable;
2119

2220
/**
2321
* Workflow that results in the creation of a {@link WebGraphQlHandler} or
@@ -29,8 +27,6 @@ public interface WebGraphQlSetup {
2927

3028
WebGraphQlSetup interceptor(WebGraphQlInterceptor... interceptors);
3129

32-
WebGraphQlSetup threadLocalAccessor(@Nullable ThreadLocalAccessor accessor);
33-
3430
WebGraphQlHandler toWebGraphQlHandler();
3531

3632
org.springframework.graphql.server.webmvc.GraphQlHttpHandler toHttpHandler();

0 commit comments

Comments
 (0)