Search in sources :

Example 1 with ScriptCompilationException

use of org.springframework.scripting.ScriptCompilationException 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 ScriptCompilationException

use of org.springframework.scripting.ScriptCompilationException 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 ScriptCompilationException

use of org.springframework.scripting.ScriptCompilationException 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 ScriptCompilationException

use of org.springframework.scripting.ScriptCompilationException in project spring-framework by spring-projects.

the class GroovyScriptFactory method getScriptedObjectType.

@Override
public Class<?> getScriptedObjectType(ScriptSource scriptSource) throws IOException, ScriptCompilationException {
    synchronized (this.scriptClassMonitor) {
        try {
            if (this.scriptClass == null || scriptSource.isModified()) {
                // New script content...
                this.wasModifiedForTypeCheck = true;
                this.scriptClass = getGroovyClassLoader().parseClass(scriptSource.getScriptAsString(), scriptSource.suggestedClassName());
                if (Script.class.isAssignableFrom(this.scriptClass)) {
                    // A Groovy script, probably creating an instance: let's execute it.
                    Object result = executeScript(scriptSource, this.scriptClass);
                    this.scriptResultClass = (result != null ? result.getClass() : null);
                    this.cachedResult = new CachedResultHolder(result);
                } else {
                    this.scriptResultClass = this.scriptClass;
                }
            }
            return this.scriptResultClass;
        } catch (CompilationFailedException ex) {
            this.scriptClass = null;
            this.scriptResultClass = null;
            this.cachedResult = null;
            throw new ScriptCompilationException(scriptSource, ex);
        }
    }
}
Also used : GroovyObject(groovy.lang.GroovyObject) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) ScriptCompilationException(org.springframework.scripting.ScriptCompilationException)

Example 5 with ScriptCompilationException

use of org.springframework.scripting.ScriptCompilationException in project spring-framework by spring-projects.

the class GroovyScriptFactory method getScriptedObject.

/**
	 * Loads and parses the Groovy script via the GroovyClassLoader.
	 * @see groovy.lang.GroovyClassLoader
	 */
@Override
public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces) throws IOException, ScriptCompilationException {
    synchronized (this.scriptClassMonitor) {
        try {
            Class<?> scriptClassToExecute;
            this.wasModifiedForTypeCheck = false;
            if (this.cachedResult != null) {
                Object result = this.cachedResult.object;
                this.cachedResult = null;
                return result;
            }
            if (this.scriptClass == null || scriptSource.isModified()) {
                // New script content...
                this.scriptClass = getGroovyClassLoader().parseClass(scriptSource.getScriptAsString(), scriptSource.suggestedClassName());
                if (Script.class.isAssignableFrom(this.scriptClass)) {
                    // A Groovy script, probably creating an instance: let's execute it.
                    Object result = executeScript(scriptSource, this.scriptClass);
                    this.scriptResultClass = (result != null ? result.getClass() : null);
                    return result;
                } else {
                    this.scriptResultClass = this.scriptClass;
                }
            }
            scriptClassToExecute = this.scriptClass;
            // Process re-execution outside of the synchronized block.
            return executeScript(scriptSource, scriptClassToExecute);
        } catch (CompilationFailedException ex) {
            this.scriptClass = null;
            this.scriptResultClass = null;
            throw new ScriptCompilationException(scriptSource, ex);
        }
    }
}
Also used : GroovyObject(groovy.lang.GroovyObject) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) ScriptCompilationException(org.springframework.scripting.ScriptCompilationException)

Aggregations

ScriptCompilationException (org.springframework.scripting.ScriptCompilationException)7 EvalError (bsh.EvalError)3 GroovyObject (groovy.lang.GroovyObject)2 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)2 Interpreter (bsh.Interpreter)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Map (java.util.Map)1 INodeGroupExtensionPoint (org.jumpmind.symmetric.ext.INodeGroupExtensionPoint)1 Test (org.junit.Test)1 ScriptSource (org.springframework.scripting.ScriptSource)1