Skip to content

Commit 9783cb1

Browse files
committed
Inline AbstractServerImplBuilder
1 parent 9e768a6 commit 9783cb1

File tree

2 files changed

+106
-98
lines changed

2 files changed

+106
-98
lines changed

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

Lines changed: 11 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616

1717
package io.grpc.internal;
1818

19-
import static com.google.common.base.Preconditions.checkArgument;
2019
import static com.google.common.base.Preconditions.checkNotNull;
2120

2221
import com.google.common.annotations.VisibleForTesting;
23-
import com.google.common.util.concurrent.MoreExecutors;
2422
import io.grpc.BinaryLog;
25-
import io.grpc.BindableService;
2623
import io.grpc.CompressorRegistry;
2724
import io.grpc.Deadline;
2825
import io.grpc.DecompressorRegistry;
@@ -60,114 +57,37 @@ public static ServerBuilder<?> forPort(int port) {
6057
}
6158

6259
// defaults
63-
private static final ObjectPool<? extends Executor> DEFAULT_EXECUTOR_POOL =
60+
protected static final ObjectPool<? extends Executor> DEFAULT_EXECUTOR_POOL =
6461
SharedResourcePool.forResource(GrpcUtil.SHARED_CHANNEL_EXECUTOR);
65-
private static final HandlerRegistry DEFAULT_FALLBACK_REGISTRY = new DefaultFallbackRegistry();
66-
private static final DecompressorRegistry DEFAULT_DECOMPRESSOR_REGISTRY =
62+
protected static final HandlerRegistry DEFAULT_FALLBACK_REGISTRY = new DefaultFallbackRegistry();
63+
protected static final DecompressorRegistry DEFAULT_DECOMPRESSOR_REGISTRY =
6764
DecompressorRegistry.getDefaultInstance();
68-
private static final CompressorRegistry DEFAULT_COMPRESSOR_REGISTRY =
65+
protected static final CompressorRegistry DEFAULT_COMPRESSOR_REGISTRY =
6966
CompressorRegistry.getDefaultInstance();
70-
private static final long DEFAULT_HANDSHAKE_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(120);
67+
protected static final long DEFAULT_HANDSHAKE_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(120);
7168

7269
// mutable state
7370
final InternalHandlerRegistry.Builder registryBuilder =
7471
new InternalHandlerRegistry.Builder();
7572
final List<ServerTransportFilter> transportFilters = new ArrayList<>();
7673
final List<ServerInterceptor> interceptors = new ArrayList<>();
77-
private final List<ServerStreamTracer.Factory> streamTracerFactories = new ArrayList<>();
74+
protected final List<ServerStreamTracer.Factory> streamTracerFactories = new ArrayList<>();
7875
HandlerRegistry fallbackRegistry = DEFAULT_FALLBACK_REGISTRY;
7976
ObjectPool<? extends Executor> executorPool = DEFAULT_EXECUTOR_POOL;
8077
DecompressorRegistry decompressorRegistry = DEFAULT_DECOMPRESSOR_REGISTRY;
8178
CompressorRegistry compressorRegistry = DEFAULT_COMPRESSOR_REGISTRY;
8279
long handshakeTimeoutMillis = DEFAULT_HANDSHAKE_TIMEOUT_MILLIS;
8380
Deadline.Ticker ticker = Deadline.getSystemTicker();
84-
private boolean statsEnabled = true;
85-
private boolean recordStartedRpcs = true;
86-
private boolean recordFinishedRpcs = true;
87-
private boolean recordRealTimeMetrics = false;
88-
private boolean tracingEnabled = true;
81+
protected boolean statsEnabled = true;
82+
protected boolean recordStartedRpcs = true;
83+
protected boolean recordFinishedRpcs = true;
84+
protected boolean recordRealTimeMetrics = false;
85+
protected boolean tracingEnabled = true;
8986
@Nullable BinaryLog binlog;
9087
TransportTracer.Factory transportTracerFactory = TransportTracer.getDefaultFactory();
9188
InternalChannelz channelz = InternalChannelz.instance();
9289
CallTracer.Factory callTracerFactory = CallTracer.getDefaultFactory();
9390

