Search in sources :

Example 26 with ScriptException

use of javax.script.ScriptException in project jmeter by apache.

the class JSR223TestElement method processFileOrScript.

/**
     * This method will run inline script or file script with special behaviour for file script:
     * - If ScriptEngine implements Compilable script will be compiled and cached
     * - If not if will be run
     * @param scriptEngine ScriptEngine
     * @param bindings {@link Bindings} might be null
     * @return Object returned by script
     * @throws IOException when reading the script fails
     * @throws ScriptException when compiling or evaluation of the script fails
     */
protected Object processFileOrScript(ScriptEngine scriptEngine, Bindings bindings) throws IOException, ScriptException {
    if (bindings == null) {
        bindings = scriptEngine.createBindings();
    }
    populateBindings(bindings);
    File scriptFile = new File(getFilename());
    // Hack: bsh-2.0b5.jar BshScriptEngine implements Compilable but throws
    // "java.lang.Error: unimplemented"
    boolean supportsCompilable = scriptEngine instanceof Compilable && // NOSONAR // $NON-NLS-1$
    !("bsh.engine.BshScriptEngine".equals(scriptEngine.getClass().getName()));
    try {
        if (!StringUtils.isEmpty(getFilename())) {
            if (scriptFile.exists() && scriptFile.canRead()) {
                if (supportsCompilable) {
                    String cacheKey = // $NON-NLS-1$
                    getScriptLanguage() + "#" + scriptFile.getAbsolutePath() + // $NON-NLS-1$
                    "#" + scriptFile.lastModified();
                    CompiledScript compiledScript = compiledScriptsCache.get(cacheKey);
                    if (compiledScript == null) {
                        synchronized (compiledScriptsCache) {
                            compiledScript = compiledScriptsCache.get(cacheKey);
                            if (compiledScript == null) {
                                // TODO Charset ?
                                try (BufferedReader fileReader = new BufferedReader(new FileReader(scriptFile), (int) scriptFile.length())) {
                                    compiledScript = ((Compilable) scriptEngine).compile(fileReader);
                                    compiledScriptsCache.put(cacheKey, compiledScript);
                                }
                            }
                        }
                    }
                    return compiledScript.eval(bindings);
                } else {
                    // TODO Charset ?
                    try (BufferedReader fileReader = new BufferedReader(new FileReader(scriptFile), (int) scriptFile.length())) {
                        return scriptEngine.eval(fileReader, bindings);
                    }
                }
            } else {
                throw new ScriptException("Script file '" + scriptFile.getAbsolutePath() + "' does not exist or is unreadable for element:" + getName());
            }
        } else if (!StringUtils.isEmpty(getScript())) {
            if (supportsCompilable && !StringUtils.isEmpty(cacheKey)) {
                computeScriptMD5();
                CompiledScript compiledScript = compiledScriptsCache.get(this.scriptMd5);
                if (compiledScript == null) {
                    synchronized (compiledScriptsCache) {
                        compiledScript = compiledScriptsCache.get(this.scriptMd5);
                        if (compiledScript == null) {
                            compiledScript = ((Compilable) scriptEngine).compile(getScript());
                            compiledScriptsCache.put(this.scriptMd5, compiledScript);
                        }
                    }
                }
                return compiledScript.eval(bindings);
            } else {
                return scriptEngine.eval(getScript(), bindings);
            }
        } else {
            throw new ScriptException("Both script file and script text are empty for element:" + getName());
        }
    } catch (ScriptException ex) {
        Throwable rootCause = ex.getCause();
        if (isStopCondition(rootCause)) {
            throw (RuntimeException) ex.getCause();
        } else {
            throw ex;
        }
    }
}
Also used : CompiledScript(javax.script.CompiledScript) ScriptException(javax.script.ScriptException) Compilable(javax.script.Compilable) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 27 with ScriptException

use of javax.script.ScriptException in project jmeter by apache.

the class JSR223Assertion method getResult.

