From 2e8c8ebedb3faa8487d60e45c0bc9e9c83dee06c Mon Sep 17 00:00:00 2001 From: Daniel Fiala Date: Thu, 25 Apr 2024 13:29:15 +0200 Subject: [PATCH] feat(transcoding): added check to disable build step when transcoding is not enabled --- .../java/io/quarkus/deployment/Feature.java | 1 + .../deployment/GrpcTranscodingProcessor.java | 50 +++++++++++-------- extensions/grpc/runtime/pom.xml | 4 -- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/Feature.java b/core/deployment/src/main/java/io/quarkus/deployment/Feature.java index 25c5149188eb4f..09565fcf7c5d7f 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/Feature.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/Feature.java @@ -27,6 +27,7 @@ public enum Feature { FLYWAY, GRPC_CLIENT, GRPC_SERVER, + GRPC_TRANSCODING, HIBERNATE_ORM, HIBERNATE_ENVERS, HIBERNATE_ORM_PANACHE, diff --git a/extensions/grpc/deployment/src/main/java/io/quarkus/grpc/deployment/GrpcTranscodingProcessor.java b/extensions/grpc/deployment/src/main/java/io/quarkus/grpc/deployment/GrpcTranscodingProcessor.java index 2779244c0d9de3..a47baedf99229b 100644 --- a/extensions/grpc/deployment/src/main/java/io/quarkus/grpc/deployment/GrpcTranscodingProcessor.java +++ b/extensions/grpc/deployment/src/main/java/io/quarkus/grpc/deployment/GrpcTranscodingProcessor.java @@ -1,8 +1,9 @@ package io.quarkus.grpc.deployment; +import static io.quarkus.deployment.Feature.GRPC_TRANSCODING; + import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -12,8 +13,6 @@ import org.jboss.jandex.ClassInfo; import org.jboss.jandex.DotName; import org.jboss.jandex.MethodInfo; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import io.quarkus.arc.deployment.AdditionalBeanBuildItem; import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem; @@ -29,7 +28,9 @@ import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.CombinedIndexBuildItem; import io.quarkus.deployment.builditem.FeatureBuildItem; +import io.quarkus.deployment.builditem.ServiceStartBuildItem; import io.quarkus.deployment.builditem.ShutdownContextBuildItem; +import io.quarkus.grpc.runtime.config.GrpcTranscodingConfig; import io.quarkus.grpc.transcoding.GrpcTranscodingContainer; import io.quarkus.grpc.transcoding.GrpcTranscodingMethod; import io.quarkus.grpc.transcoding.GrpcTranscodingRecorder; @@ -40,35 +41,39 @@ class GrpcTranscodingProcessor { - private static final String FEATURE = "quarkus-grpc-transcoding"; - private static final Logger log = LoggerFactory.getLogger(GrpcTranscodingProcessor.class); - - private static final String[] PRODUCES = new String[] { "application/json" }; - private static final String[] CONSUMES = new String[] { "application/json" }; - - @BuildStep - FeatureBuildItem feature() { - return new FeatureBuildItem(FEATURE); - } - @BuildStep void processGeneratedBeans(CombinedIndexBuildItem index, + GrpcTranscodingConfig transcodingConfig, + BuildProducer features, BuildProducer transformers, BuildProducer marshallings, BuildProducer delegatingBeans) { - Set generatedBeans = new HashSet<>(); + // Check if the gRPC transcoding feature is enabled + if (!transcodingConfig.enabled) { + return; + } - for (ClassInfo generatedBean : index.getIndex().getAllKnownImplementors(GrpcDotNames.GRPC_TRANSCODING)) { - DotName generatedBeanName = generatedBean.name(); - generatedBeans.add(generatedBeanName); + Map> methods = new HashMap<>(); + for (ClassInfo generatedBean : index.getIndex().getAllKnownImplementors(GrpcDotNames.GRPC_TRANSCODING)) { // Extract gRPC transcoding configuration from methods and store the results List transcodingMethods = collectTranscodingMethods(generatedBean); - marshallings.produce(new GrpcTranscodingBuildItem(generatedBeanName, transcodingMethods)); + methods.put(generatedBean.name(), transcodingMethods); + } + + if (methods.isEmpty()) { + return; } + for (Map.Entry> entry : methods.entrySet()) { + marshallings.produce(new GrpcTranscodingBuildItem(entry.getKey(), entry.getValue())); + } + + features.produce(new FeatureBuildItem(GRPC_TRANSCODING)); delegatingBeans.produce(AdditionalBeanBuildItem.unremovableOf(GrpcTranscodingContainer.class)); + Set generatedBeans = methods.keySet(); + transformers.produce(new AnnotationsTransformerBuildItem(new AnnotationsTransformer() { @Override public boolean appliesTo(AnnotationTarget.Kind kind) { @@ -90,11 +95,12 @@ public void transform(TransformationContext context) { @BuildStep @Record(value = ExecutionTime.RUNTIME_INIT) @Consume(SyntheticBeansRuntimeInitBuildItem.class) - GrpcTranscodingServerBuildItem buildTranscoding(GrpcTranscodingRecorder recorder, + ServiceStartBuildItem buildTranscoding(GrpcTranscodingRecorder recorder, VertxBuildItem vertx, VertxWebRouterBuildItem routerBuildItem, List marshallings, Capabilities capabilities, + BuildProducer transcodingServer, ShutdownContextBuildItem shutdown) { // Build a map to organize the collected gRPC transcoding methods by service name Map> methods = new HashMap<>(); @@ -106,7 +112,9 @@ GrpcTranscodingServerBuildItem buildTranscoding(GrpcTranscodingRecorder recorder // Create and initialize the gRPC transcoding server RuntimeValue server = recorder.initializeMarshallingServer(vertx.getVertx(), routerBuildItem.getHttpRouter(), shutdown, methods, capabilities.isPresent(Capability.SECURITY)); - return new GrpcTranscodingServerBuildItem(server); + + transcodingServer.produce(new GrpcTranscodingServerBuildItem(server)); + return new ServiceStartBuildItem(GRPC_TRANSCODING); } private static List collectTranscodingMethods(ClassInfo service) { diff --git a/extensions/grpc/runtime/pom.xml b/extensions/grpc/runtime/pom.xml index f200dddc867d13..4e5c6517b293da 100644 --- a/extensions/grpc/runtime/pom.xml +++ b/extensions/grpc/runtime/pom.xml @@ -13,10 +13,6 @@ Quarkus - gRPC - Runtime Serve and consume gRPC services - - com.google.api.grpc - proto-google-common-protos - jakarta.annotation jakarta.annotation-api