From 9861ec7c7a1c690735749e456fa3633d501c9122 Mon Sep 17 00:00:00 2001 From: Barzilai Spinak Date: Fri, 24 Jul 2020 19:11:09 -0300 Subject: [PATCH] Groovy classes now honor ${} syntax for the src attribute --- doc/src/asciidoc/module_groovy.adoc | 8 ++++++++ .../java/org/jpos/groovy/GroovyRequestListener.java | 8 +++++--- .../groovy/src/main/java/org/jpos/q2/qbean/Groovy.java | 10 +++++++--- .../transaction/participant/GroovyGroupSelector.java | 5 ----- .../transaction/participant/GroovyParticipant.java | 8 ++++++-- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/doc/src/asciidoc/module_groovy.adoc b/doc/src/asciidoc/module_groovy.adoc index 901611c805..927cca1a59 100644 --- a/doc/src/asciidoc/module_groovy.adoc +++ b/doc/src/asciidoc/module_groovy.adoc @@ -27,6 +27,14 @@ Although not strictly required by this particular QBean Groovy adaptor, the `jpo module also includes the handy `groovy-sql` dependency. ====== + +In all the modules described below, the `src` attribute honors the `${...}` syntax. +So, you could have, for example, `src="${groovy.scriptdir}/MyGroovyScript.groovy`. + +You can explicitly resolve the value from a system property using `$sys{...}`, or from +the environment using `$env{...}`. + + ==== Groovy QBean In jPOS 2.0.7 (and later), `QFactory.properties` maps the `groovy` keyword to diff --git a/modules/groovy/src/main/java/org/jpos/groovy/GroovyRequestListener.java b/modules/groovy/src/main/java/org/jpos/groovy/GroovyRequestListener.java index 90431c4de6..6fd36fa111 100644 --- a/modules/groovy/src/main/java/org/jpos/groovy/GroovyRequestListener.java +++ b/modules/groovy/src/main/java/org/jpos/groovy/GroovyRequestListener.java @@ -28,6 +28,7 @@ import org.jpos.iso.ISOMsg; import org.jpos.iso.ISORequestListener; import org.jpos.iso.ISOSource; +import org.jpos.q2.QFactory; import org.jpos.util.Log; import java.io.File; @@ -213,11 +214,11 @@ else if (script instanceof String) } - /** Helper method to {@link #setConfiguration(Element)} - * + /** + * Helper method to {@link #setConfiguration(Element)} * Returns a String, a File, or a fully parsed {@code Class<groovy.lang.Script>}. */ - private Object getScript (Element e) throws ConfigurationException + protected Object getScript (Element e) throws ConfigurationException { compiled= cfg.getBoolean("compiled", true); String src= e.getAttributeValue("src"); @@ -226,6 +227,7 @@ private Object getScript (Element e) throws ConfigurationException if (src != null) { + src= QFactory.getAttributeValue(e, "src"); // effective src, after ${} replacements scriptName= src; // should be a file path, will be output as file name part f= new File(src); if (!f.canRead()) diff --git a/modules/groovy/src/main/java/org/jpos/q2/qbean/Groovy.java b/modules/groovy/src/main/java/org/jpos/q2/qbean/Groovy.java index d709103295..380f15300e 100644 --- a/modules/groovy/src/main/java/org/jpos/q2/qbean/Groovy.java +++ b/modules/groovy/src/main/java/org/jpos/q2/qbean/Groovy.java @@ -24,6 +24,7 @@ import org.jdom2.Element; import org.jpos.groovy.GroovySetup; import org.jpos.q2.QBeanSupport; +import org.jpos.q2.QFactory; import java.io.File; import java.net.URL; @@ -49,9 +50,12 @@ public void run() { binding.setVariable("log", getLog()); binding.setVariable("cfg", getConfiguration()); GroovyShell shell = new GroovyShell(binding); - String scr = e.getAttributeValue("src"); - if (scr != null) - shell.evaluate(new File(scr)); + String src = e.getAttributeValue("src"); + if (src != null) + { + src= QFactory.getAttributeValue(e, "src"); + shell.evaluate(new File(src)); + } else shell.evaluate(e.getText()); } catch (Exception e) { diff --git a/modules/groovy/src/main/java/org/jpos/transaction/participant/GroovyGroupSelector.java b/modules/groovy/src/main/java/org/jpos/transaction/participant/GroovyGroupSelector.java index 7ba0edea42..7535271ed9 100644 --- a/modules/groovy/src/main/java/org/jpos/transaction/participant/GroovyGroupSelector.java +++ b/modules/groovy/src/main/java/org/jpos/transaction/participant/GroovyGroupSelector.java @@ -63,11 +63,6 @@ public String select(long id, Serializable context) { @Override public void setConfiguration(Element e) throws ConfigurationException { super.setConfiguration(e); - - ClassLoader thisCL= this.getClass().getClassLoader(); - URL scriptURL= thisCL.getResource("org/jpos/groovy/JPOSGroovyDefaults.groovy"); - GroovySetup.runScriptOnce(scriptURL); - select = getScript(e.getChild("select")); } diff --git a/modules/groovy/src/main/java/org/jpos/transaction/participant/GroovyParticipant.java b/modules/groovy/src/main/java/org/jpos/transaction/participant/GroovyParticipant.java index 24aad8c287..255e1bf660 100644 --- a/modules/groovy/src/main/java/org/jpos/transaction/participant/GroovyParticipant.java +++ b/modules/groovy/src/main/java/org/jpos/transaction/participant/GroovyParticipant.java @@ -34,6 +34,7 @@ import org.jpos.core.XmlConfigurable; import org.jpos.core.Configuration; import org.jpos.core.ConfigurationException; +import org.jpos.q2.QFactory; import org.jpos.transaction.TransactionParticipant; import org.jpos.transaction.AbortParticipant; import org.jpos.transaction.Context; @@ -228,7 +229,9 @@ public void setConfiguration(Element e) throws ConfigurationException { } - /** Returns a String, a File, or a fully parsed Class<groovy.lang.Script> + /** + * Helper method to {@link #setConfiguration(Element)} + * Returns a String, a File, or a fully parsed Class<groovy.lang.Script> */ protected Object getScript (Element e) throws ConfigurationException { @@ -241,7 +244,8 @@ protected Object getScript (Element e) throws ConfigurationException if (src != null) { - scriptNames.put(elName, src); // the file path, as given + src= QFactory.getAttributeValue(e, "src"); // effective src, after ${} replacements + scriptNames.put(elName, src); // the resolved file path, as given f= new File(src); if (!f.canRead()) throw new ConfigurationException ("Can't read '" + src + "'");