From e01cec5c2c23fc38e4a3f3ff92d7c08617f76d30 Mon Sep 17 00:00:00 2001 From: Eric Gribkoff Date: Mon, 19 Jun 2017 23:20:59 -0700 Subject: [PATCH] netty: push sslContext initialization into transportCreationParamsFilterFactory --- .../io/grpc/netty/NettyChannelBuilder.java | 88 ++++++++++--------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java b/netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java index 18770a8e37..3683c62427 100644 --- a/netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java +++ b/netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java @@ -432,7 +432,6 @@ public final class NettyChannelBuilder private final Class channelType; private final Map, ?> channelOptions; private final NegotiationType negotiationType; - private final SslContext sslContext; private final EventLoopGroup group; private final boolean usingSharedGroup; private final int flowControlWindow; @@ -452,23 +451,10 @@ public final class NettyChannelBuilder this.channelType = channelType; this.negotiationType = negotiationType; this.channelOptions = new HashMap, Object>(channelOptions); - if (negotiationType == NegotiationType.TLS && sslContext == null) { - try { - sslContext = GrpcSslContexts.forClient().build(); - } catch (SSLException ex) { - throw new RuntimeException(ex); - } - } - this.sslContext = sslContext; if (transportCreationParamsFilterFactory == null) { - transportCreationParamsFilterFactory = new TransportCreationParamsFilterFactory() { - @Override - public TransportCreationParamsFilter create( - SocketAddress targetServerAddress, String authority, String userAgent) { - return new DynamicNettyTransportParams(targetServerAddress, authority, userAgent); - } - }; + transportCreationParamsFilterFactory = + new DefaultNettyTransportCreationParamsFilterFactory(sslContext); } this.transportCreationParamsFilterFactory = transportCreationParamsFilterFactory; @@ -523,38 +509,60 @@ public final class NettyChannelBuilder } } - @CheckReturnValue - private final class DynamicNettyTransportParams implements TransportCreationParamsFilter { + private final class DefaultNettyTransportCreationParamsFilterFactory + implements TransportCreationParamsFilterFactory { + private final SslContext sslContext; - private final SocketAddress targetServerAddress; - private final String authority; - @Nullable private final String userAgent; + private DefaultNettyTransportCreationParamsFilterFactory(SslContext sslContext) { + if (negotiationType == NegotiationType.TLS && sslContext == null) { + try { + sslContext = GrpcSslContexts.forClient().build(); + } catch (SSLException ex) { + throw new RuntimeException(ex); + } + } + this.sslContext = sslContext; + } - private DynamicNettyTransportParams( + @Override + public TransportCreationParamsFilter create( SocketAddress targetServerAddress, String authority, String userAgent) { - this.targetServerAddress = targetServerAddress; - this.authority = authority; - this.userAgent = userAgent; + return new DynamicNettyTransportParams(targetServerAddress, authority, userAgent); } - @Override - public SocketAddress getTargetServerAddress() { - return targetServerAddress; - } + @CheckReturnValue + private final class DynamicNettyTransportParams implements TransportCreationParamsFilter { - @Override - public String getAuthority() { - return authority; - } + private final SocketAddress targetServerAddress; + private final String authority; + @Nullable private final String userAgent; - @Override - public String getUserAgent() { - return userAgent; - } + private DynamicNettyTransportParams( + SocketAddress targetServerAddress, String authority, String userAgent) { + this.targetServerAddress = targetServerAddress; + this.authority = authority; + this.userAgent = userAgent; + } - @Override - public ProtocolNegotiator getProtocolNegotiator() { - return createProtocolNegotiator(authority, negotiationType, sslContext); + @Override + public SocketAddress getTargetServerAddress() { + return targetServerAddress; + } + + @Override + public String getAuthority() { + return authority; + } + + @Override + public String getUserAgent() { + return userAgent; + } + + @Override + public ProtocolNegotiator getProtocolNegotiator() { + return createProtocolNegotiator(authority, negotiationType, sslContext); + } } } }