INT-2177. Improved exception handling. Made ScriptExecutorFactory abstract
This commit is contained in:
committed by
Mark Fisher
parent
a0e7b3cd78
commit
eebb51f38a
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user