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