INT-1784 added support for GroovyObjectCustomizers

This commit is contained in:
Mark Fisher
2011-02-04 14:45:25 -05:00
parent f5dc6019d5
commit 3210d4a9a2
4 changed files with 52 additions and 12 deletions

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);
}
}
}