Skip to content

Commit c97ac19

Browse files
committed
INT-3976: Remove an explicit scriptClassName
JIRA: https://jira.spring.io/browse/INT-3976 The class name in case of `StaticScriptSource` (for inline scripts) is required only for Groovy native engine. Even if `GroovyScriptParser` provides an explicit `scriptClassName` option, we still can configure `GroovyScriptExecutingMessageProcessor` with the `StaticScriptSource` from Java without the script class name. Therefore the logic in the `GroovyScriptParser` isn't robust. * Remove the `GroovyScriptParser` `scriptClassName` logic altogether * Change the `GroovyScriptExecutingMessageProcessor` to rely on the Groovy native `scriptClassName` generation in case of the `scriptSource` is provided without the class name option * Fix the test for the actual generated script class name
1 parent dc94b42 commit c97ac19

File tree

5 files changed

+24
-52
lines changed

5 files changed

+24
-52
lines changed

spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessor.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.scripting.groovy.GroovyObjectCustomizer;
3535
import org.springframework.util.Assert;
3636
import org.springframework.util.ClassUtils;
37+
import org.springframework.util.StringUtils;
3738

3839
import groovy.lang.Binding;
3940
import groovy.lang.GString;
@@ -164,8 +165,14 @@ private void parseScriptIfNecessary(ScriptSource scriptSource) throws Exception
164165
try {
165166
// synchronized double check
166167
if (this.scriptClass == null || scriptSource.isModified()) {
167-
this.scriptClass = this.groovyClassLoader.parseClass(
168-
scriptSource.getScriptAsString(), scriptSource.suggestedClassName());
168+
String className = scriptSource.suggestedClassName();
169+
if (StringUtils.hasText(className)) {
170+
this.scriptClass =
171+
this.groovyClassLoader.parseClass(scriptSource.getScriptAsString(), className);
172+
}
173+
else {
174+
this.scriptClass = this.groovyClassLoader.parseClass(scriptSource.getScriptAsString());
175+
}
169176
}
170177
}
171178
finally {

spring-integration-groovy/src/main/java/org/springframework/integration/groovy/config/GroovyScriptParser.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor;
2525
import org.springframework.integration.scripting.config.AbstractScriptParser;
2626

27-
import groovy.lang.Script;
28-
2927
/**
3028
* Parser for the <groovy:script/> element.
3129
*
@@ -39,16 +37,8 @@ public class GroovyScriptParser extends AbstractScriptParser {
3937

4038

4139
@Override
42-
protected String getBeanClassName(Element element) {
43-
return GroovyScriptExecutingMessageProcessor.class.getName();
44-
}
45-
46-
/* (non-Javadoc)
47-
* @see org.springframework.integration.config.xml.AbstractScriptParser#getScriptSourceClassName()
48-
*/
49-
@Override
50-
protected String getScriptSourceClassName() {
51-
return Script.class.getName();
40+
protected Class<?> getBeanClass(Element element) {
41+
return GroovyScriptExecutingMessageProcessor.class;
5242
}
5343

5444
protected void postProcess(BeanDefinitionBuilder builder, Element element, ParserContext parserContext) {
@@ -57,7 +47,4 @@ protected void postProcess(BeanDefinitionBuilder builder, Element element, Parse
5747
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "compile-static");
5848
}
5949

60-
61-
62-
6350
}

spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyServiceActivatorTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -192,15 +192,15 @@ public void invalidInlineScript() throws Exception {
192192
Throwable cause = e.getCause();
193193
assertEquals(MissingPropertyException.class, cause.getClass());
194194
assertThat(cause.getMessage(),
195-
Matchers.containsString("No such property: ReplyRequiredException for class: groovy_lang"));
195+
Matchers.containsString("No such property: ReplyRequiredException for class: script"));
196196
throw e;
197197
}
198198

199199
}
200200

