INT-4160: Port Scripts DSL

JIRA: https://jira.spring.io/browse/INT-4160

* Rename `Scripts.script()` to processor() for better context
* Add `Scripts.messageSource()` for the `ScriptMessageSourceSpec`
* Redo `DslScriptExecutingMessageProcessor` logic to deal with `GroovyScriptExecutingMessageProcessor` via reflection, since that class is a part of `SI-Groovy` module
* Upgrade to `JRuby-9.1.5.0`

* Get rid of reflection in the `DslScriptExecutingMessageProcessor`
* Introduce `ScriptExecutingProcessorFactory` and its `GroovyAwareScriptExecutingProcessorFactory` extension in the `groovy` module
* Register `GroovyAwareScriptExecutingProcessorFactory` as a bean via `GroovyIntegrationConfigurationInitializer`
* Use bean for `ScriptExecutingProcessorFactory.BEAN_NAME` in the `DslScriptExecutingMessageProcessor` to populate `delegate` and fallback to the regular `ScriptExecutingMessageProcessor` instantiation if there is no such a `ScriptExecutingProcessorFactory.BEAN_NAME` bean
This commit is contained in:
Artem Bilan
2016-11-03 15:33:45 -04:00
committed by Gary Russell
parent 47cd4e4540
commit 1871b11e85
17 changed files with 1017 additions and 1 deletions

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2016 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;
import org.springframework.integration.scripting.AbstractScriptExecutingMessageProcessor;
import org.springframework.integration.scripting.ScriptExecutor;
import org.springframework.integration.scripting.ScriptVariableGenerator;
import org.springframework.integration.scripting.jsr223.ScriptExecutingMessageProcessor;
import org.springframework.integration.scripting.jsr223.ScriptExecutorFactory;
import org.springframework.scripting.ScriptSource;
/**
* The factory to create {@link AbstractScriptExecutingMessageProcessor} instances for provided arguments.
*
* @author Artem Bilan
* @since 5.0
*/
public class ScriptExecutingProcessorFactory {
public static final String BEAN_NAME = "integrationScriptExecutingProcessorFactory";
public AbstractScriptExecutingMessageProcessor<?> createMessageProcessor(String language,
ScriptSource scriptSource, ScriptVariableGenerator scriptVariableGenerator) {
ScriptExecutor scriptExecutor = ScriptExecutorFactory.getScriptExecutor(language);
return new ScriptExecutingMessageProcessor(scriptSource, scriptVariableGenerator, scriptExecutor);
}
}

View File

