Search in sources :

Example 1 with EvalError

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);
    }
}
Also used : Interpreter(bsh.Interpreter) StringReader(java.io.StringReader) EvalError(bsh.EvalError) IOException(java.io.IOException) ScriptCompilationException(org.springframework.scripting.ScriptCompilationException) Map(java.util.Map)

Example 2 with EvalError

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);
        }
    }
}
Also used : EvalError(bsh.EvalError) ScriptCompilationException(org.springframework.scripting.ScriptCompilationException)

Example 3 with EvalError

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);
        }
    }
}
Also used : EvalError(bsh.EvalError) ScriptCompilationException(org.springframework.scripting.ScriptCompilationException)

Example 4 with EvalError

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;
}
Also used : Interpreter(bsh.Interpreter) HashMap(java.util.HashMap) EvalError(bsh.EvalError)

Example 5 with EvalError

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;
}
Also used : Interpreter(bsh.Interpreter) EvalError(bsh.EvalError)

Aggregations

EvalError (bsh.EvalError)12 Interpreter (bsh.Interpreter)9 TargetError (bsh.TargetError)4 ScriptCompilationException (org.springframework.scripting.ScriptCompilationException)3 Map (java.util.Map)2 BshClassManager (bsh.BshClassManager)1 NameSpace (bsh.NameSpace)1 ParseException (bsh.ParseException)1 File (java.io.File)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 TreeSet (java.util.TreeSet)1 IoException (org.jumpmind.exception.IoException)1 IExtensionPoint (org.jumpmind.extension.IExtensionPoint)1 SymmetricException (org.jumpmind.symmetric.SymmetricException)1