Search in sources :

Example 41 with Bindings

use of javax.script.Bindings in project incubator-atlas by apache.

the class Titan0Graph method executeGremlinScript.

private Object executeGremlinScript(String gremlinQuery) throws AtlasBaseException {
    Object result = null;
    ScriptEngine engine = getGremlinScriptEngine();
    try {
        Bindings bindings = engine.createBindings();
        bindings.put("g", getGraph());
        result = engine.eval(gremlinQuery, bindings);
    } catch (ScriptException e) {
        throw new AtlasBaseException(AtlasErrorCode.GREMLIN_SCRIPT_EXECUTION_FAILED, gremlinQuery);
    } finally {
        releaseGremlinScriptEngine(engine);
    }
    return result;
}
Also used : ScriptException(javax.script.ScriptException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 42 with Bindings

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

the class JSR223Sampler method sample.

@Override
public SampleResult sample(Entry entry) {
    SampleResult result = new SampleResult();
    result.setSampleLabel(getName());
    result.setSuccessful(true);
    result.setResponseCodeOK();
    result.setResponseMessageOK();
    final String filename = getFilename();
    if (filename.length() > 0) {
        result.setSamplerData("File: " + filename);
    } else {
        result.setSamplerData(getScript());
    }
    result.setDataType(SampleResult.TEXT);
    result.sampleStart();
    try {
        ScriptEngine scriptEngine = getScriptEngine();
        Bindings bindings = scriptEngine.createBindings();
        bindings.put("SampleResult", result);
        Object ret = processFileOrScript(scriptEngine, bindings);
        if (ret != null && (result.getResponseData() == null || result.getResponseData().length == 0)) {
            result.setResponseData(ret.toString(), null);
        }
    } catch (IOException | ScriptException e) {
        log.error("Problem in JSR223 script {}, message: {}", getName(), e, e);
        result.setSuccessful(false);
        // $NON-NLS-1$
        result.setResponseCode("500");
        result.setResponseMessage(e.toString());
    }
    result.sampleEnd();
    return result;
}
Also used : ScriptException(javax.script.ScriptException) SampleResult(org.apache.jmeter.samplers.SampleResult) IOException(java.io.IOException) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 43 with Bindings

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

the class Groovy method execute.

/** {@inheritDoc} */
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    Bindings bindings = scriptEngine.createBindings();
    populateBindings(bindings);
    String script = ((CompoundVariable) values[0]).execute();
    //$NON-NLS-1$
    String varName = "";
    if (values.length > 1) {
        varName = ((CompoundVariable) values[1]).execute().trim();
    }
    //$NON-NLS-1$
    String resultStr = "";
    try {
        // Pass in some variables
        if (currentSampler != null) {
            // $NON-NLS-1$ 
            bindings.put("sampler", currentSampler);
        }
        if (previousResult != null) {
            //$NON-NLS-1$
            bindings.put("prev", previousResult);
        }
        // $NON-NLS-1$ (this name is fixed)
        bindings.put("log", log);
        // Add variables for access to context and variables
        bindings.put("threadName", Thread.currentThread().getName());
        JMeterContext jmctx = JMeterContextService.getContext();
        // $NON-NLS-1$ (this name is fixed)
        bindings.put("ctx", jmctx);
        JMeterVariables vars = jmctx.getVariables();
        // $NON-NLS-1$ (this name is fixed)
        bindings.put("vars", vars);
        Properties props = JMeterUtils.getJMeterProperties();
        // $NON-NLS-1$ (this name is fixed)
        bindings.put("props", props);
        // For use in debugging:
        // $NON-NLS-1$ (this name is fixed)
        bindings.put("OUT", System.out);
        // Execute the script
        Object out = scriptEngine.eval(script, bindings);
        if (out != null) {
            resultStr = out.toString();
        }
        if (varName.length() > 0) {
            // vars will be null on TestPlan
            if (vars != null) {
                vars.put(varName, resultStr);
            }
        }
    } catch (// Mainly for bsh.EvalError
    Exception ex) {
        log.warn("Error running groovy script", ex);
    }
    if (log.isDebugEnabled()) {
        log.debug("__groovy(" + script + "," + varName + ")=" + resultStr);
    }
    return resultStr;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) Properties(java.util.Properties) Bindings(javax.script.Bindings)

Example 44 with Bindings

use of javax.script.Bindings 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 45 with Bindings

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

the class Routes method getPattern.

/**
     * Returns the pattern.
     * @param event The log event passed to the script (if there is a script.)
     * @param scriptStaticVariables The script's static variables.
     * @return the pattern.
     */
public String getPattern(final LogEvent event, final ConcurrentMap<Object, Object> scriptStaticVariables) {
    if (patternScript != null) {
        final ScriptManager scriptManager = configuration.getScriptManager();
        final Bindings bindings = scriptManager.createBindings(patternScript);
        bindings.put(STATIC_VARIABLES_KEY, scriptStaticVariables);
        bindings.put(LOG_EVENT_KEY, event);
        final Object object = scriptManager.execute(patternScript.getName(), bindings);
        bindings.remove(LOG_EVENT_KEY);
        return Objects.toString(object, null);
    }
    return pattern;
}
Also used : ScriptManager(org.apache.logging.log4j.core.script.ScriptManager) Bindings(javax.script.Bindings)

Aggregations

Bindings (javax.script.Bindings)150 SimpleBindings (javax.script.SimpleBindings)77 Test (org.junit.Test)36 ScriptException (javax.script.ScriptException)30 ScriptContext (javax.script.ScriptContext)26 SimpleScriptContext (javax.script.SimpleScriptContext)24 ScriptEngine (javax.script.ScriptEngine)20 SlingBindings (org.apache.sling.api.scripting.SlingBindings)18 Test (org.testng.annotations.Test)17 CompiledScript (javax.script.CompiledScript)14 Resource (org.apache.sling.api.resource.Resource)11 Map (java.util.Map)10 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)10 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)10 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 ScriptEngineManager (javax.script.ScriptEngineManager)9 PrintWriter (java.io.PrintWriter)8 StringWriter (java.io.StringWriter)8 ArrayList (java.util.ArrayList)7