diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/ScriptItemProcessor.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/ScriptItemProcessor.java index 17f3b54a30..7fad4db9c8 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/ScriptItemProcessor.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/ScriptItemProcessor.java @@ -108,9 +108,24 @@ public void setItemBindingVariableName(String itemBindingVariableName) { this.itemBindingVariableName = itemBindingVariableName; } + /** + *
+ * 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. + *
+ * + * @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"); @@ -118,9 +133,9 @@ public void afterPropertiesSet() throws Exception { 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); } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/ScriptItemProcessorTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/ScriptItemProcessorTests.java index 36298843d6..e294c0085f 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/ScriptItemProcessorTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/ScriptItemProcessorTests.java @@ -19,6 +19,8 @@ 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 void testItemBindingNameChange() throws Exception { assertEquals("Incorrect transformed value", "SS", scriptItemProcessor.process("ss")); } + @Test + public void testBshScriptEvaluator() throws Exception { + assumeTrue(languageExists("bsh")); + + ScriptItemProcessor