Search in sources :

Example 56 with ScriptEngine

use of javax.script.ScriptEngine in project logging-log4j2 by apache.

the class ScriptManager method addScript.

public void addScript(final AbstractScript script) {
    final ScriptEngine engine = manager.getEngineByName(script.getLanguage());
    if (engine == null) {
        logger.error("No ScriptEngine found for language " + script.getLanguage() + ". Available languages are: " + languages);
        return;
    }
    if (engine.getFactory().getParameter(KEY_THREADING) == null) {
        scriptRunners.put(script.getName(), new ThreadLocalScriptRunner(script));
    } else {
        scriptRunners.put(script.getName(), new MainScriptRunner(engine, script));
    }
    if (script instanceof ScriptFile) {
        final ScriptFile scriptFile = (ScriptFile) script;
        final Path path = scriptFile.getPath();
        if (scriptFile.isWatched() && path != null) {
            watchManager.watchFile(path.toFile(), this);
        }
    }
}
Also used : Path(java.nio.file.Path) ScriptEngine(javax.script.ScriptEngine)

Example 57 with ScriptEngine

use of javax.script.ScriptEngine in project logging-log4j2 by apache.

the class ScriptManager method fileModified.

@Override
public void fileModified(final File file) {
    final ScriptRunner runner = scriptRunners.get(file.toString());
    if (runner == null) {
        logger.info("{} is not a running script");
        return;
    }
    final ScriptEngine engine = runner.getScriptEngine();
    final AbstractScript script = runner.getScript();
    if (engine.getFactory().getParameter(KEY_THREADING) == null) {
        scriptRunners.put(script.getName(), new ThreadLocalScriptRunner(script));
    } else {
        scriptRunners.put(script.getName(), new MainScriptRunner(engine, script));
    }
}
Also used : ScriptEngine(javax.script.ScriptEngine)

Example 58 with ScriptEngine

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

the class JSR223PostProcessor method process.

@Override
public void process() {
    try {
        ScriptEngine scriptEngine = getScriptEngine();
        processFileOrScript(scriptEngine, null);
    } catch (ScriptException | IOException e) {
        log.error("Problem in JSR223 script, {}", getName(), e);
    }
}
Also used : ScriptException(javax.script.ScriptException) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine)

Example 59 with ScriptEngine

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

the class JavaScript method executeWithNashorn.

/**
     * 
     * @param previousResult {@link SampleResult}
     * @param currentSampler {@link Sampler}
     * @param jmctx {@link JMeterContext}
     * @param vars {@link JMeterVariables}
     * @param script Javascript code
     * @param varName variable name
     * @return result as String
     * @throws InvalidVariableException
     */
private String executeWithNashorn(SampleResult previousResult, Sampler currentSampler, JMeterContext jmctx, JMeterVariables vars, String script, String varName) throws InvalidVariableException {
    String resultStr = null;
    try {
        ScriptContext newContext = new SimpleScriptContext();
        ScriptEngine engine = getInstance().getEngineByName(JavaScript.NASHORN_ENGINE_NAME);
        Bindings bindings = engine.createBindings();
        // Set up some objects for the script to play with
        //$NON-NLS-1$
        bindings.put("log", log);
        //$NON-NLS-1$
        bindings.put("ctx", jmctx);
        //$NON-NLS-1$
        bindings.put("vars", vars);
        //$NON-NLS-1$
        bindings.put("props", JMeterUtils.getJMeterProperties());
        //$NON-NLS-1$
        bindings.put("threadName", Thread.currentThread().getName());
        //$NON-NLS-1$
        bindings.put("sampler", currentSampler);
        //$NON-NLS-1$
        bindings.put("sampleResult", previousResult);
        newContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        Object result = engine.eval(script, newContext);
        resultStr = result.toString();
        if (varName != null && vars != null) {
            // vars can be null if run from TestPlan
            vars.put(varName, resultStr);
        }
    } catch (Exception e) {
        log.error("Error processing Javascript: [" + script + "]\n", e);
        throw new InvalidVariableException("Error processing Javascript: [" + script + "]", e);
    }
    return resultStr;
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine) RhinoException(org.mozilla.javascript.RhinoException)

Example 60 with ScriptEngine

use of javax.script.ScriptEngine 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)

Aggregations

ScriptEngine (javax.script.ScriptEngine)175 ScriptEngineManager (javax.script.ScriptEngineManager)70 ScriptException (javax.script.ScriptException)46 Test (org.junit.Test)29 Bindings (javax.script.Bindings)20 IOException (java.io.IOException)19 Map (java.util.Map)14 Invocable (javax.script.Invocable)13 File (java.io.File)11 FileReader (java.io.FileReader)11 ScriptEngineFactory (javax.script.ScriptEngineFactory)11 HashMap (java.util.HashMap)10 ScriptContext (javax.script.ScriptContext)10 SimpleBindings (javax.script.SimpleBindings)10 InputStreamReader (java.io.InputStreamReader)9 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)9 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)9 PrintWriter (java.io.PrintWriter)7 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)7 JRubyTest (org.enumerable.lambda.support.jruby.JRubyTest)7