Fix stream closing 85/55685/1
authorBogumil Zebek <bogumil.zebek@nokia.com>
Tue, 3 Jul 2018 10:32:59 +0000 (12:32 +0200)
committerBogumil Zebek <bogumil.zebek@nokia.com>
Tue, 3 Jul 2018 10:33:54 +0000 (12:33 +0200)
Change-Id: I9c837a3e62d7834922ab0c191590875359a69441
Issue-ID: AAI-1358
Signed-off-by: Bogumil Zebek <bogumil.zebek@nokia.com>
aai-core/src/main/java/org/onap/aai/restcore/util/GenerateEdgeRules.java
aai-core/src/main/java/org/onap/aai/util/AAIConfig.java
aai-core/src/main/java/org/onap/aai/util/HttpsAuthExternalClient.java
aai-core/src/main/java/org/onap/aai/util/genxsd/PutRelationPathSet.java
aai-core/src/main/java/org/onap/aai/util/genxsd/YAMLfromOXM.java

index afbcf33..4884f40 100644 (file)
@@ -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<String, List<EdgeRuleBean>> 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<EdgeRuleBean> rulesToWrite)
+        throws IOException, TemplateException {
+
+
+        try (Writer file = new FileWriter(new File(filePath))) {
+            Map<String, List<EdgeRuleBean>> wrappedRules = new HashMap<>();
+            wrappedRules.put("wrappedRules", rulesToWrite);
+            fileTemplate.process(wrappedRules, file);
+        }
+    }
+
     private static Map<String, Integer> retrieveHeaderMap(String line){
 
         if(line == null)
index d1b2163..505e994 100644 (file)
@@ -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;
index 84aa690..888b04f 100644 (file)
@@ -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
index 3e41194..ca5a6fa 100644 (file)
@@ -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;
index ce5406a..da3855c 100644 (file)
@@ -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();