Skip to content

Commit

Permalink
execute groovy shell scripts with proper locks
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoayyed committed Aug 30, 2023
1 parent 566ea6b commit 7980a29
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.apereo.cas.util.scripting;

import groovy.lang.Binding;
import groovy.lang.Script;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -14,8 +16,11 @@
* @since 6.0.0
*/
@Getter
@Slf4j
public class GroovyShellScript implements ExecutableCompiledGroovyScript {
private final transient Script groovyScript;
private static final ThreadLocal<Map<String, Object>> BINDING_THREAD_LOCAL = new ThreadLocal<>();

private final Script groovyScript;
private final String script;

public GroovyShellScript(final String script) {
Expand All @@ -34,11 +39,28 @@ public void execute(final Object[] args) {
}

@Override
public <T> T execute(final Object[] args, final Class<T> clazz, final boolean failOnError) {
if (this.groovyScript != null) {
return ScriptingUtils.executeGroovyShellScript(this.groovyScript, clazz);
public synchronized <T> T execute(final Object[] args, final Class<T> clazz, final boolean failOnError) {
try {
LOGGER.trace("Beginning to execute script [{}]", this);
val binding = BINDING_THREAD_LOCAL.get();
if (groovyScript != null) {
if (binding != null && !binding.isEmpty()) {
LOGGER.trace("Setting binding [{}]", binding);
groovyScript.setBinding(new Binding(binding));
}
LOGGER.trace("Current binding [{}]", groovyScript.getBinding());
val result = ScriptingUtils.executeGroovyShellScript(groovyScript, clazz);
LOGGER.debug("Groovy script [{}] returns result [{}]", this, result);
return result;
}
return null;
} finally {
BINDING_THREAD_LOCAL.remove();
if (groovyScript != null) {
groovyScript.setBinding(new Binding(Map.of()));
}
LOGGER.trace("Completed script execution [{}]", this);
}
return null;
}

@Override
Expand All @@ -47,17 +69,14 @@ public <T> T execute(final String methodName, final Class<T> clazz, final Object
}

@Override
public void setBinding(final Map<String, Object> variables) {
if (variables != null && !variables.isEmpty()) {
val binding = this.groovyScript.getBinding();
variables.forEach(binding::setVariable);
}
public void setBinding(final Map<String, Object> args) {
BINDING_THREAD_LOCAL.set(new HashMap<>(args));
}

@Override
public String toString() {
return new ToStringBuilder(this)
.append("script", script)
.toString();
.append("script", script)
.toString();
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Platform metadata for releases, POM generation, etc.
#################################################
group=org.apereo.cas
version=6.6.11-SNAPSHOT
version=6.6.11
projectUrl=https://www.apereo.org/cas
projectInceptionYear=2004
projectScmUrl=scm:[email protected]:apereo/cas.git
Expand Down

0 comments on commit 7980a29

Please sign in to comment.