diff --git a/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/DefaultScriptVariableGenerator.java b/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/DefaultScriptVariableGenerator.java index 744cefdec4..e5db4586d5 100644 --- a/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/DefaultScriptVariableGenerator.java +++ b/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/DefaultScriptVariableGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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. @@ -24,6 +24,10 @@ import org.springframework.integration.Message; import org.springframework.util.CollectionUtils; /** + * A default {@link ScriptVariableGenerator} implementation; used by script processors. + * The result of {@link #generateScriptVariables(Message)} is a {@link Map} of any provided {@code variables} + * plus {@code payload} and {@code headers} from the {@code Message} argument. + * * @author Oleg Zhurakousky * @author Mark Fisher * @since 2.0.2 @@ -53,7 +57,7 @@ public class DefaultScriptVariableGenerator implements ScriptVariableGenerator { if (!CollectionUtils.isEmpty(this.variableMap)) { for (Map.Entry entry : this.variableMap.entrySet()) { scriptVariables.put(entry.getKey(), entry.getValue()); - } + } } return scriptVariables; } diff --git a/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/ScriptVariableGenerator.java b/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/ScriptVariableGenerator.java index 7cb5b4e113..fb7e0db38b 100644 --- a/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/ScriptVariableGenerator.java +++ b/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/ScriptVariableGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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. @@ -21,6 +21,9 @@ import java.util.Map; import org.springframework.integration.Message; /** + * Strategy interface to provide a {@link Map} of variables to the script execution context. + * Variables may be extracted from the {@link Message} argument. + * * @author Oleg Zhurakousky * @since 2.0.2 */ diff --git a/src/reference/docbook/groovy.xml b/src/reference/docbook/groovy.xml index 7a9adbefb4..4845df307a 100644 --- a/src/reference/docbook/groovy.xml +++ b/src/reference/docbook/groovy.xml @@ -57,6 +57,8 @@ Setting a custom GroovyObjectCustomizer is not mutually exclusive with <variable> sub-elements or the script-variable-generator attribute. It can also be provided when defining an inline script. + For more information regarding <variable> and script-variable-generator, see the + paragraph 'Script variable bindings' of . diff --git a/src/reference/docbook/scripting.xml b/src/reference/docbook/scripting.xml index a2a000233b..559bd018ce 100644 --- a/src/reference/docbook/scripting.xml +++ b/src/reference/docbook/scripting.xml @@ -8,26 +8,26 @@ Scripting support With Spring Integration 2.1 we've added support for the - JSR223 Scripting for Java specification, - introduced in Java version 6. This allows you to use scripts written in any supported language including - Ruby/JRuby, Javascript and Groovy to provide the logic for various integration components similar to the way - the Spring Expression Language (SpEL) is used in Spring Integration. For more information about JSR223 please refer to the + JSR223 Scripting for Java specification, + introduced in Java version 6. This allows you to use scripts written in any supported language including + Ruby/JRuby, Javascript and Groovy to provide the logic for various integration components similar to the way + the Spring Expression Language (SpEL) is used in Spring Integration. For more information about JSR223 please refer to the documentation - Note that this feature requires Java 6 or higher. Sun developed a JSR223 reference implementation which works with + Note that this feature requires Java 6 or higher. Sun developed a JSR223 reference implementation which works with Java 5 but it is not officially supported and we have not tested it with Spring Integration. In order to use a JVM scripting language, a JSR223 implementation for that language must be included in your class path. Java 6 natively - supports Javascript. The Groovy and + supports Javascript. The Groovy and JRuby projects provide JSR233 support in their standard distribution. Other language implementations may be available or under development. Please refer to the appropriate project website for more information. - Various JSR223 language implementations have been developed by third parties. A particular implementation's compatibility - with Spring Integration depends on how well it conforms to the specification and/or the implementer's interpretation of the specification. - - If you plan to use Groovy as your scripting language, we recommended you use Spring-Integration's Groovy Support + Various JSR223 language implementations have been developed by third parties. A particular implementation's compatibility + with Spring Integration depends on how well it conforms to the specification and/or the implementer's interpretation of the specification. + + If you plan to use Groovy as your scripting language, we recommended you use Spring-Integration's Groovy Support as it offers additional features specific to Groovy. However you will find this section relevant as well. @@ -50,28 +50,30 @@ </int:filter> <int:filter input-channel="inlineScriptInput"> - <int-script:script lang="groovy"><![CDATA[ + <int-script:script lang="groovy"> + <![CDATA[ return payload == 'good' - ]]></int-script:script> + ]]> + </int-script:script> </int:filter> Here, you see that the script can be included inline - or can reference a resource location via the location attribute. Additionally the lang attribute + or can reference a resource location via the location attribute. Additionally the lang attribute corresponds to the language name (or JSR223 alias) Other Spring Integration endpoint elements which support scripting include router, service-activator, - transformer, and splitter. The scripting configuration in each case would be identical to the above + transformer, and splitter. The scripting configuration in each case would be identical to the above (besides the endpoint element). - Another useful feature of Scripting support is the ability to update (reload) scripts without - having to restart the Application Context. To accomplish this, specify the refresh-check-delay + Another useful feature of Scripting support is the ability to update (reload) scripts without + having to restart the Application Context. To accomplish this, specify the refresh-check-delay attribute on the script element: <int-script:script location="..." refresh-check-delay="5000"/> In the above example, the script location will be checked for updates every 5 seconds. If the script is updated, - any invocation that occurs later than 5 seconds since the update will result in execution of the new script. + any invocation that occurs later than 5 seconds since the update will result in execution of the new script. <int-script:script location="..." refresh-check-delay="0"/> @@ -82,12 +84,12 @@ This is the default behavior. Inline scripts can not be reloaded. <int-script:script location="..." refresh-check-delay="-1"/> - + Script variable bindings - + - Variable bindings are required to enable the script to reference variables externally provided to the script's execution context. - As we have seen, payload and headers are used as binding variables by default. You can bind additional variables + Variable bindings are required to enable the script to reference variables externally provided to the script's execution context. + As we have seen, payload and headers are used as binding variables by default. You can bind additional variables to a script via <variable> sub-elements: @@ -96,29 +98,32 @@ ]]> As shown in the above example, you can bind a script variable either to a scalar value or a Spring bean reference. Note that payload and headers will still be included as binding variables. - + - If you need more control over how variables are generated, you can implement your own Java class + If you need more control over how variables are generated, you can implement your own Java class using the ScriptVariableGenerator strategy: generateScriptVariables(Message message); - + }]]> This interface requires you to implement the method generateScriptVariables(Message). The Message argument allows you to access any data available in the Message payload and headers and the return value is - the Map of bound variables. This method will be called every time the script is executed for a Message. All you need to do is + the Map of bound variables. This method will be called every time the script is executed for a Message. All you need to do is provide an implementation of ScriptVariableGenerator and reference it with the script-variable-generator attribute: ]]> - + If a script-variable-generator is not provided, script components use + org.springframework.integration.scripting.DefaultScriptVariableGenerator, which merges + any provided <variable>s with payload and headers + variables from the Message in its generateScriptVariables(Message) method. - You cannot provide both the script-variable-generator attribute and <variable> sub-element(s) + You cannot provide both the script-variable-generator attribute and <variable> sub-element(s) as they are mutually exclusive. Also, custom variable bindings cannot be used with an inline script.