201201
@Test(expected = BeanDefinitionParsingException.class)
202202
public void variablesAndScriptVariableGenerator() throws Exception {
203-
new ClassPathXmlApplicationContext("GroovyServiceActivatorTests-fail-withgenerator-context.xml",
203+
new ClassPathXmlApplicationContext("GroovyServiceActivatorTests-fail-withgenerator-context.xml",
204204
this.getClass()).close();
205205
}
206206

@@ -212,7 +212,7 @@ public void testGroovyScriptForOutboundChannelAdapter() {
212212

213213

214214
public static class SampleScriptVariSource implements ScriptVariableGenerator {
215-
215+
216216
public Map<String, Object> generateScriptVariables(Message<?> message) {
217217
Map<String, Object> variables = new HashMap<String, Object>();
218218
variables.put("foo", "foo");
@@ -222,7 +222,7 @@ public Map<String, Object> generateScriptVariables(Message<?> message) {
222222
variables.put("headers", message.getHeaders());
223223
return variables;
224224
}
225-
225+
226226
}
227227

228228

@@ -233,7 +233,7 @@ public static class MyGroovyCustomizer implements GroovyObjectCustomizer {
233233
public void customize(GroovyObject goo) {
234234
this.executed = true;
235235
}
236-
236+
237237
}
238238

239239
}

spring-integration-scripting/src/main/java/org/springframework/integration/scripting/config/AbstractScriptParser.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,11 @@ protected boolean shouldGenerateIdAsFallback() {
5050
return true;
5151
}
5252

53-
@Override
54-
protected abstract String getBeanClassName(Element element);
55-
56-
protected abstract String getScriptSourceClassName();
57-
5853
@Override
5954
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
6055
String scriptLocation = element.getAttribute(LOCATION_ATTRIBUTE);
6156
String scriptText = DomUtils.getTextValue(element);
62-
if (!(StringUtils.hasText(scriptLocation) ^ StringUtils.hasText(scriptText))) {
57+
if (StringUtils.hasText(scriptLocation) == StringUtils.hasText(scriptText)) {
6358
parserContext.getReaderContext().error(
6459
"Either the 'location' attribute or inline script text must be provided, but not both.", element);
6560
return;
@@ -80,12 +75,7 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
8075
scriptLocation));
8176
}
8277
else {
83-
if (getScriptSourceClassName() != null) {
84-
builder.addConstructorArgValue(new StaticScriptSource(scriptText, getScriptSourceClassName()));
85-
}
86-
else {
87-
builder.addConstructorArgValue(new StaticScriptSource(scriptText));
88-
}
78+
builder.addConstructorArgValue(new StaticScriptSource(scriptText));
8979
}
9080

9181
BeanMetadataElement scriptVariableGeneratorDef = null;
@@ -150,7 +140,7 @@ public Object put(String key, Object value) {
150140
String variableName = childElement.getAttribute("name");
151141
String variableValue = childElement.getAttribute("value");
152142
String variableRef = childElement.getAttribute("ref");
153-
if (!(StringUtils.hasText(variableValue) ^ StringUtils.hasText(variableRef))) {
143+
if (StringUtils.hasText(variableValue) == StringUtils.hasText(variableRef)) {
154144
parserContext.getReaderContext().error(
155145
"Exactly one of the 'ref' attribute or 'value' attribute, " + " is required for element "
156146
+ IntegrationNamespaceUtils.createElementDescription(element) + ".", element);

spring-integration-scripting/src/main/java/org/springframework/integration/scripting/config/jsr223/ScriptParser.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,22 @@
2424
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2525
import org.springframework.beans.factory.xml.ParserContext;
2626
import org.springframework.integration.scripting.config.AbstractScriptParser;
27+
import org.springframework.integration.scripting.jsr223.ScriptExecutingMessageProcessor;
2728
import org.springframework.integration.scripting.jsr223.ScriptExecutorFactory;
2829
import org.springframework.util.StringUtils;
2930

3031
/**
3132
* @author David Turanski
33+
* @author Artem Bilan
3234
* @since 2.1
3335
*/
3436
public class ScriptParser extends AbstractScriptParser {
3537

3638
private static final String LANGUAGE_ATTRIBUTE = "lang";
3739

3840
@Override
39-
protected String getBeanClassName(Element element) {
40-
return "org.springframework.integration.scripting.jsr223.ScriptExecutingMessageProcessor";
41-
}
42-
43-
/* (non-Javadoc)
44-
* @see org.springframework.integration.config.xml.AbstractScriptParser#getScriptSourceClassName()
45-
*/
46-
@Override
47-
protected String getScriptSourceClassName() {
48-
return null;
41+
protected Class<?> getBeanClass(Element element) {
42+
return ScriptExecutingMessageProcessor.class;
4943
}
5044

5145
@Override
@@ -71,12 +65,6 @@ protected void postProcess(BeanDefinitionBuilder builder, Element element, Parse
7165
builder.addConstructorArgValue(ScriptExecutorFactory.getScriptExecutor(language));
7266
}
7367

74-
/**
75-
* @param scriptLocation
76-
* @param parserContext
77-
* @param element
78-
* @return the language
79-
*/
8068
private String getLanguageFromFileExtension(String scriptLocation, ParserContext parserContext, Element element) {
8169
ScriptEngineManager engineManager = new ScriptEngineManager();
8270
ScriptEngine engine = null;

0 commit comments

Comments
 (0)