From 6120edf4e969ebfce8aa906e2a7247cdb18daf39 Mon Sep 17 00:00:00 2001 From: Barzilai Spinak Date: Thu, 20 Feb 2020 21:05:31 -0300 Subject: [PATCH 1/2] groovy: renaming/generalizing ContextDefaults.groovy to JPOSGroovyDefaults.groovy --- .../main/java/org/jpos/groovy/GroovyRequestListener.java | 5 +++++ .../groovy/src/main/java/org/jpos/q2/qbean/Groovy.java | 6 ++++++ .../jpos/transaction/participant/GroovyParticipant.java | 4 ++-- .../JPOSGroovyDefaults.groovy} | 8 ++++---- 4 files changed, 17 insertions(+), 6 deletions(-) rename modules/groovy/src/main/resources/org/jpos/{transaction/ContextDefaults.groovy => groovy/JPOSGroovyDefaults.groovy} (94%) 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 2895ba6fe2..74a365c92c 100644 --- a/modules/groovy/src/main/java/org/jpos/groovy/GroovyRequestListener.java +++ b/modules/groovy/src/main/java/org/jpos/groovy/GroovyRequestListener.java @@ -31,6 +31,7 @@ import java.io.File; import java.io.IOException; +import java.net.URL; import java.util.Arrays; import java.util.HashSet; @@ -147,6 +148,10 @@ public void setConfiguration (Configuration cfg) @Override public void setConfiguration(Element e) throws ConfigurationException { + ClassLoader thisCL= this.getClass().getClassLoader(); + URL scriptURL= thisCL.getResource("org/jpos/groovy/JPOSGroovyDefaults.groovy"); + GroovySetup.runScriptOnce(scriptURL); + xmlCfg= e; script= getScript(e.getChild("script")); 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 596f93db3c..7bca2b0802 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 @@ -21,9 +21,11 @@ import groovy.lang.Binding; import groovy.lang.GroovyShell; import org.jdom2.Element; +import org.jpos.groovy.GroovySetup; import org.jpos.q2.QBeanSupport; import java.io.File; +import java.net.URL; /** * Groovy Interpreter QBean. @@ -36,6 +38,10 @@ public void startService() { public void run() { try { + ClassLoader thisCL= this.getClass().getClassLoader(); + URL scriptURL= thisCL.getResource("org/jpos/groovy/JPOSGroovyDefaults.groovy"); + GroovySetup.runScriptOnce(scriptURL); + Element e = getPersist(); Binding binding = new Binding(); binding.setVariable("qbean", this); 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 9d341c6d50..ccc05925f6 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 @@ -110,7 +110,7 @@ public class GroovyParticipant extends Log private TransactionManager tm; protected Configuration cfg; - private final String groovyShellKey = ".groovy-" + Integer.toString(hashCode()); + private final String groovyShellKey = ".groovy-" + hashCode(); @SuppressWarnings("unchecked") @@ -197,7 +197,7 @@ public void setConfiguration(Configuration cfg) throws ConfigurationException { @Override public void setConfiguration(Element e) throws ConfigurationException { ClassLoader thisCL= this.getClass().getClassLoader(); - URL scriptURL= thisCL.getResource("org/jpos/transaction/ContextDefaults.groovy"); + URL scriptURL= thisCL.getResource("org/jpos/groovy/JPOSGroovyDefaults.groovy"); GroovySetup.runScriptOnce(scriptURL); compiled= cfg.getBoolean("compiled", true); diff --git a/modules/groovy/src/main/resources/org/jpos/transaction/ContextDefaults.groovy b/modules/groovy/src/main/resources/org/jpos/groovy/JPOSGroovyDefaults.groovy similarity index 94% rename from modules/groovy/src/main/resources/org/jpos/transaction/ContextDefaults.groovy rename to modules/groovy/src/main/resources/org/jpos/groovy/JPOSGroovyDefaults.groovy index b8bd0e1b07..a35aa05896 100644 --- a/modules/groovy/src/main/resources/org/jpos/transaction/ContextDefaults.groovy +++ b/modules/groovy/src/main/resources/org/jpos/groovy/JPOSGroovyDefaults.groovy @@ -1,6 +1,6 @@ /* * jPOS Project [http://jpos.org] - * Copyright (C) 2000-2017 jPOS Software SRL + * Copyright (C) 2000-2020 jPOS Software SRL * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -16,11 +16,12 @@ * along with this program. If not, see . */ - -// Groovy initialization to spice up Context object +package org.jpos.groovy import org.jpos.transaction.* +// Groovy initialization to spice up Context object + def ctxmc= Context.metaClass @@ -42,4 +43,3 @@ ctxmc.putAt= { Object key, Object val -> } -return ctxmc From 39430f4156c13316700cc628be174c265c9e13a2 Mon Sep 17 00:00:00 2001 From: Barzilai Spinak Date: Fri, 21 Feb 2020 03:50:29 -0300 Subject: [PATCH 2/2] groovy: provide metamethod equivalents of Caller#info --- .../org/jpos/groovy/JPOSGroovyDefaults.groovy | 65 ++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/modules/groovy/src/main/resources/org/jpos/groovy/JPOSGroovyDefaults.groovy b/modules/groovy/src/main/resources/org/jpos/groovy/JPOSGroovyDefaults.groovy index a35aa05896..987368f461 100644 --- a/modules/groovy/src/main/resources/org/jpos/groovy/JPOSGroovyDefaults.groovy +++ b/modules/groovy/src/main/resources/org/jpos/groovy/JPOSGroovyDefaults.groovy @@ -18,11 +18,15 @@ package org.jpos.groovy -import org.jpos.transaction.* +import groovy.transform.CompileStatic +import groovy.transform.Field -// Groovy initialization to spice up Context object +import org.jpos.transaction.Context +import org.jpos.util.Caller +// ### Groovy initialization to spice up Context object + def ctxmc= Context.metaClass ctxmc.getProperty= { String name -> @@ -43,3 +47,60 @@ ctxmc.putAt= { Object key, Object val -> } +// ### Groovy alternatives for org.jpos.util.Caller#info() methods + + +// We want to ignore stack elements belonging to these classes +// This list and the shouldExclude method are inspired by org.codehaus.groovy.runtime.StackTraceUtils.isApplicationClass() +@Field static final List filter= [ + "groovy.lang.", "org.codehaus.groovy.", "org.apache.groovy.", "gjdk.groovy.", + "sun.reflect.", "java.lang.reflect.", + "org.jpos.groovy.JPOSGroovyDefaults." ] +@CompileStatic +private static boolean shouldExclude(String className) +{ + for (String f in filter) + { + if (className.startsWith(f)) + return true + } + return false +} + + +/* Based on org.jpos.util.Caller.info(int) */ +@CompileStatic +private static String info(int pos) +{ + StackTraceElement [] stels= Thread.currentThread().getStackTrace() + StackTraceElement st= stels[stels.length - 1] // avoid jumping out of the stack + + // Find the useful stack position, when ignoring internal groovy and java reflection stuff + int wantedPos= pos + 3 + pos= 0 + for (StackTraceElement el : stels) + { + if (!shouldExclude(el.getClassName())) + { + if (pos++ == wantedPos) + { + st= el // found it! + break + } + } + } + + String clazz = st.getClassName() + StringBuilder sb = new StringBuilder(Caller.shortClassName(clazz)) + return sb.append(".").append(st.getMethodName()) + .append(':') + .append(Integer.toString(st.getLineNumber())) + .toString() +} + + +def callermc= Caller.metaClass + +callermc.static.info = { int p -> info(p) } +callermc.static.info = { -> info(0) } +