Skip to content

Commit

Permalink
feat(transcoding): added check to disable build step when transcoding…
Browse files Browse the repository at this point in the history
… is not enabled
  • Loading branch information
zZHorizonZz committed Apr 25, 2024
1 parent df2ee59 commit 2e8c8eb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum Feature {
FLYWAY,
GRPC_CLIENT,
GRPC_SERVER,
GRPC_TRANSCODING,
HIBERNATE_ORM,
HIBERNATE_ENVERS,
HIBERNATE_ORM_PANACHE,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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<FeatureBuildItem> features,
BuildProducer<AnnotationsTransformerBuildItem> transformers,
BuildProducer<GrpcTranscodingBuildItem> marshallings,
BuildProducer<AdditionalBeanBuildItem> delegatingBeans) {
Set<DotName> 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<DotName, List<GrpcTranscodingMethod>> methods = new HashMap<>();

for (ClassInfo generatedBean : index.getIndex().getAllKnownImplementors(GrpcDotNames.GRPC_TRANSCODING)) {
// Extract gRPC transcoding configuration from methods and store the results
List<GrpcTranscodingMethod> transcodingMethods = collectTranscodingMethods(generatedBean);
marshallings.produce(new GrpcTranscodingBuildItem(generatedBeanName, transcodingMethods));
methods.put(generatedBean.name(), transcodingMethods);
}

if (methods.isEmpty()) {
return;
}

for (Map.Entry<DotName, List<GrpcTranscodingMethod>> entry : methods.entrySet()) {
marshallings.produce(new GrpcTranscodingBuildItem(entry.getKey(), entry.getValue()));
}

features.produce(new FeatureBuildItem(GRPC_TRANSCODING));
delegatingBeans.produce(AdditionalBeanBuildItem.unremovableOf(GrpcTranscodingContainer.class));

Set<DotName> generatedBeans = methods.keySet();

transformers.produce(new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {
@Override
public boolean appliesTo(AnnotationTarget.Kind kind) {
Expand All @@ -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<GrpcTranscodingBuildItem> marshallings,
Capabilities capabilities,
BuildProducer<GrpcTranscodingServerBuildItem> transcodingServer,
ShutdownContextBuildItem shutdown) {
// Build a map to organize the collected gRPC transcoding methods by service name
Map<String, List<GrpcTranscodingMethod>> methods = new HashMap<>();
Expand All @@ -106,7 +112,9 @@ GrpcTranscodingServerBuildItem buildTranscoding(GrpcTranscodingRecorder recorder
// Create and initialize the gRPC transcoding server
RuntimeValue<GrpcTranscodingServer> 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<GrpcTranscodingMethod> collectTranscodingMethods(ClassInfo service) {
Expand Down
4 changes: 0 additions & 4 deletions extensions/grpc/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
<name>Quarkus - gRPC - Runtime</name>
<description>Serve and consume gRPC services</description>
<dependencies>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-common-protos</artifactId>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
Expand Down

0 comments on commit 2e8c8eb

Please sign in to comment.