3939
4040public abstract class ChaincodeBase implements Chaincode {
4141
42+ public static final String CORE_CHAINCODE_LOGGING_SHIM = "CORE_CHAINCODE_LOGGING_SHIM" ;
43+ public static final String CORE_CHAINCODE_LOGGING_LEVEL = "CORE_CHAINCODE_LOGGING_LEVEL" ;
44+
4245 @ Override
4346 public abstract Response init (ChaincodeStub stub );
4447
@@ -59,9 +62,9 @@ public abstract class ChaincodeBase implements Chaincode {
5962
6063 private String id ;
6164
62- private final static String CORE_CHAINCODE_ID_NAME = "CORE_CHAINCODE_ID_NAME" ;
63- private final static String CORE_PEER_ADDRESS = "CORE_PEER_ADDRESS" ;
64- private final static String CORE_PEER_TLS_ENABLED = "CORE_PEER_TLS_ENABLED" ;
65+ private static final String CORE_CHAINCODE_ID_NAME = "CORE_CHAINCODE_ID_NAME" ;
66+ private static final String CORE_PEER_ADDRESS = "CORE_PEER_ADDRESS" ;
67+ private static final String CORE_PEER_TLS_ENABLED = "CORE_PEER_TLS_ENABLED" ;
6568 private static final String CORE_PEER_TLS_ROOTCERT_FILE = "CORE_PEER_TLS_ROOTCERT_FILE" ;
6669 private static final String ENV_TLS_CLIENT_KEY_PATH = "CORE_TLS_CLIENT_KEY_PATH" ;
6770 private static final String ENV_TLS_CLIENT_CERT_PATH = "CORE_TLS_CLIENT_CERT_PATH" ;
@@ -76,22 +79,21 @@ public abstract class ChaincodeBase implements Chaincode {
7679 * @param args command line arguments
7780 */
7881 public void start (String [] args ) {
79- processEnvironmentOptions ();
80- processCommandLineOptions (args );
81- initializeLogging ();
8282 try {
83+ processEnvironmentOptions ();
84+ processCommandLineOptions (args );
85+ initializeLogging ();
8386 validateOptions ();
8487 final ChaincodeID chaincodeId = ChaincodeID .newBuilder ().setName (this .id ).build ();
8588 final ManagedChannelBuilder <?> channelBuilder = newChannelBuilder ();
8689 final Handler handler = new Handler (chaincodeId , this );
8790 new ChaincodeSupportStream (channelBuilder , handler ::onChaincodeMessage , handler ::nextOutboundChaincodeMessage );
88-
89- } catch (IllegalArgumentException e ) {
91+ } catch (Exception e ) {
9092 logger .fatal ("Chaincode could not start" , e );
9193 }
9294 }
9395
94- private void initializeLogging () {
96+ void initializeLogging () {
9597 System .setProperty ("java.util.logging.SimpleFormatter.format" , "%1$tH:%1$tM:%1$tS:%1$tL %4$-7.7s %2$s %5$s%6$s%n" );
9698 final Logger rootLogger = Logger .getLogger ("" );
9799 for (java .util .logging .Handler handler : rootLogger .getHandlers ()) {
@@ -110,32 +112,35 @@ public synchronized String format(LogRecord record) {
110112 });
111113 }
112114 // set logging level of shim logger
113- Logger .getLogger ("org.hyperledger.fabric.shim" ).setLevel (mapLevel (System .getenv ("CORE_CHAINCODE_LOGGING_SHIM" )));
115+ Level shimLogLevel = mapLevel (System .getenv (CORE_CHAINCODE_LOGGING_SHIM ));
116+ Logger .getLogger (ChaincodeBase .class .getPackage ().getName ()).setLevel (shimLogLevel );
114117
115118 // set logging level of chaincode logger
116- Logger .getLogger (this .getClass ().getPackage ().getName ()).setLevel (mapLevel (System .getenv ("CORE_CHAINCODE_LOGGING_LEVEL" )));
119+ Level chaincodeLogLevel = mapLevel (System .getenv (CORE_CHAINCODE_LOGGING_LEVEL ));
120+ Logger .getLogger (this .getClass ().getPackage ().getName ()).setLevel (chaincodeLogLevel );
117121
118122 }
119123
120124 private Level mapLevel (String level ) {
121- switch (level ) {
122- case "CRITICAL" :
123- case "ERROR " :
124- return Level . SEVERE ;
125- case "WARNING" :
126- return Level . WARNING ;
127- case "INFO" :
128- return Level . INFO ;
129- case "NOTICE" :
130- return Level . CONFIG ;
131- case "DEBUG" :
132- return Level . FINEST ;
133- default :
134- return Level . INFO ;
125+ if (level != null ) {
126+ switch ( level ) {
127+ case "CRITICAL " :
128+ case "ERROR" :
129+ return Level . SEVERE ;
130+ case " WARNING" :
131+ return Level . WARNING ;
132+ case " INFO" :
133+ return Level . INFO ;
134+ case "NOTICE" :
135+ return Level . CONFIG ;
136+ case "DEBUG" :
137+ return Level . FINEST ;
138+ }
135139 }
140+ return Level .INFO ;
136141 }
137142
138- private void validateOptions () {
143+ void validateOptions () {
139144 if (this .id == null ) {
140145 throw new IllegalArgumentException (format ("The chaincode id must be specified using either the -i or --i command line options or the %s environment variable." , CORE_CHAINCODE_ID_NAME ));
141146 }
@@ -152,7 +157,7 @@ private void validateOptions() {
152157 }
153158 }
154159
155- private void processCommandLineOptions (String [] args ) {
160+ void processCommandLineOptions (String [] args ) {
156161 Options options = new Options ();
157162 options .addOption ("a" , "peer.address" , true , "Address of peer to connect to" );
158163 options .addOption (null , "peerAddress" , true , "Address of peer to connect to" );
@@ -182,7 +187,6 @@ private void processCommandLineOptions(String[] args) {
182187 }
183188 } catch (Exception e ) {
184189 logger .warn ("cli parsing failed with exception" , e );
185-
186190 }
187191
188192 logger .info ("<<<<<<<<<<<<<CommandLine options>>>>>>>>>>>>" );
@@ -194,7 +198,7 @@ private void processCommandLineOptions(String[] args) {
194198 logger .info ("CORE_TLS_CLIENT_CERT_PATH" + this .tlsClientCertPath );
195199 }
196200
197- private void processEnvironmentOptions () {
201+ void processEnvironmentOptions () {
198202 if (System .getenv ().containsKey (CORE_CHAINCODE_ID_NAME )) {
199203 this .id = System .getenv (CORE_CHAINCODE_ID_NAME );
200204 }
@@ -224,35 +228,31 @@ private void processEnvironmentOptions() {
224228 logger .info ("CORE_TLS_CLIENT_CERT_PATH" + this .tlsClientCertPath );
225229 }
226230
227- private ManagedChannelBuilder <?> newChannelBuilder () {
231+ ManagedChannelBuilder <?> newChannelBuilder () throws IOException {
228232 final NettyChannelBuilder builder = NettyChannelBuilder .forAddress (host , port );
229233 logger .info ("Configuring channel connection to peer." );
230234
231235 if (tlsEnabled ) {
232- logger .info ("TLS is enabled" );
233- try {
234- byte ckb [] = Files .readAllBytes (Paths .get (this .tlsClientKeyPath ));
235- byte ccb [] = Files .readAllBytes (Paths .get (this .tlsClientCertPath ));
236-
237-
238- final SslContext sslContext = GrpcSslContexts .forClient ()
239- .trustManager (new File (this .tlsClientRootCertPath ))
240- .keyManager (
241- new ByteArrayInputStream (Base64 .getDecoder ().decode (ccb )),
242- new ByteArrayInputStream (Base64 .getDecoder ().decode (ckb )))
243- .build ();
244- builder .negotiationType (NegotiationType .TLS );
245- builder .sslContext (sslContext );
246- logger .info ("TLS context built: " + sslContext );
247- } catch (IOException e ) {
248- logger .fatal ("failed connect to peer" , e );
249- }
236+ builder .negotiationType (NegotiationType .TLS );
237+ builder .sslContext (createSSLContext ());
250238 } else {
251239 builder .usePlaintext (true );
252240 }
253241 return builder ;
254242 }
255243
244+ SslContext createSSLContext () throws IOException {
245+ byte ckb [] = Files .readAllBytes (Paths .get (this .tlsClientKeyPath ));
246+ byte ccb [] = Files .readAllBytes (Paths .get (this .tlsClientCertPath ));
247+
248+ return GrpcSslContexts .forClient ()
249+ .trustManager (new File (this .tlsClientRootCertPath ))
250+ .keyManager (
251+ new ByteArrayInputStream (Base64 .getDecoder ().decode (ccb )),
252+ new ByteArrayInputStream (Base64 .getDecoder ().decode (ckb )))
253+ .build ();
254+ }
255+
256256 protected static Response newSuccessResponse (String message , byte [] payload ) {
257257 return new Response (SUCCESS , message , payload );
258258 }
@@ -295,4 +295,32 @@ private static byte[] printStackTrace(Throwable throwable) {
295295 throwable .printStackTrace (new PrintWriter (buffer ));
296296 return buffer .toString ().getBytes (StandardCharsets .UTF_8 );
297297 }
298+
299+ String getHost () {
300+ return host ;
301+ }
302+
303+ int getPort () {
304+ return port ;
305+ }
306+
307+ boolean isTlsEnabled () {
308+ return tlsEnabled ;
309+ }
310+
311+ String getTlsClientKeyPath () {
312+ return tlsClientKeyPath ;
313+ }
314+
315+ String getTlsClientCertPath () {
316+ return tlsClientCertPath ;
317+ }
318+
319+ String getTlsClientRootCertPath () {
320+ return tlsClientRootCertPath ;
321+ }
322+
323+ String getId () {
324+ return id ;
325+ }
298326}
0 commit comments