-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring in ContextContainer and implementation (#10)
* Refactoring in ContextContainer Rename SimpleContextContainer to DefaultContextContainer. Drop ContextAccessorLoader and ThreadLocalAccessorLoader in favor of configuring instances of those directly on DefaultContextContainer. The layers above (e.g. Web Framework, GraphQL transport, etc.) have the responsibility to know what accessors should be used, and therefore ContextContainer should be directly configurable. In other words it cannot be a global mechanism like ServiceLoader. * Drop NOOP constants and implementations If a ContextContainerPropagator isn't found, it should be an IllegalStateException. It means we don't know how to save to a specific external context, and something is wrong. Or otherwise it would silently not propagate. If a ContextContainer isn't present, there are several cases. - the code expects a ContextContainer and therefore needs to check through the isNoop() method anyway to protect itself, but it might forget to do so. - the code expects there may or may not be a ContextContainer, but wants to start a new one if not found. Here it also needs to check isNoop(), but if it doesn't do so, it could even lead to errors in code that expects an actual ContextContainer and not a noop. - only code that expects there may or may not be a ContextContainer and doesn't care either way benefits; arguably though a noop lurking always has the potential of causing side effects and surprises. For ContextContainerPropagator, we now always throw IllegalStateException if there isn't a compatible one registered. For ContextContainer, restoreFrom raises ISE while separately there is a restoreIfPresentFrom that returns null if not found. * Polishing and minor refactoring Ordering of methods in DefaultContextContainer consistent with the order in ContextContainer. Rename ContextContainerPropagator to ContextContainerAdapter.
- Loading branch information
1 parent
1d04f0a
commit 306ba5c
Showing
11 changed files
with
157 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
...propagation-api/src/main/java/io/micrometer/contextpropagation/ContextAccessorLoader.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...ion-api/src/main/java/io/micrometer/contextpropagation/ContextContainerAdapterLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2002-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micrometer.contextpropagation; | ||
|
||
import java.util.Map; | ||
import java.util.ServiceLoader; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* Loads {@link ThreadLocalAccessor} and {@link ContextAccessor}. | ||
*/ | ||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
final class ContextContainerAdapterLoader { | ||
|
||
private static final ServiceLoader<ContextContainerAdapter> adapters = ServiceLoader.load(ContextContainerAdapter.class); | ||
|
||
private static final Map<Class, ContextContainerAdapter> cache = new ConcurrentHashMap<>(); | ||
|
||
static ContextContainerAdapter getAdapterToWrite(Object context) { | ||
return cache.computeIfAbsent(context.getClass(), aClass -> { | ||
for (ContextContainerAdapter contextContainerAdapter : adapters) { | ||
if (contextContainerAdapter.supportsSaveTo(aClass)) { | ||
return contextContainerAdapter; | ||
} | ||
} | ||
throw new IllegalStateException( | ||
"No ContextContainerAdapter for context type: " + context.getClass()); | ||
}); | ||
} | ||
|
||
static ContextContainerAdapter getAdapterToRead(Object context) { | ||
return cache.computeIfAbsent(context.getClass(), aClass -> { | ||
for (ContextContainerAdapter contextContainerAdapter : adapters) { | ||
if (contextContainerAdapter.supportsRestoreFrom(aClass)) { | ||
return contextContainerAdapter; | ||
} | ||
} | ||
throw new IllegalStateException( | ||
"No ContextContainerAdapter for context type: " + context.getClass()); | ||
}); | ||
} | ||
} |
Oops, something went wrong.