INT-1576,INT-1579,INT-2025: added spring-integration-scripting and fixed HeaderEnricher issue

This commit is contained in:
David Turanski
2011-08-03 15:15:00 +05:30
parent f94411e11d
commit 207f043587
64 changed files with 1735 additions and 239 deletions

View File

@@ -360,6 +360,15 @@ project('spring-integration-rmi') {
compileJava.options.compilerArgs = ["${xLintArg},-deprecation"]
}
project('spring-integration-scripting') {
description = 'Spring Integration Scripting Support'
dependencies {
compile project(":spring-integration-core")
testCompile project(":spring-integration-test")
testCompile("org.jruby:jruby:1.6.3")
testCompile("org.codehaus.groovy:groovy-all:1.7.5")
}
}
project('spring-integration-security') {
description = 'Spring Integration Security Support'

View File

@@ -33,6 +33,7 @@ include 'spring-integration-mail'
include 'spring-integration-mongodb'
include 'spring-integration-redis'
include 'spring-integration-rmi'
include 'spring-integration-scripting'
include 'spring-integration-security'
include 'spring-integration-sftp'
include 'spring-integration-stream'

View File

@@ -40,7 +40,7 @@ public abstract class AbstractScriptExecutingMessageProcessor<T> implements Mess
Assert.notNull(scriptVariableGenerator, "scriptVariableGenerator must not be null");
this.scriptVariableGenerator = scriptVariableGenerator;
}
/**
* Executes the script and returns the result.

View File

@@ -96,7 +96,14 @@ public class HeaderEnricher implements Transformer {
if (shouldOverwrite == null) {
shouldOverwrite = this.defaultOverwrite;
}
Object value = valueProcessor.processMessage(message);
/**
* Only evaluate value if necessary
*/
Object value = null;
if (headerMap.get(key) == null || shouldOverwrite || !this.shouldSkipNulls){
value = valueProcessor.processMessage(message);
}
if ((value != null && shouldOverwrite) || headerMap.get(key) == null || (value == null && !this.shouldSkipNulls)) {
headerMap.put(key, value);
}

View File

@@ -1,3 +1,5 @@
http\://www.springframework.org/schema/integration/spring-integration-1.0.xsd=org/springframework/integration/config/xml/spring-integration-1.0.xsd
http\://www.springframework.org/schema/integration/spring-integration-2.0.xsd=org/springframework/integration/config/xml/spring-integration-2.0.xsd
http\://www.springframework.org/schema/integration/spring-integration.xsd=org/springframework/integration/config/xml/spring-integration-2.0.xsd
http\://www.springframework.org/schema/integration/spring-integration.xsd=org/springframework/integration/config/xml/spring-integration-2.0.xsd
http\://www.springframework.org/schema/integration/scripting/spring-integration-scripting-core.xsd=org/springframework/integration/scripting/config/xml/spring-integration-scripting-core-2.1.xsd
http\://www.springframework.org/schema/integration/scripting/spring-integration-scripting-core-2.1.xsd=org/springframework/integration/scripting/config/xml/spring-integration-scripting-core-2.1.xsd

View File

@@ -18,6 +18,10 @@
<header-enricher input-channel="beanResolvingInput" output-channel="output">
<header name="num" expression="headers.num ^ @testBean.value" overwrite="true"/>
</header-enricher>
<header-enricher input-channel="expressionNotExecutedInput" output-channel="output">
<header name="num" expression="headers.num / 0" />
</header-enricher>
<beans:bean id="testBean" class="org.springframework.integration.transformer.SpelHeaderEnricherIntegrationTests$TestBean"/>

View File

@@ -42,6 +42,9 @@ public class SpelHeaderEnricherIntegrationTests {
@Autowired
private MessageChannel beanResolvingInput;
@Autowired
private MessageChannel expressionNotExecutedInput;
@Autowired @Qualifier("output")
private PollableChannel output;
@@ -62,6 +65,14 @@ public class SpelHeaderEnricherIntegrationTests {
Message<?> result = output.receive(0);
assertEquals(243, result.getHeaders().get("num"));
}
@Test
public void suppressBeanExecution(){
Message<?> message = MessageBuilder.withPayload("test").setHeader("num", 3).build();
this.expressionNotExecutedInput.send(message);
Message<?> result = output.receive(0);
assertEquals(3, result.getHeaders().get("num"));
}
static class TestBean {

View File

@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<pluginVersion><![CDATA[2.5.0.201008251000-M3]]></pluginVersion>
<configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes>
<enableImports><![CDATA[false]]></enableImports>
<configs>
</configs>
<configSets>
</configSets>
</beansProjectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<pluginVersion><![CDATA[2.7.1.201107082359-RELEASE]]></pluginVersion>
<configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes>
<enableImports><![CDATA[false]]></enableImports>
<configs>
</configs>
<configSets>
</configSets>
</beansProjectDescription>

View File

@@ -87,5 +87,4 @@ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessag
// Don't use the same script (class) name for all invocations by default
return getClass().getSimpleName() + message.getHeaders().getId().toString().replaceAll("-", "");
}
}

View File

