From e36ff419c287a73d23a195f2663ba0dd219214f0 Mon Sep 17 00:00:00 2001 From: Santiago Pericas-Geertsen Date: Tue, 20 Aug 2024 16:00:36 -0400 Subject: [PATCH] Renames package-private Grpc type to GrpcRouteHandler. The name Grpc is too generic and is also used in the API. Adds missing requires in module-info.java. --- .../webserver/grpc/GrpcProtocolHandler.java | 4 +- .../webserver/grpc/GrpcProtocolSelector.java | 2 +- .../io/helidon/webserver/grpc/GrpcRoute.java | 17 +++- .../grpc/{Grpc.java => GrpcRouteHandler.java} | 84 +++++++++---------- .../helidon/webserver/grpc/GrpcRouting.java | 16 ++-- .../webserver/grpc/GrpcServiceRoute.java | 24 +++--- webserver/grpc/src/main/java/module-info.java | 4 + 7 files changed, 84 insertions(+), 67 deletions(-) rename webserver/grpc/src/main/java/io/helidon/webserver/grpc/{Grpc.java => GrpcRouteHandler.java} (63%) diff --git a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcProtocolHandler.java b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcProtocolHandler.java index dfe74d2d763..73b7968cf9b 100644 --- a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcProtocolHandler.java +++ b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcProtocolHandler.java @@ -80,7 +80,7 @@ class GrpcProtocolHandler implements Http2SubProtocolSelector.SubProto private final int streamId; private final Http2Settings serverSettings; private final Http2Settings clientSettings; - private final Grpc route; + private final GrpcRouteHandler route; private final AtomicInteger numMessages = new AtomicInteger(); private final LinkedBlockingQueue listenerQueue = new LinkedBlockingQueue<>(); private final StreamFlowControl flowControl; @@ -99,7 +99,7 @@ class GrpcProtocolHandler implements Http2SubProtocolSelector.SubProto Http2Settings clientSettings, StreamFlowControl flowControl, Http2StreamState currentStreamState, - Grpc route) { + GrpcRouteHandler route) { this.prologue = prologue; this.headers = headers; this.streamWriter = streamWriter; diff --git a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcProtocolSelector.java b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcProtocolSelector.java index aae1a90df32..70ad98faaa2 100644 --- a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcProtocolSelector.java +++ b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcProtocolSelector.java @@ -71,7 +71,7 @@ public SubProtocolResult subProtocol(ConnectionContext ctx, if (contentType.startsWith("application/grpc")) { GrpcRouting routing = router.routing(GrpcRouting.class, GrpcRouting.empty()); - Grpc route = routing.findRoute(prologue); + GrpcRouteHandler route = routing.findRoute(prologue); if (route == null) { return new SubProtocolResult(true, diff --git a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcRoute.java b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcRoute.java index 0230feb6753..7128fe9b7fa 100644 --- a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcRoute.java +++ b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcRoute.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2023 Oracle and/or its affiliates. + * Copyright (c) 2022, 2024 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,20 @@ import io.helidon.webserver.Route; abstract class GrpcRoute implements Route { - abstract Grpc toGrpc(HttpPrologue grpcPrologue); + /** + * Finds a match for an HTTP prologue. + * + * @param prologue the prologue + * @return the match result + */ abstract PathMatchers.MatchResult accepts(HttpPrologue prologue); + + /** + * Obtains a handler from an HTTP prologue. + * + * @param grpcPrologue the prologue + * @return the handler + */ + abstract GrpcRouteHandler handler(HttpPrologue grpcPrologue); } diff --git a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/Grpc.java b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcRouteHandler.java similarity index 63% rename from webserver/grpc/src/main/java/io/helidon/webserver/grpc/Grpc.java rename to webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcRouteHandler.java index 5fa7228832b..92c74008991 100644 --- a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/Grpc.java +++ b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcRouteHandler.java @@ -29,77 +29,77 @@ import io.grpc.ServerServiceDefinition; import io.grpc.stub.ServerCalls; -class Grpc extends GrpcRoute { +class GrpcRouteHandler extends GrpcRoute { private final MethodDescriptor method; private final PathMatcher pathMatcher; private final ServerCallHandler callHandler; - private Grpc(MethodDescriptor method, - PathMatcher pathMatcher, - ServerCallHandler callHandler) { + private GrpcRouteHandler(MethodDescriptor method, + PathMatcher pathMatcher, + ServerCallHandler callHandler) { this.method = method; this.pathMatcher = pathMatcher; this.callHandler = callHandler; } - static Grpc unary(Descriptors.FileDescriptor proto, - String serviceName, - String methodName, - ServerCalls.UnaryMethod method) { + static GrpcRouteHandler unary(Descriptors.FileDescriptor proto, + String serviceName, + String methodName, + ServerCalls.UnaryMethod method) { return grpc(proto, serviceName, methodName, ServerCalls.asyncUnaryCall(method)); } - static Grpc bidi(Descriptors.FileDescriptor proto, - String serviceName, - String methodName, - ServerCalls.BidiStreamingMethod method) { + static GrpcRouteHandler bidi(Descriptors.FileDescriptor proto, + String serviceName, + String methodName, + ServerCalls.BidiStreamingMethod method) { return grpc(proto, serviceName, methodName, ServerCalls.asyncBidiStreamingCall(method)); } - static Grpc serverStream(Descriptors.FileDescriptor proto, - String serviceName, - String methodName, - ServerCalls.ServerStreamingMethod method) { + static GrpcRouteHandler serverStream(Descriptors.FileDescriptor proto, + String serviceName, + String methodName, + ServerCalls.ServerStreamingMethod method) { return grpc(proto, serviceName, methodName, ServerCalls.asyncServerStreamingCall(method)); } - static Grpc clientStream(Descriptors.FileDescriptor proto, - String serviceName, - String methodName, - ServerCalls.ClientStreamingMethod method) { + static GrpcRouteHandler clientStream(Descriptors.FileDescriptor proto, + String serviceName, + String methodName, + ServerCalls.ClientStreamingMethod method) { return grpc(proto, serviceName, methodName, ServerCalls.asyncClientStreamingCall(method)); } /** - * Create a {@link io.helidon.webserver.grpc.Grpc gRPC route} from a {@link ServerMethodDefinition}. + * Create a {@link GrpcRouteHandler gRPC route} from a {@link ServerMethodDefinition}. * * @param definition the {@link ServerMethodDefinition} representing the method to execute * @param proto an optional protocol buffer {@link com.google.protobuf.Descriptors.FileDescriptor} * containing the service definition * @param the request type * @param the response type - * @return a {@link io.helidon.webserver.grpc.Grpc gRPC route} created + * @return a {@link GrpcRouteHandler gRPC route} created * from the {@link ServerMethodDefinition} */ - static Grpc methodDefinition(ServerMethodDefinition definition, - Descriptors.FileDescriptor proto) { + static GrpcRouteHandler methodDefinition(ServerMethodDefinition definition, + Descriptors.FileDescriptor proto) { return grpc(definition.getMethodDescriptor(), definition.getServerCallHandler(), proto); } @SuppressWarnings("unchecked") - public static Grpc bindableMethod(BindableService service, - ServerMethodDefinition method) { + public static GrpcRouteHandler bindableMethod(BindableService service, + ServerMethodDefinition method) { ServerServiceDefinition definition = service.bindService(); String path = definition.getServiceDescriptor().getName() + "/" + method.getMethodDescriptor().getBareMethodName(); - return new Grpc<>((MethodDescriptor) method.getMethodDescriptor(), - PathMatchers.exact(path), - (ServerCallHandler) method.getServerCallHandler()); + return new GrpcRouteHandler<>((MethodDescriptor) method.getMethodDescriptor(), + PathMatchers.exact(path), + (ServerCallHandler) method.getServerCallHandler()); } @Override - Grpc toGrpc(HttpPrologue grpcPrologue) { + GrpcRouteHandler handler(HttpPrologue grpcPrologue) { return this; } @@ -115,10 +115,10 @@ ServerCallHandler callHandler() { return callHandler; } - private static Grpc grpc(Descriptors.FileDescriptor proto, - String serviceName, - String methodName, - ServerCallHandler callHandler) { + private static GrpcRouteHandler grpc(Descriptors.FileDescriptor proto, + String serviceName, + String methodName, + ServerCallHandler callHandler) { Descriptors.ServiceDescriptor svc = proto.findServiceByName(serviceName); Descriptors.MethodDescriptor mtd = svc.findMethodByName(methodName); @@ -140,12 +140,12 @@ private static Grpc grpc(Descriptors.FileDescriptor pro .setType(getMethodType(mtd)).setFullMethodName(path).setRequestMarshaller(reqMarshaller) .setResponseMarshaller(resMarshaller).setSampledToLocalTracing(true); - return new Grpc<>(grpcDesc.build(), PathMatchers.exact(path), callHandler); + return new GrpcRouteHandler<>(grpcDesc.build(), PathMatchers.exact(path), callHandler); } /** - * Create a {@link io.helidon.webserver.grpc.Grpc gRPC route} from a {@link MethodDescriptor}. + * Create a {@link GrpcRouteHandler gRPC route} from a {@link MethodDescriptor}. * * @param grpcDesc the {@link MethodDescriptor} describing the method to execute * @param callHandler the {@link io.grpc.ServerCallHandler} that will execute the method @@ -153,13 +153,13 @@ private static Grpc grpc(Descriptors.FileDescriptor pro * the service definition * @param the request type * @param the response type - * @return a {@link io.helidon.webserver.grpc.Grpc gRPC route} created + * @return a {@link GrpcRouteHandler gRPC route} created * from the {@link io.grpc.ServerMethodDefinition} */ - private static Grpc grpc(MethodDescriptor grpcDesc, - ServerCallHandler callHandler, - Descriptors.FileDescriptor proto) { - return new Grpc<>(grpcDesc, PathMatchers.exact(grpcDesc.getFullMethodName()), callHandler); + private static GrpcRouteHandler grpc(MethodDescriptor grpcDesc, + ServerCallHandler callHandler, + Descriptors.FileDescriptor proto) { + return new GrpcRouteHandler<>(grpcDesc, PathMatchers.exact(grpcDesc.getFullMethodName()), callHandler); } private static String getClassName(Descriptors.Descriptor descriptor) { @@ -173,7 +173,7 @@ private static String getClassName(Descriptors.Descriptor descriptor) { @SuppressWarnings("unchecked") private static Class load(String className) { try { - return (Class) Grpc.class.getClassLoader().loadClass(className); + return (Class) GrpcRouteHandler.class.getClassLoader().loadClass(className); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Failed to load class \"" + className + "\" for grpc", e); } diff --git a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcRouting.java b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcRouting.java index 277075340d3..66a60d66ab6 100644 --- a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcRouting.java +++ b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcRouting.java @@ -109,11 +109,11 @@ public List services() { return services; } - Grpc findRoute(HttpPrologue prologue) { + GrpcRouteHandler findRoute(HttpPrologue prologue) { for (GrpcRoute route : routes) { PathMatchers.MatchResult accepts = route.accepts(prologue); if (accepts.accepted()) { - return route.toGrpc(prologue); + return route.handler(prologue); } } @@ -219,7 +219,7 @@ public Builder unary(Descriptors.FileDescriptor proto, String serviceName, String methodName, ServerCalls.UnaryMethod method) { - return route(Grpc.unary(proto, serviceName, methodName, method)); + return route(GrpcRouteHandler.unary(proto, serviceName, methodName, method)); } /** @@ -237,7 +237,7 @@ public Builder bidi(Descriptors.FileDescriptor proto, String serviceName, String methodName, ServerCalls.BidiStreamingMethod method) { - return route(Grpc.bidi(proto, serviceName, methodName, method)); + return route(GrpcRouteHandler.bidi(proto, serviceName, methodName, method)); } /** @@ -255,7 +255,7 @@ public Builder serverStream(Descriptors.FileDescriptor proto, String serviceName, String methodName, ServerCalls.ServerStreamingMethod method) { - return route(Grpc.serverStream(proto, serviceName, methodName, method)); + return route(GrpcRouteHandler.serverStream(proto, serviceName, methodName, method)); } /** @@ -273,7 +273,7 @@ public Builder clientStream(Descriptors.FileDescriptor proto, String serviceName, String methodName, ServerCalls.ClientStreamingMethod method) { - return route(Grpc.clientStream(proto, serviceName, methodName, method)); + return route(GrpcRouteHandler.clientStream(proto, serviceName, methodName, method)); } /** @@ -285,7 +285,7 @@ public Builder clientStream(Descriptors.FileDescriptor proto, */ public Builder service(Descriptors.FileDescriptor proto, BindableService service) { for (ServerMethodDefinition method : service.bindService().getMethods()) { - route(Grpc.methodDefinition(method, proto)); + route(GrpcRouteHandler.methodDefinition(method, proto)); } return this; } @@ -298,7 +298,7 @@ public Builder service(Descriptors.FileDescriptor proto, BindableService service */ public Builder service(ServerServiceDefinition service) { for (ServerMethodDefinition method : service.getMethods()) { - route(Grpc.methodDefinition(method, null)); + route(GrpcRouteHandler.methodDefinition(method, null)); } return this; } diff --git a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcServiceRoute.java b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcServiceRoute.java index e7bef980539..5d084463d4f 100644 --- a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcServiceRoute.java +++ b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcServiceRoute.java @@ -32,9 +32,9 @@ class GrpcServiceRoute extends GrpcRoute { private final String serviceName; - private final List> routes; + private final List> routes; - private GrpcServiceRoute(String serviceName, List> routes) { + private GrpcServiceRoute(String serviceName, List> routes) { this.serviceName = serviceName; this.routes = routes; } @@ -62,9 +62,9 @@ static GrpcRoute create(GrpcService service) { static GrpcRoute create(BindableService service) { ServerServiceDefinition definition = service.bindService(); String serviceName = definition.getServiceDescriptor().getName(); - List> routes = new LinkedList<>(); + List> routes = new LinkedList<>(); service.bindService().getMethods().forEach( - method -> routes.add(Grpc.bindableMethod(service, method))); + method -> routes.add(GrpcRouteHandler.bindableMethod(service, method))); return new GrpcServiceRoute(serviceName, routes); } @@ -82,8 +82,8 @@ static GrpcRoute create(GrpcServiceDescriptor service, WeightedBag toGrpc(HttpPrologue prologue) { - for (Grpc route : routes) { + GrpcRouteHandler handler(HttpPrologue prologue) { + for (GrpcRouteHandler route : routes) { PathMatchers.MatchResult accepts = route.accepts(prologue); if (accepts.accepted()) { return route; @@ -94,7 +94,7 @@ static GrpcRoute create(GrpcServiceDescriptor service, WeightedBag route : routes) { + for (GrpcRouteHandler route : routes) { PathMatchers.MatchResult accepts = route.accepts(prologue); if (accepts.accepted()) { return accepts; @@ -104,7 +104,7 @@ PathMatchers.MatchResult accepts(HttpPrologue prologue) { } static class Routing implements GrpcService.Routing { - private final List> routes = new LinkedList<>(); + private final List> routes = new LinkedList<>(); private final Descriptors.FileDescriptor proto; private final String serviceName; @@ -115,27 +115,27 @@ static class Routing implements GrpcService.Routing { @Override public GrpcService.Routing unary(String methodName, ServerCalls.UnaryMethod method) { - routes.add(Grpc.unary(proto, serviceName, methodName, method)); + routes.add(GrpcRouteHandler.unary(proto, serviceName, methodName, method)); return this; } @Override public GrpcService.Routing bidi(String methodName, ServerCalls.BidiStreamingMethod method) { - routes.add(Grpc.bidi(proto, serviceName, methodName, method)); + routes.add(GrpcRouteHandler.bidi(proto, serviceName, methodName, method)); return this; } @Override public GrpcService.Routing serverStream(String methodName, ServerCalls.ServerStreamingMethod method) { - routes.add(Grpc.serverStream(proto, serviceName, methodName, method)); + routes.add(GrpcRouteHandler.serverStream(proto, serviceName, methodName, method)); return this; } @Override public GrpcService.Routing clientStream(String methodName, ServerCalls.ClientStreamingMethod method) { - routes.add(Grpc.clientStream(proto, serviceName, methodName, method)); + routes.add(GrpcRouteHandler.clientStream(proto, serviceName, methodName, method)); return this; } diff --git a/webserver/grpc/src/main/java/module-info.java b/webserver/grpc/src/main/java/module-info.java index 1ed9cd47c06..3f08139e28d 100644 --- a/webserver/grpc/src/main/java/module-info.java +++ b/webserver/grpc/src/main/java/module-info.java @@ -35,6 +35,10 @@ requires io.helidon.tracing; requires io.helidon.common.config; + requires io.grpc; + requires io.grpc.stub; + requires com.google.protobuf; + requires transitive io.helidon.grpc.core; requires static io.helidon.common.features.api;