@@ -0,0 +1,126 @@
/*
* Copyright 2016 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.dsl;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.scripting.AbstractScriptExecutingMessageProcessor;
import org.springframework.integration.scripting.RefreshableResourceScriptSource;
import org.springframework.integration.scripting.ScriptVariableGenerator;
import org.springframework.integration.scripting.config.ScriptExecutingProcessorFactory;
import org.springframework.integration.scripting.jsr223.ScriptExecutingMessageProcessor;
import org.springframework.integration.scripting.jsr223.ScriptExecutorFactory;
import org.springframework.messaging.Message;
import org.springframework.scripting.ScriptSource;
import org.springframework.util.StringUtils;
/**
* The adapter {@link MessageProcessor} around {@link AbstractScriptExecutingMessageProcessor}.
* Delegates to the {@code GroovyScriptExecutingMessageProcessor}, if provided {@link #lang}
* matches to {@code groovy} string and {@code spring-integration-groovy} jar is in classpath.
* Otherwise to the {@link ScriptExecutingMessageProcessor}.
*
* @author Artem Bilan
*
* @since 5.0
*/
class DslScriptExecutingMessageProcessor
implements MessageProcessor<Object>, InitializingBean, ApplicationContextAware {
private Resource script;
private String location;
private String lang;
private long refreshCheckDelay = -1;
private ScriptVariableGenerator variableGenerator;
private ApplicationContext applicationContext;
private AbstractScriptExecutingMessageProcessor<?> delegate;
DslScriptExecutingMessageProcessor(Resource script) {
this.script = script;
}
DslScriptExecutingMessageProcessor(String location) {
this.location = location;
}
public void setLang(String lang) {
this.lang = lang;
}
public void setRefreshCheckDelay(Long refreshCheckDelay) {
this.refreshCheckDelay = refreshCheckDelay;
}
public void setVariableGenerator(ScriptVariableGenerator variableGenerator) {
this.variableGenerator = variableGenerator;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void afterPropertiesSet() throws Exception {
if (StringUtils.hasText(this.location)) {
this.script = this.applicationContext.getResource(this.location);
}
ScriptSource scriptSource = new RefreshableResourceScriptSource(this.script, this.refreshCheckDelay);
if (!StringUtils.hasText(this.lang)) {
String filename = this.script.getFilename();
int index = filename.lastIndexOf(".") + 1;
if (index < 1) {
throw new BeanCreationException("'lang' isn't provided and there is 'file extension' for script " +
"resource: " + this.script);
}
this.lang = filename.substring(index);
}
if (this.applicationContext.containsBean(ScriptExecutingProcessorFactory.BEAN_NAME)) {
ScriptExecutingProcessorFactory processorFactory =
this.applicationContext.getBean(ScriptExecutingProcessorFactory.BEAN_NAME,
ScriptExecutingProcessorFactory.class);
this.delegate = processorFactory.createMessageProcessor(this.lang, scriptSource, this.variableGenerator);
}
else {
this.delegate = new ScriptExecutingMessageProcessor(scriptSource, this.variableGenerator,
ScriptExecutorFactory.getScriptExecutor(this.lang));
}
this.delegate.setBeanFactory(this.applicationContext);
this.delegate.setBeanClassLoader(this.applicationContext.getClassLoader());
}
@Override
public Object processMessage(Message<?> message) {
return this.delegate.processMessage(message);
}
}

View File

@@ -0,0 +1,133 @@
/*
* Copyright 2016 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.dsl;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import org.springframework.core.io.Resource;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.dsl.ComponentsRegistration;
import org.springframework.integration.dsl.MessageSourceSpec;
import org.springframework.integration.endpoint.MessageProcessorMessageSource;
import org.springframework.integration.scripting.ScriptVariableGenerator;
import org.springframework.integration.support.MapBuilder;
/**
* The {@link MessageSourceSpec} for Dynamic Language Scripts.
* Delegates configuration options to the {@link ScriptSpec}.
* Produces {@link MessageProcessorMessageSource}.
* *
* @author Artem Bilan
*
* @since 5.0
*
* @see ScriptSpec
* @see MessageProcessorMessageSource
*/
public class ScriptMessageSourceSpec extends MessageSourceSpec<ScriptMessageSourceSpec, MessageSource<?>>
implements ComponentsRegistration {
private final ScriptSpec delegate;
public ScriptMessageSourceSpec(Resource scriptResource) {
this.delegate = new ScriptSpec(scriptResource);
}
public ScriptMessageSourceSpec(String scriptLocation) {
this.delegate = new ScriptSpec(scriptLocation);
}
/**
* The script lang (Groovy, ruby, python etc.).
* @param lang the script lang
* @return the current spec
* @see ScriptSpec#lang
*/
public ScriptMessageSourceSpec lang(String lang) {
this.delegate.lang(lang);
return this;
}
/**
* The {@link ScriptVariableGenerator} to use.
* @param variableGenerator the {@link ScriptVariableGenerator}
* @return the current spec
* @see ScriptSpec#variableGenerator
*/
public ScriptMessageSourceSpec variableGenerator(ScriptVariableGenerator variableGenerator) {
this.delegate.variableGenerator(variableGenerator);
return this;
}
/**
* The script variables to use.
* @param variables the script variables
* @return the current spec
* @see ScriptSpec#variables(MapBuilder)
*/
public ScriptMessageSourceSpec variables(MapBuilder<?, String, Object> variables) {
this.delegate.variables(variables);
return this;
}
/**
* The script variables to use.
* @param variables the script variables
* @return the current spec
* @see ScriptSpec#variables(Map)
*/
public ScriptMessageSourceSpec variables(Map<String, Object> variables) {
this.delegate.variables(variables);
return this;
}
/**
* The script variable to use.
* @param name the name of variable
* @param value the value of variable
* @return the current spec
* @see ScriptSpec#variable
*/
public ScriptMessageSourceSpec variable(String name, Object value) {
this.delegate.variable(name, value);
return this;
}
/**
* The refreshCheckDelay in milliseconds for refreshable script resource.
* @param refreshCheckDelay the refresh check delay milliseconds
* @return the current spec
* @see ScriptSpec#refreshCheckDelay
*/
public ScriptMessageSourceSpec refreshCheckDelay(long refreshCheckDelay) {
this.delegate.refreshCheckDelay(refreshCheckDelay);
return this;
}
@Override
protected MessageSource<?> doGet() {
return new MessageProcessorMessageSource(this.delegate.get());
}
@Override
public Collection<Object> getComponentsToRegister() {
return Collections.singletonList(this.delegate.get());
}
}

View File

@@ -0,0 +1,138 @@
/*
* Copyright 2016 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.dsl;
import java.util.HashMap;
import java.util.Map;
import org.springframework.core.io.Resource;
import org.springframework.integration.dsl.MessageProcessorSpec;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.scripting.DefaultScriptVariableGenerator;
import org.springframework.integration.scripting.ScriptVariableGenerator;
import org.springframework.integration.support.MapBuilder;
import org.springframework.util.Assert;
/**
* The {@link MessageProcessorSpec} implementation for the {@link DslScriptExecutingMessageProcessor}.
*
* @author Artem Bilan
*
* @since 5.0
*/
public class ScriptSpec extends MessageProcessorSpec<ScriptSpec> {
private final DslScriptExecutingMessageProcessor processor;
private ScriptVariableGenerator variableGenerator;
private Map<String, Object> variables = new HashMap<String, Object>();
ScriptSpec(Resource scriptResource) {
Assert.notNull(scriptResource);
this.processor = new DslScriptExecutingMessageProcessor(scriptResource);
}
ScriptSpec(String scriptLocation) {
Assert.hasText(scriptLocation);
this.processor = new DslScriptExecutingMessageProcessor(scriptLocation);
}
/**
* The script lang (Groovy, ruby, python etc.).
* @param lang the script lang
* @return the current spec
* @see DslScriptExecutingMessageProcessor#setLang
*/
public ScriptSpec lang(String lang) {
Assert.hasText(lang);
this.processor.setLang(lang);
return this;
}
/**
* The refreshCheckDelay in milliseconds for refreshable script resource.
* @param refreshCheckDelay the refresh check delay milliseconds
* @return the current spec
* @see org.springframework.integration.scripting.RefreshableResourceScriptSource
*/
public ScriptSpec refreshCheckDelay(long refreshCheckDelay) {
this.processor.setRefreshCheckDelay(refreshCheckDelay);
return this;
}
/**
* The {@link ScriptVariableGenerator} to use.
* @param variableGenerator the {@link ScriptVariableGenerator}
* @return the current spec
* @see org.springframework.integration.scripting.AbstractScriptExecutingMessageProcessor
*/
public ScriptSpec variableGenerator(ScriptVariableGenerator variableGenerator) {
Assert.notNull(variableGenerator);
Assert.state(this.variables.isEmpty(), "'variableGenerator' and 'variables' are mutually exclusive");
this.variableGenerator = variableGenerator;
return this;
}
/**
* The script variables to use.
* @param variables the script variables {@link MapBuilder}
* @return the current spec
* @see DefaultScriptVariableGenerator
*/
public ScriptSpec variables(MapBuilder<?, String, Object> variables) {
return variables(variables.get());
}
/**
* The script variables to use
* @param variables the script variables {@link Map}
* @return the current spec
* @see DefaultScriptVariableGenerator
*/
public ScriptSpec variables(Map<String, Object> variables) {
Assert.notEmpty(variables);
Assert.state(this.variableGenerator == null, "'variableGenerator' and 'variables' are mutually exclusive");
this.variables.putAll(variables);
return this;
}
/**
* The script variable to use.
* @param name the name of variable
* @param value the value of variable
* @return the current spec
* @see DefaultScriptVariableGenerator
*/
public ScriptSpec variable(String name, Object value) {
Assert.hasText(name);
Assert.state(this.variableGenerator == null, "'variableGenerator' and 'variables' are mutually exclusive");
this.variables.put(name, value);
return this;
}
@Override
protected MessageProcessor<?> doGet() {
if (this.variableGenerator == null) {
this.variableGenerator = new DefaultScriptVariableGenerator(this.variables);
}
this.processor.setVariableGenerator(this.variableGenerator);
return this.processor;
}
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2016 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.dsl;
import org.springframework.core.io.Resource;
/**
* The factory for Dynamic Language Scripts (Groovy, Ruby, Python, JavaScript etc.).
*
* @author Artem Bilan
*
* @since 5.0
*/
public final class Scripts {
/**
* The factory method to produce {@link ScriptSpec} based on the {@link Resource}.
* The {@link Resource} must represent the real file and can be injected like:
* <pre class="code">
* &#064;Value("com/my/project/scripts/FilterScript.groovy")
* private Resource filterScript;
* </pre>
* @param scriptResource the script file {@link Resource}
* @return the ScriptSpec instance
*/
public static ScriptSpec processor(Resource scriptResource) {
return new ScriptSpec(scriptResource);
}
/**
* The factory method to produce {@link ScriptSpec} based on the script file location.
* @param scriptLocation the path to the script file.
* {@code file:}, {@code ftp:}, {@code s3:} etc.
* The {@code classpath:} can be omitted.
* @return the ScriptSpec instance
*/
public static ScriptSpec processor(String scriptLocation) {
return new ScriptSpec(scriptLocation);
}
/**
* Factory for the {@link ScriptMessageSourceSpec} based on the {@link Resource}.
* The {@link Resource} must represent the real file and can be injected like:
* <pre class="code">
* &#064;Value("com/my/project/scripts/FilterScript.groovy")
* private Resource filterScript;
* </pre>
* @param scriptResource the script {@link Resource}
* @return the {@link ScriptMessageSourceSpec}
*/
public static ScriptMessageSourceSpec messageSource(Resource scriptResource) {
return new ScriptMessageSourceSpec(scriptResource);
}
/**
* Factory for the {@link ScriptMessageSourceSpec} based on the script location.
* @param scriptLocation the path to the script file.
* {@code file:}, {@code ftp:}, {@code s3:} etc.
* The {@code classpath:} can be omitted.
* @return the {@link ScriptMessageSourceSpec}
*/
public static ScriptMessageSourceSpec messageSource(String scriptLocation) {
return new ScriptMessageSourceSpec(scriptLocation);
}
private Scripts() {
}
}

View File

@@ -0,0 +1,4 @@
/**
* Provides Scripting Components support for Spring Integration Java DSL.
*/
package org.springframework.integration.scripting.dsl;