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 ea2fa78a01..ee4fe3e7d5 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 @@ -29,7 +29,7 @@ import org.springframework.util.CollectionUtils; * @author Oleg Zhurakousky * @since 2.0.2 */ -public class DefaultScriptVariableSource implements BeanFactoryAware, ScriptVariableSource { +class DefaultScriptVariableSource implements BeanFactoryAware, ScriptVariablesGenerator { protected volatile ListableBeanFactory beanFactory; @@ -47,7 +47,7 @@ public class DefaultScriptVariableSource implements BeanFactoryAware, ScriptVari this.beanFactory = (beanFactory instanceof ListableBeanFactory) ? (ListableBeanFactory) beanFactory : null; } - public Map resolveScriptVariables(Message message){ + public Map generateScriptVariables(Message message){ Map scriptVariables = new HashMap(); // Ad Message attributes if (message != null) { diff --git a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyCommandMessageProcessor.java b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyCommandMessageProcessor.java index 0be8700ce2..3e0ff427ee 100644 --- a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyCommandMessageProcessor.java +++ b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyCommandMessageProcessor.java @@ -30,9 +30,9 @@ import org.springframework.util.Assert; */ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessageProcessor { - private final ScriptVariableSource scriptVariableSource; + private final ScriptVariablesGenerator scriptVariableSource; - public GroovyCommandMessageProcessor(ScriptVariableSource scriptVariableSource) { + public GroovyCommandMessageProcessor(ScriptVariablesGenerator scriptVariableSource) { this.scriptVariableSource = scriptVariableSource; } @@ -50,7 +50,7 @@ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessag MapResolvingBindingCustomizer bindingCustomizer = new MapResolvingBindingCustomizer(); GroovyScriptFactory factory = new GroovyScriptFactory(this.getClass().getSimpleName(), bindingCustomizer); if (this.scriptVariableSource != null){ - bindingCustomizer.setResolvedScriptVariables(this.scriptVariableSource.resolveScriptVariables(message)); + bindingCustomizer.setResolvedScriptVariables(this.scriptVariableSource.generateScriptVariables(message)); } Object result = factory.getScriptedObject(scriptSource, null); return (result instanceof GString) ? result.toString() : result; diff --git a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessor.java b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessor.java index 1339bd3875..9b18bb1512 100644 --- a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessor.java +++ b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessor.java @@ -38,7 +38,7 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti private volatile ScriptSource scriptSource; - protected final ScriptVariableSource scriptVariableSource; + protected final ScriptVariablesGenerator scriptVariableSource; /** * Create a processor for the given {@link ScriptSource}. @@ -47,7 +47,7 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti this(scriptSource, new DefaultScriptVariableSource()); } - public GroovyScriptExecutingMessageProcessor(ScriptSource scriptSource, ScriptVariableSource scriptVariableSource) { + public GroovyScriptExecutingMessageProcessor(ScriptSource scriptSource, ScriptVariablesGenerator scriptVariableSource) { this.scriptSource = scriptSource; this.scriptVariableSource = scriptVariableSource; this.scriptFactory = new GroovyScriptFactory(this.getClass().getSimpleName(), this.customizer); @@ -63,7 +63,7 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti Assert.notNull(scriptSource, "scriptSource must not be null"); synchronized (this) { if (this.scriptVariableSource != null){ - this.customizer.setResolvedScriptVariables(this.scriptVariableSource.resolveScriptVariables(message)); + this.customizer.setResolvedScriptVariables(this.scriptVariableSource.generateScriptVariables(message)); } Object result = this.scriptFactory.getScriptedObject(scriptSource, null); return (result instanceof GString) ? result.toString() : result; diff --git a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/ScriptVariableSource.java b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/ScriptVariablesGenerator.java similarity index 87% rename from spring-integration-groovy/src/main/java/org/springframework/integration/groovy/ScriptVariableSource.java rename to spring-integration-groovy/src/main/java/org/springframework/integration/groovy/ScriptVariablesGenerator.java index 3864c68bce..b7f3595775 100644 --- a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/ScriptVariableSource.java +++ b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/ScriptVariablesGenerator.java @@ -24,7 +24,7 @@ import org.springframework.integration.Message; * @author Oleg Zhurakousky * */ -public interface ScriptVariableSource { +public interface ScriptVariablesGenerator { - Map resolveScriptVariables(Message message); + Map generateScriptVariables(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 8d033f21bc..d068778a66 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 @@ -24,7 +24,7 @@ import org.springframework.integration.Message; import org.springframework.integration.config.AbstractSimpleMessageHandlerFactoryBean; import org.springframework.integration.core.MessageHandler; import org.springframework.integration.groovy.GroovyCommandMessageProcessor; -import org.springframework.integration.groovy.ScriptVariableSource; +import org.springframework.integration.groovy.ScriptVariablesGenerator; import org.springframework.integration.handler.ServiceActivatingHandler; import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.util.CustomizableThreadCreator; @@ -58,14 +58,14 @@ public class GroovyControlBusFactoryBean extends AbstractSimpleMessageHandlerFac return handler; } - private class ManagedBeansScriptVariableSource implements ScriptVariableSource { + private class ManagedBeansScriptVariableSource implements ScriptVariablesGenerator { private final ListableBeanFactory beanFactory; public ManagedBeansScriptVariableSource(BeanFactory beanFactory){ this.beanFactory = (beanFactory instanceof ListableBeanFactory) ? (ListableBeanFactory) beanFactory : null; } - public Map resolveScriptVariables(Message message) { + public Map generateScriptVariables(Message message) { Map variables = new HashMap(); variables.put("headers", message.getHeaders()); if (this.beanFactory != null){ diff --git a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/config/GroovyScriptParser.java b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/config/GroovyScriptParser.java index 9dd319c9bc..cdd2144644 100644 --- a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/config/GroovyScriptParser.java +++ b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/config/GroovyScriptParser.java @@ -59,15 +59,14 @@ public class GroovyScriptParser extends AbstractSingleBeanDefinitionParser { } List variableElements = DomUtils.getChildElementsByTagName(element, "variable"); + String scriptVariableSourceName = element.getAttribute("script-variable-source"); - if (StringUtils.hasText(scriptText) && variableElements.size() > 0){ - parserContext.getReaderContext().error("Variable bindings are not allowed when using inline groovy script. " + + if (StringUtils.hasText(scriptText) && (variableElements.size() > 0 || StringUtils.hasText(scriptVariableSourceName))){ + parserContext.getReaderContext().error("Variable bindings or custom ScriptVariabelSource are not allowed when using inline groovy script. " + "Specify location of the script via 'location' attribute instead", element); return; } - String scriptVariableSourceName = element.getAttribute("script-variable-source"); - if (StringUtils.hasText(scriptVariableSourceName) && variableElements.size() > 0){ parserContext.getReaderContext().error("'script-variable-source' and 'variable' sub-element are mutualy exclusive. Must use one or the other.", element); return; 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 e3bfbd8191..77a3555195 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 @@ -72,8 +72,8 @@ public class GroovyScriptExecutingMessageProcessorTests { TestResource resource = new TestResource(script, "simpleTest"); ScriptSource scriptSource = new ResourceScriptSource(resource); Object result = null; - class CustomScriptVariableSource implements ScriptVariableSource { - public Map resolveScriptVariables(Message message) { + class CustomScriptVariableSource implements ScriptVariablesGenerator { + public Map generateScriptVariables(Message message) { Map variables = new HashMap(); variables.put("date", System.nanoTime()); variables.put("payload", message.getPayload()); @@ -82,7 +82,7 @@ public class GroovyScriptExecutingMessageProcessorTests { } } for (int i = 0; i < 5; i++) { - ScriptVariableSource scriptVariableSource = new CustomScriptVariableSource(); + ScriptVariablesGenerator scriptVariableSource = new CustomScriptVariableSource(); MessageProcessor processor = new GroovyScriptExecutingMessageProcessor(scriptSource, scriptVariableSource); Object newResult = processor.processMessage(message); assertFalse(newResult.equals(result)); // make sure that we get different nanotime verifying that resolveScriptVariables() is invoked diff --git a/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/GroovyScriptPayloadMessageProcessorTests.java b/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/GroovyScriptPayloadMessageProcessorTests.java index 9c91bc088b..07c334a4ef 100644 --- a/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/GroovyScriptPayloadMessageProcessorTests.java +++ b/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/GroovyScriptPayloadMessageProcessorTests.java @@ -64,7 +64,7 @@ public class GroovyScriptPayloadMessageProcessorTests { public void testSimpleExecutionWithContext() throws Exception { Message message = MessageBuilder.withPayload("\"spam is $spam foo is $headers.foo\"") .setHeader("foo", "bar").build(); - ScriptVariableSource scriptVariableSource = + ScriptVariablesGenerator scriptVariableSource = new DefaultScriptVariableSource(Collections.singletonMap("spam",(Object)"bucket")); MessageProcessor processor = new GroovyCommandMessageProcessor(scriptVariableSource); Object result = processor.processMessage(message); 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 2affb0bcb2..7c42a2e6a9 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 @@ -34,8 +34,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; 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.groovy.ScriptVariablesGenerator; import org.springframework.integration.support.MessageBuilder; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -129,8 +128,8 @@ public class GroovyServiceActivatorTests { new ClassPathXmlApplicationContext("GroovyServiceActivatorTests-fail-withsource-context.xml", this.getClass()); } - public static class SampleScriptVariSource implements ScriptVariableSource{ - public Map resolveScriptVariables(Message message) { + public static class SampleScriptVariSource implements ScriptVariablesGenerator{ + public Map generateScriptVariables(Message message) { Map variables = new HashMap(); variables.put("foo", "foo"); variables.put("bar", "bar");