summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKrishnajinka <kris.jinka@samsung.com>2018-07-06 11:40:55 +0900
committerKrishnajinka <kris.jinka@samsung.com>2018-07-09 10:08:05 +0900
commitda8d16cb974f9b203a59fb350e2179bf19ac3bce (patch)
treef48a34880dabe8fe14aa5cf3a1a47089c320daad
parentf5d3fa206b636a7e235b57b43a7812c92258208a (diff)
use try with resources
Issue-ID: APPC-1046 Change-Id: I28ca35b8a888875f91afda05c4dedee3ed301dde Signed-off-by: Krishnajinka <kris.jinka@samsung.com>
-rw-r--r--appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/ConnectionBuilder.java25
-rw-r--r--appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/DebugLog.java6
-rw-r--r--appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java70
3 files changed, 51 insertions, 50 deletions
diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/ConnectionBuilder.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/ConnectionBuilder.java
index c01fad75f..2272dd26b 100644
--- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/ConnectionBuilder.java
+++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/ConnectionBuilder.java
@@ -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();
+ }
}
/**
diff --git a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/DebugLog.java b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/DebugLog.java
index fb9d7a392..18fcc8413 100644
--- a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/DebugLog.java
+++ b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/DebugLog.java
@@ -69,9 +69,9 @@ public class DebugLog {
// write to it. If it does not exist, don't write to it.
File tmpFile = new File(fileName);
if (tmpFile.exists()) {
- BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true));
- out.write(dataToWrite);
- out.close();
+ try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) {
+ out.write(dataToWrite);
+ }
}
} catch (IOException e) {
DebugLog.printRTAriDebug(fn, "writeToFile() exception: " + e);
diff --git a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java
index 53aaad331..28f00069a 100644
--- a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java
+++ b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java
@@ -583,11 +583,11 @@ public class SshJcraftWrapper {
// write to it. If it does not exist, don't write to it.
File tmpFile = new File(fileName);
if (tmpFile.exists()) {
- BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true));
- // out.write(dataToWrite);
- // out.write(getTheDate() +": " +Thread.currentThread().getName() +": "+dataToWrite);
- out.write(getTheDate() + ": " + tId + ": " + dataToWrite);
- out.close();
+ try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) {
+ // out.write(dataToWrite);
+ // out.write(getTheDate() +": " +Thread.currentThread().getName() +": "+dataToWrite);
+ out.write(getTheDate() + ": " + tId + ": " + dataToWrite);
+ }
}
} catch (IOException e) {
debugLog.printRTAriDebug(fn, "Caught an IOException: e=" + e);
@@ -604,9 +604,9 @@ public class SshJcraftWrapper {
// write to it. If it does not exist, don't write to it.
File tmpFile = new File(fileName);
if (tmpFile.exists()) {
- BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true));
- out.write(dataToWrite);
- out.close();
+ try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) {
+ out.write(dataToWrite);
+ }
}
} catch (IOException e) {
debugLog.printRTAriDebug(fn, "Caught an IOException: e=" + e);
@@ -635,10 +635,10 @@ public class SshJcraftWrapper {
{
// if ((tmpFile.exists()) && (tmpFile.setWritable(true, true)))
if (tmpFile.exists()) {
- BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true));
- // out.write("<!-- "+getTheDate() +": " +tId +" -->\n");
- out.write(dataToWrite.toString());
- out.close();
+ try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) {
+ // out.write("<!-- "+getTheDate() +": " +tId +" -->\n");
+ out.write(dataToWrite.toString());
+ }
}
}
} catch (IOException e) {
@@ -656,10 +656,10 @@ public class SshJcraftWrapper {
File tmpFile = new File(fileName);
// if ((tmpFile.exists()) && (tmpFile.setWritable(true, true)))
if (tmpFile.exists()) {
- BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true));
- // out.write("<!-- "+getTheDate() +": " +tId +" -->\n");
- out.write(charBuffer, 0, len);
- out.close();
+ try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) {
+ // out.write("<!-- "+getTheDate() +": " +tId +" -->\n");
+ out.write(charBuffer, 0, len);
+ }
}
} catch (IOException e) {
System.err.println("writeToFile() exception: " + e);
@@ -712,29 +712,29 @@ public class SshJcraftWrapper {
public String getLastFewLinesOfFile(File file, int linesToRead) throws IOException {
String fn = "SshJcraftWrapper.getLastFewLinesOfFile";
- RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
- int lines = 0;
- StringBuilder builder = new StringBuilder();
String tail = "";
- long length = file.length();
- length--;
- randomAccessFile.seek(length);
- for (long seek = length; seek >= 0; --seek) {
- randomAccessFile.seek(seek);
- char c = (char) randomAccessFile.read();
- builder.append(c);
- if (c == '\n') {
- builder = builder.reverse();
- // System.out.println(builder.toString());
- tail = builder.toString() + tail;
- lines++;
- builder.setLength(0);
- if (lines == linesToRead) {
- break;
+ try(RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
+ int lines = 0;
+ StringBuilder builder = new StringBuilder();
+ long length = file.length();
+ length--;
+ randomAccessFile.seek(length);
+ for (long seek = length; seek >= 0; --seek) {
+ randomAccessFile.seek(seek);
+ char c = (char) randomAccessFile.read();
+ builder.append(c);
+ if (c == '\n') {
+ builder = builder.reverse();
+ // System.out.println(builder.toString());
+ tail = builder.toString() + tail;
+ lines++;
+ builder.setLength(0);
+ if (lines == linesToRead) {
+ break;
+ }
}
}
}
- randomAccessFile.close();
if (!jcraftReadSwConfigFileFromDisk()) {
debugLog.printRTAriDebug(fn, "tail='" + tail + "'");
}