Skip to content

Commit f57f019

Browse files
committed
xds: Copy ForwardingServerBuilder to xds for XdsServerBuilder
This reduces ABI issues caused by returning the more precise XdsServerBuilder in the API. See grpc#7552. ForwardingServerBuilder isn't yet public, so just copy it; it'll be easy to clean up.
1 parent b3429ec commit f57f019

File tree

2 files changed

+182
-87
lines changed

2 files changed

+182
-87
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
* Copyright 2020 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.xds;
18+
19+
import com.google.common.base.MoreObjects;
20+
import io.grpc.BinaryLog;
21+
import io.grpc.BindableService;
22+
import io.grpc.CompressorRegistry;
23+
import io.grpc.DecompressorRegistry;
24+
import io.grpc.HandlerRegistry;
25+
import io.grpc.Server;
26+
import io.grpc.ServerBuilder;
27+
import io.grpc.ServerInterceptor;
28+
import io.grpc.ServerServiceDefinition;
29+
import io.grpc.ServerStreamTracer;
30+
import io.grpc.ServerTransportFilter;
31+
import java.io.File;
32+
import java.io.InputStream;
33+
import java.util.concurrent.Executor;
34+
import java.util.concurrent.TimeUnit;
35+
import javax.annotation.Nullable;
36+
37+
/**
38+
* A {@link ServerBuilder} that delegates all its builder method to another builder by default.
39+
*
40+
* @param <T> The type of the subclass extending this abstract class.
41+
*/
42+
abstract class ForwardingServerBuilder<T extends ServerBuilder<T>> extends ServerBuilder<T> {
43+
44+
/** The default constructor. */
45+
protected ForwardingServerBuilder() {}
46+
47+
/**
48+
* This method serves to force sub classes to "hide" this static factory.
49+
*/
50+
public static ServerBuilder<?> forPort(int port) {
51+
throw new UnsupportedOperationException("Subclass failed to hide static factory");
52+
}
53+
54+
/**
55+
* Returns the delegated {@code ServerBuilder}.
56+
*/
57+
protected abstract ServerBuilder<?> delegate();
58+
59+
@Override
60+
public T directExecutor() {
61+
delegate().directExecutor();
62+
return thisT();
63+
}
64+
65+
@Override
66+
public T executor(@Nullable Executor executor) {
67+
delegate().executor(executor);
68+
return thisT();
69+
}
70+
71+
@Override
72+
public T addService(ServerServiceDefinition service) {
73+
delegate().addService(service);
74+
return thisT();
75+
}
76+
77+
@Override
78+
public T addService(BindableService bindableService) {
79+
delegate().addService(bindableService);
80+
return thisT();
81+
}
82+
83+
@Override
84+
public T intercept(ServerInterceptor interceptor) {
85+
delegate().intercept(interceptor);
86+
return thisT();
87+
}
88+
89+
@Override
90+
public T addTransportFilter(ServerTransportFilter filter) {
91+
delegate().addTransportFilter(filter);
92+
return thisT();
93+
}
94+
95+
@Override
96+
public T addStreamTracerFactory(ServerStreamTracer.Factory factory) {
97+
delegate().addStreamTracerFactory(factory);
98+
return thisT();
99+
}
100+
101+
@Override
102+
public T fallbackHandlerRegistry(@Nullable HandlerRegistry fallbackRegistry) {
103+
delegate().fallbackHandlerRegistry(fallbackRegistry);
104+
return thisT();
105+
}
106+
107+
@Override
108+
public T useTransportSecurity(File certChain, File privateKey) {
109+
delegate().useTransportSecurity(certChain, privateKey);
110+
return thisT();
111+
}
112+
113+
@Override
114+
public T useTransportSecurity(InputStream certChain, InputStream privateKey) {
115+
delegate().useTransportSecurity(certChain, privateKey);
116+
return thisT();
117+
}
118+
119+
@Override
120+
public T decompressorRegistry(@Nullable DecompressorRegistry registry) {
121+
delegate().decompressorRegistry(registry);
122+
return thisT();
123+
}
124+
125+
@Override
126+
public T compressorRegistry(@Nullable CompressorRegistry registry) {
127+
delegate().compressorRegistry(registry);
128+
return thisT();
129+
}
130+
131+
@Override
132+
public T handshakeTimeout(long timeout, TimeUnit unit) {
133+
delegate().handshakeTimeout(timeout, unit);
134+
return thisT();
135+
}
136+
137+
@Override
138+
public T maxInboundMessageSize(int bytes) {
139+
delegate().maxInboundMessageSize(bytes);
140+
return thisT();
141+
}
142+
143+
@Override
144+
public T maxInboundMetadataSize(int bytes) {
145+
delegate().maxInboundMetadataSize(bytes);
146+
return thisT();
147+
}
148+
149+
@Override
150+
public T setBinaryLog(BinaryLog binaryLog) {
151+
delegate().setBinaryLog(binaryLog);
152+
return thisT();
153+
}
154+
155+
/**
156+
* Returns the {@link Server} built by the delegate by default. Overriding method can return
157+
* different value.
158+
*/
159+
@Override
160+
public Server build() {
161+
return delegate().build();
162+
}
163+
164+
@Override
165+
public String toString() {
166+
return MoreObjects.toStringHelper(this).add("delegate", delegate()).toString();
167+
}
168+
169+
/**
170+
* Returns the correctly typed version of the builder.
171+
*/
172+
protected final T thisT() {
173+
@SuppressWarnings("unchecked")
174+
T thisT = (T) this;
175+
return thisT;
176+
}
177+
}

