INT-1784 added support for GroovyObjectCustomizers
This commit is contained in:
@@ -19,6 +19,7 @@ import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageHandlingException;
|
||||
import org.springframework.integration.handler.MessageProcessor;
|
||||
import org.springframework.scripting.ScriptSource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Base {@link MessageProcessor} for scripting implementations to extend.
|
||||
@@ -36,8 +37,8 @@ public abstract class AbstractScriptExecutingMessageProcessor<T> implements Mess
|
||||
}
|
||||
|
||||
protected AbstractScriptExecutingMessageProcessor(ScriptVariableGenerator scriptVariableGenerator) {
|
||||
this.scriptVariableGenerator = (scriptVariableGenerator != null) ? scriptVariableGenerator
|
||||
: new DefaultScriptVariableGenerator();
|
||||
Assert.notNull(scriptVariableGenerator, "scriptVariableGenerator must not be null");
|
||||
this.scriptVariableGenerator = scriptVariableGenerator;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import org.springframework.integration.Message;
|
||||
import org.springframework.integration.scripting.AbstractScriptExecutingMessageProcessor;
|
||||
import org.springframework.integration.scripting.ScriptVariableGenerator;
|
||||
import org.springframework.scripting.ScriptSource;
|
||||
import org.springframework.scripting.groovy.GroovyObjectCustomizer;
|
||||
import org.springframework.scripting.groovy.GroovyScriptFactory;
|
||||
import org.springframework.scripting.support.StaticScriptSource;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -34,6 +35,9 @@ import org.springframework.util.CollectionUtils;
|
||||
*/
|
||||
public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessageProcessor<Object> {
|
||||
|
||||
private volatile GroovyObjectCustomizer customizer;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a GroovyCommandMessageProcessor that will use the DefaultScriptVariableGenerator.
|
||||
*/
|
||||
@@ -49,6 +53,13 @@ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessag
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets a {@link GroovyObjectCustomizer} for this processor.
|
||||
*/
|
||||
public void setCustomizer(GroovyObjectCustomizer customizer) {
|
||||
this.customizer = customizer;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ScriptSource getScriptSource(Message<?> message) {
|
||||
Object payload = message.getPayload();
|
||||
@@ -56,14 +67,17 @@ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessag
|
||||
String className = generateScriptName(message);
|
||||
return new StaticScriptSource((String) payload, className);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) throws Exception {
|
||||
Assert.notNull(scriptSource, "scriptSource must not be null");
|
||||
VariableBindingGroovyObjectCustomizer customizer = new VariableBindingGroovyObjectCustomizer();
|
||||
GroovyScriptFactory factory = new GroovyScriptFactory(this.getClass().getSimpleName(), customizer);
|
||||
VariableBindingGroovyObjectCustomizerDecorator customizerDecorator = new VariableBindingGroovyObjectCustomizerDecorator();
|
||||
if (this.customizer != null) {
|
||||
customizerDecorator.setCustomizer(this.customizer);
|
||||
}
|
||||
GroovyScriptFactory factory = new GroovyScriptFactory(this.getClass().getSimpleName(), customizerDecorator);
|
||||
if (!CollectionUtils.isEmpty(variables)) {
|
||||
customizer.setVariables(variables);
|
||||
customizerDecorator.setVariables(variables);
|
||||
}
|
||||
Object result = factory.getScriptedObject(scriptSource, null);
|
||||
return (result instanceof GString) ? result.toString() : result;
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.springframework.integration.Message;
|
||||
import org.springframework.integration.scripting.AbstractScriptExecutingMessageProcessor;
|
||||
import org.springframework.integration.scripting.ScriptVariableGenerator;
|
||||
import org.springframework.scripting.ScriptSource;
|
||||
import org.springframework.scripting.groovy.GroovyObjectCustomizer;
|
||||
import org.springframework.scripting.groovy.GroovyScriptFactory;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@@ -38,25 +39,40 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti
|
||||
|
||||
private final GroovyScriptFactory scriptFactory;
|
||||
|
||||
private final VariableBindingGroovyObjectCustomizer customizer = new VariableBindingGroovyObjectCustomizer();
|
||||
private final VariableBindingGroovyObjectCustomizerDecorator
|
||||
customizerDecorator = new VariableBindingGroovyObjectCustomizerDecorator();
|
||||
|
||||
private volatile ScriptSource scriptSource;
|
||||
|
||||
|
||||
/**
|
||||
* Create a processor for the given {@link ScriptSource}.
|
||||
* Create a processor for the given {@link ScriptSource} that will use a
|
||||
* DefaultScriptVariableGenerator.
|
||||
*/
|
||||
public GroovyScriptExecutingMessageProcessor(ScriptSource scriptSource) {
|
||||
this(scriptSource, null);
|
||||
super();
|
||||
this.scriptSource = scriptSource;
|
||||
this.scriptFactory = new GroovyScriptFactory(this.getClass().getSimpleName(), this.customizerDecorator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a processor for the given {@link ScriptSource} that will use the provided
|
||||
* ScriptVariableGenerator.
|
||||
*/
|
||||
public GroovyScriptExecutingMessageProcessor(ScriptSource scriptSource, ScriptVariableGenerator scriptVariableGenerator) {
|
||||
super(scriptVariableGenerator);
|
||||
this.scriptSource = scriptSource;
|
||||
this.scriptFactory = new GroovyScriptFactory(this.getClass().getSimpleName(), this.customizer);
|
||||
this.scriptFactory = new GroovyScriptFactory(this.getClass().getSimpleName(), this.customizerDecorator);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets a {@link GroovyObjectCustomizer} for this processor.
|
||||
*/
|
||||
public void setCustomizer(GroovyObjectCustomizer customizer) {
|
||||
this.customizerDecorator.setCustomizer(customizer);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ScriptSource getScriptSource(Message<?> message) {
|
||||
return this.scriptSource;
|
||||
@@ -67,7 +83,7 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti
|
||||
Assert.notNull(scriptSource, "scriptSource must not be null");
|
||||
synchronized (this) {
|
||||
if (!CollectionUtils.isEmpty(variables)) {
|
||||
this.customizer.setVariables(variables);
|
||||
this.customizerDecorator.setVariables(variables);
|
||||
}
|
||||
Object result = this.scriptFactory.getScriptedObject(scriptSource, null);
|
||||
return (result instanceof GString) ? result.toString() : result;
|
||||
|
||||
@@ -28,15 +28,21 @@ import org.springframework.util.Assert;
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
class VariableBindingGroovyObjectCustomizer implements GroovyObjectCustomizer {
|
||||
class VariableBindingGroovyObjectCustomizerDecorator implements GroovyObjectCustomizer {
|
||||
|
||||
private volatile Map<String, ?> variables;
|
||||
|
||||
private volatile GroovyObjectCustomizer customizer;
|
||||
|
||||
|
||||
public void setVariables(Map<String, ?> variables) {
|
||||
this.variables = variables;
|
||||
}
|
||||
|
||||
public void setCustomizer(GroovyObjectCustomizer customizer) {
|
||||
this.customizer = customizer;
|
||||
}
|
||||
|
||||
public void customize(GroovyObject goo) {
|
||||
Assert.state(goo instanceof Script, "Expected a Script");
|
||||
if (this.variables != null) {
|
||||
@@ -45,6 +51,9 @@ class VariableBindingGroovyObjectCustomizer implements GroovyObjectCustomizer {
|
||||
binding.setVariable(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
if (this.customizer != null) {
|
||||
this.customizer.customize(goo);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user