use of bsh.EvalError in project spring-framework by spring-projects.
the class BshScriptEvaluator method evaluate.
@Override
public Object evaluate(ScriptSource script, Map<String, Object> arguments) {
try {
Interpreter interpreter = new Interpreter();
interpreter.setClassLoader(this.classLoader);
if (arguments != null) {
for (Map.Entry<String, Object> entry : arguments.entrySet()) {
interpreter.set(entry.getKey(), entry.getValue());
}
}
return interpreter.eval(new StringReader(script.getScriptAsString()));
} catch (IOException ex) {
throw new ScriptCompilationException(script, "Cannot access BeanShell script", ex);
} catch (EvalError ex) {
throw new ScriptCompilationException(script, ex);
}
}
use of bsh.EvalError in project spring-framework by spring-projects.
the class BshScriptFactory method getScriptedObject.
/**
* Load and parse the BeanShell script via {@link BshScriptUtils}.
* @see BshScriptUtils#createBshObject(String, Class[], ClassLoader)
*/
@Override
public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces) throws IOException, ScriptCompilationException {
Class<?> clazz;
try {
synchronized (this.scriptClassMonitor) {
boolean requiresScriptEvaluation = (this.wasModifiedForTypeCheck && this.scriptClass == null);
this.wasModifiedForTypeCheck = false;
if (scriptSource.isModified() || requiresScriptEvaluation) {
// New script content: Let's check whether it evaluates to a Class.
Object result = BshScriptUtils.evaluateBshScript(scriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader);
if (result instanceof Class) {
// A Class: We'll cache the Class here and create an instance
// outside of the synchronized block.
this.scriptClass = (Class<?>) result;
} else {
// already evaluated object.
return result;
}
}
clazz = this.scriptClass;
}
} catch (EvalError ex) {
this.scriptClass = null;
throw new ScriptCompilationException(scriptSource, ex);
}
if (clazz != null) {
// A Class: We need to create an instance for every call.
try {
return ReflectionUtils.accessibleConstructor(clazz).newInstance();
} catch (Throwable ex) {
throw new ScriptCompilationException(scriptSource, "Could not instantiate script class: " + clazz.getName(), ex);
}
} else {
// Not a Class: We need to evaluate the script for every call.
try {
return BshScriptUtils.createBshObject(scriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader);
} catch (EvalError ex) {
throw new ScriptCompilationException(scriptSource, ex);
}
}
}
use of bsh.EvalError in project spring-framework by spring-projects.
the class BshScriptFactory method getScriptedObjectType.
@Override
public Class<?> getScriptedObjectType(ScriptSource scriptSource) throws IOException, ScriptCompilationException {
synchronized (this.scriptClassMonitor) {
try {
if (scriptSource.isModified()) {
// New script content: Let's check whether it evaluates to a Class.
this.wasModifiedForTypeCheck = true;
this.scriptClass = BshScriptUtils.determineBshObjectType(scriptSource.getScriptAsString(), this.beanClassLoader);
}
return this.scriptClass;
} catch (EvalError ex) {
this.scriptClass = null;
throw new ScriptCompilationException(scriptSource, ex);
}
}
}
use of bsh.EvalError in project symmetric-ds by JumpMind.
the class AbstractDatabaseWriter method script.
protected boolean script(CsvData data) {
try {
statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
String script = data.getParsedData(CsvData.ROW_DATA)[0];
Map<String, Object> variables = new HashMap<String, Object>();
bindVariables(variables);
Interpreter interpreter = new Interpreter();
if (variables != null) {
for (String variableName : variables.keySet()) {
interpreter.set(variableName, variables.get(variableName));
}
}
if (log.isDebugEnabled()) {
log.debug("About to run: {}", script);
}
interpreter.eval(script);
statistics.get(batch).increment(DataWriterStatisticConstants.SCRIPTCOUNT);
} catch (EvalError e) {
throw new RuntimeException(e);
}
return true;
}
use of bsh.EvalError in project symmetric-ds by JumpMind.
the class AbstractParameterService method substituteScripts.
protected String substituteScripts(String value) {
if (log.isDebugEnabled()) {
log.debug("substituteScripts starting value is: {}", value);
}
int startTick = StringUtils.indexOf(value, '`');
if (startTick != -1) {
int endTick = StringUtils.lastIndexOf(value, '`');
if (endTick != -1 && startTick != endTick) {
// there's a bean shell script present in this case
String script = StringUtils.substring(value, startTick + 1, endTick);
if (log.isDebugEnabled()) {
log.debug("Script found. Script is is: {}", script);
}
Interpreter interpreter = new Interpreter();
try {
interpreter.set("hostName", AppUtils.getHostName());
interpreter.set("log", log);
interpreter.set("nodeGroupId", nodeGroupId);
interpreter.set("syncUrl", syncUrl);
interpreter.set("registrationUrl", registrationUrl);
interpreter.set("externalId", externalId);
interpreter.set("engineName", engineName);
Object scriptResult = interpreter.eval(script);
if (scriptResult == null) {
scriptResult = "";
}
if (log.isDebugEnabled()) {
log.debug("Script output is: {}", scriptResult);
}
value = StringUtils.substring(value, 0, startTick) + scriptResult.toString() + StringUtils.substring(value, endTick + 1);
} catch (EvalError e) {
throw new RuntimeException(e.getMessage(), e);
}
if (log.isDebugEnabled()) {
log.debug("substituteScripts return value is {}", value);
}
}
}
return value;
}
Aggregations