1616
1717package org .hyperledger .java .shim ;
1818
19- import com .google .protobuf .ByteString ;
20- import io .grpc .ManagedChannel ;
21- import io .grpc .netty .GrpcSslContexts ;
22- import io .grpc .netty .NegotiationType ;
23- import io .grpc .netty .NettyChannelBuilder ;
24- import io .grpc .stub .StreamObserver ;
25- import io .netty .handler .ssl .SslContext ;
19+ import java .io .File ;
20+
21+ import javax .net .ssl .SSLException ;
22+
2623import org .apache .commons .cli .CommandLine ;
2724import org .apache .commons .cli .DefaultParser ;
2825import org .apache .commons .cli .Options ;
2926import org .apache .commons .logging .Log ;
3027import org .apache .commons .logging .LogFactory ;
3128import org .hyperledger .protos .Chaincode .ChaincodeID ;
32- import org .hyperledger .protos .Chaincodeshim .ChaincodeMessage ;
33- import org .hyperledger .protos .Chaincodeshim .ChaincodeMessage .Type ;
3429import org .hyperledger .protos .ChaincodeSupportGrpc ;
3530import org .hyperledger .protos .ChaincodeSupportGrpc .ChaincodeSupportStub ;
31+ import org .hyperledger .protos .Chaincodeshim .ChaincodeMessage ;
32+ import org .hyperledger .protos .Chaincodeshim .ChaincodeMessage .Type ;
3633
37- import javax .net .ssl .SSLException ;
38- import java .io .File ;
34+ import com .google .protobuf .ByteString ;
35+
36+ import io .grpc .ManagedChannel ;
37+ import io .grpc .netty .GrpcSslContexts ;
38+ import io .grpc .netty .NegotiationType ;
39+ import io .grpc .netty .NettyChannelBuilder ;
40+ import io .grpc .stub .StreamObserver ;
41+ import io .netty .handler .ssl .SslContext ;
3942
4043public abstract class ChaincodeBase {
4144
@@ -57,35 +60,16 @@ public abstract class ChaincodeBase {
5760 private Handler handler ;
5861 private String id = getChaincodeID ();
5962
63+ private final static String CORE_CHAINCODE_ID_NAME = "CORE_CHAINCODE_ID_NAME" ;
64+ private final static String CORE_PEER_ADDRESS = "CORE_PEER_ADDRESS" ;
65+ private final static String CORE_PEER_TLS_ENABLED = "CORE_PEER_TLS_ENABLED" ;
66+ private final static String CORE_PEER_TLS_SERVERHOSTOVERRIDE = "CORE_PEER_TLS_SERVERHOSTOVERRIDE" ;
67+
6068 // Start entry point for chaincodes bootstrap.
6169 public void start (String [] args ) {
62- Options options = new Options ();
63- options .addOption ("a" , "peerAddress" , true , "Address of peer to connect to" );
64- options .addOption ("s" , "securityEnabled" , false , "Present if security is enabled" );
65- options .addOption ("i" , "id" , true , "Identity of chaincode" );
66- options .addOption ("o" , "hostNameOverride" , true , "Hostname override for server certificate" );
67- try {
68- CommandLine cl = new DefaultParser ().parse (options , args );
69- if (cl .hasOption ('a' )) {
70- host = cl .getOptionValue ('a' );
71- port = new Integer (host .split (":" )[1 ]);
72- host = host .split (":" )[0 ];
73- }
74- if (cl .hasOption ('s' )) {
75- tlsEnabled = true ;
76- logger .debug ("TLS enabled" );
77- if (cl .hasOption ('o' )){
78- hostOverrideAuthority = cl .getOptionValue ('o' );
79- logger .debug ("server host override given " + hostOverrideAuthority );
80- }
81- }
82- if (cl .hasOption ('i' )) {
83- id = cl .getOptionValue ('i' );
84- }
85- } catch (Exception e ) {
86- logger .warn ("cli parsing failed with exception" ,e );
87-
88- }
70+
71+ processEnvironmentOptions ();
72+ processCommandLineOptions (args );
8973
9074 Runnable chaincode = () -> {
9175 logger .trace ("chaincode started" );
@@ -96,6 +80,51 @@ public void start(String[] args) {
9680 };
9781 new Thread (chaincode ).start ();
9882 }
83+
84+ private void processCommandLineOptions (String [] args ) {
85+ Options options = new Options ();
86+ options .addOption ("a" , "peerAddress" , true , "Address of peer to connect to" );
87+ options .addOption ("s" , "securityEnabled" , false , "Present if security is enabled" );
88+ options .addOption ("i" , "id" , true , "Identity of chaincode" );
89+ options .addOption ("o" , "hostNameOverride" , true , "Hostname override for server certificate" );
90+ try {
91+ CommandLine cl = new DefaultParser ().parse (options , args );
92+ if (cl .hasOption ('a' )) {
93+ host = cl .getOptionValue ('a' );
94+ port = new Integer (host .split (":" )[1 ]);
95+ host = host .split (":" )[0 ];
96+ }
97+ if (cl .hasOption ('s' )) {
98+ tlsEnabled = true ;
99+ logger .debug ("TLS enabled" );
100+ if (cl .hasOption ('o' )){
101+ hostOverrideAuthority = cl .getOptionValue ('o' );
102+ logger .debug ("server host override given " + hostOverrideAuthority );
103+ }
104+ }
105+ if (cl .hasOption ('i' )) {
106+ id = cl .getOptionValue ('i' );
107+ }
108+ } catch (Exception e ) {
109+ logger .warn ("cli parsing failed with exception" ,e );
110+
111+ }
112+ }
113+
114+ private void processEnvironmentOptions () {
115+ if (System .getenv ().containsKey (CORE_CHAINCODE_ID_NAME )) {
116+ this .id = System .getenv (CORE_CHAINCODE_ID_NAME );
117+ }
118+ if (System .getenv ().containsKey (CORE_PEER_ADDRESS )) {
119+ this .host = System .getenv (CORE_PEER_ADDRESS );
120+ }
121+ if (System .getenv ().containsKey (CORE_PEER_TLS_ENABLED )) {
122+ this .tlsEnabled = Boolean .parseBoolean (System .getenv (CORE_PEER_TLS_ENABLED ));
123+ if (System .getenv ().containsKey (CORE_PEER_TLS_SERVERHOSTOVERRIDE )) {
124+ this .hostOverrideAuthority = System .getenv (CORE_PEER_TLS_SERVERHOSTOVERRIDE );
125+ }
126+ }
127+ }
99128
100129 public ManagedChannel newPeerClientConnection () {
101130 NettyChannelBuilder builder = NettyChannelBuilder .forAddress (host , port );
@@ -142,13 +171,13 @@ public void onNext(ChaincodeMessage message) {
142171 e .printStackTrace ();
143172 System .exit (-1 );
144173 //TODO
145- // } else if (err != nil) {
146- // logger.Error(fmt.Sprintf("Received error from server: %s, ending chaincode stream", err))
147- // return
148- // } else if (in == nil) {
149- // err = fmt.Errorf("Received nil message, ending chaincode stream")
150- // logger.debug("Received nil message, ending chaincode stream")
151- // return
174+ // } else if (err != nil) {
175+ // logger.Error(fmt.Sprintf("Received error from server: %s, ending chaincode stream", err))
176+ // return
177+ // } else if (in == nil) {
178+ // err = fmt.Errorf("Received nil message, ending chaincode stream")
179+ // logger.debug("Received nil message, ending chaincode stream")
180+ // return
152181 }
153182 }
154183
0 commit comments