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; 18 19 import com.google.common.base.MoreObjects; 20 import com.google.errorprone.annotations.DoNotCall; 21 import java.io.File; 22 import java.io.InputStream; 23 import java.util.concurrent.Executor; 24 import java.util.concurrent.TimeUnit; 25 import javax.annotation.Nullable; 26 27 /** 28 * A {@link ServerBuilder} that delegates all its builder methods to another builder by default. 29 * 30 * @param <T> The type of the subclass extending this abstract class. 31 * @since 1.34.0 32 */ 33 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/7393") 34 public abstract class ForwardingServerBuilder<T extends ServerBuilder<T>> extends ServerBuilder<T> { 35 36 /** The default constructor. */ ForwardingServerBuilder()37 protected ForwardingServerBuilder() {} 38 39 /** 40 * This method serves to force sub classes to "hide" this static factory. 41 */ 42 @DoNotCall("Unsupported") forPort(int port)43 public static ServerBuilder<?> forPort(int port) { 44 throw new UnsupportedOperationException("Subclass failed to hide static factory"); 45 } 46 47 /** 48 * Returns the delegated {@code ServerBuilder}. 49 */ delegate()50 protected abstract ServerBuilder<?> delegate(); 51 52 @Override directExecutor()53 public T directExecutor() { 54 delegate().directExecutor(); 55 return thisT(); 56 } 57 58 @Override executor(@ullable Executor executor)59 public T executor(@Nullable Executor executor) { 60 delegate().executor(executor); 61 return thisT(); 62 } 63 64 @Override callExecutor(ServerCallExecutorSupplier executorSupplier)65 public T callExecutor(ServerCallExecutorSupplier executorSupplier) { 66 delegate().callExecutor(executorSupplier); 67 return thisT(); 68 } 69 70 @Override addService(ServerServiceDefinition service)71 public T addService(ServerServiceDefinition service) { 72 delegate().addService(service); 73 return thisT(); 74 } 75 76 @Override addService(BindableService bindableService)77 public T addService(BindableService bindableService) { 78 delegate().addService(bindableService); 79 return thisT(); 80 } 81 82 @Override intercept(ServerInterceptor interceptor)83 public T intercept(ServerInterceptor interceptor) { 84 delegate().intercept(interceptor); 85 return thisT(); 86 } 87 88 @Override addTransportFilter(ServerTransportFilter filter)89 public T addTransportFilter(ServerTransportFilter filter) { 90 delegate().addTransportFilter(filter); 91 return thisT(); 92 } 93 94 @Override addStreamTracerFactory(ServerStreamTracer.Factory factory)95 public T addStreamTracerFactory(ServerStreamTracer.Factory factory) { 96 delegate().addStreamTracerFactory(factory); 97 return thisT(); 98 } 99 100 @Override fallbackHandlerRegistry(@ullable HandlerRegistry fallbackRegistry)101 public T fallbackHandlerRegistry(@Nullable HandlerRegistry fallbackRegistry) { 102 delegate().fallbackHandlerRegistry(fallbackRegistry); 103 return thisT(); 104 } 105 106 @Override useTransportSecurity(File certChain, File privateKey)107 public T useTransportSecurity(File certChain, File privateKey) { 108 delegate().useTransportSecurity(certChain, privateKey); 109 return thisT(); 110 } 111 112 @Override useTransportSecurity(InputStream certChain, InputStream privateKey)113 public T useTransportSecurity(InputStream certChain, InputStream privateKey) { 114 delegate().useTransportSecurity(certChain, privateKey); 115 return thisT(); 116 } 117 118 @Override decompressorRegistry(@ullable DecompressorRegistry registry)119 public T decompressorRegistry(@Nullable DecompressorRegistry registry) { 120 delegate().decompressorRegistry(registry); 121 return thisT(); 122 } 123 124 @Override compressorRegistry(@ullable CompressorRegistry registry)125 public T compressorRegistry(@Nullable CompressorRegistry registry) { 126 delegate().compressorRegistry(registry); 127 return thisT(); 128 } 129 130 @Override handshakeTimeout(long timeout, TimeUnit unit)131 public T handshakeTimeout(long timeout, TimeUnit unit) { 132 delegate().handshakeTimeout(timeout, unit); 133 return thisT(); 134 } 135 136 @Override keepAliveTime(long keepAliveTime, TimeUnit timeUnit)137 public T keepAliveTime(long keepAliveTime, TimeUnit timeUnit) { 138 delegate().keepAliveTime(keepAliveTime, timeUnit); 139 return thisT(); 140 } 141 142 @Override keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit)143 public T keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit) { 144 delegate().keepAliveTimeout(keepAliveTimeout, timeUnit); 145 return thisT(); 146 } 147 148 @Override maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit)149 public T maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit) { 150 delegate().maxConnectionIdle(maxConnectionIdle, timeUnit); 151 return thisT(); 152 } 153 154 @Override maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit)155 public T maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit) { 156 delegate().maxConnectionAge(maxConnectionAge, timeUnit); 157 return thisT(); 158 } 159 160 @Override maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit)161 public T maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit) { 162 delegate().maxConnectionAgeGrace(maxConnectionAgeGrace, timeUnit); 163 return thisT(); 164 } 165 166 @Override permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit)167 public T permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit) { 168 delegate().permitKeepAliveTime(keepAliveTime, timeUnit); 169 return thisT(); 170 } 171 172 @Override permitKeepAliveWithoutCalls(boolean permit)173 public T permitKeepAliveWithoutCalls(boolean permit) { 174 delegate().permitKeepAliveWithoutCalls(permit); 175 return thisT(); 176 } 177 178 @Override maxInboundMessageSize(int bytes)179 public T maxInboundMessageSize(int bytes) { 180 delegate().maxInboundMessageSize(bytes); 181 return thisT(); 182 } 183 184 @Override maxInboundMetadataSize(int bytes)185 public T maxInboundMetadataSize(int bytes) { 186 delegate().maxInboundMetadataSize(bytes); 187 return thisT(); 188 } 189 190 @Override setBinaryLog(BinaryLog binaryLog)191 public T setBinaryLog(BinaryLog binaryLog) { 192 delegate().setBinaryLog(binaryLog); 193 return thisT(); 194 } 195 196 /** 197 * Returns the {@link Server} built by the delegate by default. Overriding method can return 198 * different value. 199 */ 200 @Override build()201 public Server build() { 202 return delegate().build(); 203 } 204 205 @Override toString()206 public String toString() { 207 return MoreObjects.toStringHelper(this).add("delegate", delegate()).toString(); 208 } 209 210 /** 211 * Returns the correctly typed version of the builder. 212 */ thisT()213 private T thisT() { 214 @SuppressWarnings("unchecked") 215 T thisT = (T) this; 216 return thisT; 217 } 218 } 219