94-
@Override
95-
public final T directExecutor() {
96-
return executor(MoreExecutors.directExecutor());
97-
}
98-
99-
@Override
100-
public final T executor(@Nullable Executor executor) {
101-
this.executorPool = executor != null ? new FixedObjectPool<>(executor) : DEFAULT_EXECUTOR_POOL;
102-
return thisT();
103-
}
104-
105-
@Override
106-
public final T addService(ServerServiceDefinition service) {
107-
registryBuilder.addService(checkNotNull(service, "service"));
108-
return thisT();
109-
}
110-
111-
@Override
112-
public final T addService(BindableService bindableService) {
113-
return addService(checkNotNull(bindableService, "bindableService").bindService());
114-
}
115-
116-
@Override
117-
public final T addTransportFilter(ServerTransportFilter filter) {
118-
transportFilters.add(checkNotNull(filter, "filter"));
119-
return thisT();
120-
}
121-
122-
@Override
123-
public final T intercept(ServerInterceptor interceptor) {
124-
interceptors.add(checkNotNull(interceptor, "interceptor"));
125-
return thisT();
126-
}
127-
128-
@Override
129-
public final T addStreamTracerFactory(ServerStreamTracer.Factory factory) {
130-
streamTracerFactories.add(checkNotNull(factory, "factory"));
131-
return thisT();
132-
}
133-
134-
@Override
135-
public final T fallbackHandlerRegistry(@Nullable HandlerRegistry registry) {
136-
this.fallbackRegistry = registry != null ? registry : DEFAULT_FALLBACK_REGISTRY;
137-
return thisT();
138-
}
139-
140-
@Override
141-
public final T decompressorRegistry(@Nullable DecompressorRegistry registry) {
142-
this.decompressorRegistry = registry != null ? registry : DEFAULT_DECOMPRESSOR_REGISTRY;
143-
return thisT();
144-
}
145-
146-
@Override
147-
public final T compressorRegistry(@Nullable CompressorRegistry registry) {
148-
this.compressorRegistry = registry != null ? registry : DEFAULT_COMPRESSOR_REGISTRY;
149-
return thisT();
150-
}
151-
152-
@Override
153-
public final T handshakeTimeout(long timeout, TimeUnit unit) {
154-
checkArgument(timeout > 0, "handshake timeout is %s, but must be positive", timeout);
155-
this.handshakeTimeoutMillis = checkNotNull(unit, "unit").toMillis(timeout);
156-
return thisT();
157-
}
158-
159-
@Override
160-
public final T setBinaryLog(@Nullable BinaryLog binaryLog) {
161-
this.binlog = binaryLog;
162-
return thisT();
163-
}
164-
165-
@VisibleForTesting
166-
public final T setTransportTracerFactory(TransportTracer.Factory transportTracerFactory) {
167-
this.transportTracerFactory = transportTracerFactory;
168-
return thisT();
169-
}
170-
17191
/**
17292
* Disable or enable stats features. Enabled by default.
17393
*/
@@ -294,12 +214,6 @@ protected final TransportTracer.Factory getTransportTracerFactory() {
294214
protected abstract List<? extends io.grpc.internal.InternalServer> buildTransportServers(
295215
List<? extends ServerStreamTracer.Factory> streamTracerFactories);
296216

297-
private T thisT() {
298-
@SuppressWarnings("unchecked")
299-
T thisT = (T) this;
300-
return thisT;
301-
}
302-
303217
private static final class DefaultFallbackRegistry extends HandlerRegistry {
304218
@Override
305219
public List<ServerServiceDefinition> getServices() {

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

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,31 @@
1616

1717
package io.grpc.internal;
1818

19+
import static com.google.common.base.Preconditions.checkArgument;
20+
import static com.google.common.base.Preconditions.checkNotNull;
21+
22+
import com.google.common.annotations.VisibleForTesting;
1923
import com.google.common.base.Preconditions;
24+
import com.google.common.util.concurrent.MoreExecutors;
25+
import io.grpc.BinaryLog;
26+
import io.grpc.BindableService;
27+
import io.grpc.CompressorRegistry;
2028
import io.grpc.Context;
2129
import io.grpc.Deadline;
30+
import io.grpc.DecompressorRegistry;
31+
import io.grpc.HandlerRegistry;
2232
import io.grpc.InternalChannelz;
2333
import io.grpc.Server;
2434
import io.grpc.ServerBuilder;
35+
import io.grpc.ServerInterceptor;
36+
import io.grpc.ServerServiceDefinition;
2537
import io.grpc.ServerStreamTracer;
38+
import io.grpc.ServerTransportFilter;
2639
import java.io.File;
2740
import java.util.List;
2841
import java.util.concurrent.Executor;
42+
import java.util.concurrent.TimeUnit;
43+
import javax.annotation.Nullable;
2944

3045
/**
3146
* Default builder for {@link io.grpc.Server} instances, for usage in Transport implementations.
@@ -61,6 +76,84 @@ protected List<? extends InternalServer> buildTransportServers(
6176
return clientTransportServersBuilder.buildClientTransportServers(streamTracerFactories);
6277
}
6378

79+
@Override
80+
public final ServerImplBuilder directExecutor() {
81+
return executor(MoreExecutors.directExecutor());
82+
}
83+
84+
@Override
85+
public final ServerImplBuilder executor(@Nullable Executor executor) {
86+
this.executorPool = executor != null ? new FixedObjectPool<>(executor) : DEFAULT_EXECUTOR_POOL;
87+
return this;
88+
}
89+
90+
@Override
91+
public final ServerImplBuilder addService(ServerServiceDefinition service) {
92+
registryBuilder.addService(checkNotNull(service, "service"));
93+
return this;
94+
}
95+
96+
@Override
97+
public final ServerImplBuilder addService(BindableService bindableService) {
98+
return addService(checkNotNull(bindableService, "bindableService").bindService());
99+
}
100+
101+
@Override
102+
public final ServerImplBuilder addTransportFilter(ServerTransportFilter filter) {
103+
transportFilters.add(checkNotNull(filter, "filter"));
104+
return this;
105+
}
106+
107+
@Override
108+
public final ServerImplBuilder intercept(ServerInterceptor interceptor) {
109+
interceptors.add(checkNotNull(interceptor, "interceptor"));
110+
return this;
111+
}
112+
113+
@Override
114+
public final ServerImplBuilder addStreamTracerFactory(ServerStreamTracer.Factory factory) {
115+
streamTracerFactories.add(checkNotNull(factory, "factory"));
116+
return this;
117+
}
118+
119+
@Override
120+
public final ServerImplBuilder fallbackHandlerRegistry(@Nullable HandlerRegistry registry) {
121+
this.fallbackRegistry = registry != null ? registry : DEFAULT_FALLBACK_REGISTRY;
122+
return this;
123+
}
124+
125+
@Override
126+
public final ServerImplBuilder decompressorRegistry(@Nullable DecompressorRegistry registry) {
127+
this.decompressorRegistry = registry != null ? registry : DEFAULT_DECOMPRESSOR_REGISTRY;
128+
return this;
129+
}
130+
131+
@Override
132+
public final ServerImplBuilder compressorRegistry(@Nullable CompressorRegistry registry) {
133+
this.compressorRegistry = registry != null ? registry : DEFAULT_COMPRESSOR_REGISTRY;
134+
return this;
135+
}
136+
137+
@Override
138+
public final ServerImplBuilder handshakeTimeout(long timeout, TimeUnit unit) {
139+
checkArgument(timeout > 0, "handshake timeout is %s, but must be positive", timeout);
140+
this.handshakeTimeoutMillis = checkNotNull(unit, "unit").toMillis(timeout);
141+
return this;
142+
}
143+
144+
@Override
145+
public final ServerImplBuilder setBinaryLog(@Nullable BinaryLog binaryLog) {
146+
this.binlog = binaryLog;
147+
return this;
148+
}
149+
150+
@VisibleForTesting
151+
public final ServerImplBuilder setTransportTracerFactory(
152+
TransportTracer.Factory transportTracerFactory) {
153+
this.transportTracerFactory = transportTracerFactory;
154+
return this;
155+
}
156+
64157
@Override
65158
public void setDeadlineTicker(Deadline.Ticker ticker) {
66159
super.setDeadlineTicker(ticker);
@@ -107,6 +200,7 @@ public ServerImplBuilder useTransportSecurity(File certChain, File privateKey) {
107200
}
108201

109202
public static ServerBuilder<?> forPort(int port) {
110-
throw new UnsupportedOperationException("ClientTransportServersBuilder is required");
203+
throw new UnsupportedOperationException(
204+
"ClientTransportServersBuilder is required, use a constructor");
111205
}
112206
}

0 commit comments

Comments
 (0)