From c3b99e4bcf9486fddeab453e3c6872e9b2d884f2 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 31 Jan 2011 09:15:16 -0500 Subject: [PATCH] INT-1727 polished implementation of DefaultScriptVariableSource as well as isolated the ScriptVariableSource implementation used by the Control Bus into a private inner class --- .../groovy/DefaultScriptVariableSource.java | 11 ---------- .../config/GroovyControlBusFactoryBean.java | 22 ++++++++++++++----- ...yScriptExecutingMessageProcessorTests.java | 9 ++++++-- .../config/GroovyServiceActivatorTests.java | 10 +++++++-- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/DefaultScriptVariableSource.java b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/DefaultScriptVariableSource.java index 1b1a883348..ea2fa78a01 100644 --- a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/DefaultScriptVariableSource.java +++ b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/DefaultScriptVariableSource.java @@ -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 variables, Message message){ - - } } diff --git a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/config/GroovyControlBusFactoryBean.java b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/config/GroovyControlBusFactoryBean.java index 5df85ae54c..8d033f21bc 100644 --- a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/config/GroovyControlBusFactoryBean.java +++ b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/config/GroovyControlBusFactoryBean.java @@ -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 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 resolveScriptVariables(Message message) { + Map variables = new HashMap(); + 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; } } } diff --git a/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessorTests.java b/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessorTests.java index 39b1abe05a..e3bfbd8191 100644 --- a/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessorTests.java +++ b/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessorTests.java @@ -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 variables, Message message){ + class CustomScriptVariableSource implements ScriptVariableSource { + public Map resolveScriptVariables(Message message) { + Map variables = new HashMap(); variables.put("date", System.nanoTime()); + variables.put("payload", message.getPayload()); + variables.put("headers", message.getHeaders()); + return variables; } } for (int i = 0; i < 5; i++) { diff --git a/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyServiceActivatorTests.java b/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyServiceActivatorTests.java index b82e432f84..2affb0bcb2 100644 --- a/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyServiceActivatorTests.java +++ b/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyServiceActivatorTests.java @@ -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 variables, Message message){ + public static class SampleScriptVariSource implements ScriptVariableSource{ + public Map resolveScriptVariables(Message message) { + Map variables = new HashMap(); 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; } } }