Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions framework/src/main/groovy/org/moqui/impl/actions/XmlAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.Map;

public class XmlAction {
Expand All @@ -39,6 +40,7 @@ public class XmlAction {
protected final String location;
/** The Groovy class compiled from the script transformed from the XML actions text using the FTL template. */
private Class groovyClassInternal = null;
private final ReentrantLock groovyClassLock = new ReentrantLock();

public XmlAction(ExecutionContextFactoryImpl ecfi, MNode xmlNode, String location) {
this.ecfi = ecfi;
Expand Down Expand Up @@ -93,18 +95,21 @@ public Class getGroovyClass() {
if (groovyClassInternal != null) return groovyClassInternal;
return makeGroovyClass();
}
protected synchronized Class makeGroovyClass() {
if (groovyClassInternal != null) return groovyClassInternal;
String curGroovy = getGroovyString();
// if (logger.isTraceEnabled()) logger.trace("Xml Action [${location}] groovyString: ${curGroovy}")
protected Class makeGroovyClass() {
groovyClassLock.lock();
try {
groovyClassInternal = ecfi.compileGroovy(curGroovy, StringUtilities.cleanStringForJavaName(location));
} catch (Throwable t) {
groovyClassInternal = null;
logger.error("Error parsing groovy String at [" + location + "]:\n" + writeGroovyWithLines() + "\n");
throw t;
}
return groovyClassInternal;
if (groovyClassInternal != null) return groovyClassInternal;
String curGroovy = getGroovyString();
// if (logger.isTraceEnabled()) logger.trace("Xml Action [${location}] groovyString: ${curGroovy}")
try {
groovyClassInternal = ecfi.compileGroovy(curGroovy, StringUtilities.cleanStringForJavaName(location));
} catch (Throwable t) {
groovyClassInternal = null;
logger.error("Error parsing groovy String at [" + location + "]:\n" + writeGroovyWithLines() + "\n");
throw t;
}
return groovyClassInternal;
} finally { groovyClassLock.unlock(); }
}

public String getGroovyString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory

import javax.cache.Cache
import java.util.concurrent.locks.ReentrantLock

@CompileStatic
class XmlActionsScriptRunner implements ScriptRunner {
protected final static Logger logger = LoggerFactory.getLogger(XmlActionsScriptRunner.class)

protected ExecutionContextFactoryImpl ecfi
protected Cache<String, XmlAction> scriptXmlActionLocationCache
protected Template xmlActionsTemplate = null
protected volatile Template xmlActionsTemplate = null
private final ReentrantLock loadXmlActionLock = new ReentrantLock()
private final ReentrantLock makeTemplateLock = new ReentrantLock()

XmlActionsScriptRunner() { }

Expand All @@ -54,34 +57,40 @@ class XmlActionsScriptRunner implements ScriptRunner {
if (xa == null) xa = loadXmlAction(location)
return xa
}
protected synchronized XmlAction loadXmlAction(String location) {
XmlAction xa = (XmlAction) scriptXmlActionLocationCache.get(location)
if (xa == null) {
xa = new XmlAction(ecfi, ecfi.resourceFacade.getLocationText(location, false), location)
scriptXmlActionLocationCache.put(location, xa)
}
return xa
protected XmlAction loadXmlAction(String location) {
loadXmlActionLock.lock()
try {
XmlAction xa = (XmlAction) scriptXmlActionLocationCache.get(location)
if (xa == null) {
xa = new XmlAction(ecfi, ecfi.resourceFacade.getLocationText(location, false), location)
scriptXmlActionLocationCache.put(location, xa)
}
return xa
} finally { loadXmlActionLock.unlock() }
}

Template getXmlActionsTemplate() {
if (xmlActionsTemplate == null) makeXmlActionsTemplate()
return xmlActionsTemplate
}
protected synchronized void makeXmlActionsTemplate() {
if (xmlActionsTemplate != null) return

String templateLocation = ecfi.confXmlRoot.first("resource-facade").attribute("xml-actions-template-location")
Template newTemplate = null
Reader templateReader = null
protected void makeXmlActionsTemplate() {
makeTemplateLock.lock()
try {
templateReader = new InputStreamReader(ecfi.resourceFacade.getLocationStream(templateLocation))
newTemplate = new Template(templateLocation, templateReader,
ecfi.resourceFacade.ftlTemplateRenderer.getFtlConfiguration())
} catch (Exception e) {
logger.error("Error while initializing XMLActions template at [${templateLocation}]", e)
} finally {
if (templateReader != null) templateReader.close()
}
xmlActionsTemplate = newTemplate
if (xmlActionsTemplate != null) return

String templateLocation = ecfi.confXmlRoot.first("resource-facade").attribute("xml-actions-template-location")
Template newTemplate = null
Reader templateReader = null
try {
templateReader = new InputStreamReader(ecfi.resourceFacade.getLocationStream(templateLocation))
newTemplate = new Template(templateLocation, templateReader,
ecfi.resourceFacade.ftlTemplateRenderer.getFtlConfiguration())
} catch (Exception e) {
logger.error("Error while initializing XMLActions template at [${templateLocation}]", e)
} finally {
if (templateReader != null) templateReader.close()
}
xmlActionsTemplate = newTemplate
} finally { makeTemplateLock.unlock() }
}
}
Loading