nl.topicus.jdbc.shaded.io.grpc.internal.AbstractServerImplBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spanner-jdbc Show documentation
Show all versions of spanner-jdbc Show documentation
JDBC Driver for Google Cloud Spanner
/*
* Copyright 2014, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package nl.topicus.jdbc.shaded.io.grpc.internal;
import static nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.base.MoreObjects.firstNonNull;
import static nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.base.Preconditions.checkNotNull;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.annotations.VisibleForTesting;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.util.concurrent.MoreExecutors;
import nl.topicus.jdbc.shaded.com.google.instrumentation.stats.Stats;
import nl.topicus.jdbc.shaded.com.google.instrumentation.stats.StatsContextFactory;
import nl.topicus.jdbc.shaded.com.google.instrumentation.trace.Tracing;
import nl.topicus.jdbc.shaded.io.grpc.BindableService;
import nl.topicus.jdbc.shaded.io.grpc.CompressorRegistry;
import nl.topicus.jdbc.shaded.io.grpc.Context;
import nl.topicus.jdbc.shaded.io.grpc.DecompressorRegistry;
import nl.topicus.jdbc.shaded.io.grpc.HandlerRegistry;
import nl.topicus.jdbc.shaded.io.grpc.Internal;
import nl.topicus.jdbc.shaded.io.grpc.InternalNotifyOnServerBuild;
import nl.topicus.jdbc.shaded.io.grpc.ServerBuilder;
import nl.topicus.jdbc.shaded.io.grpc.ServerMethodDefinition;
import nl.topicus.jdbc.shaded.io.grpc.ServerServiceDefinition;
import nl.topicus.jdbc.shaded.io.grpc.ServerStreamTracer;
import nl.topicus.jdbc.shaded.io.grpc.ServerTransportFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
import nl.topicus.jdbc.shaded.javax.annotation.Nullable;
/**
* The base class for server builders.
*
* @param The concrete type for this builder.
*/
public abstract class AbstractServerImplBuilder>
extends ServerBuilder {
private static final HandlerRegistry EMPTY_FALLBACK_REGISTRY = new HandlerRegistry() {
@Override
public List getServices() {
return Collections.emptyList();
}
@Override
public ServerMethodDefinition, ?> lookupMethod(String methodName,
@Nullable String authority) {
return null;
}
};
private final InternalHandlerRegistry.Builder registryBuilder =
new InternalHandlerRegistry.Builder();
private final ArrayList transportFilters =
new ArrayList();
private final List notifyOnBuildList =
new ArrayList();
private final List streamTracerFactories =
new ArrayList();
@Nullable
private HandlerRegistry fallbackRegistry;
@Nullable
private Executor executor;
@Nullable
private DecompressorRegistry decompressorRegistry;
@Nullable
private CompressorRegistry nl.topicus.jdbc.shaded.com.ressorRegistry;
@Nullable
private StatsContextFactory statsFactory;
@Override
public final T directExecutor() {
return executor(MoreExecutors.directExecutor());
}
@Override
public final T executor(@Nullable Executor executor) {
this.executor = executor;
return thisT();
}
@Override
public final T addService(ServerServiceDefinition service) {
registryBuilder.addService(service);
return thisT();
}
@Override
public final T addService(BindableService bindableService) {
if (bindableService instanceof InternalNotifyOnServerBuild) {
notifyOnBuildList.add((InternalNotifyOnServerBuild) bindableService);
}
return addService(bindableService.bindService());
}
@Override
public final T addTransportFilter(ServerTransportFilter filter) {
transportFilters.add(checkNotNull(filter, "filter"));
return thisT();
}
@Override
public final T addStreamTracerFactory(ServerStreamTracer.Factory factory) {
streamTracerFactories.add(checkNotNull(factory, "factory"));
return thisT();
}
@Override
public final T fallbackHandlerRegistry(HandlerRegistry registry) {
this.fallbackRegistry = registry;
return thisT();
}
@Override
public final T decompressorRegistry(DecompressorRegistry registry) {
decompressorRegistry = registry;
return thisT();
}
@Override
public final T nl.topicus.jdbc.shaded.com.ressorRegistry(CompressorRegistry registry) {
nl.topicus.jdbc.shaded.com.ressorRegistry = registry;
return thisT();
}
/**
* Override the default stats implementation.
*/
@VisibleForTesting
protected T statsContextFactory(StatsContextFactory statsFactory) {
this.statsFactory = statsFactory;
return thisT();
}
@Override
public ServerImpl build() {
ArrayList tracerFactories =
new ArrayList();
StatsContextFactory statsFactory =
this.statsFactory != null ? this.statsFactory : Stats.getStatsContextFactory();
if (statsFactory != null) {
CensusStatsModule censusStats =
new CensusStatsModule(
statsFactory, GrpcUtil.STOPWATCH_SUPPLIER, true /** only matters on client-side **/);
tracerFactories.add(censusStats.getServerTracerFactory());
}
CensusTracingModule censusTracing =
new CensusTracingModule(Tracing.getTracer(), Tracing.getBinaryPropagationHandler());
tracerFactories.add(censusTracing.getServerTracerFactory());
tracerFactories.addAll(streamTracerFactories);
nl.topicus.jdbc.shaded.io.grpc.internal.InternalServer transportServer =
buildTransportServer(Collections.unmodifiableList(tracerFactories));
ServerImpl server = new ServerImpl(getExecutorPool(),
SharedResourcePool.forResource(GrpcUtil.TIMER_SERVICE), registryBuilder.build(),
firstNonNull(fallbackRegistry, EMPTY_FALLBACK_REGISTRY), transportServer,
Context.ROOT, firstNonNull(decompressorRegistry, DecompressorRegistry.getDefaultInstance()),
firstNonNull(nl.topicus.jdbc.shaded.com.ressorRegistry, CompressorRegistry.getDefaultInstance()),
transportFilters);
for (InternalNotifyOnServerBuild notifyTarget : notifyOnBuildList) {
notifyTarget.notifyOnBuild(server);
}
return server;
}
private ObjectPool extends Executor> getExecutorPool() {
final Executor savedExecutor = executor;
if (savedExecutor == null) {
return SharedResourcePool.forResource(GrpcUtil.SHARED_CHANNEL_EXECUTOR);
}
return new ObjectPool() {
@Override
public Executor getObject() {
return savedExecutor;
}
@Override
public Executor returnObject(Object object) {
return null;
}
};
}
/**
* Children of AbstractServerBuilder should override this method to provide transport specific
* information for the server. This method is mean for Transport implementors and should not be
* used by normal users.
*
* @param streamTracerFactories an immutable list of stream tracer factories
*/
@Internal
protected abstract nl.topicus.jdbc.shaded.io.grpc.internal.InternalServer buildTransportServer(
List streamTracerFactories);
private T thisT() {
@SuppressWarnings("unchecked")
T thisT = (T) this;
return thisT;
}
}