"Applied fix from trunk for revision: 1857099"

------------------------------------------------------------------------
r1857099 | jleroux | 2019-04-08 11:17:55 +0200 (lun. 08 avr. 2019) | 12 lignes

Improved: Using try-with-resources with File IO Objects.
(OFBIZ-9855)

Uses try with resources for File IO objects like ObjectInputStream and other 
objects. File classes have the AutoCloseable interface.

jleroux: this is only a part of the 2nd patch. The 1st patch will be committed
soon too...

All tests pass

Thanks: Pradhan Yash Sharma
------------------------------------------------------------------------


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/branches/release17.12@1857487 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacques Le Roux
2019-04-13 17:50:42 +00:00
parent bf73437f27
commit 69a46f08f4
17 changed files with 36 additions and 100 deletions
@@ -283,40 +283,22 @@ public final class MiniLangUtil {
*/
public static void writeMiniLangDocument(URL xmlURL, Document document) {
URL styleSheetURL = null;
InputStream styleSheetInStream = null;
Transformer transformer = null;
try {
styleSheetURL = FlexibleLocation.resolveLocation("component://minilang/config/MiniLang.xslt");
styleSheetInStream = styleSheetURL.openStream();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
try (InputStream styleSheetInStream = styleSheetURL.openStream()) {
transformer = transformerFactory.newTransformer(new StreamSource(styleSheetInStream));
}
} catch (Exception e) {
Debug.logWarning(e, "Error reading minilang/config/MiniLang.xslt: ", module);
return;
} finally {
if (styleSheetInStream != null) {
try {
styleSheetInStream.close();
} catch (IOException e) {
Debug.logWarning(e, "Error closing minilang/config/MiniLang.xslt: ", module);
}
}
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(xmlURL.getFile());
try (FileOutputStream fos = new FileOutputStream(xmlURL.getFile())) {
UtilXml.transformDomDocument(transformer, document, fos);
Debug.logInfo("Saved Mini-language file " + xmlURL, module);
} catch (Exception e) {
Debug.logWarning(e, "Error writing mini-language file " + xmlURL + ": ", module);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
Debug.logWarning(e, "Error closing " + xmlURL + ": ", module);
}
}
}
}