INT-1576,INT-1579,INT-2025: added spring-integration-scripting and fixed HeaderEnricher issue

This commit is contained in:
David Turanski
2011-08-03 15:15:00 +05:30
parent f94411e11d
commit 207f043587
64 changed files with 1735 additions and 239 deletions

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2002-2011 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;
import java.util.Map;
import org.springframework.scripting.ScriptSource;
/**
* @author David Turanski
* @since 2.1
*/
public interface ScriptExecutor {
/**
*
* @param scriptSource
* @return
*/
public abstract Object executeScript(ScriptSource scriptSource);
/**
*
* @param scriptSource
* @param variables -bind variable
* @return
*/
public abstract Object executeScript(ScriptSource scriptSource,Map<String,Object> variables);
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2002-2010 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.jsr223;
import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHandler;
/**
* @author David Turanski
* @since 2.1
*/
public class Jsr223NamespaceHandler extends AbstractIntegrationNamespaceHandler {
public void init() {
this.registerBeanDefinitionParser("script", new Jsr223ScriptParser());
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2002-2011 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.jsr223;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.AbstractScriptParser;
import org.springframework.integration.scripting.jsr223.Jsr223ScriptExecutor;
import org.springframework.util.Assert;
import org.w3c.dom.Element;
/**
* @author David Turanski
* @since 2.1
*/
public class Jsr223ScriptParser extends AbstractScriptParser {
private static final String LANGUAGE_ATTRIBUTE = "lang";
@Override
protected String getBeanClassName(Element element) {
return "org.springframework.integration.scripting.jsr223.Jsr223ScriptExecutingMessageProcessor";
}
/* (non-Javadoc)
* @see org.springframework.integration.config.xml.AbstractScriptParser#getScriptSourceClassName()
*/
@Override
protected String getScriptSourceClassName() {
return null;
}
protected void postProcess(BeanDefinitionBuilder builder, Element element, ParserContext parserContext){
String language = element.getAttribute(LANGUAGE_ATTRIBUTE);
Assert.hasLength(language, "Attribute " + LANGUAGE_ATTRIBUTE + " is required");
builder.addConstructorArgValue(new Jsr223ScriptExecutor(language));
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright 2002-2011 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.jsr223;
import java.util.Map;
import org.springframework.integration.Message;
import org.springframework.integration.scripting.AbstractScriptExecutingMessageProcessor;
import org.springframework.integration.scripting.DefaultScriptVariableGenerator;
import org.springframework.integration.scripting.ScriptExecutor;
import org.springframework.integration.scripting.ScriptVariableGenerator;
import org.springframework.scripting.ScriptSource;
import org.springframework.util.Assert;
/**
* @author David Turanski
* @since 2.1
*/
public class Jsr223ScriptExecutingMessageProcessor extends AbstractScriptExecutingMessageProcessor<Object> {
private final Jsr223ScriptExecutor scriptExecutor;
private volatile ScriptSource scriptSource;
/**
* Create a processor for the {@link ScriptSource} using the provided {@link ScriptExecutor} using the DefaultScriptVariableGenerator
* @param scriptSource
* @param scriptExecutor
*/
public Jsr223ScriptExecutingMessageProcessor(ScriptSource scriptSource, Jsr223ScriptExecutor scriptExecutor) {
super();
this.scriptSource = scriptSource;
this.scriptExecutor = scriptExecutor;
}
/**
* Create a processor for the {@link ScriptSource} using the provided {@link ScriptExecutor}
* @param scriptSource
* @param scriptExecutor
*/
public Jsr223ScriptExecutingMessageProcessor(ScriptSource scriptSource, ScriptVariableGenerator scriptVariableGenerator,Jsr223ScriptExecutor scriptExecutor) {
super(scriptVariableGenerator);
this.scriptSource = scriptSource;
this.scriptExecutor = scriptExecutor;
}
/**
* Create a processor for the {@link ScriptSource} using the provided {@link ScriptExecutor} using the DefaultScriptVariableGenerator
* @param scriptSource
* @param scriptExecutor
* @param variables
*/
public Jsr223ScriptExecutingMessageProcessor(ScriptSource scriptSource,Jsr223ScriptExecutor scriptExecutor,Map<String,Object> variables ) {
super(new DefaultScriptVariableGenerator(variables));
this.scriptSource = scriptSource;
this.scriptExecutor = scriptExecutor;
}
@Override
protected ScriptSource getScriptSource(Message<?> message) {
return this.scriptSource;
}
@Override
protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) throws Exception {
Assert.notNull(scriptSource, "scriptSource must not be null");
return this.scriptExecutor.executeScript(scriptSource,variables);
}
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright 2002-2011 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.jsr223;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import javax.script.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.integration.scripting.ScriptExecutor;
import org.springframework.scripting.ScriptSource;
import org.springframework.util.Assert;
/**
* Executes Jsr223 scripts
*
* @author David Turanski
* @since 2.1
*/
public class Jsr223ScriptExecutor implements ScriptExecutor {
private static Log log = LogFactory.getLog(Jsr223ScriptExecutor.class);
private final ScriptEngine scriptEngine;
/**
* Set the engine name (language)
* @param language
*/
public Jsr223ScriptExecutor(String language) {
ScriptEngineManager manager = new ScriptEngineManager();
if (log.isDebugEnabled()){
for (ScriptEngineFactory factory: manager.getEngineFactories()) {
log.debug(factory.getNames());
}
}
scriptEngine = manager.getEngineByName(language);
Assert.notNull(scriptEngine,"JVM cannot create a script engine for name [" + language + "]");
}
/*
* (non-Javadoc)
*
* @see
* com.dturanski.test.jruby.ScriptExecutor#executeScript(org.springframework
* .scripting.ScriptSource)
*/
public Object executeScript(ScriptSource scriptSource) {
return this.executeScript(scriptSource,null);
}
/*
* (non-Javadoc)
*
* @see
* com.dturanski.test.jruby.ScriptExecutor#bindVariable(java.lang.String,
* java.lang.Object)
*/
private void bindVariable(String variableName, Object value) {
scriptEngine.put(variableName, value);
}
/* (non-Javadoc)
* @see org.springframework.integration.jsr233.ScriptExecutor#executeScript(org.springframework.scripting.ScriptSource, java.util.Map)
*/
public Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) {
Assert.notNull(scriptSource, "scriptSource must not be null");
synchronized (this) {
Object obj = null;
try {
scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE).clear();
if (variables != null ) {
for (Entry<String, Object> entry: variables.entrySet()){
bindVariable(entry.getKey(), entry.getValue());
}
}
obj = scriptEngine.eval(scriptSource.getScriptAsString());
}
catch (ScriptException e) {
throw new RuntimeException(e);
}
catch (IOException e) {
throw new RuntimeException(e);
}
return obj;
}
}
}

View File

@@ -0,0 +1 @@
http\://www.springframework.org/schema/integration/script=org.springframework.integration.scripting.config.jsr223.Jsr223NamespaceHandler

View File

@@ -0,0 +1,2 @@
http\://www.springframework.org/schema/integration/script-2.1.xsd=org/springframework/integration/script/config/spring-integration-script-2.1.xsd
http\://www.springframework.org/schema/integration/script.xsd=org/springframework/integration/script/config/spring-integration-script-2.1.xsd

View File

@@ -0,0 +1,4 @@
# Tooling related information for the integration script namespace
http\://www.springframework.org/schema/integration/script@name=integration script Namespace
http\://www.springframework.org/schema/integration/script@prefix=int-script
http\://www.springframework.org/schema/integration/script@icon=org/springframework/integration/script/config/spring-integration-script.gif

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.springframework.org/schema/integration/script"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.springframework.org/schema/integration/script"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:include
schemaLocation="http://www.springframework.org/schema/integration/scripting/spring-integration-scripting-core-2.1.xsd" />
<xsd:element name="script">
<xsd:annotation>
<xsd:documentation>
Configures an inner bean that will generate a
JSR 223 compliant script.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:complexType name="Jsr223Script">
<xsd:complexContent>
<xsd:extension base="ScriptType">
<xsd:attribute name="lang" use="required">
<xsd:annotation>
<xsd:documentation>
The script language or JSR 223 scripting engine name
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>