1 /* 2 * Copyright 2017 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.util.List; 22 import java.util.Map; 23 import java.util.concurrent.Executor; 24 import java.util.concurrent.TimeUnit; 25 import javax.annotation.Nullable; 26 27 /** 28 * A {@link ManagedChannelBuilder} that delegates all its builder methods to another builder by 29 * default. 30 * 31 * @param <T> The type of the subclass extending this abstract class. 32 * 33 * @since 1.7.0 34 */ 35 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/3363") 36 public abstract class ForwardingChannelBuilder<T extends ForwardingChannelBuilder<T>> 37 extends ManagedChannelBuilder<T> { 38 39 /** 40 * The default constructor. 41 */ ForwardingChannelBuilder()42 protected ForwardingChannelBuilder() {} 43 44 /** 45 * This method serves to force sub classes to "hide" this static factory. 46 */ 47 @DoNotCall("Unsupported") forAddress(String name, int port)48 public static ManagedChannelBuilder<?> forAddress(String name, int port) { 49 throw new UnsupportedOperationException("Subclass failed to hide static factory"); 50 } 51 52 /** 53 * This method serves to force sub classes to "hide" this static factory. 54 */ 55 @DoNotCall("Unsupported") forTarget(String target)56 public static ManagedChannelBuilder<?> forTarget(String target) { 57 throw new UnsupportedOperationException("Subclass failed to hide static factory"); 58 } 59 60 /** 61 * Returns the delegated {@code ManagedChannelBuilder}. 62 */ delegate()63 protected abstract ManagedChannelBuilder<?> delegate(); 64 65 @Override directExecutor()66 public T directExecutor() { 67 delegate().directExecutor(); 68 return thisT(); 69 } 70 71 @Override executor(Executor executor)72 public T executor(Executor executor) { 73 delegate().executor(executor); 74 return thisT(); 75 } 76 77 @Override offloadExecutor(Executor executor)78 public T offloadExecutor(Executor executor) { 79 delegate().offloadExecutor(executor); 80 return thisT(); 81 } 82 83 @Override intercept(List<ClientInterceptor> interceptors)84 public T intercept(List<ClientInterceptor> interceptors) { 85 delegate().intercept(interceptors); 86 return thisT(); 87 } 88 89 @Override intercept(ClientInterceptor... interceptors)90 public T intercept(ClientInterceptor... interceptors) { 91 delegate().intercept(interceptors); 92 return thisT(); 93 } 94 95 @Override userAgent(String userAgent)96 public T userAgent(String userAgent) { 97 delegate().userAgent(userAgent); 98 return thisT(); 99 } 100 101 @Override overrideAuthority(String authority)102 public T overrideAuthority(String authority) { 103 delegate().overrideAuthority(authority); 104 return thisT(); 105 } 106 107 @Override usePlaintext()108 public T usePlaintext() { 109 delegate().usePlaintext(); 110 return thisT(); 111 } 112 113 @Override useTransportSecurity()114 public T useTransportSecurity() { 115 delegate().useTransportSecurity(); 116 return thisT(); 117 } 118 119 @Deprecated 120 @Override nameResolverFactory(NameResolver.Factory resolverFactory)121 public T nameResolverFactory(NameResolver.Factory resolverFactory) { 122 delegate().nameResolverFactory(resolverFactory); 123 return thisT(); 124 } 125 126 @Override defaultLoadBalancingPolicy(String policy)127 public T defaultLoadBalancingPolicy(String policy) { 128 delegate().defaultLoadBalancingPolicy(policy); 129 return thisT(); 130 } 131 132 @Override enableFullStreamDecompression()133 public T enableFullStreamDecompression() { 134 delegate().enableFullStreamDecompression(); 135 return thisT(); 136 } 137 138 @Override decompressorRegistry(DecompressorRegistry registry)139 public T decompressorRegistry(DecompressorRegistry registry) { 140 delegate().decompressorRegistry(registry); 141 return thisT(); 142 } 143 144 @Override compressorRegistry(CompressorRegistry registry)145 public T compressorRegistry(CompressorRegistry registry) { 146 delegate().compressorRegistry(registry); 147 return thisT(); 148 } 149 150 @Override idleTimeout(long value, TimeUnit unit)151 public T idleTimeout(long value, TimeUnit unit) { 152 delegate().idleTimeout(value, unit); 153 return thisT(); 154 } 155 156 @Override maxInboundMessageSize(int max)157 public T maxInboundMessageSize(int max) { 158 delegate().maxInboundMessageSize(max); 159 return thisT(); 160 } 161 162 @Override maxInboundMetadataSize(int max)163 public T maxInboundMetadataSize(int max) { 164 delegate().maxInboundMetadataSize(max); 165 return thisT(); 166 } 167 168 @Override keepAliveTime(long keepAliveTime, TimeUnit timeUnit)169 public T keepAliveTime(long keepAliveTime, TimeUnit timeUnit) { 170 delegate().keepAliveTime(keepAliveTime, timeUnit); 171 return thisT(); 172 } 173 174 @Override keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit)175 public T keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit) { 176 delegate().keepAliveTimeout(keepAliveTimeout, timeUnit); 177 return thisT(); 178 } 179 180 @Override keepAliveWithoutCalls(boolean enable)181 public T keepAliveWithoutCalls(boolean enable) { 182 delegate().keepAliveWithoutCalls(enable); 183 return thisT(); 184 } 185 186 @Override maxRetryAttempts(int maxRetryAttempts)187 public T maxRetryAttempts(int maxRetryAttempts) { 188 delegate().maxRetryAttempts(maxRetryAttempts); 189 return thisT(); 190 } 191 192 @Override maxHedgedAttempts(int maxHedgedAttempts)193 public T maxHedgedAttempts(int maxHedgedAttempts) { 194 delegate().maxHedgedAttempts(maxHedgedAttempts); 195 return thisT(); 196 } 197 198 @Override retryBufferSize(long bytes)199 public T retryBufferSize(long bytes) { 200 delegate().retryBufferSize(bytes); 201 return thisT(); 202 } 203 204 @Override perRpcBufferLimit(long bytes)205 public T perRpcBufferLimit(long bytes) { 206 delegate().perRpcBufferLimit(bytes); 207 return thisT(); 208 } 209 210 @Override disableRetry()211 public T disableRetry() { 212 delegate().disableRetry(); 213 return thisT(); 214 } 215 216 @Override enableRetry()217 public T enableRetry() { 218 delegate().enableRetry(); 219 return thisT(); 220 } 221 222 @Override setBinaryLog(BinaryLog binaryLog)223 public T setBinaryLog(BinaryLog binaryLog) { 224 delegate().setBinaryLog(binaryLog); 225 return thisT(); 226 } 227 228 @Override maxTraceEvents(int maxTraceEvents)229 public T maxTraceEvents(int maxTraceEvents) { 230 delegate().maxTraceEvents(maxTraceEvents); 231 return thisT(); 232 } 233 234 @Override proxyDetector(ProxyDetector proxyDetector)235 public T proxyDetector(ProxyDetector proxyDetector) { 236 delegate().proxyDetector(proxyDetector); 237 return thisT(); 238 } 239 240 @Override defaultServiceConfig(@ullable Map<String, ?> serviceConfig)241 public T defaultServiceConfig(@Nullable Map<String, ?> serviceConfig) { 242 delegate().defaultServiceConfig(serviceConfig); 243 return thisT(); 244 } 245 246 @Override disableServiceConfigLookUp()247 public T disableServiceConfigLookUp() { 248 delegate().disableServiceConfigLookUp(); 249 return thisT(); 250 } 251 252 /** 253 * Returns the {@link ManagedChannel} built by the delegate by default. Overriding method can 254 * return different value. 255 */ 256 @Override build()257 public ManagedChannel build() { 258 return delegate().build(); 259 } 260 261 @Override toString()262 public String toString() { 263 return MoreObjects.toStringHelper(this).add("delegate", delegate()).toString(); 264 } 265 266 /** 267 * Returns the correctly typed version of the builder. 268 */ thisT()269 protected final T thisT() { 270 @SuppressWarnings("unchecked") 271 T thisT = (T) this; 272 return thisT; 273 } 274 } 275