From eb693adc65142322af4652f4a63b29c5a8a3114b Mon Sep 17 00:00:00 2001 From: Barzilai Spinak Date: Fri, 11 Aug 2017 13:18:58 +0200 Subject: [PATCH 1/3] GroovyParticipant: old copy/paste bug in abort method --- .../org/jpos/transaction/participant/GroovyParticipant.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 b75794b9ab..4840f25a28 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 @@ -100,7 +100,7 @@ public void commit(long id, Serializable ctx) { } } public void abort(long id, Serializable ctx) { - if (commit != null) { + if (abort != null) { try { eval(getShell(id, ctx), abort); } catch (Exception e) { From 4de7f1959aef94f950cd2cd26bf5d4e1d68d04ef Mon Sep 17 00:00:00 2001 From: Barzilai Spinak Date: Fri, 11 Aug 2017 13:21:03 +0200 Subject: [PATCH 2/3] upgrading to groovy 2.4.12 --- libraries.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries.gradle b/libraries.gradle index fa9dfb0689..825fca5a65 100644 --- a/libraries.gradle +++ b/libraries.gradle @@ -11,7 +11,7 @@ ext { jaxrsVersion = '2.0.1' jsonSchemaVersion = '2.2.6' jacksonVersion = '2.7.4' - groovyVersion = '2.4.6' + groovyVersion = '2.4.12' vaadinVersion = '8.1.0.rc1' libraries = [ From 185e9760025d8bab1f4e52096f6285190796a57b Mon Sep 17 00:00:00 2001 From: Barzilai Spinak Date: Fri, 11 Aug 2017 13:25:42 +0200 Subject: [PATCH 3/3] GroovyParticipant: huge performance improvements. Now it parses and pre-compiles scripts using GroovyClassLoader --- .../participant/GroovyParticipant.java | 174 +++++++++++++++--- 1 file changed, 144 insertions(+), 30 deletions(-) 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 4840f25a28..ea93dabd61 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 @@ -18,43 +18,57 @@ package org.jpos.transaction.participant; -import groovy.lang.Binding; -import groovy.lang.GroovyShell; +import java.io.File; +import java.io.IOException; +import java.io.Serializable; + import org.jdom2.Element; import org.jpos.core.Configurable; +import org.jpos.core.XmlConfigurable; import org.jpos.core.Configuration; import org.jpos.core.ConfigurationException; -import org.jpos.core.XmlConfigurable; +import org.jpos.transaction.TransactionParticipant; import org.jpos.transaction.AbortParticipant; import org.jpos.transaction.Context; import org.jpos.transaction.TransactionManager; -import org.jpos.transaction.TransactionParticipant; import org.jpos.util.Log; -import java.io.File; -import java.io.IOException; -import java.io.Serializable; + +import groovy.lang.GroovyClassLoader; +import groovy.lang.Script; +import groovy.lang.Binding; +import groovy.lang.GroovyShell; + /** - * A TransactionParticipant whose prepare, commit and abort methods can be - * specified using Groovy scripts.
- * To indicate what code to execute for any of the TM life-cycle methods just add - * an element named 'prepare', 'commit' or 'abort' and optionally 'prepare-for-abort' - * contained in that of the participant.
+ *

A TransactionParticipant whose prepare, commit and abort methods can be + * specified using Groovy scripts.

* + *

To indicate what code to execute for any of the TM life-cycle methods just add + * an element named 'prepare', 'commit' or 'abort' and optionally 'prepare-for-abort' + * contained in that of the participant. + *
* The 'prepare' and 'prepare-for-abort' methods are expected to return an Integer object * with the TM standard result values (PREPARE, ABORT and its modifiers). + *
+ * The Groovy script code can be placed as part of the element's content (a CDATA section + * is recommended), or in an external file pointed to by the 'src' attribute. We also + * recommend adding a "realm" attribute to identify errors in the logs, especially if you + * have several instances of GroovyParticipant in your transaction manager. + *
+ * By default, scripts are pre-compiled by a GroovyClassLoader. If you want the script + * to be evaluated each time, then set the "compiled" property to "false".

* - * The Groovy script code can be placed as part of the element's content or in an - * external file pointed by the 'src' attribute. - * - * Usage: + * Add a transaction participant like this: * *
- *     Add a transaction participant like this:
  *     <participant class="org.jpos.transaction.participant.GroovyParticipant" logger="Q2" realm="groovy-test">
  *       <prepare src="deploy/prepare.groovy" />
  *       <commit src="deploy/commit.groovy" />
- *       <abort src="deploy/abort.groovy" />
+ *       <abort>
+ *         <![CDATA[
+ *             // ... embedded script
+ *         ]]>
+ *       </abort>
  *     </participant>
  * 
*/ @@ -63,46 +77,87 @@ public class GroovyParticipant extends Log implements TransactionParticipant, AbortParticipant, XmlConfigurable, Configurable { + private boolean compiled= true; + private GroovyClassLoader gcl; + + // prepare, prepareForAbort, commit, and abort + // can be instances of String, File, or Class