diff --git a/core/src/main/java/io/grpc/util/MutableHandlerRegistry.java b/core/src/main/java/io/grpc/util/MutableHandlerRegistry.java index 28b823ddac..d83ff6e26d 100644 --- a/core/src/main/java/io/grpc/util/MutableHandlerRegistry.java +++ b/core/src/main/java/io/grpc/util/MutableHandlerRegistry.java @@ -31,6 +31,7 @@ package io.grpc.util; +import io.grpc.BindableService; import io.grpc.ExperimentalApi; import io.grpc.HandlerRegistry; import io.grpc.MethodDescriptor; @@ -55,11 +56,28 @@ public final class MutableHandlerRegistry extends HandlerRegistry { private final ConcurrentMap services = new ConcurrentHashMap(); + /** + * Registers a service. + * + * @return the previously registered service with the same service descriptor name if exists, + * otherwise {@code null}. + */ @Nullable public ServerServiceDefinition addService(ServerServiceDefinition service) { return services.put(service.getServiceDescriptor().getName(), service); } + /** + * Registers a service. + * + * @return the previously registered service with the same service descriptor name if exists, + * otherwise {@code null}. + */ + @Nullable + public ServerServiceDefinition addService(BindableService bindableService) { + return addService(bindableService.bindService()); + } + public boolean removeService(ServerServiceDefinition service) { return services.remove(service.getServiceDescriptor().getName(), service); } diff --git a/core/src/test/java/io/grpc/util/MutableHandlerRegistryTest.java b/core/src/test/java/io/grpc/util/MutableHandlerRegistryTest.java index 42ead8488b..c98daddfeb 100644 --- a/core/src/test/java/io/grpc/util/MutableHandlerRegistryTest.java +++ b/core/src/test/java/io/grpc/util/MutableHandlerRegistryTest.java @@ -38,6 +38,7 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import io.grpc.BindableService; import io.grpc.MethodDescriptor.Marshaller; import io.grpc.MethodDescriptor.MethodType; import io.grpc.MethodDescriptor; @@ -129,6 +130,22 @@ public class MutableHandlerRegistryTest { assertNull(registry.lookupMethod("completely/random")); } + @Test + public void simpleLookupWithBindable() { + BindableService bindableService = + new BindableService() { + @Override + public ServerServiceDefinition bindService() { + return basicServiceDefinition; + } + }; + + assertNull(registry.addService(bindableService)); + + ServerMethodDefinition method = registry.lookupMethod("basic/flow"); + assertSame(flowMethodDefinition, method); + } + @Test public void multiServiceLookup() { assertNull(registry.addService(basicServiceDefinition));