use try with resources
[appc.git] / appc-adapters / appc-ansible-adapter / appc-ansible-adapter-bundle / src / main / java / org / onap / appc / adapter / ansible / impl / ConnectionBuilder.java
index c01fad7..2272dd2 100644 (file)
@@ -78,22 +78,23 @@ public class ConnectionBuilder {
             KeyManagementException, NoSuchAlgorithmException, APPCException {
 
         /* Point to the certificate */
-        FileInputStream fs = new FileInputStream(certFile);
+        try(FileInputStream fs = new FileInputStream(certFile)) {
 
-        /* Generate a certificate from the X509 */
-        CertificateFactory cf = CertificateFactory.getInstance("X.509");
-        X509Certificate cert = (X509Certificate) cf.generateCertificate(fs);
+            /* Generate a certificate from the X509 */
+            CertificateFactory cf = CertificateFactory.getInstance("X.509");
+            X509Certificate cert = (X509Certificate) cf.generateCertificate(fs);
 
-        /* Create a keystore object and load the certificate there */
-        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
-        keystore.load(null, null);
-        keystore.setCertificateEntry("cacert", cert);
+            /* Create a keystore object and load the certificate there */
+            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
+            keystore.load(null, null);
+            keystore.setCertificateEntry("cacert", cert);
 
-        SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(keystore).build();
-        SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslcontext,
-                SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
+            SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(keystore).build();
+            SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslcontext,
+                    SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
 
-        httpClient = HttpClients.custom().setSSLSocketFactory(factory).build();
+            httpClient = HttpClients.custom().setSSLSocketFactory(factory).build();
+        }
     }
 
     /**