Skip to content

Commit

Permalink
Used try with resource for MNode.parse method
Browse files Browse the repository at this point in the history
  • Loading branch information
dixitdeepak committed Oct 29, 2024
1 parent b52180b commit 5c95b66
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,14 @@ 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())
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,11 @@ class ScreenFacadeImpl implements ScreenFacade {
protected synchronized MNode makeWidgetTemplatesNodeByLocation(String templateLocation) {
MNode templatesNode = (MNode) widgetTemplateLocationCache.get(templateLocation)
if (templatesNode != null) return templatesNode

templatesNode = MNode.parse(templateLocation, ecfi.resourceFacade.getLocationStream(templateLocation))
try (InputStream is = ecfi.resourceFacade.getLocationStream(templateLocation)) {
templatesNode = MNode.parse(templateLocation, is)
widgetTemplateLocationCache.put(templateLocation, templatesNode)
}

return templatesNode
}

Expand Down
10 changes: 5 additions & 5 deletions framework/src/main/java/org/moqui/util/MNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ public static MNode parse(ResourceReference rr) throws BaseException {
String location = rr.getLocation();
MNode cached = parsedNodeCache.get(location);
if (cached != null && cached.lastModified >= rr.getLastModified()) return cached;

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 {
Expand All @@ -69,9 +72,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 {
Expand Down

0 comments on commit 5c95b66

Please sign in to comment.