@@ -16,116 +16,38 @@
package org.springframework.integration.groovy.config;
import java.util.List;
import org.w3c.dom.Element;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.beans.factory.xml.XmlReaderContext;
import org.springframework.integration.config.xml.AbstractScriptParser;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.scripting.support.StaticScriptSource;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
/**
* @author Mark Fisher
* @author Oleg Zhurakousky
* @since 2.0
*/
public class GroovyScriptParser extends AbstractSingleBeanDefinitionParser {
private static final String LOCATION_ATTRIBUTE = "location";
private static final String REFRESH_CHECK_DELAY_ATTRIBUTE = "refresh-check-delay";
public class GroovyScriptParser extends AbstractScriptParser {
@Override
protected String getBeanClassName(Element element) {
return "org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor";
}
protected boolean shouldGenerateIdAsFallback() {
return true;
}
/* (non-Javadoc)
* @see org.springframework.integration.config.xml.AbstractScriptParser#getScriptSourceClassName()
*/
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String scriptLocation = element.getAttribute(LOCATION_ATTRIBUTE);
String scriptText = DomUtils.getTextValue(element);
if (!(StringUtils.hasText(scriptLocation) ^ StringUtils.hasText(scriptText))) {
parserContext.getReaderContext().error("Either the 'location' attribute or inline script text must be provided, but not both.", element);
return;
}
List<Element> variableElements = DomUtils.getChildElementsByTagName(element, "variable");
String scriptVariableGeneratorName = element.getAttribute("script-variable-generator");
if (StringUtils.hasText(scriptText) && (variableElements.size() > 0 || StringUtils.hasText(scriptVariableGeneratorName))) {
parserContext.getReaderContext().error("Variable bindings or custom ScriptVariableGenerator are not allowed when using an inline groovy script. " +
"Specify location of the script via 'location' attribute instead", element);
return;
}
if (StringUtils.hasText(scriptVariableGeneratorName) && variableElements.size() > 0) {
parserContext.getReaderContext().error("'script-variable-generator' and 'variable' sub-elements are mutualy exclusive.", element);
return;
}
if (StringUtils.hasText(scriptLocation)) {
builder.addConstructorArgValue(this.resolveScriptLocation(element, parserContext.getReaderContext(), scriptLocation));
}
else {
builder.addConstructorArgValue(new StaticScriptSource(scriptText, "groovy.lang.Script"));
}
if (!StringUtils.hasText(scriptVariableGeneratorName)) {
BeanDefinitionBuilder scriptVariableGeneratorBuilder =
BeanDefinitionBuilder.genericBeanDefinition("org.springframework.integration.scripting.DefaultScriptVariableGenerator");
ManagedMap<String, Object> variableMap = new ManagedMap<String, Object>();
for (Element childElement : variableElements) {
String variableName = childElement.getAttribute("name");
String variableValue = childElement.getAttribute("value");
String variableRef = childElement.getAttribute("ref");
if (!(StringUtils.hasText(variableValue) ^ StringUtils.hasText(variableRef))) {
parserContext.getReaderContext().error("Exactly one of the 'ref' attribute or 'value' attribute, " +
" is required for element " +
IntegrationNamespaceUtils.createElementDescription(element) + ".", element);
}
if (StringUtils.hasText(variableValue)) {
variableMap.put(variableName, variableValue);
}
else {
variableMap.put(variableName, new RuntimeBeanReference(variableRef));
}
}
if (!CollectionUtils.isEmpty(variableMap)) {
scriptVariableGeneratorBuilder.addConstructorArgValue(variableMap);
}
scriptVariableGeneratorName = BeanDefinitionReaderUtils.registerWithGeneratedName(
scriptVariableGeneratorBuilder.getBeanDefinition(), parserContext.getRegistry());
}
builder.addConstructorArgReference(scriptVariableGeneratorName);
protected String getScriptSourceClassName() {
return "groovy.lang.Script";
}
protected void postProcess(BeanDefinitionBuilder builder, Element element, ParserContext parserContext) {
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "customizer");
}
private Object resolveScriptLocation(Element element, XmlReaderContext readerContext, String scriptLocation) {
String refreshDelayText = element.getAttribute(REFRESH_CHECK_DELAY_ATTRIBUTE);
String beanClassName = "org.springframework.integration.groovy.config.RefreshableResourceScriptSource";
BeanDefinitionBuilder resourceScriptSourceBuilder =
BeanDefinitionBuilder.genericBeanDefinition(beanClassName);
resourceScriptSourceBuilder.addConstructorArgValue(scriptLocation);
if (StringUtils.hasText(refreshDelayText)) {
resourceScriptSourceBuilder.addConstructorArgValue(refreshDelayText);
}
else {
resourceScriptSourceBuilder.addConstructorArgValue(-1L);
}
return resourceScriptSourceBuilder.getBeanDefinition();
}
}

View File

@@ -1,74 +0,0 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.groovy.config;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.core.io.Resource;
import org.springframework.scripting.ScriptSource;
import org.springframework.scripting.support.ResourceScriptSource;
/**
* @author Dave Syer
* @author Oleg Zhurakousky
* @since 2.0
*/
public class RefreshableResourceScriptSource implements ScriptSource {
private final long refreshDelay;
private final ResourceScriptSource source;
private final AtomicLong lastModifiedChecked = new AtomicLong(System.currentTimeMillis());
private volatile String script;
public RefreshableResourceScriptSource(Resource resource, long refreshDelay) {
this.refreshDelay = refreshDelay;
this.source = new ResourceScriptSource(resource);
try {
this.script = this.source.getScriptAsString();
}
catch (IOException e) {
this.lastModifiedChecked.set(0);
}
}
public String getScriptAsString() throws IOException {
this.script = source.getScriptAsString();
return this.script;
}
public String suggestedClassName() {
return this.source.suggestedClassName();
}
public boolean isModified() {
if (this.refreshDelay < 0) {
return false;
}
long time = System.currentTimeMillis();
if (this.refreshDelay == 0 || (time - this.lastModifiedChecked.get()) > this.refreshDelay) {
this.lastModifiedChecked.set(time);
return this.source.isModified();
}
return false;
}
}

