BATCH-2270: Allow ScriptEvaluator to be injectable in the ScriptItemProcessor
This commit is contained in:
committed by
Mahmoud Ben Hassine
parent
3d2de1bc04
commit
a20db5e842
@@ -106,9 +106,24 @@ public class ScriptItemProcessor<I, O> implements ItemProcessor<I, O>, Initializ
|
||||
this.itemBindingVariableName = itemBindingVariableName;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Provides the ability to set a custom {@link org.springframework.scripting.ScriptEvaluator}
|
||||
* implementation. If not set, a {@link org.springframework.scripting.support.StandardScriptEvaluator}
|
||||
* will be used by default.
|
||||
* </p>
|
||||
*
|
||||
* @param scriptEvaluator the {@link org.springframework.scripting.ScriptEvaluator} to use
|
||||
*/
|
||||
public void setScriptEvaluator(ScriptEvaluator scriptEvaluator) {
|
||||
this.scriptEvaluator = scriptEvaluator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
scriptEvaluator = new StandardScriptEvaluator();
|
||||
if(scriptEvaluator == null) {
|
||||
scriptEvaluator = new StandardScriptEvaluator();
|
||||
}
|
||||
|
||||
Assert.state(scriptSource != null || script != null,
|
||||
"Either the script source or script file must be provided");
|
||||
@@ -116,9 +131,9 @@ public class ScriptItemProcessor<I, O> implements ItemProcessor<I, O>, Initializ
|
||||
Assert.state(scriptSource == null || script == null,
|
||||
"Either a script source or script file must be provided, not both");
|
||||
|
||||
if (scriptSource != null) {
|
||||
if (scriptSource != null && scriptEvaluator instanceof StandardScriptEvaluator) {
|
||||
Assert.isTrue(!StringUtils.isEmpty(language),
|
||||
"Language must be provided when using script source");
|
||||
"Language must be provided when using the default ScriptEvaluator and raw source code");
|
||||
|
||||
((StandardScriptEvaluator) scriptEvaluator).setLanguage(language);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.scripting.bsh.BshScriptEvaluator;
|
||||
import org.springframework.scripting.groovy.GroovyScriptEvaluator;
|
||||
|
||||
import javax.script.ScriptEngineFactory;
|
||||
import javax.script.ScriptEngineManager;
|
||||
@@ -200,6 +202,30 @@ public class ScriptItemProcessorTests {
|
||||
assertEquals("Incorrect transformed value", "SS", scriptItemProcessor.process("ss"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBshScriptEvaluator() throws Exception {
|
||||
assumeTrue(languageExists("bsh"));
|
||||
|
||||
ScriptItemProcessor<String, Object> scriptItemProcessor = new ScriptItemProcessor<String, Object>();
|
||||
scriptItemProcessor.setScriptEvaluator(new BshScriptEvaluator());
|
||||
scriptItemProcessor.setScriptSource("String process(String item) { return item.toUpperCase(); } process(item);", "bsh");
|
||||
scriptItemProcessor.afterPropertiesSet();
|
||||
|
||||
assertEquals("Incorrect transformed value", "SS", scriptItemProcessor.process("ss"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGroovyScriptEvaluator() throws Exception {
|
||||
assumeTrue(languageExists("groovy"));
|
||||
|
||||
ScriptItemProcessor<String, Object> scriptItemProcessor = new ScriptItemProcessor<String, Object>();
|
||||
scriptItemProcessor.setScriptEvaluator(new GroovyScriptEvaluator());
|
||||
scriptItemProcessor.setScriptSource("def process(item) { return item.toUpperCase() } \n process(item)", "groovy");
|
||||
scriptItemProcessor.afterPropertiesSet();
|
||||
|
||||
assertEquals("Incorrect transformed value", "SS", scriptItemProcessor.process("ss"));
|
||||
}
|
||||
|
||||
private boolean languageExists(String engineName) {
|
||||
return availableLanguages.contains(engineName);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user