Skip to content

Commit 90fbf9b

Browse files
lukaszx0ejona86
authored andcommitted
Adapt BindableService in ServerBuilder#addService
Makes binding services to server as simple as it can get.
1 parent 30d4f95 commit 90fbf9b

File tree

19 files changed

+67
-31
lines changed

19 files changed

+67
-31
lines changed

benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/BenchmarkServiceGrpc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public com.google.common.util.concurrent.ListenableFuture<io.grpc.benchmarks.pro
157157
}
158158
}
159159

160-
public static abstract class AbstractBenchmarkService implements BenchmarkService, io.grpc.stub.BindableService {
160+
public static abstract class AbstractBenchmarkService implements BenchmarkService, io.grpc.BindableService {
161161
@Override public io.grpc.ServerServiceDefinition bindService() {
162162
return BenchmarkServiceGrpc.bindService(this);
163163
}

benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/WorkerServiceGrpc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public com.google.common.util.concurrent.ListenableFuture<io.grpc.benchmarks.pro
213213
}
214214
}
215215

216-
public static abstract class AbstractWorkerService implements WorkerService, io.grpc.stub.BindableService {
216+
public static abstract class AbstractWorkerService implements WorkerService, io.grpc.BindableService {
217217
@Override public io.grpc.ServerServiceDefinition bindService() {
218218
return WorkerServiceGrpc.bindService(this);
219219
}

compiler/src/java_plugin/cpp/java_generator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ void GenerateService(const ServiceDescriptor* service,
776776
vars["MethodType"] = "io.grpc.MethodDescriptor.MethodType";
777777
vars["ServerMethodDefinition"] =
778778
"io.grpc.ServerMethodDefinition";
779-
vars["BindableService"] = "io.grpc.stub.BindableService";
779+
vars["BindableService"] = "io.grpc.BindableService";
780780
vars["ServerServiceDefinition"] =
781781
"io.grpc.ServerServiceDefinition";
782782
vars["AbstractStub"] = "io.grpc.stub.AbstractStub";

compiler/src/test/golden/TestService.java.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public class TestServiceGrpc {
224224
}
225225
}
226226

227-
public static abstract class AbstractTestService implements TestService, io.grpc.stub.BindableService {
227+
public static abstract class AbstractTestService implements TestService, io.grpc.BindableService {
228228
@Override public io.grpc.ServerServiceDefinition bindService() {
229229
return TestServiceGrpc.bindService(this);
230230
}

compiler/src/testLite/golden/TestService.java.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public class TestServiceGrpc {
224224
}
225225
}
226226

227-
public static abstract class AbstractTestService implements TestService, io.grpc.stub.BindableService {
227+
public static abstract class AbstractTestService implements TestService, io.grpc.BindableService {
228228
@Override public io.grpc.ServerServiceDefinition bindService() {
229229
return TestServiceGrpc.bindService(this);
230230
}

compiler/src/testNano/golden/TestService.java.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public class TestServiceGrpc {
302302
}
303303
}
304304

305-
public static abstract class AbstractTestService implements TestService, io.grpc.stub.BindableService {
305+
public static abstract class AbstractTestService implements TestService, io.grpc.BindableService {
306306
@Override public io.grpc.ServerServiceDefinition bindService() {
307307
return TestServiceGrpc.bindService(this);
308308
}

stub/src/main/java/io/grpc/stub/BindableService.java renamed to core/src/main/java/io/grpc/BindableService.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,22 @@
2929
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131

32-
package io.grpc.stub;
33-
34-
import io.grpc.ExperimentalApi;
35-
import io.grpc.ServerServiceDefinition;
32+
package io.grpc;
3633

3734
/**
3835
* Provides a way to bind instance of service implementation to server.
3936
*
40-
* <p>Enables dynamic creation of {@link ServerServiceDefinition} when exact type of service class
41-
* is unknown.
37+
* <p>It is used by service's abstract class generated by compiler (eg.:
38+
* RouteGuideGrpc.AbstractRouteGuide for RouteGuide service) and lets implementation classes to be
39+
* bind to server.
4240
*
43-
* <pre><code>Class<? extends BindableService> service = ...;
44-
* ServerBuilder.forPort(...).addService(service.newInstance().bindService());
45-
* </code></pre></p>
41+
* <pre><code>
42+
* class RouteGuideService extends RouteGuideGrpc.AbstractRouteGuide {
43+
* // ...
44+
* }
4645
*
47-
* <p>It is used by service's abstract class generated by compiler (eg.:
48-
* RouteGuideGrpc.AbstractRouteGuide for RouteGuide service). Service implementation classes which
49-
* want to be "dynamically" bind should inherit from this base class.</p>
46+
* Server server = ServerBuilder.forPort(1234).addService(new RouteGuideService()).build();
47+
* </code></pre></p>
5048
*/
5149
@ExperimentalApi
5250
public interface BindableService {

core/src/main/java/io/grpc/ServerBuilder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,22 @@ public static ServerBuilder<?> forPort(int port) {
7474
/**
7575
* Adds a service implementation to the handler registry.
7676
*
77+
* @param service ServerServiceDefinition object
7778
* @throws UnsupportedOperationException if this builder does not support dynamically adding
7879
* services.
7980
*/
8081
public abstract T addService(ServerServiceDefinition service);
8182

83+
/**
84+
* Adds a service implementation to the handler registry.
85+
*
86+
* @param bindableService BindableService object
87+
* @throws UnsupportedOperationException if this builder does not support dynamically adding
88+
* services.
89+
*/
90+
@ExperimentalApi
91+
public abstract T addService(BindableService bindableService);
92+
8293
/**
8394
* Makes the server use TLS.
8495
*

core/src/main/java/io/grpc/ServerInterceptors.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public static ServerServiceDefinition interceptForward(ServerServiceDefinition s
6161
return interceptForward(serviceDef, Arrays.asList(interceptors));
6262
}
6363

64+
@ExperimentalApi
65+
public static ServerServiceDefinition interceptForward(BindableService bindableService,
66+
ServerInterceptor... interceptors) {
67+
return interceptForward(bindableService.bindService(), Arrays.asList(interceptors));
68+
}
69+
6470
/**
6571
* Create a new {@code ServerServiceDefinition} whose {@link ServerCallHandler}s will call
6672
* {@code interceptors} before calling the pre-existing {@code ServerCallHandler}. The first
@@ -92,6 +98,12 @@ public static ServerServiceDefinition intercept(ServerServiceDefinition serviceD
9298
return intercept(serviceDef, Arrays.asList(interceptors));
9399
}
94100

101+
@ExperimentalApi
102+
public static ServerServiceDefinition intercept(BindableService bindableService,
103+
ServerInterceptor... interceptors) {
104+
return intercept(bindableService.bindService(), Arrays.asList(interceptors));
105+
}
106+
95107
/**
96108
* Create a new {@code ServerServiceDefinition} whose {@link ServerCallHandler}s will call
97109
* {@code interceptors} before calling the pre-existing {@code ServerCallHandler}. The last

core/src/main/java/io/grpc/internal/AbstractServerImplBuilder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.google.common.base.Preconditions;
3737
import com.google.common.util.concurrent.MoreExecutors;
3838

39+
import io.grpc.BindableService;
3940
import io.grpc.CompressorRegistry;
4041
import io.grpc.Context;
4142
import io.grpc.DecompressorRegistry;
@@ -98,6 +99,8 @@ public final T executor(@Nullable Executor executor) {
9899
*
99100
* <p>This is supported only if the user didn't provide a handler registry, or the provided one is
100101
* a {@link MutableHandlerRegistry}. Otherwise it throws an UnsupportedOperationException.
102+
*
103+
* @param service ServerServiceDefinition object.
101104
*/
102105
@Override
103106
public final T addService(ServerServiceDefinition service) {
@@ -108,6 +111,19 @@ public final T addService(ServerServiceDefinition service) {
108111
throw new UnsupportedOperationException("Underlying HandlerRegistry is not mutable");
109112
}
110113

114+
/**
115+
* Adds a service implementation to the handler registry.
116+
*
117+
* <p>This is supported only if the user didn't provide a handler registry, or the provided one is
118+
* a {@link MutableHandlerRegistry}. Otherwise it throws an UnsupportedOperationException.
119+
*
120+
* @param bindableService BindableService object.
121+
*/
122+
@Override
123+
public final T addService(BindableService bindableService) {
124+
return addService(bindableService.bindService());
125+
}
126+
111127
@Override
112128
public final T decompressorRegistry(DecompressorRegistry registry) {
113129
decompressorRegistry = registry;

0 commit comments

Comments
 (0)