@Override
public AssertionResult getResult(SampleResult response) {
    AssertionResult result = new AssertionResult(getName());
    try {
        ScriptEngine scriptEngine = getScriptEngine();
        Bindings bindings = scriptEngine.createBindings();
        bindings.put("SampleResult", response);
        bindings.put("AssertionResult", result);
        processFileOrScript(scriptEngine, bindings);
        result.setError(false);
    } catch (IOException | ScriptException e) {
        log.error("Problem in JSR223 script: {}", getName(), e);
        result.setError(true);
        result.setFailureMessage(e.toString());
    }
    return result;
}
Also used : ScriptException(javax.script.ScriptException) IOException(java.io.IOException) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 28 with ScriptException

use of javax.script.ScriptException in project jmeter by apache.

the class JSR223Listener method sampleOccurred.

@Override
public void sampleOccurred(SampleEvent event) {
    try {
        ScriptEngine scriptEngine = getScriptEngine();
        Bindings bindings = scriptEngine.createBindings();
        bindings.put("sampleEvent", event);
        bindings.put("sampleResult", event.getResult());
        processFileOrScript(scriptEngine, bindings);
    } catch (ScriptException | IOException e) {
        log.error("Problem in JSR223 script, {}", getName(), e);
    }
}
Also used : ScriptException(javax.script.ScriptException) IOException(java.io.IOException) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 29 with ScriptException

use of javax.script.ScriptException in project sling by apache.

the class RhinoJavaScriptEngineTest method testPreserveScopeBetweenEvals.

public void testPreserveScopeBetweenEvals() throws ScriptException {
    MockRhinoJavaScriptEngineFactory factory = new MockRhinoJavaScriptEngineFactory();
    ScriptEngine engine = factory.getScriptEngine();
    Bindings context = new SimpleBindings();
    engine.eval("var f = 1", context);
    Object result = null;
    try {
        result = engine.eval("f += 1", context);
    } catch (ScriptException e) {
        TestCase.fail(e.getMessage());
    }
    assertTrue(result instanceof Double);
    assertEquals(2.0, result);
}
Also used : ScriptException(javax.script.ScriptException) SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 30 with ScriptException

use of javax.script.ScriptException in project sling by apache.

the class SightlyScriptEngine method eval.

@Override
public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
    checkArguments(reader, scriptContext);
    Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SlingBindings slingBindings = new SlingBindings();
    slingBindings.putAll(bindings);
    final SlingHttpServletRequest request = slingBindings.getRequest();
    if (request == null) {
        throw new SightlyException("Missing SlingHttpServletRequest from ScriptContext.");
    }
    final Object oldValue = request.getAttribute(SlingBindings.class.getName());
    try {
        request.setAttribute(SlingBindings.class.getName(), slingBindings);
        SightlyCompiledScript compiledScript = internalCompile(reader, scriptContext);
        return compiledScript.eval(scriptContext);
    } catch (Exception e) {
        throw new ScriptException(e);
    } finally {
        request.setAttribute(SlingBindings.class.getName(), oldValue);
    }
}
Also used : ScriptException(javax.script.ScriptException) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SightlyException(org.apache.sling.scripting.sightly.SightlyException) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ScriptException(javax.script.ScriptException) SightlyException(org.apache.sling.scripting.sightly.SightlyException)

Aggregations

ScriptException (javax.script.ScriptException)106 ScriptEngine (javax.script.ScriptEngine)42 IOException (java.io.IOException)41 Bindings (javax.script.Bindings)30 ScriptEngineManager (javax.script.ScriptEngineManager)20 InputStreamReader (java.io.InputStreamReader)12 CompiledScript (javax.script.CompiledScript)12 Map (java.util.Map)11 Invocable (javax.script.Invocable)11 ScriptContext (javax.script.ScriptContext)11 SimpleBindings (javax.script.SimpleBindings)10 File (java.io.File)8 Reader (java.io.Reader)7 Writer (java.io.Writer)7 HashMap (java.util.HashMap)7 PrintWriter (java.io.PrintWriter)6 ArrayList (java.util.ArrayList)6 SlingBindings (org.apache.sling.api.scripting.SlingBindings)6 MissingMethodException (groovy.lang.MissingMethodException)5 MissingPropertyException (groovy.lang.MissingPropertyException)5