View File

@@ -1,2 +1,3 @@
http\://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.0.xsd=org/springframework/integration/groovy/config/spring-integration-groovy-2.0.xsd
http\://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd=org/springframework/integration/groovy/config/spring-integration-groovy-2.0.xsd
http\://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.1.xsd=org/springframework/integration/groovy/config/spring-integration-groovy-2.1.xsd
http\://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd=org/springframework/integration/groovy/config/spring-integration-groovy-2.1.xsd

View File

@@ -30,8 +30,8 @@ import org.junit.Rule;
import org.junit.Test;
import org.springframework.core.io.AbstractResource;
import org.springframework.integration.Message;
import org.springframework.integration.groovy.config.RefreshableResourceScriptSource;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.scripting.RefreshableResourceScriptSource;
import org.springframework.integration.scripting.ScriptVariableGenerator;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.scripting.ScriptSource;

View File

@@ -1,12 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy
http://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<channel id="output">
<queue/>

View File

@@ -3,12 +3,9 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy
http://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<filter input-channel="referencedScriptInput">
<groovy:script location="org/springframework/integration/groovy/config/GroovyFilterTests.groovy"/>

View File

@@ -3,9 +3,9 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.0.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<int:channel id="inputA"/>

View File

@@ -3,12 +3,9 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy
http://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer" xmlns="http://www.springframework.org/schema/beans">
<property name="customEditors">

View File

@@ -3,12 +3,9 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy
http://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<channel id="shortStrings">
<queue/>

View File

@@ -6,7 +6,7 @@
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.0.xsd
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<service-activator input-channel="referencedScriptInput">

View File

@@ -3,12 +3,9 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy
http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<service-activator input-channel="inlineScriptInput">
<groovy:script>

View File

@@ -3,12 +3,9 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy
http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<service-activator input-channel="withScriptVariableGenerator">
<groovy:script location="org/springframework/integration/groovy/config/GroovyServiceActivatorTests.groovy"

View File

@@ -3,12 +3,9 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy
http://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<splitter input-channel="referencedScriptInput">
<groovy:script location="org/springframework/integration/groovy/config/GroovySplitterTests.groovy"/>

View File

@@ -3,12 +3,9 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy
http://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<transformer input-channel="referencedScriptInput">
<groovy:script location="org/springframework/integration/groovy/config/GroovyTransformerTests.groovy"/>

View File

