INT-1727 polished implementation of DefaultScriptVariableSource as well as isolated the ScriptVariableSource implementation used by the Control Bus into a private inner class
This commit is contained in:
@@ -61,17 +61,6 @@ public class DefaultScriptVariableSource implements BeanFactoryAware, ScriptVari
|
||||
scriptVariables.put(variableName, variableValue);
|
||||
}
|
||||
}
|
||||
// custom logic
|
||||
this.doResolveScriptVariables(scriptVariables, message);
|
||||
return scriptVariables;
|
||||
}
|
||||
/**
|
||||
* Will allow further customization to the map of script variables
|
||||
* that will be accessible to script executing engine
|
||||
*
|
||||
* @param variables
|
||||
*/
|
||||
protected void doResolveScriptVariables(Map<String, Object> variables, Message<?> message){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,15 +13,18 @@
|
||||
|
||||
package org.springframework.integration.groovy.config;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.context.Lifecycle;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.config.AbstractSimpleMessageHandlerFactoryBean;
|
||||
import org.springframework.integration.core.MessageHandler;
|
||||
import org.springframework.integration.groovy.DefaultScriptVariableSource;
|
||||
import org.springframework.integration.groovy.GroovyCommandMessageProcessor;
|
||||
import org.springframework.integration.groovy.ScriptVariableSource;
|
||||
import org.springframework.integration.handler.ServiceActivatingHandler;
|
||||
import org.springframework.jmx.export.annotation.ManagedResource;
|
||||
import org.springframework.util.CustomizableThreadCreator;
|
||||
@@ -43,8 +46,7 @@ public class GroovyControlBusFactoryBean extends AbstractSimpleMessageHandlerFac
|
||||
|
||||
@Override
|
||||
protected MessageHandler createHandler() {
|
||||
DefaultScriptVariableSource scriptVariableSource = new ManagedBeansScriptVariableSource();
|
||||
scriptVariableSource.setBeanFactory(this.getBeanFactory());
|
||||
ManagedBeansScriptVariableSource scriptVariableSource = new ManagedBeansScriptVariableSource(this.getBeanFactory());
|
||||
GroovyCommandMessageProcessor processor = new GroovyCommandMessageProcessor(scriptVariableSource);
|
||||
return this.configureHandler(new ServiceActivatingHandler(processor));
|
||||
}
|
||||
@@ -56,9 +58,16 @@ public class GroovyControlBusFactoryBean extends AbstractSimpleMessageHandlerFac
|
||||
return handler;
|
||||
}
|
||||
|
||||
private class ManagedBeansScriptVariableSource extends DefaultScriptVariableSource {
|
||||
@Override
|
||||
protected void doResolveScriptVariables(Map<String, Object> variables, Message<?> message){
|
||||
private class ManagedBeansScriptVariableSource implements ScriptVariableSource {
|
||||
private final ListableBeanFactory beanFactory;
|
||||
|
||||
public ManagedBeansScriptVariableSource(BeanFactory beanFactory){
|
||||
this.beanFactory = (beanFactory instanceof ListableBeanFactory) ? (ListableBeanFactory) beanFactory : null;
|
||||
}
|
||||
|
||||
public Map<String, Object> resolveScriptVariables(Message<?> message) {
|
||||
Map<String, Object> variables = new HashMap<String, Object>();
|
||||
variables.put("headers", message.getHeaders());
|
||||
if (this.beanFactory != null){
|
||||
for (String name : this.beanFactory.getBeanDefinitionNames()) {
|
||||
Object bean = this.beanFactory.getBean(name);
|
||||
@@ -69,6 +78,7 @@ public class GroovyControlBusFactoryBean extends AbstractSimpleMessageHandlerFac
|
||||
}
|
||||
}
|
||||
}
|
||||
return variables;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertFalse;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@@ -71,9 +72,13 @@ public class GroovyScriptExecutingMessageProcessorTests {
|
||||
TestResource resource = new TestResource(script, "simpleTest");
|
||||
ScriptSource scriptSource = new ResourceScriptSource(resource);
|
||||
Object result = null;
|
||||
class CustomScriptVariableSource extends DefaultScriptVariableSource{
|
||||
protected void doResolveScriptVariables(Map<String, Object> variables, Message<?> message){
|
||||
class CustomScriptVariableSource implements ScriptVariableSource {
|
||||
public Map<String, Object> resolveScriptVariables(Message<?> message) {
|
||||
Map<String, Object> variables = new HashMap<String, Object>();
|
||||
variables.put("date", System.nanoTime());
|
||||
variables.put("payload", message.getPayload());
|
||||
variables.put("headers", message.getHeaders());
|
||||
return variables;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 5; i++) {
|
||||
|
||||
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -34,6 +35,7 @@ import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.groovy.DefaultScriptVariableSource;
|
||||
import org.springframework.integration.groovy.ScriptVariableSource;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -127,11 +129,15 @@ public class GroovyServiceActivatorTests {
|
||||
new ClassPathXmlApplicationContext("GroovyServiceActivatorTests-fail-withsource-context.xml", this.getClass());
|
||||
}
|
||||
|
||||
public static class SampleScriptVariSource extends DefaultScriptVariableSource{
|
||||
protected void doResolveScriptVariables(Map<String, Object> variables, Message<?> message){
|
||||
public static class SampleScriptVariSource implements ScriptVariableSource{
|
||||
public Map<String, Object> resolveScriptVariables(Message<?> message) {
|
||||
Map<String, Object> variables = new HashMap<String, Object>();
|
||||
variables.put("foo", "foo");
|
||||
variables.put("bar", "bar");
|
||||
variables.put("date", new Date());
|
||||
variables.put("payload", message.getPayload());
|
||||
variables.put("headers", message.getHeaders());
|
||||
return variables;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user