Skip to content

Commit

Permalink
Updates tests and implementations after changes to gRPC API. All anno…
Browse files Browse the repository at this point in the history
…tations are under the Grpc interfaces, including the client API annotations GrpcProxy and GrpcChannel.
  • Loading branch information
spericas committed Jul 23, 2024
1 parent d536413 commit 2c0b0d7
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 109 deletions.
24 changes: 24 additions & 0 deletions grpc/api/src/main/java/io/helidon/grpc/api/Grpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,28 @@ public interface Grpc {
*/
Class<?> value();
}

/**
* An annotation that can be used to specify the name of a configured gRPC channel.
* channel to inject, or the name of the host to connect to, as described in
*/
@Target({TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RUNTIME)
@interface GrpcChannel {

/**
* The name of the configured channel.
*
* @return name of the channel
*/
String value();
}

/**
* An annotation used to mark an injection point for a gRPC service client proxy.
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RUNTIME)
@interface GrpcProxy {
}
}
38 changes: 0 additions & 38 deletions grpc/api/src/main/java/io/helidon/grpc/api/GrpcChannel.java

This file was deleted.

30 changes: 0 additions & 30 deletions grpc/api/src/main/java/io/helidon/grpc/api/GrpcProxy.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.concurrent.locks.ReentrantLock;

import io.helidon.config.Config;
import io.helidon.grpc.api.GrpcChannel;
import io.helidon.grpc.api.Grpc;

import io.grpc.Channel;
import jakarta.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -57,12 +57,12 @@ public class ChannelProducer {
* @return a gRPC {@link io.grpc.Channel}
*/
@Produces
@GrpcChannel(value = GrpcChannelsProvider.DEFAULT_CHANNEL_NAME)
@Grpc.GrpcChannel(value = GrpcChannelsProvider.DEFAULT_CHANNEL_NAME)
public Channel get(InjectionPoint injectionPoint) {
GrpcChannel qualifier = injectionPoint.getQualifiers()
Grpc.GrpcChannel qualifier = injectionPoint.getQualifiers()
.stream()
.filter(q -> q.annotationType().equals(GrpcChannel.class))
.map(q -> (GrpcChannel) q)
.filter(q -> q.annotationType().equals(Grpc.GrpcChannel.class))
.map(q -> (Grpc.GrpcChannel) q)
.findFirst()
.orElse(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import java.util.function.Supplier;

import io.helidon.common.Builder;
import io.helidon.grpc.api.GrpcMarshaller;
import io.helidon.grpc.api.GrpcMethod;
import io.helidon.grpc.api.Grpc;
import io.helidon.grpc.core.MethodHandler;
import io.helidon.microprofile.grpc.core.AbstractServiceBuilder;
import io.helidon.microprofile.grpc.core.AnnotatedMethod;
Expand Down Expand Up @@ -108,10 +107,10 @@ public ClientServiceDescriptor.Builder build() {
* @param methodList the list of methods to add
*/
private void addServiceMethods(ClientServiceDescriptor.Builder builder, AnnotatedMethodList methodList) {
for (AnnotatedMethod am : methodList.withAnnotation(GrpcMethod.class)) {
for (AnnotatedMethod am : methodList.withAnnotation(Grpc.GrpcMethod.class)) {
addServiceMethod(builder, am);
}
for (AnnotatedMethod am : methodList.withMetaAnnotation(GrpcMethod.class)) {
for (AnnotatedMethod am : methodList.withMetaAnnotation(Grpc.GrpcMethod.class)) {
addServiceMethod(builder, am);
}
}
Expand All @@ -126,7 +125,7 @@ private void addServiceMethods(ClientServiceDescriptor.Builder builder, Annotate
* @param method the {@link io.helidon.microprofile.grpc.core.AnnotatedMethod} representing the method to add
*/
private void addServiceMethod(ClientServiceDescriptor.Builder builder, AnnotatedMethod method) {
GrpcMethod annotation = method.firstAnnotationOrMetaAnnotation(GrpcMethod.class);
Grpc.GrpcMethod annotation = method.firstAnnotationOrMetaAnnotation(Grpc.GrpcMethod.class);
String name = determineMethodName(method, annotation);

MethodHandler handler = handlerSuppliers().stream()
Expand Down Expand Up @@ -187,8 +186,9 @@ public void accept(ClientMethodDescriptor.Rules config) {
.responseType(responseType)
.methodHandler(methodHandler);

if (method.isAnnotationPresent(GrpcMarshaller.class)) {
config.marshallerSupplier(ModelHelper.getMarshallerSupplier(method.getAnnotation(GrpcMarshaller.class)));
if (method.isAnnotationPresent(Grpc.GrpcMarshaller.class)) {
config.marshallerSupplier(ModelHelper.getMarshallerSupplier(
method.getAnnotation(Grpc.GrpcMarshaller.class)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import java.util.HashSet;
import java.util.Set;

import io.helidon.grpc.api.GrpcChannel;
import io.helidon.grpc.api.GrpcProxy;
import io.helidon.grpc.api.Grpc;

import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.spi.AfterBeanDiscovery;
Expand Down Expand Up @@ -54,7 +53,7 @@ public void addBeans(@Observes BeforeBeanDiscovery event) {
/**
* Process injection points.
* <p>
* In this method injection points that have the {@link GrpcProxy} are processed
* In this method injection points that have the {@link io.helidon.grpc.api.Grpc.GrpcProxy} are processed
* and their types are stored so that in the {@link #afterBean(
*jakarta.enterprise.inject.spi.AfterBeanDiscovery, jakarta.enterprise.inject.spi.BeanManager)}
* we can manually create a producer for the correct service proxy type.
Expand All @@ -65,16 +64,16 @@ public void addBeans(@Observes BeforeBeanDiscovery event) {
*/
public <T, X> void gatherApplications(@Observes ProcessInjectionPoint<T, X> pip) {
Annotated annotated = pip.getInjectionPoint().getAnnotated();
if (annotated.isAnnotationPresent(GrpcProxy.class)) {
if (annotated.isAnnotationPresent(Grpc.GrpcProxy.class)) {
Type type = pip.getInjectionPoint().getType();
proxyTypes.add(type);
}
}

/**
* Process the previously captured {@link GrpcProxy} injection points.
* Process the previously captured {@link io.helidon.grpc.api.Grpc.GrpcProxy} injection points.
* <p>
* For each {@link GrpcProxy} injection point we create a producer bean
* For each {@link io.helidon.grpc.api.Grpc.GrpcProxy} injection point we create a producer bean
* for the required type.
*
* @param event the {@link jakarta.enterprise.inject.spi.AfterBeanDiscovery} event
Expand All @@ -84,8 +83,8 @@ public void afterBean(@Observes AfterBeanDiscovery event, BeanManager beanManage
AnnotatedType<GrpcProxyProducer> producerType = beanManager.createAnnotatedType(GrpcProxyProducer.class);
AnnotatedMethod<? super GrpcProxyProducer> producerMethod = producerType.getMethods()
.stream()
.filter(m -> m.isAnnotationPresent(GrpcProxy.class))
.filter(m -> m.isAnnotationPresent(GrpcChannel.class))
.filter(m -> m.isAnnotationPresent(Grpc.GrpcProxy.class))
.filter(m -> m.isAnnotationPresent(Grpc.GrpcChannel.class))
.findFirst()
.orElse(null);
if (producerMethod != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package io.helidon.microprofile.grpc.client;

import io.helidon.grpc.api.GrpcChannel;
import io.helidon.grpc.api.GrpcProxy;
import io.helidon.grpc.api.Grpc;
import io.helidon.microprofile.grpc.core.ModelHelper;

import io.grpc.Channel;
Expand All @@ -39,7 +38,7 @@ private GrpcProxyProducer() {
/**
* A CDI producer method that produces a client proxy for a gRPC service that
* will connect to the server using the channel specified via
* {@link io.helidon.grpc.api.GrpcChannel} annotation on the proxy interface
* {@link io.helidon.grpc.api.Grpc.GrpcChannel} annotation on the proxy interface
* or injection point, or the default {@link io.grpc.Channel}.
* <p>
* This is not a real producer method but is used as a stub by the gRPC client
Expand All @@ -48,17 +47,17 @@ private GrpcProxyProducer() {
* @param injectionPoint the injection point where the client proxy is to be injected
* @return a gRPC client proxy
*/
@GrpcProxy
@GrpcChannel(value = GrpcChannelsProvider.DEFAULT_CHANNEL_NAME)
@Grpc.GrpcProxy
@Grpc.GrpcChannel(value = GrpcChannelsProvider.DEFAULT_CHANNEL_NAME)
static Object proxyUsingNamedChannel(InjectionPoint injectionPoint, ChannelProducer producer) {
Class<?> type = ModelHelper.getGenericType(injectionPoint.getType());

String channelName;
if (injectionPoint.getAnnotated().isAnnotationPresent(GrpcChannel.class)) {
channelName = injectionPoint.getAnnotated().getAnnotation(GrpcChannel.class).value();
if (injectionPoint.getAnnotated().isAnnotationPresent(Grpc.GrpcChannel.class)) {
channelName = injectionPoint.getAnnotated().getAnnotation(Grpc.GrpcChannel.class).value();
} else {
channelName = type.isAnnotationPresent(GrpcChannel.class)
? type.getAnnotation(GrpcChannel.class).value()
channelName = type.isAnnotationPresent(Grpc.GrpcChannel.class)
? type.getAnnotation(Grpc.GrpcChannel.class).value()
: GrpcChannelsProvider.DEFAULT_CHANNEL_NAME;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
import io.helidon.common.configurable.Resource;
import io.helidon.common.tls.Tls;
import io.helidon.grpc.api.Grpc;
import io.helidon.grpc.api.GrpcChannel;
import io.helidon.grpc.api.GrpcMarshaller;
import io.helidon.grpc.api.GrpcProxy;
import io.helidon.grpc.api.Unary;
import io.helidon.microprofile.grpc.server.GrpcMpCdiExtension;
import io.helidon.microprofile.testing.junit5.AddBean;
import io.helidon.microprofile.testing.junit5.AddExtension;
Expand All @@ -54,7 +50,7 @@ class EchoServiceTest {
private WebTarget webTarget;

@Inject
@GrpcProxy
@Grpc.GrpcProxy
private EchoServiceClient proxyClient;

@Test
Expand Down Expand Up @@ -118,11 +114,11 @@ public void onCompleted() {
assertThat(future.get(5, TimeUnit.SECONDS), is("Howdy"));
}

@Grpc
@GrpcMarshaller("java")
@Grpc.GrpcService
@Grpc.GrpcMarshaller("java")
public static class EchoService {

@Unary("Echo")
@Grpc.Unary("Echo")
public void echo(String request, StreamObserver<String> observer) {
try {
complete(observer, request);
Expand All @@ -132,12 +128,12 @@ public void echo(String request, StreamObserver<String> observer) {
}
}

@Grpc("EchoService")
@GrpcMarshaller("java")
@GrpcChannel(value = "echo-channel")
@Grpc.GrpcService("EchoService")
@Grpc.GrpcMarshaller("java")
@Grpc.GrpcChannel(value = "echo-channel")
public interface EchoServiceClient {

@Unary("Echo")
@Grpc.Unary("Echo")
void echo(String request, StreamObserver<String> observer);
}
}

0 comments on commit 2c0b0d7

Please sign in to comment.