From f08bc1ab4c53ec6b35afc2115022bd2104638b8b Mon Sep 17 00:00:00 2001 From: Deepak Dixit Date: Mon, 4 Nov 2024 10:16:33 +0530 Subject: [PATCH] Used try with resource to for MNode.parse method --- .../impl/context/ExecutionContextFactoryImpl.groovy | 5 ++++- .../org/moqui/impl/screen/ScreenFacadeImpl.groovy | 7 +++++-- framework/src/main/java/org/moqui/util/MNode.java | 10 ++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/framework/src/main/groovy/org/moqui/impl/context/ExecutionContextFactoryImpl.groovy b/framework/src/main/groovy/org/moqui/impl/context/ExecutionContextFactoryImpl.groovy index e4ac01a88..321f21a1d 100644 --- a/framework/src/main/groovy/org/moqui/impl/context/ExecutionContextFactoryImpl.groovy +++ b/framework/src/main/groovy/org/moqui/impl/context/ExecutionContextFactoryImpl.groovy @@ -345,12 +345,15 @@ class ExecutionContextFactoryImpl implements ExecutionContextFactory { URL defaultConfUrl = this.class.getClassLoader().getResource("MoquiDefaultConf.xml") if (defaultConfUrl == null) throw new IllegalArgumentException("Could not find MoquiDefaultConf.xml file on the classpath") - MNode newConfigXmlRoot = MNode.parse(defaultConfUrl.toString(), defaultConfUrl.newInputStream()) + //MNode newConfigXmlRoot = MNode.parse(defaultConfUrl.toString(), defaultConfUrl.newInputStream()) + try (InputStream is = defaultConfUrl.newInputStream()) { + MNode newConfigXmlRoot = MNode.parse(defaultConfUrl.toString(), is) // just merge the component configuration, needed before component init is done mergeConfigComponentNodes(newConfigXmlRoot, runtimeConfXmlRoot) return newConfigXmlRoot + } } protected void initComponents(MNode baseConfigNode) { File versionJsonFile = new File(runtimePath + "/version.json") diff --git a/framework/src/main/groovy/org/moqui/impl/screen/ScreenFacadeImpl.groovy b/framework/src/main/groovy/org/moqui/impl/screen/ScreenFacadeImpl.groovy index 42070e4c8..42ad634ae 100644 --- a/framework/src/main/groovy/org/moqui/impl/screen/ScreenFacadeImpl.groovy +++ b/framework/src/main/groovy/org/moqui/impl/screen/ScreenFacadeImpl.groovy @@ -299,11 +299,14 @@ class ScreenFacadeImpl implements ScreenFacade { } protected synchronized MNode makeWidgetTemplatesNodeByLocation(String templateLocation) { - MNode templatesNode = (MNode) widgetTemplateLocationCache.get(templateLocation) + //MNode templatesNode = (MNode) widgetTemplateLocationCache.get(templateLocation) + try (InputStream is = ecfi.resourceFacade.getLocationStream(templateLocation)) { if (templatesNode != null) return templatesNode - templatesNode = MNode.parse(templateLocation, ecfi.resourceFacade.getLocationStream(templateLocation)) + //templatesNode = MNode.parse(templateLocation, ecfi.resourceFacade.getLocationStream(templateLocation)) + templatesNode = MNode.parse(templateLocation, is) widgetTemplateLocationCache.put(templateLocation, templatesNode) + } return templatesNode } diff --git a/framework/src/main/java/org/moqui/util/MNode.java b/framework/src/main/java/org/moqui/util/MNode.java index 4076c45e5..a78d51d51 100644 --- a/framework/src/main/java/org/moqui/util/MNode.java +++ b/framework/src/main/java/org/moqui/util/MNode.java @@ -55,10 +55,15 @@ public static MNode parse(ResourceReference rr) throws BaseException { MNode cached = parsedNodeCache.get(location); if (cached != null && cached.lastModified >= rr.getLastModified()) return cached; - MNode node = parse(location, rr.openStream()); + //MNode node = parse(location, rr.openStream()); + try (InputStream is = rr.openStream()) { + MNode node = parse(location, is); node.lastModified = rr.getLastModified(); if (node.lastModified > 0) parsedNodeCache.put(location, node); return node; + } catch (IOException e) { + throw new BaseException(e); + } } /** Parse from an InputStream and close the stream */ public static MNode parse(String location, InputStream is) throws BaseException { @@ -69,9 +74,6 @@ public static MNode parse(String location, InputStream is) throws BaseException } catch (IOException e) { logger.error("Error closing XML stream from " + location, e); throw new BaseException("Error parsing XML stream from " + location, e); - } finally { - try { is.close(); } - catch (IOException e) { logger.error("Error closing XML stream from " + location, e); } } } public static MNode parse(File fl) throws BaseException {