From 79c841bc022e2baf42a852716100c2280ce40840 Mon Sep 17 00:00:00 2001 From: Lukasz Strzalkowski Date: Wed, 6 Apr 2016 02:07:22 +0200 Subject: [PATCH] Add Abstract{ServiceName} class in compiled service class --- .../proto/BenchmarkServiceGrpc.java | 6 ++ .../benchmarks/proto/WorkerServiceGrpc.java | 6 ++ .../src/java_plugin/cpp/java_generator.cpp | 23 ++++++++ compiler/src/test/golden/TestService.java.txt | 6 ++ .../src/testLite/golden/TestService.java.txt | 6 ++ .../src/testNano/golden/TestService.java.txt | 6 ++ .../grpc/examples/helloworld/GreeterGrpc.java | 6 ++ .../examples/routeguide/RouteGuideGrpc.java | 6 ++ .../grpc/io/grpc/grpclb/LoadBalancerGrpc.java | 6 ++ .../integration/ReconnectServiceGrpc.java | 6 ++ .../testing/integration/TestServiceGrpc.java | 6 ++ .../integration/UnimplementedServiceGrpc.java | 6 ++ .../java/io/grpc/stub/BindableService.java | 59 +++++++++++++++++++ 13 files changed, 148 insertions(+) create mode 100644 stub/src/main/java/io/grpc/stub/BindableService.java diff --git a/benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/BenchmarkServiceGrpc.java b/benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/BenchmarkServiceGrpc.java index edf1b5b3e27..edbb25103e1 100644 --- a/benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/BenchmarkServiceGrpc.java +++ b/benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/BenchmarkServiceGrpc.java @@ -157,6 +157,12 @@ public com.google.common.util.concurrent.ListenableFuturePrint("}\n"); } +static void PrintAbstractServiceClass(const ServiceDescriptor* service, + map* vars, + Printer* p) { + p->Print( + *vars, + "public static abstract class Abstract$service_name$" + " implements $service_name$, $BindableService$ {\n"); + p->Indent(); + p->Print(*vars, + "@Override public $ServerServiceDefinition$ bindService() {\n" + ); + p->Indent(); + p->Print(*vars, + "return $service_class_name$.bindService(this);\n" + ); + p->Outdent(); + p->Print("}\n"); + p->Outdent(); + p->Print("}\n\n"); +} + static void PrintService(const ServiceDescriptor* service, map* vars, Printer* p, @@ -704,6 +725,7 @@ static void PrintService(const ServiceDescriptor* service, PrintStub(service, vars, p, ASYNC_CLIENT_IMPL, generate_nano); PrintStub(service, vars, p, BLOCKING_CLIENT_IMPL, generate_nano); PrintStub(service, vars, p, FUTURE_CLIENT_IMPL, generate_nano); + PrintAbstractServiceClass(service, vars, p); PrintMethodHandlerClass(service, vars, p, generate_nano); PrintBindServiceMethod(service, vars, p, generate_nano); p->Outdent(); @@ -754,6 +776,7 @@ void GenerateService(const ServiceDescriptor* service, vars["MethodType"] = "io.grpc.MethodDescriptor.MethodType"; vars["ServerMethodDefinition"] = "io.grpc.ServerMethodDefinition"; + vars["BindableService"] = "io.grpc.stub.BindableService"; vars["ServerServiceDefinition"] = "io.grpc.ServerServiceDefinition"; vars["AbstractStub"] = "io.grpc.stub.AbstractStub"; diff --git a/compiler/src/test/golden/TestService.java.txt b/compiler/src/test/golden/TestService.java.txt index e025d21737b..4f19fde85ed 100644 --- a/compiler/src/test/golden/TestService.java.txt +++ b/compiler/src/test/golden/TestService.java.txt @@ -224,6 +224,12 @@ public class TestServiceGrpc { } } + public static abstract class AbstractTestService implements TestService, io.grpc.stub.BindableService { + @Override public io.grpc.ServerServiceDefinition bindService() { + return TestServiceGrpc.bindService(this); + } + } + private static final int METHODID_UNARY_CALL = 0; private static final int METHODID_STREAMING_OUTPUT_CALL = 1; private static final int METHODID_STREAMING_INPUT_CALL = 2; diff --git a/compiler/src/testLite/golden/TestService.java.txt b/compiler/src/testLite/golden/TestService.java.txt index c46ee56fd1c..da72d6fa4d3 100644 --- a/compiler/src/testLite/golden/TestService.java.txt +++ b/compiler/src/testLite/golden/TestService.java.txt @@ -224,6 +224,12 @@ public class TestServiceGrpc { } } + public static abstract class AbstractTestService implements TestService, io.grpc.stub.BindableService { + @Override public io.grpc.ServerServiceDefinition bindService() { + return TestServiceGrpc.bindService(this); + } + } + private static final int METHODID_UNARY_CALL = 0; private static final int METHODID_STREAMING_OUTPUT_CALL = 1; private static final int METHODID_STREAMING_INPUT_CALL = 2; diff --git a/compiler/src/testNano/golden/TestService.java.txt b/compiler/src/testNano/golden/TestService.java.txt index 5c37877374d..3aacbf81f42 100644 --- a/compiler/src/testNano/golden/TestService.java.txt +++ b/compiler/src/testNano/golden/TestService.java.txt @@ -302,6 +302,12 @@ public class TestServiceGrpc { } } + public static abstract class AbstractTestService implements TestService, io.grpc.stub.BindableService { + @Override public io.grpc.ServerServiceDefinition bindService() { + return TestServiceGrpc.bindService(this); + } + } + private static final int METHODID_UNARY_CALL = 0; private static final int METHODID_STREAMING_OUTPUT_CALL = 1; private static final int METHODID_STREAMING_INPUT_CALL = 2; diff --git a/examples/src/generated/main/grpc/io/grpc/examples/helloworld/GreeterGrpc.java b/examples/src/generated/main/grpc/io/grpc/examples/helloworld/GreeterGrpc.java index 7779ae56668..4bd8c48b8b9 100644 --- a/examples/src/generated/main/grpc/io/grpc/examples/helloworld/GreeterGrpc.java +++ b/examples/src/generated/main/grpc/io/grpc/examples/helloworld/GreeterGrpc.java @@ -138,6 +138,12 @@ public com.google.common.util.concurrent.ListenableFuture implements diff --git a/examples/src/generated/main/grpc/io/grpc/examples/routeguide/RouteGuideGrpc.java b/examples/src/generated/main/grpc/io/grpc/examples/routeguide/RouteGuideGrpc.java index 4345e7e03be..2d2fdb913ae 100644 --- a/examples/src/generated/main/grpc/io/grpc/examples/routeguide/RouteGuideGrpc.java +++ b/examples/src/generated/main/grpc/io/grpc/examples/routeguide/RouteGuideGrpc.java @@ -205,6 +205,12 @@ public com.google.common.util.concurrent.ListenableFuture implements diff --git a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/ReconnectServiceGrpc.java b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/ReconnectServiceGrpc.java index 79f7bb62941..fe0ad03e6f8 100644 --- a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/ReconnectServiceGrpc.java +++ b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/ReconnectServiceGrpc.java @@ -175,6 +175,12 @@ public com.google.common.util.concurrent.ListenableFuture implements diff --git a/stub/src/main/java/io/grpc/stub/BindableService.java b/stub/src/main/java/io/grpc/stub/BindableService.java new file mode 100644 index 00000000000..b76bd6fd771 --- /dev/null +++ b/stub/src/main/java/io/grpc/stub/BindableService.java @@ -0,0 +1,59 @@ +/* + * Copyright 2016, Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package io.grpc.stub; + +import io.grpc.ExperimentalApi; +import io.grpc.ServerServiceDefinition; + +/** + * Provides a way to bind instance of service implementation to server. + * + *

Enables dynamic creation of {@link ServerServiceDefinition} when exact type of service class + * is unknown. + * + *

Class service = ...;
+ * ServerBuilder.forPort(...).addService(service.newInstance().bindService());
+ * 

+ * + *

It is used by service's abstract class generated by compiler (eg.: + * RouteGuideGrpc.AbstractRouteGuide for RouteGuide service). Service implementation classes which + * want to be "dynamically" bind should inherit from this base class.

+ */ +@ExperimentalApi +public interface BindableService { + /** + * Creates {@link ServerServiceDefinition} object for current instance of service implementation. + * + * @return ServerServiceDefinition object. + */ + ServerServiceDefinition bindService(); +}