From b118cd814ea0a230486c4179d0bcdc27d39d92b1 Mon Sep 17 00:00:00 2001 From: vempo Date: Sun, 22 Oct 2017 19:03:46 +0300 Subject: Additinal fixes for resources not being released More fixes for input streams not being properly closed, with unit tests and other minor improvements (code cleanup and coding conventions). Change-Id: I6751f924a1469d49b996e4f1d6c61371af6714b1 Issue-ID: SDC-291 Signed-off-by: vempo --- .../sdc/translator/services/heattotosca/HeatToToscaUtil.java | 9 ++++++--- .../java/org/openecomp/sdc/translator/utils/ResourceWalker.java | 3 +-- 2 files changed, 7 insertions(+), 5 deletions(-) (limited to 'openecomp-be/lib/openecomp-sdc-translator-lib') diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java index c1f69cf3bf..4e1a60fff8 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java @@ -119,9 +119,12 @@ public class HeatToToscaUtil { mdcDataDebugMessage.debugEntryMessage(null, null); HeatToToscaTranslator heatToToscaTranslator = HeatToToscaTranslatorFactory.getInstance().createInterface(); - InputStream fileContent = fileNameContentMap.getFileContent(SdcCommon.MANIFEST_NAME); - heatToToscaTranslator.addManifest(SdcCommon.MANIFEST_NAME, FileUtils.toByteArray(fileContent)); + try (InputStream fileContent = fileNameContentMap.getFileContent(SdcCommon.MANIFEST_NAME)) { + heatToToscaTranslator.addManifest(SdcCommon.MANIFEST_NAME, FileUtils.toByteArray(fileContent)); + } catch (IOException e) { + throw new RuntimeException("Failed to read manifest", e); + } fileNameContentMap.getFileList().stream() .filter(fileName -> !(fileName.equals(SdcCommon.MANIFEST_NAME))).forEach( @@ -143,7 +146,7 @@ public class HeatToToscaUtil { return heatToToscaTranslator.translate(); } catch (IOException e) { // rethrow as a RuntimeException to keep the signature backward compatible - throw new RuntimeException(e); + throw new RuntimeException("Failed to read Heat template tree", e); } } diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/utils/ResourceWalker.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/utils/ResourceWalker.java index 353b791a1f..9ff1ff9992 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/utils/ResourceWalker.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/utils/ResourceWalker.java @@ -40,8 +40,7 @@ public class ResourceWalker { Exception { Map filesContent = new HashMap<>(); traverse(resourceDirectoryToStart, (fileName, stream) -> { - BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); - try { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) { filesContent.put(fileName, IOUtils.toString(reader)); } catch (IOException exception) { MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API, -- cgit 1.2.3-korg