1010import io .grpc .Server ;
1111import io .grpc .netty .shaded .io .grpc .netty .NettyServerBuilder ;
1212import io .grpc .netty .shaded .io .netty .handler .ssl .SslContextBuilder ;
13+ import io .grpc .netty .shaded .io .netty .handler .ssl .ApplicationProtocolConfig ;
14+ import io .grpc .netty .shaded .io .netty .handler .ssl .ApplicationProtocolNames ;
1315import org .apache .commons .logging .Log ;
1416import org .apache .commons .logging .LogFactory ;
1517
2325 */
2426public final class NettyGrpcServer implements GrpcServer {
2527
26- private static Log logger = LogFactory .getLog (NettyGrpcServer .class );
28+ private static final Log LOGGER = LogFactory .getLog (NettyGrpcServer .class );
2729
2830 private final Server server ;
2931 /**
@@ -53,30 +55,40 @@ public NettyGrpcServer(final ChaincodeBase chaincodeBase, final ChaincodeServerP
5355 .maxInboundMessageSize (chaincodeServerProperties .getMaxInboundMessageSize ());
5456
5557 if (chaincodeServerProperties .isTlsEnabled ()) {
58+ SslContextBuilder sslContextBuilder ;
5659 final File keyCertChainFile = Paths .get (chaincodeServerProperties .getKeyCertChainFile ()).toFile ();
5760 final File keyFile = Paths .get (chaincodeServerProperties .getKeyFile ()).toFile ();
5861
5962 if (chaincodeServerProperties .getKeyPassword () == null || chaincodeServerProperties .getKeyPassword ().isEmpty ()) {
60- serverBuilder . sslContext ( SslContextBuilder .forServer (keyCertChainFile , keyFile ). build () );
63+ sslContextBuilder = SslContextBuilder .forServer (keyCertChainFile , keyFile );
6164 } else {
62- serverBuilder . sslContext ( SslContextBuilder .forServer (keyCertChainFile , keyFile , chaincodeServerProperties .getKeyPassword ()). build ());
65+ sslContextBuilder = SslContextBuilder .forServer (keyCertChainFile , keyFile , chaincodeServerProperties .getKeyPassword ());
6366 }
67+
68+ ApplicationProtocolConfig apn = new ApplicationProtocolConfig (
69+ ApplicationProtocolConfig .Protocol .ALPN ,
70+ ApplicationProtocolConfig .SelectorFailureBehavior .NO_ADVERTISE ,
71+ ApplicationProtocolConfig .SelectedListenerFailureBehavior .ACCEPT ,
72+ ApplicationProtocolNames .HTTP_2 );
73+ sslContextBuilder .applicationProtocolConfig (apn );
74+
75+ serverBuilder .sslContext (sslContextBuilder .build ());
6476 }
6577
66- logger .info ("<<<<<<<<<<<<<chaincodeServerProperties>>>>>>>>>>>>:\n " );
67- logger .info ("PortChaincodeServer:" + chaincodeServerProperties .getPortChaincodeServer ());
68- logger .info ("MaxInboundMetadataSize:" + chaincodeServerProperties .getMaxInboundMetadataSize ());
69- logger .info ("MaxInboundMessageSize:" + chaincodeServerProperties .getMaxInboundMessageSize ());
70- logger .info ("MaxConnectionAgeSeconds:" + chaincodeServerProperties .getMaxConnectionAgeSeconds ());
71- logger .info ("KeepAliveTimeoutSeconds:" + chaincodeServerProperties .getKeepAliveTimeoutSeconds ());
72- logger .info ("PermitKeepAliveTimeMinutes:" + chaincodeServerProperties .getPermitKeepAliveTimeMinutes ());
73- logger .info ("KeepAliveTimeMinutes:" + chaincodeServerProperties .getKeepAliveTimeMinutes ());
74- logger .info ("PermitKeepAliveWithoutCalls:" + chaincodeServerProperties .getPermitKeepAliveWithoutCalls ());
75- logger .info ("KeyPassword:" + chaincodeServerProperties .getKeyPassword ());
76- logger .info ("KeyCertChainFile:" + chaincodeServerProperties .getKeyCertChainFile ());
77- logger .info ("KeyFile:" + chaincodeServerProperties .getKeyFile ());
78- logger .info ("isTlsEnabled:" + chaincodeServerProperties .isTlsEnabled ());
79- logger .info ("\n " );
78+ LOGGER .info ("<<<<<<<<<<<<<chaincodeServerProperties>>>>>>>>>>>>:\n " );
79+ LOGGER .info ("PortChaincodeServer:" + chaincodeServerProperties .getPortChaincodeServer ());
80+ LOGGER .info ("MaxInboundMetadataSize:" + chaincodeServerProperties .getMaxInboundMetadataSize ());
81+ LOGGER .info ("MaxInboundMessageSize:" + chaincodeServerProperties .getMaxInboundMessageSize ());
82+ LOGGER .info ("MaxConnectionAgeSeconds:" + chaincodeServerProperties .getMaxConnectionAgeSeconds ());
83+ LOGGER .info ("KeepAliveTimeoutSeconds:" + chaincodeServerProperties .getKeepAliveTimeoutSeconds ());
84+ LOGGER .info ("PermitKeepAliveTimeMinutes:" + chaincodeServerProperties .getPermitKeepAliveTimeMinutes ());
85+ LOGGER .info ("KeepAliveTimeMinutes:" + chaincodeServerProperties .getKeepAliveTimeMinutes ());
86+ LOGGER .info ("PermitKeepAliveWithoutCalls:" + chaincodeServerProperties .getPermitKeepAliveWithoutCalls ());
87+ LOGGER .info ("KeyPassword:" + chaincodeServerProperties .getKeyPassword ());
88+ LOGGER .info ("KeyCertChainFile:" + chaincodeServerProperties .getKeyCertChainFile ());
89+ LOGGER .info ("KeyFile:" + chaincodeServerProperties .getKeyFile ());
90+ LOGGER .info ("isTlsEnabled:" + chaincodeServerProperties .isTlsEnabled ());
91+ LOGGER .info ("\n " );
8092
8193 this .server = serverBuilder .build ();
8294 }
@@ -87,11 +99,11 @@ public NettyGrpcServer(final ChaincodeBase chaincodeBase, final ChaincodeServerP
8799 * @throws IOException
88100 */
89101 public void start () throws IOException {
90- logger .info ("start grpc server" );
102+ LOGGER .info ("start grpc server" );
91103 Runtime .getRuntime ()
92104 .addShutdownHook (
93105 new Thread (() -> {
94- // Use stderr here since the logger may have been reset by its JVM shutdown hook.
106+ // Use stderr here since the LOGGER may have been reset by its JVM shutdown hook.
95107 System .err .println ("*** shutting down gRPC server since JVM is shutting down" );
96108 NettyGrpcServer .this .stop ();
97109 System .err .println ("*** server shut down" );
@@ -105,15 +117,15 @@ public void start() throws IOException {
105117 * @throws InterruptedException
106118 */
107119 public void blockUntilShutdown () throws InterruptedException {
108- logger .info ("Waits for the server to become terminated." );
120+ LOGGER .info ("Waits for the server to become terminated." );
109121 server .awaitTermination ();
110122 }
111123
112124 /**
113125 * shutdown now grpc server.
114126 */
115127 public void stop () {
116- logger .info ("shutdown now grpc server." );
128+ LOGGER .info ("shutdown now grpc server." );
117129 server .shutdownNow ();
118130 }
119131}
0 commit comments