Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Groovy classes now honor ${} syntax for the src attribute #168

Merged
merged 1 commit into from
Jul 24, 2020
Merged
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
8 changes: 8 additions & 0 deletions doc/src/asciidoc/module_groovy.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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())
Expand Down
10 changes: 7 additions & 3 deletions modules/groovy/src/main/java/org/jpos/q2/qbean/Groovy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand All @@ -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 + "'");
Expand Down