From 825208513498704df4c076d9e5918e2022a56443 Mon Sep 17 00:00:00 2001 From: Bogumil Zebek Date: Tue, 3 Jul 2018 12:32:59 +0200 Subject: Fix stream closing Change-Id: I9c837a3e62d7834922ab0c191590875359a69441 Issue-ID: AAI-1358 Signed-off-by: Bogumil Zebek --- .../onap/aai/restcore/util/GenerateEdgeRules.java | 54 ++++++++-------------- .../src/main/java/org/onap/aai/util/AAIConfig.java | 3 +- .../org/onap/aai/util/HttpsAuthExternalClient.java | 11 +++-- .../onap/aai/util/genxsd/PutRelationPathSet.java | 17 ++++--- .../java/org/onap/aai/util/genxsd/YAMLfromOXM.java | 10 +--- 5 files changed, 35 insertions(+), 60 deletions(-) (limited to 'aai-core/src/main/java') diff --git a/aai-core/src/main/java/org/onap/aai/restcore/util/GenerateEdgeRules.java b/aai-core/src/main/java/org/onap/aai/restcore/util/GenerateEdgeRules.java index afbcf337..4884f400 100644 --- a/aai-core/src/main/java/org/onap/aai/restcore/util/GenerateEdgeRules.java +++ b/aai-core/src/main/java/org/onap/aai/restcore/util/GenerateEdgeRules.java @@ -140,42 +140,13 @@ public class GenerateEdgeRules { Configuration configuration = new Configuration(); Template template = configuration.getTemplate("src/main/resources/edgerulesTemplate.ftlh"); - Writer file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v12.json")); - Map> wrappedRules = new HashMap<>(); - wrappedRules.put("wrappedRules", rulesToWriteV12); - template.process(wrappedRules, file); - file.close(); - - file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v7.json")); - wrappedRules = new HashMap<>(); - wrappedRules.put("wrappedRules", rulesToWriteV7); - template.process(wrappedRules, file); - file.close(); - - file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v8.json")); - wrappedRules = new HashMap<>(); - wrappedRules.put("wrappedRules", rulesToWriteV8); - template.process(wrappedRules, file); - file.close(); - - - file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v9.json")); - wrappedRules = new HashMap<>(); - wrappedRules.put("wrappedRules", rulesToWriteV9); - template.process(wrappedRules, file); - file.close(); - - file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v10.json")); - wrappedRules = new HashMap<>(); - wrappedRules.put("wrappedRules", rulesToWriteV10); - template.process(wrappedRules, file); - file.close(); - - file = new FileWriter(new File("src/main/resources/EdgeRulesWithNewLabels_v11.json")); - wrappedRules = new HashMap<>(); - wrappedRules.put("wrappedRules", rulesToWriteV11); - template.process(wrappedRules, file); - file.close(); + + saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v12.json", template, rulesToWriteV12); + saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v7.json", template, rulesToWriteV7); + saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v8.json", template, rulesToWriteV8); + saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v9.json", template, rulesToWriteV9); + saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v10.json", template, rulesToWriteV10); + saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v11.json", template, rulesToWriteV11); } catch(Exception ex){ ex.printStackTrace(); @@ -184,6 +155,17 @@ public class GenerateEdgeRules { } + private static void saveRulesIntoTheFile(String filePath, Template fileTemplate, List rulesToWrite) + throws IOException, TemplateException { + + + try (Writer file = new FileWriter(new File(filePath))) { + Map> wrappedRules = new HashMap<>(); + wrappedRules.put("wrappedRules", rulesToWrite); + fileTemplate.process(wrappedRules, file); + } + } + private static Map retrieveHeaderMap(String line){ if(line == null) diff --git a/aai-core/src/main/java/org/onap/aai/util/AAIConfig.java b/aai-core/src/main/java/org/onap/aai/util/AAIConfig.java index d1b21634..505e9940 100644 --- a/aai-core/src/main/java/org/onap/aai/util/AAIConfig.java +++ b/aai-core/src/main/java/org/onap/aai/util/AAIConfig.java @@ -143,8 +143,7 @@ public class AAIConfig { LOGGER.debug("Reloading config from " + propFileName); - try { - InputStream is = new FileInputStream(propFileName); + try(InputStream is = new FileInputStream(propFileName)){ newServerProps = new Properties(); newServerProps.load(is); propsInitialized = true; diff --git a/aai-core/src/main/java/org/onap/aai/util/HttpsAuthExternalClient.java b/aai-core/src/main/java/org/onap/aai/util/HttpsAuthExternalClient.java index 84aa6903..888b04f2 100644 --- a/aai-core/src/main/java/org/onap/aai/util/HttpsAuthExternalClient.java +++ b/aai-core/src/main/java/org/onap/aai/util/HttpsAuthExternalClient.java @@ -119,11 +119,12 @@ public class HttpsAuthExternalClient { String alg = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(alg); - FileInputStream tin = new FileInputStream(truststore_path); - KeyStore ts = KeyStore.getInstance("PKCS12"); - char[] tpwd = truststore_password.toCharArray(); - ts.load(tin, tpwd); - tmf.init(ts); + try(FileInputStream tin = new FileInputStream(truststore_path)) { + KeyStore ts = KeyStore.getInstance("PKCS12"); + char[] tpwd = truststore_password.toCharArray(); + ts.load(tin, tpwd); + tmf.init(ts); + } //ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); // Updating key manager to null, to disable two way SSL diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/PutRelationPathSet.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/PutRelationPathSet.java index 3e411946..ca5a6fa7 100644 --- a/aai-core/src/main/java/org/onap/aai/util/genxsd/PutRelationPathSet.java +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/PutRelationPathSet.java @@ -117,26 +117,25 @@ public class PutRelationPathSet { } } private void writeRelationsFile() { - File examplefilePath = new File(GenerateXsd.getYamlDir() + "/relations/" + version.name()+"/"+opId.replace("RelationshipListRelationship", "") + ".json"); - logger.debug(String.join("exampleFilePath: ", examplefilePath.toString())); - FileOutputStream fop = null; + File exampleFilePath = new File(GenerateXsd.getYamlDir() + "/relations/" + version.name()+"/"+opId.replace("RelationshipListRelationship", "") + ".json"); + logger.debug(String.join("exampleFilePath: ", exampleFilePath.toString())); + try { - if (!examplefilePath.exists()) { - examplefilePath.getParentFile().mkdirs(); - examplefilePath.createNewFile(); + if (!exampleFilePath.exists()) { + exampleFilePath.getParentFile().mkdirs(); + exampleFilePath.createNewFile(); } - fop = new FileOutputStream(examplefilePath); } catch(Exception e) { e.printStackTrace(); return; } - try { + + try(FileOutputStream fop = new FileOutputStream(exampleFilePath)){ if(relations.size() > 0) {fop.write("[\n".getBytes());} fop.write(String.join(",\n", relations).getBytes()); if(relations.size() > 0) {fop.write("\n]\n".getBytes());} fop.flush(); - fop.close(); } catch (Exception e) { e.printStackTrace(); return; diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/YAMLfromOXM.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/YAMLfromOXM.java index ce5406a6..da3855c3 100644 --- a/aai-core/src/main/java/org/onap/aai/util/genxsd/YAMLfromOXM.java +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/YAMLfromOXM.java @@ -447,15 +447,9 @@ public class YAMLfromOXM extends OxmFileProcessor { logger.error( "Exception creating output file " + outfileName); e.printStackTrace(); } - BufferedWriter bw = null; - try { - Charset charset = Charset.forName("UTF-8"); - Path path = Paths.get(outfileName); - bw = Files.newBufferedWriter(path, charset); + + try(BufferedWriter bw = Files.newBufferedWriter(Paths.get(outfileName), Charset.forName("UTF-8"))){ bw.write(fileContent); - if ( bw != null ) { - bw.close(); - } } catch ( IOException e) { logger.error( "Exception writing output file " + outfileName); e.printStackTrace(); -- cgit 1.2.3-korg