@@ -22,6 +22,7 @@ import java.io.IOException;
import org.junit.Test;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.integration.scripting.RefreshableResourceScriptSource;
/**
* @author Dave Syer

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<pluginVersion><![CDATA[2.7.1.201107082359-RELEASE]]></pluginVersion>
<configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes>
<enableImports><![CDATA[false]]></enableImports>
<configs>
</configs>
<configSets>
</configSets>
</beansProjectDescription>

View File

@@ -0,0 +1,179 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-scripting</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<name>Spring Integration Scripting Support</name>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/java</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
<testResource>
<directory>src/test/resources</directory>
<includes>
<include>**/*</include>
</includes>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
<excludes>
<exclude>**/*Abstract*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>http://download.java.net/maven/2</id>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>http://maven.springframework.org/milestone</id>
<url>http://maven.springframework.org/milestone</url>
</repository>
<repository>
<id>http://maven.springframework.org/release</id>
<url>http://maven.springframework.org/release</url>
</repository>
<repository>
<id>http://maven.springframework.org/snapshot</id>
<url>http://maven.springframework.org/snapshot</url>
</repository>
<repository>
<id>http://repository.springsource.com/maven/bundles/external</id>
<url>http://repository.springsource.com/maven/bundles/external</url>
</repository>
<repository>
<id>http://repository.springsource.com/maven/bundles/milestone</id>
<url>http://repository.springsource.com/maven/bundles/milestone</url>
</repository>
<repository>
<id>http://repository.springsource.com/maven/bundles/release</id>
<url>http://repository.springsource.com/maven/bundles/release</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymockclassextension</artifactId>
<version>2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.0.M2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-test</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>grovy-all</artifactId>
<version>1.7.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby</artifactId>
<version>1.6.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF8</project.build.sourceEncoding>
</properties>
</project>

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.integration.scripting;
import java.util.Map;
import org.springframework.scripting.ScriptSource;
/**
* @author David Turanski
* @since 2.1
*/
public interface ScriptExecutor {
/**
*
* @param scriptSource
* @return
*/
public abstract Object executeScript(ScriptSource scriptSource);
/**
*
* @param scriptSource
* @param variables -bind variable
* @return
*/
public abstract Object executeScript(ScriptSource scriptSource,Map<String,Object> variables);
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.scripting.config.jsr223;
import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHandler;
/**
* @author David Turanski
* @since 2.1
*/
public class Jsr223NamespaceHandler extends AbstractIntegrationNamespaceHandler {
public void init() {
this.registerBeanDefinitionParser("script", new Jsr223ScriptParser());
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.scripting.config.jsr223;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.AbstractScriptParser;
import org.springframework.integration.scripting.jsr223.Jsr223ScriptExecutor;
import org.springframework.util.Assert;
import org.w3c.dom.Element;
/**
* @author David Turanski
* @since 2.1
*/
public class Jsr223ScriptParser extends AbstractScriptParser {
private static final String LANGUAGE_ATTRIBUTE = "lang";
@Override
protected String getBeanClassName(Element element) {
return "org.springframework.integration.scripting.jsr223.Jsr223ScriptExecutingMessageProcessor";
}
/* (non-Javadoc)
* @see org.springframework.integration.config.xml.AbstractScriptParser#getScriptSourceClassName()
*/
@Override
protected String getScriptSourceClassName() {
return null;
}
protected void postProcess(BeanDefinitionBuilder builder, Element element, ParserContext parserContext){
String language = element.getAttribute(LANGUAGE_ATTRIBUTE);
Assert.hasLength(language, "Attribute " + LANGUAGE_ATTRIBUTE + " is required");
builder.addConstructorArgValue(new Jsr223ScriptExecutor(language));
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.integration.scripting.jsr223;
import java.util.Map;
import org.springframework.integration.Message;
import org.springframework.integration.scripting.AbstractScriptExecutingMessageProcessor;
import org.springframework.integration.scripting.DefaultScriptVariableGenerator;
import org.springframework.integration.scripting.ScriptExecutor;
import org.springframework.integration.scripting.ScriptVariableGenerator;
import org.springframework.scripting.ScriptSource;
import org.springframework.util.Assert;
/**
* @author David Turanski
* @since 2.1
*/
public class Jsr223ScriptExecutingMessageProcessor extends AbstractScriptExecutingMessageProcessor<Object> {
private final Jsr223ScriptExecutor scriptExecutor;
private volatile ScriptSource scriptSource;
/**
* Create a processor for the {@link ScriptSource} using the provided {@link ScriptExecutor} using the DefaultScriptVariableGenerator
* @param scriptSource
* @param scriptExecutor
*/
public Jsr223ScriptExecutingMessageProcessor(ScriptSource scriptSource, Jsr223ScriptExecutor scriptExecutor) {
super();
this.scriptSource = scriptSource;
this.scriptExecutor = scriptExecutor;
}
/**
* Create a processor for the {@link ScriptSource} using the provided {@link ScriptExecutor}
* @param scriptSource
* @param scriptExecutor
*/
public Jsr223ScriptExecutingMessageProcessor(ScriptSource scriptSource, ScriptVariableGenerator scriptVariableGenerator,Jsr223ScriptExecutor scriptExecutor) {
super(scriptVariableGenerator);
this.scriptSource = scriptSource;
this.scriptExecutor = scriptExecutor;
}
/**
* Create a processor for the {@link ScriptSource} using the provided {@link ScriptExecutor} using the DefaultScriptVariableGenerator
* @param scriptSource
* @param scriptExecutor
* @param variables
*/
public Jsr223ScriptExecutingMessageProcessor(ScriptSource scriptSource,Jsr223ScriptExecutor scriptExecutor,Map<String,Object> variables ) {
super(new DefaultScriptVariableGenerator(variables));
this.scriptSource = scriptSource;
this.scriptExecutor = scriptExecutor;
}
@Override
protected ScriptSource getScriptSource(Message<?> message) {
return this.scriptSource;
}
@Override
protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) throws Exception {
Assert.notNull(scriptSource, "scriptSource must not be null");
return this.scriptExecutor.executeScript(scriptSource,variables);
}
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.integration.scripting.jsr223;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import javax.script.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.integration.scripting.ScriptExecutor;
import org.springframework.scripting.ScriptSource;
import org.springframework.util.Assert;
/**
* Executes Jsr223 scripts
*
* @author David Turanski
* @since 2.1
*/
public class Jsr223ScriptExecutor implements ScriptExecutor {
private static Log log = LogFactory.getLog(Jsr223ScriptExecutor.class);
private final ScriptEngine scriptEngine;
/**
* Set the engine name (language)
* @param language
*/
public Jsr223ScriptExecutor(String language) {
ScriptEngineManager manager = new ScriptEngineManager();
if (log.isDebugEnabled()){
for (ScriptEngineFactory factory: manager.getEngineFactories()) {
log.debug(factory.getNames());
}
}
scriptEngine = manager.getEngineByName(language);
Assert.notNull(scriptEngine,"JVM cannot create a script engine for name [" + language + "]");
}
/*
* (non-Javadoc)
*
* @see
* com.dturanski.test.jruby.ScriptExecutor#executeScript(org.springframework
* .scripting.ScriptSource)
*/
public Object executeScript(ScriptSource scriptSource) {
return this.executeScript(scriptSource,null);
}
/*
* (non-Javadoc)
*
* @see
* com.dturanski.test.jruby.ScriptExecutor#bindVariable(java.lang.String,
* java.lang.Object)
*/
private void bindVariable(String variableName, Object value) {
scriptEngine.put(variableName, value);
}
/* (non-Javadoc)
* @see org.springframework.integration.jsr233.ScriptExecutor#executeScript(org.springframework.scripting.ScriptSource, java.util.Map)
*/
public Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) {
Assert.notNull(scriptSource, "scriptSource must not be null");
synchronized (this) {
Object obj = null;
try {
scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE).clear();
if (variables != null ) {
for (Entry<String, Object> entry: variables.entrySet()){
bindVariable(entry.getKey(), entry.getValue());
}
}
obj = scriptEngine.eval(scriptSource.getScriptAsString());
}
catch (ScriptException e) {
throw new RuntimeException(e);
}
catch (IOException e) {
throw new RuntimeException(e);
}
return obj;
}
}
}

View File

@@ -0,0 +1 @@
http\://www.springframework.org/schema/integration/script=org.springframework.integration.scripting.config.jsr223.Jsr223NamespaceHandler

View File

@@ -0,0 +1,2 @@
http\://www.springframework.org/schema/integration/script-2.1.xsd=org/springframework/integration/script/config/spring-integration-script-2.1.xsd
http\://www.springframework.org/schema/integration/script.xsd=org/springframework/integration/script/config/spring-integration-script-2.1.xsd

View File

@@ -0,0 +1,4 @@
# Tooling related information for the integration script namespace
http\://www.springframework.org/schema/integration/script@name=integration script Namespace
http\://www.springframework.org/schema/integration/script@prefix=int-script
http\://www.springframework.org/schema/integration/script@icon=org/springframework/integration/script/config/spring-integration-script.gif

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.springframework.org/schema/integration/script"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.springframework.org/schema/integration/script"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:include
schemaLocation="http://www.springframework.org/schema/integration/scripting/spring-integration-scripting-core-2.1.xsd" />
<xsd:element name="script">
<xsd:annotation>
<xsd:documentation>
Configures an inner bean that will generate a
JSR 223 compliant script.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:complexType name="Jsr223Script">
<xsd:complexContent>
<xsd:extension base="ScriptType">
<xsd:attribute name="lang" use="required">
<xsd:annotation>
<xsd:documentation>
The script language or JSR 223 scripting engine name
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<filter input-channel="referencedScriptInput">
<script:script lang="groovy" location="org/springframework/integration/scripting/config/jsr223/Jsr223FilterTests.groovy"/>
</filter>
<filter input-channel="inlineScriptInput">
<script:script lang="groovy"><![CDATA[
return payload == 'good'
]]></script:script>
</filter>
</beans:beans>

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.scripting.config.jsr223;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @author David Turanski
* @since 2.1
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Jsr223FilterTests {
@Autowired
private MessageChannel referencedScriptInput;
@Autowired
private MessageChannel inlineScriptInput;
@Test
public void referencedScript() {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
Message<?> message1 = MessageBuilder.withPayload("test-1")
.setReplyChannel(replyChannel)
.setHeader("type", "bad")
.build();
Message<?> message2 = MessageBuilder.withPayload("test-2")
.setReplyChannel(replyChannel)
.setHeader("type", "good")
.build();
this.referencedScriptInput.send(message1);
this.referencedScriptInput.send(message2);
assertEquals("test-2", replyChannel.receive(0).getPayload());
assertNull(replyChannel.receive(0));
}
@Test
public void inlineScript() {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
Message<?> message1 = MessageBuilder.withPayload("bad").setReplyChannel(replyChannel).build();
Message<?> message2 = MessageBuilder.withPayload("good").setReplyChannel(replyChannel).build();
this.inlineScriptInput.send(message1);
this.inlineScriptInput.send(message2);
Message<?> received = replyChannel.receive(0);
assertNotNull(received);
assertEquals("good", received.getPayload());
assertEquals(message2, received);
assertNull(replyChannel.receive(0));
}
}

View File

@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<int:channel id="inputA"/>
<int:header-enricher input-channel="inputA" output-channel="outputA">
<int:header name="TEST_HEADER">
<script:script lang="jruby" location="org/springframework/integration/scripting/config/jsr223/Jsr223HeaderEnricherTests.rb"/>
</int:header>
</int:header-enricher>
<int:channel id="outputA">
<int:queue/>
</int:channel>
<int:channel id="inputB"/>
<int:header-enricher input-channel="inputB" output-channel="outputB">
<int:header name="TEST_HEADER">
<script:script lang="javascript">
<![CDATA[
function js(){ return 'js';} js();
]]>
</script:script>
</int:header>
</int:header-enricher>
<int:channel id="outputB">
<int:queue/>
</int:channel>
</beans>

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.scripting.config.jsr223;
import static junit.framework.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Oleg Zhurakousky
* @author David Turanski
* @since 2.1
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Jsr223HeaderEnricherTests {
@Autowired
private MessageChannel inputA;
@Autowired
private QueueChannel outputA;
@Autowired
private MessageChannel inputB;
@Autowired
private QueueChannel outputB;
@Test
public void referencedScript() throws Exception{
inputA.send(new GenericMessage<String>("Hello"));
assertEquals("jruby", outputA.receive(1000).getHeaders().get("TEST_HEADER"));
}
@Test
public void inlineScript() throws Exception{
inputB.send(new GenericMessage<String>("Hello"));
assertEquals("js", outputB.receive(1000).getHeaders().get("TEST_HEADER"));
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer" xmlns="http://www.springframework.org/schema/beans">
<property name="customEditors">
<map>
<entry key="org.springframework.core.io.Resource" value="org.springframework.integration.scripting.config.jsr223.Jsr223RefreshTests$ResourceEditor"/>
</map>
</property>
</bean>
<transformer input-channel="referencedScriptInput">
<script:script lang="ruby" location="Jsr223RefreshTests.rb" refresh-check-delay="0"/>
</transformer>
</beans:beans>

View File

@@ -0,0 +1,94 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.scripting.config.jsr223;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.beans.PropertyEditorSupport;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.AbstractResource;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @author David Turanski
* @since 2.1
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Jsr223RefreshTests {
@Autowired
private MessageChannel referencedScriptInput;
@Test
public void referencedScript() throws Exception {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
this.referencedScriptInput.send(MessageBuilder.withPayload("test").setReplyChannel(replyChannel).build());
assertEquals("ruby-test-1", replyChannel.receive(0).getPayload());
this.referencedScriptInput.send(MessageBuilder.withPayload("test").setReplyChannel(replyChannel).build());
assertEquals("ruby-test-0", replyChannel.receive(0).getPayload());
assertNull(replyChannel.receive(0));
}
public static class ResourceEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
super.setValue(new CycleResource());
}
}
private static class CycleResource extends AbstractResource {
private int count = -1;
private String[] scripts = {"\"ruby-#{$payload}-0\"", "\"ruby-#{$payload}-1\""};
public String getDescription() {
return "CycleResource";
}
@Override
public String getFilename() throws IllegalStateException {
return "CycleResource";
}
@Override
public long lastModified() throws IOException {
return -1;
}
public InputStream getInputStream() throws IOException {
if (++count>scripts.length-1) {
count = 0;
}
return new ByteArrayInputStream(scripts[count].getBytes());
}
}
}

View File

@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:int-script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<channel id="shortStrings">
<queue/>
</channel>
<channel id="longStrings">
<queue/>
</channel>
<router input-channel="referencedScriptInput">
<int-script:script lang="javascript" location="org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests.js">
<variable name="maxLen" value="3"/>
</int-script:script>
</router>
<router input-channel="inlineScriptInput">
<int-script:script lang="javascript"><![CDATA[
(function(){
return payload.length > 5 ? "longStrings" : "shortStrings";
})();
]]></int-script:script>
</router>
</beans:beans>

View File

@@ -0,0 +1,97 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.scripting.config.jsr223;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @author David Turanski
* @since 2.1
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Jsr223RouterTests {
@Autowired
private MessageChannel referencedScriptInput;
@Autowired
private MessageChannel inlineScriptInput;
@Autowired
private PollableChannel longStrings;
@Autowired
private PollableChannel shortStrings;
@Test
public void referencedScript() { // long is > 3
Message<?> message1 = new GenericMessage<String>("aardvark");
Message<?> message2 = new GenericMessage<String>("bear");
Message<?> message3 = new GenericMessage<String>("cat");
Message<?> message4 = new GenericMessage<String>("dog");
Message<?> message5 = new GenericMessage<String>("elephant");
this.referencedScriptInput.send(message1);
this.referencedScriptInput.send(message2);
this.referencedScriptInput.send(message3);
this.referencedScriptInput.send(message4);
this.referencedScriptInput.send(message5);
assertEquals("cat", shortStrings.receive(0).getPayload());
assertEquals("dog", shortStrings.receive(0).getPayload());
assertEquals("aardvark", longStrings.receive(0).getPayload());
assertEquals("bear", longStrings.receive(0).getPayload());
assertEquals("elephant", longStrings.receive(0).getPayload());
assertNull(shortStrings.receive(0));
assertNull(longStrings.receive(0));
}
@Test
public void inlineScript() { // long is > 5
Message<?> message1 = new GenericMessage<String>("aardvark");
Message<?> message2 = new GenericMessage<String>("bear");
Message<?> message3 = new GenericMessage<String>("cat");
Message<?> message4 = new GenericMessage<String>("dog");
Message<?> message5 = new GenericMessage<String>("elephant");
this.inlineScriptInput.send(message1);
this.inlineScriptInput.send(message2);
this.inlineScriptInput.send(message3);
this.inlineScriptInput.send(message4);
this.inlineScriptInput.send(message5);
assertEquals("bear", shortStrings.receive(0).getPayload());
assertEquals("cat", shortStrings.receive(0).getPayload());
assertEquals("dog", shortStrings.receive(0).getPayload());
assertEquals("aardvark", longStrings.receive(0).getPayload());
assertEquals("elephant", longStrings.receive(0).getPayload());
assertNull(shortStrings.receive(0));
assertNull(longStrings.receive(0));
}
}

View File

@@ -0,0 +1,5 @@
function route(max){
return payload.length > max ? "longStrings" : "shortStrings";
}
route(maxLen);

View File

@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<service-activator input-channel="referencedScriptInput">
<script:script
lang="ruby"
location="org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests.rb">
<script:variable name="foo" value="foo"/>
<script:variable name="bar" value="bar"/>
<script:variable name="date" ref="date"/>
</script:script>
</service-activator>
<service-activator input-channel="withScriptVariableGenerator">
<script:script lang="ruby" location="org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests.rb"
script-variable-generator="scriptVarSource"/>
</service-activator>
<beans:bean id="scriptVarSource"
class="org.springframework.integration.scripting.config.jsr223.Jsr223ServiceActivatorTests.SampleScriptVariSource"/>
<service-activator input-channel="inlineScriptInput">
<script:script lang="ruby">
<![CDATA[
"inline-#{$payload}"
]]>
</script:script>
</service-activator>
<beans:bean id="date" class="java.util.Date" scope="prototype">
<aop:scoped-proxy/>
</beans:bean>
</beans:beans>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<service-activator input-channel="inlineScriptInput">
<script:script lang="ruby">
<![CDATA[
return "inline-#{$payload}-" + "#{$foo}" + " - " + $bar + " - " + $date
]]>
<script:variable name="foo" value="foo"/>
<script:variable name="bar" value="bar"/>
</script:script>
</service-activator>
</beans:beans>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<service-activator input-channel="withScriptVariableGenerator">
<script:script lang="ruby" location="org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests.rb"
script-variable-generator="scriptVarSource">
<script:variable name="foo" value="foo"/>
<script:variable name="bar" value="bar"/>
</script:script>
</service-activator>
</beans:beans>

View File

@@ -0,0 +1,152 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.scripting.config.jsr223;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
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;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
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.scripting.ScriptVariableGenerator;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @author Oleg Zhurakousky
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Jsr223ServiceActivatorTests {
@Autowired
private MessageChannel referencedScriptInput;
@Autowired
private MessageChannel inlineScriptInput;
@Autowired
private MessageChannel withScriptVariableGenerator;
@Test
public void referencedScriptAndCustomiser() throws Exception{
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
for (int i = 1; i <= 3; i++) {
Message<?> message = MessageBuilder.withPayload("test-" + i).setReplyChannel(replyChannel).build();
this.referencedScriptInput.send(message);
Thread.sleep(1000);
}
String value1 = (String) replyChannel.receive(0).getPayload();
String value2 = (String) replyChannel.receive(0).getPayload();
String value3 = (String) replyChannel.receive(0).getPayload();
System.out.println(value1 + "\n" + value2 + "\n" + value3);
assertTrue(value1.startsWith("ruby-test-1-foo - bar"));
assertTrue(value2.startsWith("ruby-test-2-foo - bar"));
assertTrue(value3.startsWith("ruby-test-3-foo - bar"));
// because we are using 'prototype bean the suffix date will be different
assertFalse(value1.substring(26).equals(value2.substring(26)));
assertFalse(value2.substring(26).equals(value3.substring(26)));
assertNull(replyChannel.receive(0));
}
@Test
public void withScriptVariableGenerator() throws Exception{
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
for (int i = 1; i <= 3; i++) {
Message<?> message = MessageBuilder.withPayload("test-" + i).setReplyChannel(replyChannel).build();
this.withScriptVariableGenerator.send(message);
Thread.sleep(1000);
}
String value1 = (String) replyChannel.receive(0).getPayload();
String value2 = (String) replyChannel.receive(0).getPayload();
String value3 = (String) replyChannel.receive(0).getPayload();
assertTrue(value1.startsWith("ruby-test-1-foo - bar"));
assertTrue(value2.startsWith("ruby-test-2-foo - bar"));
assertTrue(value3.startsWith("ruby-test-3-foo - bar"));
// because we are using 'prototype bean the suffix date will be different
assertFalse(value1.substring(26).equals(value2.substring(26)));
assertFalse(value2.substring(26).equals(value3.substring(26)));
assertNull(replyChannel.receive(0));
}
@Test
public void inlineScript() throws Exception{
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
for (int i = 1; i <= 3; i++) {
Message<?> message = MessageBuilder.withPayload("test-" + i).setReplyChannel(replyChannel).build();
this.inlineScriptInput.send(message);
}
assertEquals("inline-test-1", replyChannel.receive(0).getPayload());
assertEquals("inline-test-2", replyChannel.receive(0).getPayload());
assertEquals("inline-test-3", replyChannel.receive(0).getPayload());
assertNull(replyChannel.receive(0));
}
@Test(expected=BeanDefinitionParsingException.class)
public void inlineScriptAndVariables() throws Exception{
new ClassPathXmlApplicationContext("Jsr223ServiceActivatorTests-fail-context.xml", this.getClass());
}
@Test(expected=BeanDefinitionParsingException.class)
public void variablesAndScriptVariableGenerator() throws Exception{
new ClassPathXmlApplicationContext("Jsr223ServiceActivatorTests-fail-withgenerator-context.xml", this.getClass());
}
public static class SampleScriptVariSource implements ScriptVariableGenerator{
public Map<String, Object> generateScriptVariables(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;
}
}
}

View File

@@ -0,0 +1 @@
"ruby-#{$payload}-#{$foo} - #{$bar} - #{$date}"

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:jsr223="http://www.springframework.org/schema/integration/jsr223"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd
http://www.springframework.org/schema/integration/jsr223 http://www.springframework.org/schema/integration/jsr223/spring-integration-jsr223.xsd">
<splitter input-channel="referencedScriptInput">
<script:script lang="groovy"
location="org/springframework/integration/scripting/config/jsr223/Jsr223SplitterTests.groovy"/>
</splitter>
<splitter input-channel="inlineScriptInput">
<script:script lang="groovy"><![CDATA[
return payload.split('\\s+')
]]></script:script>
</splitter>
</beans:beans>

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.scripting.config.jsr223;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Jsr223SplitterTests {
@Autowired
private MessageChannel referencedScriptInput;
@Autowired
private MessageChannel inlineScriptInput;
@Test
public void referencedScript() {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
Message<?> message = MessageBuilder.withPayload("x,y,z").setReplyChannel(replyChannel).build();
this.referencedScriptInput.send(message);
assertEquals("x", replyChannel.receive(0).getPayload());
assertEquals("y", replyChannel.receive(0).getPayload());
assertEquals("z", replyChannel.receive(0).getPayload());
assertNull(replyChannel.receive(0));
}
@Test
public void inlineScript() {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
Message<?> message = MessageBuilder.withPayload("a b c").setReplyChannel(replyChannel).build();
this.inlineScriptInput.send(message);
assertEquals("a", replyChannel.receive(0).getPayload());
assertEquals("b", replyChannel.receive(0).getPayload());
assertEquals("c", replyChannel.receive(0).getPayload());
assertNull(replyChannel.receive(0));
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<transformer input-channel="referencedScriptInput">
<script:script
lang="ruby"
location="org/springframework/integration/scripting/config/jsr223/Jsr223TransformerTests.rb"/>
</transformer>
<transformer input-channel="inlineScriptInput">
<script:script lang="groovy"><![CDATA[
return "inline-$payload"
]]></script:script>
</transformer>
</beans:beans>

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.scripting.config.jsr223;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Jsr223TransformerTests {
@Autowired
private MessageChannel referencedScriptInput;
@Autowired
private MessageChannel inlineScriptInput;
@Test
public void referencedScript() {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
for (int i = 1; i <= 3; i++) {
Message<?> message = MessageBuilder.withPayload("test-" + i).setReplyChannel(replyChannel).build();
this.referencedScriptInput.send(message);
}
assertEquals("ruby-test-1", replyChannel.receive(0).getPayload());
assertEquals("ruby-test-2", replyChannel.receive(0).getPayload());
assertEquals("ruby-test-3", replyChannel.receive(0).getPayload());
assertNull(replyChannel.receive(0));
}
@Test
public void inlineScript() {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
for (int i = 1; i <= 3; i++) {
Message<?> message = MessageBuilder.withPayload("test-" + i).setReplyChannel(replyChannel).build();
this.inlineScriptInput.send(message);
}
assertEquals("inline-test-1", replyChannel.receive(0).getPayload().toString());
assertEquals("inline-test-2", replyChannel.receive(0).getPayload().toString());
assertEquals("inline-test-3", replyChannel.receive(0).getPayload().toString());
assertNull(replyChannel.receive(0));
}
}

View File

@@ -0,0 +1,8 @@
class Transformer
def transform(payload)
"ruby-#{payload}"
end
end
Transformer.new.transform($payload)

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.integration.scripting.jsr223;
import static org.junit.Assert.assertEquals;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.integration.Message;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.scripting.jsr223.Jsr223ScriptExecutingMessageProcessor;
import org.springframework.integration.scripting.jsr223.Jsr223ScriptExecutor;
import org.springframework.scripting.ScriptSource;
import org.springframework.scripting.support.ResourceScriptSource;
/**
* @author David Turanski
*
*/
public class Jsr223ScriptExecutingMessageProcessorTests {
Jsr223ScriptExecutor executor;
@Before
public void setUp() {
executor = new Jsr223ScriptExecutor("jruby");
}
@Test
public void testExecuteWithVariables(){
Map<String,Object> vars = new HashMap<String,Object>();
vars.put("one",1);
vars.put("two","two");
vars.put("three", new Integer(3));
ScriptSource scriptSource = new ResourceScriptSource(new ClassPathResource("/org/springframework/integration/scripting/jsr223/print_message.rb"));
Jsr223ScriptExecutingMessageProcessor messageProcessor = new Jsr223ScriptExecutingMessageProcessor(scriptSource,executor,vars);
Message<?> message = new GenericMessage<String>("hello");
Object obj = messageProcessor.processMessage(message);
assertEquals("hello modified",obj.toString().substring(0,"hello modified".length()));
}
@Test
public void testWithNoVars(){
ScriptSource scriptSource = new ResourceScriptSource(new ClassPathResource("/org/springframework/integration/scripting/jsr223/print_message.rb"));
Jsr223ScriptExecutingMessageProcessor messageProcessor = new Jsr223ScriptExecutingMessageProcessor(scriptSource,executor);
Message<?> message = new GenericMessage<String>("hello");
Object obj = messageProcessor.processMessage(message);
assertEquals("hello modified",obj.toString().substring(0,"hello modified".length()));
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.integration.scripting.jsr223;
import static org.junit.Assert.assertEquals;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.integration.scripting.ScriptExecutor;
import org.springframework.integration.scripting.jsr223.Jsr223ScriptExecutor;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.scripting.support.StaticScriptSource;
/**
* @author David Turanski
*
*/
public class Jsr223ScriptExecutorTests {
@Test
public void test(){
ScriptExecutor executor = new Jsr223ScriptExecutor("jruby");
executor.executeScript(new StaticScriptSource("puts 'hello, world'"));
executor.executeScript(new StaticScriptSource("puts 'hello, again'"));
Map<String,Object> variables = new HashMap<String,Object>();
Map<String,Object> headers = new HashMap<String,Object>();
headers.put("one",1);
headers.put("two","two");
headers.put("three", new Integer(3));
variables.put("payload", "payload");
variables.put("headers", headers);
String result = (String)executor.executeScript(
new ResourceScriptSource(new ClassPathResource("/org/springframework/integration/scripting/jsr223/print_message.rb")),
variables
);
assertEquals("payload modified",result.substring(0,"payload modified".length()));
}
@Test
public void testJs(){
ScriptExecutor executor = new Jsr223ScriptExecutor("js");
Object obj = executor.executeScript(new StaticScriptSource("function js(){ return 'js';} js();"));
assertEquals("js",obj.toString());
}
}

View File

@@ -0,0 +1,9 @@
require 'java'
class RubyHello
include_class 'com.dturanski.test.jruby.Hello'
def say
"hello,world"
end
end
RubyHello.new

View File

@@ -0,0 +1,12 @@
include Java
include_class 'java.util.Date'
#payload and headers a global variable
if $payload
$payload = $payload+" modified #{Date.new}"
end
puts $payload
if $headers
$headers.each {|key, value| puts "#{key} is #{value}" }
end
puts "#{$one} #{$two} #{$three}"
$payload

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%t] %-5p: %c - %m%n" />
</layout>
</appender>
<!-- Loggers -->
<logger name="org.springframework">
<level value="info" />
</logger>
<logger name="org.springframework.integration.jsr223">
<level value="info" />
</logger>
<!-- Root Logger -->
<root>
<priority value="info" />
<appender-ref ref="console" />
</root>
</log4j:configuration>

View File

@@ -0,0 +1,11 @@
Bundle-SymbolicName: org.springframework.integration.scripting
Bundle-Name: Spring Integration Scripting Support
Bundle-Vendor: SpringSource
Bundle-Version: ${version}
Bundle-ManifestVersion: 2
Import-Template:
org.apache.commons.logging;version="[1.1.1, 2.0.0)",
org.springframework.integration.*;version="[2.1.0, 2.1.1)",
org.springframework.*;version="[3.1.0, 4.0.0)",
org.w3c.dom.*;version="0",
javax.script.*;version="0"