3030import io .grpc .Server ;
3131import io .grpc .ServerBuilder ;
3232import io .grpc .ServerInterceptor ;
33+ import io .grpc .ServerMethodDefinition ;
3334import io .grpc .ServerServiceDefinition ;
3435import io .grpc .ServerStreamTracer ;
3536import io .grpc .ServerTransportFilter ;
@@ -52,7 +53,42 @@ public final class ServerImplBuilder extends AbstractServerImplBuilder<ServerImp
5253
5354 private static final Logger log = Logger .getLogger (ServerImplBuilder .class .getName ());
5455
56+ public static ServerBuilder <?> forPort (int port ) {
57+ throw new UnsupportedOperationException (
58+ "ClientTransportServersBuilder is required, use a constructor" );
59+ }
60+
61+ // defaults
62+ private static final ObjectPool <? extends Executor > DEFAULT_EXECUTOR_POOL =
63+ SharedResourcePool .forResource (GrpcUtil .SHARED_CHANNEL_EXECUTOR );
64+ private static final HandlerRegistry DEFAULT_FALLBACK_REGISTRY = new DefaultFallbackRegistry ();
65+ private static final DecompressorRegistry DEFAULT_DECOMPRESSOR_REGISTRY =
66+ DecompressorRegistry .getDefaultInstance ();
67+ private static final CompressorRegistry DEFAULT_COMPRESSOR_REGISTRY =
68+ CompressorRegistry .getDefaultInstance ();
69+ private static final long DEFAULT_HANDSHAKE_TIMEOUT_MILLIS = TimeUnit .SECONDS .toMillis (120 );
70+
71+ // mutable state
72+ final InternalHandlerRegistry .Builder registryBuilder =
73+ new InternalHandlerRegistry .Builder ();
74+ final List <ServerTransportFilter > transportFilters = new ArrayList <>();
75+ final List <ServerInterceptor > interceptors = new ArrayList <>();
76+ private final List <ServerStreamTracer .Factory > streamTracerFactories = new ArrayList <>();
77+ HandlerRegistry fallbackRegistry = DEFAULT_FALLBACK_REGISTRY ;
78+ ObjectPool <? extends Executor > executorPool = DEFAULT_EXECUTOR_POOL ;
79+ DecompressorRegistry decompressorRegistry = DEFAULT_DECOMPRESSOR_REGISTRY ;
80+ CompressorRegistry compressorRegistry = DEFAULT_COMPRESSOR_REGISTRY ;
81+ long handshakeTimeoutMillis = DEFAULT_HANDSHAKE_TIMEOUT_MILLIS ;
82+ Deadline .Ticker ticker = Deadline .getSystemTicker ();
83+ private boolean statsEnabled = true ;
84+ private boolean recordStartedRpcs = true ;
85+ private boolean recordFinishedRpcs = true ;
86+ private boolean recordRealTimeMetrics = false ;
87+ private boolean tracingEnabled = true ;
88+ @ Nullable BinaryLog binlog ;
89+ TransportTracer .Factory transportTracerFactory = TransportTracer .getDefaultFactory ();
5590 InternalChannelz channelz = InternalChannelz .instance ();
91+ CallTracer .Factory callTracerFactory = CallTracer .getDefaultFactory ();
5692
5793 private final ClientTransportServersBuilder clientTransportServersBuilder ;
5894
@@ -74,14 +110,8 @@ public ServerImplBuilder(ClientTransportServersBuilder clientTransportServersBui
74110 }
75111
76112 @ Override
77- public Server build () {
78- return new ServerImpl (this , buildTransportServers (getTracerFactories ()), Context .ROOT );
79- }
80-
81- @ Override
82- protected List <? extends InternalServer > buildTransportServers (
83- List <? extends ServerStreamTracer .Factory > streamTracerFactories ) {
84- return clientTransportServersBuilder .buildClientTransportServers (streamTracerFactories );
113+ public ServerImplBuilder useTransportSecurity (File certChain , File privateKey ) {
114+ throw new UnsupportedOperationException ("TLS not supported in ServerImplBuilder" );
85115 }
86116
87117 @ Override
@@ -208,6 +238,11 @@ public void setDeadlineTicker(Deadline.Ticker ticker) {
208238 this .ticker = Preconditions .checkNotNull (ticker , "ticker" );
209239 }
210240
241+ @ Override
242+ public Server build () {
243+ return new ServerImpl (this , buildTransportServers (getTracerFactories ()), Context .ROOT );
244+ }
245+
211246 @ VisibleForTesting
212247 List <? extends ServerStreamTracer .Factory > getTracerFactories () {
213248 ArrayList <ServerStreamTracer .Factory > tracerFactories = new ArrayList <>();
@@ -275,18 +310,36 @@ public InternalChannelz getChannelz() {
275310 return channelz ;
276311 }
277312
278- @ Override
279- public ObjectPool <? extends Executor > getExecutorPool () {
280- return super .getExecutorPool ();
313+ /**
314+ * Transport implementors must implement {@link ClientTransportServersBuilder} to transport
315+ * specific information for the server. This method is mean for Transport implementors and should
316+ * not be used by normal users.
317+ *
318+ * @param streamTracerFactories an immutable list of stream tracer factories
319+ */
320+ List <? extends InternalServer > buildTransportServers (
321+ List <? extends ServerStreamTracer .Factory > streamTracerFactories ) {
322+ return clientTransportServersBuilder .buildClientTransportServers (streamTracerFactories );
281323 }
282324
283- @ Override
284- public ServerImplBuilder useTransportSecurity (File certChain , File privateKey ) {
285- throw new UnsupportedOperationException ("TLS not supported in ServerImplBuilder" );
325+ private static final class DefaultFallbackRegistry extends HandlerRegistry {
326+ @ Override
327+ public List <ServerServiceDefinition > getServices () {
328+ return Collections .emptyList ();
329+ }
330+
331+ @ Nullable
332+ @ Override
333+ public ServerMethodDefinition <?, ?> lookupMethod (
334+ String methodName , @ Nullable String authority ) {
335+ return null ;
336+ }
286337 }
287338
288- public static ServerBuilder <?> forPort (int port ) {
289- throw new UnsupportedOperationException (
290- "ClientTransportServersBuilder is required, use a constructor" );
339+ /**
340+ * Returns the internal ExecutorPool for offloading tasks.
341+ */
342+ public ObjectPool <? extends Executor > getExecutorPool () {
343+ return this .executorPool ;
291344 }
292345}
0 commit comments