1616
1717package io .grpc .internal ;
1818
19- import static com .google .common .base .MoreObjects .firstNonNull ;
2019import static com .google .common .base .Preconditions .checkNotNull ;
2120
2221import com .google .common .annotations .VisibleForTesting ;
5251public abstract class AbstractServerImplBuilder <T extends AbstractServerImplBuilder <T >>
5352 extends ServerBuilder <T > {
5453
55- private static final HandlerRegistry EMPTY_FALLBACK_REGISTRY = new HandlerRegistry () {
54+ private static final ObjectPool <? extends Executor > DEFAULT_EXECUTOR_POOL =
55+ SharedResourcePool .forResource (GrpcUtil .SHARED_CHANNEL_EXECUTOR );
56+ private static final HandlerRegistry DEFAULT_FALLBACK_REGISTRY = new HandlerRegistry () {
5657 @ Override
5758 public List <ServerServiceDefinition > getServices () {
5859 return Collections .emptyList ();
@@ -64,32 +65,32 @@ public List<ServerServiceDefinition> getServices() {
6465 return null ;
6566 }
6667 };
68+ private static final DecompressorRegistry DEFAULT_DECOMPRESSOR_REGISTRY =
69+ DecompressorRegistry .getDefaultInstance ();
70+ private static final CompressorRegistry DEFAULT_COMPRESSOR_REGISTRY =
71+ CompressorRegistry .getDefaultInstance ();
6772
68- private final InternalHandlerRegistry .Builder registryBuilder =
73+ final InternalHandlerRegistry .Builder registryBuilder =
6974 new InternalHandlerRegistry .Builder ();
7075
71- private final List <ServerTransportFilter > transportFilters =
76+ final List <ServerTransportFilter > transportFilters =
7277 new ArrayList <ServerTransportFilter >();
7378
74- private final List <ServerInterceptor > interceptors = new ArrayList <ServerInterceptor >();
79+ final List <ServerInterceptor > interceptors = new ArrayList <ServerInterceptor >();
7580
7681 private final List <InternalNotifyOnServerBuild > notifyOnBuildList =
7782 new ArrayList <InternalNotifyOnServerBuild >();
7883
7984 private final List <ServerStreamTracer .Factory > streamTracerFactories =
8085 new ArrayList <ServerStreamTracer .Factory >();
8186
82- @ Nullable
83- private HandlerRegistry fallbackRegistry ;
87+ HandlerRegistry fallbackRegistry = DEFAULT_FALLBACK_REGISTRY ;
8488
85- @ Nullable
86- private Executor executor ;
89+ ObjectPool <? extends Executor > executorPool = DEFAULT_EXECUTOR_POOL ;
8790
88- @ Nullable
89- private DecompressorRegistry decompressorRegistry ;
91+ DecompressorRegistry decompressorRegistry = DEFAULT_DECOMPRESSOR_REGISTRY ;
9092
91- @ Nullable
92- private CompressorRegistry compressorRegistry ;
93+ CompressorRegistry compressorRegistry = DEFAULT_COMPRESSOR_REGISTRY ;
9394
9495 @ Nullable
9596 private StatsContextFactory statsFactory ;
@@ -101,7 +102,11 @@ public final T directExecutor() {
101102
102103 @ Override
103104 public final T executor (@ Nullable Executor executor ) {
104- this .executor = executor ;
105+ if (executor != null ) {
106+ this .executorPool = new FixedObjectPool <Executor >(executor );
107+ } else {
108+ this .executorPool = DEFAULT_EXECUTOR_POOL ;
109+ }
105110 return thisT ();
106111 }
107112
@@ -139,19 +144,31 @@ public final T addStreamTracerFactory(ServerStreamTracer.Factory factory) {
139144
140145 @ Override
141146 public final T fallbackHandlerRegistry (HandlerRegistry registry ) {
142- this .fallbackRegistry = registry ;
147+ if (registry != null ) {
148+ this .fallbackRegistry = registry ;
149+ } else {
150+ this .fallbackRegistry = DEFAULT_FALLBACK_REGISTRY ;
151+ }
143152 return thisT ();
144153 }
145154
146155 @ Override
147156 public final T decompressorRegistry (DecompressorRegistry registry ) {
148- decompressorRegistry = registry ;
157+ if (registry != null ) {
158+ decompressorRegistry = registry ;
159+ } else {
160+ decompressorRegistry = DEFAULT_DECOMPRESSOR_REGISTRY ;
161+ }
149162 return thisT ();
150163 }
151164
152165 @ Override
153166 public final T compressorRegistry (CompressorRegistry registry ) {
154- compressorRegistry = registry ;
167+ if (registry != null ) {
168+ compressorRegistry = registry ;
169+ } else {
170+ compressorRegistry = DEFAULT_COMPRESSOR_REGISTRY ;
171+ }
155172 return thisT ();
156173 }
157174
@@ -166,6 +183,18 @@ protected T statsContextFactory(StatsContextFactory statsFactory) {
166183
167184 @ Override
168185 public Server build () {
186+ ServerImpl server = new ServerImpl (
187+ this ,
188+ SharedResourcePool .forResource (GrpcUtil .TIMER_SERVICE ),
189+ buildTransportServer (Collections .unmodifiableList (getTracerFactories ())),
190+ Context .ROOT );
191+ for (InternalNotifyOnServerBuild notifyTarget : notifyOnBuildList ) {
192+ notifyTarget .notifyOnBuild (server );
193+ }
194+ return server ;
195+ }
196+
197+ private List <ServerStreamTracer .Factory > getTracerFactories () {
169198 ArrayList <ServerStreamTracer .Factory > tracerFactories =
170199 new ArrayList <ServerStreamTracer .Factory >();
171200 StatsContextFactory statsFactory =
@@ -180,37 +209,7 @@ public Server build() {
180209 new CensusTracingModule (Tracing .getTracer (), Tracing .getBinaryPropagationHandler ());
181210 tracerFactories .add (censusTracing .getServerTracerFactory ());
182211 tracerFactories .addAll (streamTracerFactories );
183-
184- io .grpc .internal .InternalServer transportServer =
185- buildTransportServer (Collections .unmodifiableList (tracerFactories ));
186- ServerImpl server = new ServerImpl (getExecutorPool (),
187- SharedResourcePool .forResource (GrpcUtil .TIMER_SERVICE ), registryBuilder .build (),
188- firstNonNull (fallbackRegistry , EMPTY_FALLBACK_REGISTRY ), transportServer ,
189- Context .ROOT , firstNonNull (decompressorRegistry , DecompressorRegistry .getDefaultInstance ()),
190- firstNonNull (compressorRegistry , CompressorRegistry .getDefaultInstance ()),
191- transportFilters , interceptors );
192- for (InternalNotifyOnServerBuild notifyTarget : notifyOnBuildList ) {
193- notifyTarget .notifyOnBuild (server );
194- }
195- return server ;
196- }
197-
198- private ObjectPool <? extends Executor > getExecutorPool () {
199- final Executor savedExecutor = executor ;
200- if (savedExecutor == null ) {
201- return SharedResourcePool .forResource (GrpcUtil .SHARED_CHANNEL_EXECUTOR );
202- }
203- return new ObjectPool <Executor >() {
204- @ Override
205- public Executor getObject () {
206- return savedExecutor ;
207- }
208-
209- @ Override
210- public Executor returnObject (Object object ) {
211- return null ;
212- }
213- };
212+ return tracerFactories ;
214213 }
215214
216215 /**
0 commit comments