Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

Commit 8be024f

Browse files
committed
Add additional code comments about the thread specific implementations
1 parent 022e9b5 commit 8be024f

File tree

5 files changed

+15
-2
lines changed

5 files changed

+15
-2
lines changed

app/src/main/java/gov/hhs/cdc/trustedintermediary/external/javalin/DomainsRegistration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ static DomainConnector constructNewDomainConnector(Class<? extends DomainConnect
122122
static Handler createHandler(
123123
Function<DomainRequest, DomainResponse> handler, boolean isProtected) {
124124
return (Context ctx) -> {
125-
ApplicationContext.clearThreadRegistrations();
125+
ApplicationContext
126+
.clearThreadRegistrations(); // clear this thread's specific registrations from
127+
// its previous use
126128

127129
LOGGER.logInfo(ctx.method().name() + " " + ctx.url());
128130

etor/src/main/java/gov/hhs/cdc/trustedintermediary/etor/EtorDomainRegistration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ public Map<HttpEndpoint, Function<DomainRequest, DomainResponse>> domainRegistra
134134
MessageLinkStorage.class, FileMessageLinkStorage.getInstance());
135135
}
136136

137-
// these are default implementations
138137
if (ApplicationContext.isPropertyPresent("REPORT_STREAM_URL_PREFIX")) {
139138
ApplicationContext.register(
140139
RSEndpointClient.class, ReportStreamEndpointClient.getInstance());
@@ -176,6 +175,8 @@ DomainResponse handleResults(DomainRequest request) {
176175
DomainResponse handleMetadata(DomainRequest request) {
177176
if (Boolean.parseBoolean(request.getHeaders().get("load-test"))
178177
&& ApplicationContext.isPropertyPresent("REPORT_STREAM_URL_PREFIX")) {
178+
// register the mock RS endpoint for this HTTP request because we don't want to call RS
179+
// for real when doing a load test.
179180
ApplicationContext.registerForThread(
180181
RSEndpointClient.class, MockRSEndpointClient.getInstance());
181182
}
@@ -235,6 +236,8 @@ protected DomainResponse handleMessageRequest(
235236

236237
if (Boolean.parseBoolean(request.getHeaders().get("load-test"))
237238
&& ApplicationContext.isPropertyPresent("REPORT_STREAM_URL_PREFIX")) {
239+
// register the mock RS endpoint for this HTTP request because we don't want to call RS
240+
// for real when doing a load test.
238241
ApplicationContext.registerForThread(
239242
RSEndpointClient.class, MockRSEndpointClient.getInstance());
240243
}

etor/src/main/java/gov/hhs/cdc/trustedintermediary/etor/metadata/partner/PartnerMetadataOrchestrator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ private PartnerMetadataOrchestrator() {}
4444
public void updateMetadataForInboundMessage(PartnerMetadata partnerMetadata)
4545
throws PartnerMetadataException {
4646

47+
// can't @Inject because the implementation can be different for this specific thread
4748
RSEndpointClient rsclient = ApplicationContext.getImplementation(RSEndpointClient.class);
4849

4950
logger.logInfo(
@@ -133,6 +134,7 @@ public Optional<PartnerMetadata> getMetadata(String inboundReportId)
133134
var outboundReportId = partnerMetadata.outboundReportId();
134135
if (metadataIsStale(partnerMetadata) && outboundReportId != null) {
135136

137+
// can't @Inject because the implementation can be different for this specific thread
136138
RSEndpointClient rsclient =
137139
ApplicationContext.getImplementation(RSEndpointClient.class);
138140

etor/src/main/java/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamSenderHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ protected Optional<String> sendToReportStream(
4242
String body, String fhirResourceId, PartnerMetadataMessageType messageType)
4343
throws UnableToSendMessageException {
4444

45+
// can't @Inject because the implementation can be different for this specific thread
4546
RSEndpointClient rsclient = ApplicationContext.getImplementation(RSEndpointClient.class);
4647

4748
String bearerToken;

shared/src/main/java/gov/hhs/cdc/trustedintermediary/context/ApplicationContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public static void register(Class<?> clazz, Object implementation) {
4343
IMPLEMENTATIONS.add(implementation.getClass());
4444
}
4545

46+
/**
47+
* Registers an implementation for a class _only_ for the current executing thread (which
48+
* currently is one-to-one with an HTTP request).
49+
*/
4650
public static void registerForThread(Class<?> clazz, Object implementation) {
4751
Map<Class<?>, Object> threadObjectMap = THREAD_OBJECT_MAP.get();
4852
if (threadObjectMap == null) {
@@ -60,6 +64,7 @@ public static void registerForThread(Class<?> clazz, Object implementation) {
6064
injectIntoNonSingleton(implementation);
6165
}
6266

67+
/** Removes the stored implementations for the current thread that calls this method. */
6368
public static void clearThreadRegistrations() {
6469
THREAD_OBJECT_MAP.remove();
6570
}

0 commit comments

Comments
 (0)