16
16
17
17
package org .hyperledger .java .shim ;
18
18
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
+
26
23
import org .apache .commons .cli .CommandLine ;
27
24
import org .apache .commons .cli .DefaultParser ;
28
25
import org .apache .commons .cli .Options ;
29
26
import org .apache .commons .logging .Log ;
30
27
import org .apache .commons .logging .LogFactory ;
31
28
import org .hyperledger .protos .Chaincode .ChaincodeID ;
32
- import org .hyperledger .protos .Chaincodeshim .ChaincodeMessage ;
33
- import org .hyperledger .protos .Chaincodeshim .ChaincodeMessage .Type ;
34
29
import org .hyperledger .protos .ChaincodeSupportGrpc ;
35
30
import org .hyperledger .protos .ChaincodeSupportGrpc .ChaincodeSupportStub ;
31
+ import org .hyperledger .protos .Chaincodeshim .ChaincodeMessage ;
32
+ import org .hyperledger .protos .Chaincodeshim .ChaincodeMessage .Type ;
36
33
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 ;
39
42
40
43
public abstract class ChaincodeBase {
41
44
@@ -57,35 +60,16 @@ public abstract class ChaincodeBase {
57
60
private Handler handler ;
58
61
private String id = getChaincodeID ();
59
62
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
+
60
68
// Start entry point for chaincodes bootstrap.
61
69
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 );
89
73
90
74
Runnable chaincode = () -> {
91
75
logger .trace ("chaincode started" );
@@ -96,6 +80,51 @@ public void start(String[] args) {
96
80
};
97
81
new Thread (chaincode ).start ();
98
82
}
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
+ }
99
128
100
129
public ManagedChannel newPeerClientConnection () {
101
130
NettyChannelBuilder builder = NettyChannelBuilder .forAddress (host , port );
@@ -142,13 +171,13 @@ public void onNext(ChaincodeMessage message) {
142
171
e .printStackTrace ();
143
172
System .exit (-1 );
144
173
//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
152
181
}
153
182
}
154
183
0 commit comments