Skip to content

Commit f2aeed1

Browse files
authored
fix: Use deployment CL to load TransportMetadata services if different from TCCL (#263)
I found working on wildfly-extras/a2a-java-sdk-server-jakarta#26 that in WildFly gRPC happens on a separate thread with its own classloader. Hence the serviceloader lookup from the default TCCL didn't work in WildFLy.
1 parent c0910e1 commit f2aeed1

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

transport/grpc/src/main/java/io/a2a/transport/grpc/handler/GrpcHandler.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,35 @@ private <V> void handleInternalError(StreamObserver<V> responseObserver, Throwab
380380
private AgentCard getAgentCardInternal() {
381381
AgentCard agentCard = getAgentCard();
382382
if (initialised.compareAndSet(false, true)) {
383-
// Validate transport configuration
384-
AgentCardValidator.validateTransportConfiguration(agentCard);
383+
// Validate transport configuration with proper classloader context
384+
validateTransportConfigurationWithCorrectClassLoader(agentCard);
385385
}
386386
return agentCard;
387387
}
388388

389+
private void validateTransportConfigurationWithCorrectClassLoader(AgentCard agentCard) {
390+
ClassLoader originalTccl = Thread.currentThread().getContextClassLoader();
391+
ClassLoader deploymentCl = getDeploymentClassLoader();
392+
boolean switchCl = deploymentCl != null && deploymentCl != originalTccl;
393+
394+
try {
395+
if (switchCl) {
396+
// Set TCCL to the classloader that loaded this class, which should have access
397+
// to the deployment classpath containing META-INF/services files
398+
Thread.currentThread().setContextClassLoader(deploymentCl);
399+
}
400+
AgentCardValidator.validateTransportConfiguration(agentCard);
401+
} finally {
402+
if (switchCl) {
403+
Thread.currentThread().setContextClassLoader(originalTccl);
404+
}
405+
}
406+
}
407+
408+
protected ClassLoader getDeploymentClassLoader() {
409+
return this.getClass().getClassLoader();
410+
}
411+
389412
public static void setStreamingSubscribedRunnable(Runnable runnable) {
390413
streamingSubscribedRunnable = runnable;
391414
}

0 commit comments

Comments
 (0)