xds/src/main/java/io/grpc/xds/XdsServerBuilder.java

+5-87
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,10 @@
1818

1919
import com.google.common.annotations.VisibleForTesting;
2020
import io.grpc.Attributes;
21-
import io.grpc.BindableService;
22-
import io.grpc.CompressorRegistry;
23-
import io.grpc.DecompressorRegistry;
2421
import io.grpc.ExperimentalApi;
25-
import io.grpc.HandlerRegistry;
22+
import io.grpc.Internal;
2623
import io.grpc.Server;
2724
import io.grpc.ServerBuilder;
28-
import io.grpc.ServerInterceptor;
29-
import io.grpc.ServerServiceDefinition;
30-
import io.grpc.ServerStreamTracer;
31-
import io.grpc.ServerTransportFilter;
3225
import io.grpc.Status;
3326
import io.grpc.netty.InternalNettyServerBuilder;
3427
import io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator;
@@ -42,16 +35,13 @@
4235
import java.io.File;
4336
import java.io.InputStream;
4437
import java.net.InetSocketAddress;
45-
import java.util.concurrent.Executor;
46-
import java.util.concurrent.TimeUnit;
47-
import javax.annotation.Nullable;
4838
import javax.net.ssl.SSLException;
4939

5040
/**
5141
* A version of {@link ServerBuilder} to create xDS managed servers that will use SDS to set up SSL
5242
* with peers. Note, this is not ready to use yet.
5343
*/
54-
public final class XdsServerBuilder extends ServerBuilder<XdsServerBuilder> {
44+
public final class XdsServerBuilder extends ForwardingServerBuilder<XdsServerBuilder> {
5545

5646
private final NettyServerBuilder delegate;
5747
private final int port;
@@ -64,63 +54,9 @@ private XdsServerBuilder(NettyServerBuilder nettyDelegate, int port) {
6454
}
6555

6656
@Override
67-
public XdsServerBuilder handshakeTimeout(long timeout, TimeUnit unit) {
68-
delegate.handshakeTimeout(timeout, unit);
69-
return this;
70-
}
71-
72-
@Override
73-
public XdsServerBuilder directExecutor() {
74-
delegate.directExecutor();
75-
return this;
76-
}
77-
78-
@Override
79-
public XdsServerBuilder addStreamTracerFactory(ServerStreamTracer.Factory factory) {
80-
delegate.addStreamTracerFactory(factory);
81-
return this;
82-
}
83-
84-
@Override
85-
public XdsServerBuilder addTransportFilter(ServerTransportFilter filter) {
86-
delegate.addTransportFilter(filter);
87-
return this;
88-
}
89-
90-
@Override
91-
public XdsServerBuilder executor(Executor executor) {
92-
delegate.executor(executor);
93-
return this;
94-
}
95-
96-
@Override
97-
public XdsServerBuilder addService(ServerServiceDefinition service) {
98-
delegate.addService(service);
99-
return this;
100-
}
101-
102-
@Override
103-
public XdsServerBuilder addService(BindableService bindableService) {
104-
delegate.addService(bindableService);
105-
return this;
106-
}
107-
108-
@Override
109-
public XdsServerBuilder fallbackHandlerRegistry(@Nullable HandlerRegistry fallbackRegistry) {
110-
delegate.fallbackHandlerRegistry(fallbackRegistry);
111-
return this;
112-
}
113-
114-
@Override
115-
public XdsServerBuilder useTransportSecurity(File certChain, File privateKey) {
116-
delegate.useTransportSecurity(certChain, privateKey);
117-
return this;
118-
}
119-
120-
@Override
121-
public XdsServerBuilder useTransportSecurity(InputStream certChain, InputStream privateKey) {
122-
delegate.useTransportSecurity(certChain, privateKey);
123-
return this;
57+
@Internal
58+
protected ServerBuilder<?> delegate() {
59+
return delegate;
12460
}
12561

12662
/**
@@ -163,24 +99,6 @@ public XdsServerBuilder useXdsSecurityWithTransportSecurityFallback(
16399
return this;
164100
}
165101

166-
@Override
167-
public XdsServerBuilder decompressorRegistry(@Nullable DecompressorRegistry registry) {
168-
delegate.decompressorRegistry(registry);
169-
return this;
170-
}
171-
172-
@Override
173-
public XdsServerBuilder compressorRegistry(@Nullable CompressorRegistry registry) {
174-
delegate.compressorRegistry(registry);
175-
return this;
176-
}
177-
178-
@Override
179-
public XdsServerBuilder intercept(ServerInterceptor interceptor) {
180-
delegate.intercept(interceptor);
181-
return this;
182-
}
183-
184102
/** Set the fallback protocolNegotiator. Pass null to unset a previously set value. */
185103
public XdsServerBuilder fallbackProtocolNegotiator(
186104
ProtocolNegotiator fallbackProtocolNegotiator) {

0 commit comments

Comments
 (0)