Search in sources :

Example 46 with Bindings

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

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

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

the class SlingBindingsVariablesListJsonServlet method getBindingsByEngine.

/**
     * Gets the {@link Bindings} object for the given {@link ScriptEngineFactory}.
     * It only considers the default context "request".
     *
     * @see <a href="https://issues.apache.org/jira/browse/SLING-3038">binding contexts(SLING-3083)</a>
     *
     * @param scriptEngineFactory the factory of the script engine, for which to retrieve the bindings
     * @param request the current request (necessary to create the bindings)
     * @param response the current response (necessary to create the bindings)
     * @return the bindings (list of key/value pairs) as defined by {@link Bindings} for the given script engine.
     * @throws IOException
     */
private Bindings getBindingsByEngine(ScriptEngineFactory scriptEngineFactory, SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
    // use default context only
    String context = SlingScriptAdapterFactory.BINDINGS_CONTEXT;
    final Collection<BindingsValuesProvider> bindingsValuesProviders = bindingsValuesProviderTracker.getBindingsValuesProviders(scriptEngineFactory, context);
    Resource invalidScriptResource = new NonExistingResource(request.getResourceResolver(), "some/invalid/scriptpath");
    DefaultSlingScript defaultSlingScript = new DefaultSlingScript(bundleContext, invalidScriptResource, scriptEngineFactory.getScriptEngine(), bindingsValuesProviders, null, null);
    // prepare the bindings (similar as in DefaultSlingScript#service)
    final SlingBindings initalBindings = new SlingBindings();
    initalBindings.setRequest(request);
    initalBindings.setResponse(response);
    final Bindings bindings = defaultSlingScript.verifySlingBindings(initalBindings);
    // only thing being added in {DefaultSlingScript#call(...)} is resource resolver
    bindings.put(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER, request.getResourceResolver());
    return bindings;
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings)

Example 49 with Bindings

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

the class BindingsValuesProvidersByContextIT method addBVP.

private void addBVP(final String id, String context, String engineName) {
    final BindingsValuesProvider bvp = new BindingsValuesProvider() {

        @Override
        public String toString() {
            return id;
        }

        @Override
        public void addBindings(Bindings b) {
        }
    };
    regs.add(bundleContext.registerService(BindingsValuesProvider.class.getName(), bvp, getProperties(context, engineName)));
}
Also used : BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) Bindings(javax.script.Bindings)

Example 50 with Bindings

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

the class BindingsValuesProvidersByContextIT method addBVPWithServiceRanking.

private void addBVPWithServiceRanking(final String id, String context, String engineName, int serviceRanking) {
    final BindingsValuesProvider bvp = new BindingsValuesProvider() {

        @Override
        public String toString() {
            return id;
        }

        @Override
        public void addBindings(Bindings b) {
        }
    };
    final Dictionary<String, Object> properties = getProperties(context, engineName);
    properties.put(Constants.SERVICE_RANKING, serviceRanking);
    regs.add(bundleContext.registerService(BindingsValuesProvider.class.getName(), bvp, properties));
}
Also used : BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) 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