INT-2177. Improved exception handling. Made ScriptExecutorFactory abstract
This commit is contained in:
committed by
Mark Fisher
parent
a0e7b3cd78
commit
eebb51f38a
@@ -1,167 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-scripting</artifactId>
|
||||
<version>2.1.0.BUILD-SNAPSHOT</version>
|
||||
<name>Spring Integration Scripting Support</name>
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The Apache Software License, Version 2.0</name>
|
||||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
|
||||
<distribution>repo</distribution>
|
||||
</license>
|
||||
</licenses>
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/java</directory>
|
||||
<includes>
|
||||
<include>**/*</include>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>**/*.java</exclude>
|
||||
</excludes>
|
||||
</resource>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<include>**/*</include>
|
||||
</includes>
|
||||
</resource>
|
||||
</resources>
|
||||
<testResources>
|
||||
<testResource>
|
||||
<directory>src/test/java</directory>
|
||||
<includes>
|
||||
<include>**/*</include>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>**/*.java</exclude>
|
||||
</excludes>
|
||||
</testResource>
|
||||
<testResource>
|
||||
<directory>src/test/resources</directory>
|
||||
<includes>
|
||||
<include>**/*</include>
|
||||
</includes>
|
||||
</testResource>
|
||||
</testResources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.5</source>
|
||||
<target>1.5</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>**/*Tests.java</include>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>**/*Abstract*.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>SpringSource External Bundle Repository</id>
|
||||
<url>http://repository.springsource.com/maven/bundles/external/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>SpringSource Milestone Repository</id>
|
||||
<url>http://maven.springframework.org/milestone/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>SpringSource Release Bundle Repository</id>
|
||||
<url>http://repository.springsource.com/maven/bundles/release/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>SpringSource Release Repository</id>
|
||||
<url>http://maven.springframework.org/release/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.easymock</groupId>
|
||||
<artifactId>easymock</artifactId>
|
||||
<version>2.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-core</artifactId>
|
||||
<version>2.1.0.BUILD-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-test</artifactId>
|
||||
<version>2.1.0.BUILD-SNAPSHOT</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.easymock</groupId>
|
||||
<artifactId>easymockclassextension</artifactId>
|
||||
<version>2.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-all</artifactId>
|
||||
<version>1.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>3.0.6.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-all</artifactId>
|
||||
<version>1.8.4</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<version>1.7.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cglib</groupId>
|
||||
<artifactId>cglib-nodep</artifactId>
|
||||
<version>2.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit-dep</artifactId>
|
||||
<version>4.8.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jruby</groupId>
|
||||
<artifactId>jruby</artifactId>
|
||||
<version>1.6.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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 org.springframework.integration.MessagingException;
|
||||
|
||||
/**
|
||||
* @author David Turanski
|
||||
* @since 2.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
|
||||
public class ScriptingException extends MessagingException {
|
||||
|
||||
public ScriptingException(String description) {
|
||||
super(description);
|
||||
}
|
||||
|
||||
|
||||
public ScriptingException(String description, Throwable cause) {
|
||||
super(description, cause);
|
||||
}
|
||||
}
|
||||
@@ -12,18 +12,17 @@
|
||||
*/
|
||||
package org.springframework.integration.scripting.jsr223;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.integration.scripting.ScriptExecutor;
|
||||
import org.springframework.integration.scripting.ScriptingException;
|
||||
import org.springframework.scripting.ScriptSource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -46,14 +45,16 @@ abstract class AbstractScriptExecutor implements ScriptExecutor {
|
||||
Assert.hasText(language, "language must not be empty");
|
||||
this.language = language;
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("using script engine : " + scriptEngineManager.getEngineByName(language).getFactory().getEngineName());
|
||||
for (String name:scriptEngineManager.getEngineByName(language).getFactory().getNames()){
|
||||
logger.debug("name=" + name);
|
||||
}
|
||||
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(this.language);
|
||||
if (scriptEngine == null) {
|
||||
logger.error(invlalidLanguageMessage(this.language));
|
||||
}
|
||||
else {
|
||||
logger.debug("using script engine : " + scriptEngine.getFactory().getEngineName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Object executeScript(ScriptSource scriptSource) {
|
||||
return this.executeScript(scriptSource, null);
|
||||
}
|
||||
@@ -61,6 +62,11 @@ abstract class AbstractScriptExecutor implements ScriptExecutor {
|
||||
public Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) {
|
||||
Object result = null;
|
||||
ScriptEngine scriptEngine = this.scriptEngineManager.getEngineByName(this.language);
|
||||
|
||||
if (scriptEngine == null) {
|
||||
throw new ScriptingException(invlalidLanguageMessage(this.language));
|
||||
}
|
||||
|
||||
try {
|
||||
if (variables != null) {
|
||||
for (Entry<String, Object> entry : variables.entrySet()) {
|
||||
@@ -72,25 +78,23 @@ abstract class AbstractScriptExecutor implements ScriptExecutor {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("executing script: " + script);
|
||||
}
|
||||
|
||||
|
||||
result = scriptEngine.eval(script);
|
||||
|
||||
|
||||
result = postProcess(result, scriptEngine, script);
|
||||
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("script executed in " + (new Date().getTime() - start.getTime()) + " ms");
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
catch (ScriptException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
||||
catch (Exception e) {
|
||||
throw new ScriptingException(e.getMessage(), e);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Subclasses may implement this to provide any special handling required
|
||||
* @param result
|
||||
@@ -100,4 +104,10 @@ abstract class AbstractScriptExecutor implements ScriptExecutor {
|
||||
*/
|
||||
protected abstract Object postProcess(Object result, ScriptEngine scriptEngine, String script);
|
||||
|
||||
private static String invlalidLanguageMessage(String language) {
|
||||
return new StringBuilder().append(ScriptEngineManager.class.getName())
|
||||
.append(" is unable to create a script engine for language '").append(language).append("'.\n")
|
||||
.append("This may be due to a missing language implementation or an invalid language name.").toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,9 +18,8 @@ import org.springframework.integration.scripting.ScriptExecutor;
|
||||
* @author David Turanski
|
||||
* @since 2.1
|
||||
*/
|
||||
public class ScriptExecutorFactory {
|
||||
public abstract class ScriptExecutorFactory {
|
||||
|
||||
private ScriptExecutorFactory(){};
|
||||
public static ScriptExecutor getScriptExecutor(String language) {
|
||||
if (language.equalsIgnoreCase("python") || language.equalsIgnoreCase("jython")){
|
||||
return new PythonScriptExecutor();
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
package org.springframework.integration.scripting.jsr223;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -20,7 +21,7 @@ import java.util.Map;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.integration.scripting.ScriptExecutor;
|
||||
import org.springframework.integration.scripting.jsr223.DefaultScriptExecutor;
|
||||
import org.springframework.integration.scripting.ScriptingException;
|
||||
import org.springframework.scripting.support.ResourceScriptSource;
|
||||
import org.springframework.scripting.support.StaticScriptSource;
|
||||
|
||||
@@ -59,7 +60,8 @@ public class Jsr223ScriptExecutorTests {
|
||||
assertEquals("js",obj.toString());
|
||||
}
|
||||
|
||||
@Test public void testPython() {
|
||||
@Test
|
||||
public void testPython() {
|
||||
ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("python");
|
||||
Object obj = executor.executeScript(new StaticScriptSource("x=2") );
|
||||
assertEquals(2,obj);
|
||||
@@ -67,4 +69,15 @@ public class Jsr223ScriptExecutorTests {
|
||||
obj = executor.executeScript(new StaticScriptSource("def foo(y):\n\tx=y\n\treturn y\nz=foo(2)") );
|
||||
assertEquals(2,obj);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidLanguageThrowsScriptingException() {
|
||||
try {
|
||||
ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("foo");
|
||||
executor.executeScript(new StaticScriptSource("x=2"));
|
||||
fail("should throw Exception");
|
||||
} catch (ScriptingException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ public class PythonScriptExecutorTests {
|
||||
public void init() {
|
||||
executor = new PythonScriptExecutor();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLiteral() {
|
||||
Object obj = executor.executeScript(new StaticScriptSource("3+4") );
|
||||
|
||||
Reference in New Issue
Block a user