Package javax.script

Examples of javax.script.SimpleBindings


            Bindings scriptBindings = this.bindings;

            if(scriptBindings == null)
            {
                scriptBindings = new SimpleBindings(this.arguments);
            }

            return (T)scriptEngine.eval(this.script, scriptBindings);
        }
        catch (ScriptException e)
View Full Code Here


        }
    }

    public <T> T eval(String script, Map<String, Object> arguments, Class<T> returnType)
    {
        return eval(script, new SimpleBindings(arguments), returnType);
    }
View Full Code Here

        CompiledScript script = calloutCache_.get(callout);
        if ( script != null )
        {
            try
            {
                Bindings binding = new SimpleBindings();
                binding.put("args", args);
                result = script.eval(binding);
            }
            catch(ScriptException ex)
            {
                logger_.warn(LogUtil.throwableToString(ex));
View Full Code Here

    }

    private Bindings verifySlingBindings(String scriptName,
            SlingBindings slingBindings) throws IOException {

      Bindings bindings = new SimpleBindings();

        final SlingHttpServletRequest request = slingBindings.getRequest();

        // check sling object
        Object slingObject = slingBindings.get(SLING);
        if (slingObject == null) {

            if ( request != null ) {
                slingObject = new ScriptHelper(this.bundleContext, this, request, slingBindings.getResponse());
            } else {
                slingObject = new ScriptHelper(this.bundleContext, this);
            }
        } else if (!(slingObject instanceof SlingScriptHelper) ) {
            throw fail(scriptName, SLING, "Wrong type");
        }
        final SlingScriptHelper sling = (SlingScriptHelper)slingObject;
        bindings.put(SLING, sling);

        if (request != null) {
            //throw fail(scriptName, REQUEST, "Missing or wrong type");

          SlingHttpServletResponse response = slingBindings.getResponse();
            if (response == null) {
                throw fail(scriptName, RESPONSE, "Missing or wrong type");
            }

            Object resourceObject = slingBindings.get(RESOURCE);
            if (resourceObject != null && !(resourceObject instanceof Resource)) {
                throw fail(scriptName, RESOURCE, "Wrong type");
            }

            Object writerObject = slingBindings.get(OUT);
            if (writerObject != null && !(writerObject instanceof PrintWriter)) {
                throw fail(scriptName, OUT, "Wrong type");
            }

            // if there is a provided sling script helper, check arguments
            if (slingBindings.get(SLING) != null) {

                if (sling.getRequest() != request) {
                    throw fail(scriptName, REQUEST,
                        "Not the same as request field of SlingScriptHelper");
                }

                if (sling.getResponse() != response) {
                    throw fail(scriptName, RESPONSE,
                        "Not the same as response field of SlingScriptHelper");
                }

                if (resourceObject != null
                    && sling.getRequest().getResource() != resourceObject) {
                    throw fail(scriptName, RESOURCE,
                        "Not the same as resource of the SlingScriptHelper request");
                }

                if (writerObject != null
                    && sling.getResponse().getWriter() != writerObject) {
                    throw fail(scriptName, OUT,
                        "Not the same as writer of the SlingScriptHelper response");
                }
            }

            // set base variables when executing inside a request
            bindings.put(REQUEST, sling.getRequest());
            bindings.put(READER, sling.getRequest().getReader());
            bindings.put(RESPONSE, sling.getResponse());
            bindings.put(RESOURCE, sling.getRequest().getResource());
            bindings.put(OUT, sling.getResponse().getWriter());

            // set the current node if the resource is node based
            Node node = sling.getRequest().getResource().adaptTo(Node.class);
            if (node != null) {
                bindings.put(NODE, node);
            }
        }

        Object logObject = slingBindings.get(LOG);
        if (logObject == null) {
            logObject = LoggerFactory.getLogger(getLoggerName());
        } else if (!(logObject instanceof Logger)) {
            throw fail(scriptName, LOG, "Wrong type");
        }
        bindings.put(LOG, logObject);

        // copy non-base variables
        for (Map.Entry<String, Object> entry : slingBindings.entrySet()) {
            if (!bindings.containsKey(entry.getKey())) {
                bindings.put(entry.getKey(), entry.getValue());
            }
        }

        return bindings;
    }
View Full Code Here

    public Object eval(String javascriptCode, Map<String, Object> data,
            final StringWriter sw) throws ScriptException {
        final PrintWriter pw = new PrintWriter(sw, true);
        ScriptContext ctx = new SimpleScriptContext();

        final Bindings b = new SimpleBindings();
        b.put("out", pw);
        if (data != null) {
            for (Map.Entry<String, Object> e : data.entrySet()) {
                b.put(e.getKey(), e.getValue());
            }
        }

        ctx.setBindings(b, ScriptContext.ENGINE_SCOPE);
        ctx.setWriter(sw);
View Full Code Here

    protected AbstractSlingScriptEngine(ScriptEngineFactory scriptEngineFactory) {
        this.scriptEngineFactory = scriptEngineFactory;
    }

    public Bindings createBindings() {
        return new SimpleBindings();
    }
View Full Code Here

        }
        return factory;
    }

    public Bindings createBindings() {
        return new SimpleBindings();
    }
View Full Code Here

                MessageFormat.format("{0} exec script", getRobotName()),
                output != null ? output : "Okay");
    }

    public static String executeScriptWithConsole(String scriptContent, boolean force) {
        Bindings bindings = new SimpleBindings();
        final JScriptConsole jScriptConsole = new JScriptConsole();
        bindings.put("console", jScriptConsole);
        String result = JscriptExecutor.executeScript(scriptContent, bindings, force);
        if (result != null){
            jScriptConsole.log(String.format("\n%s", result));
        }
        return jScriptConsole.out();
View Full Code Here

public class JscriptExecutorTest {

    @Test
    public void testExecScript() throws Exception {

        Bindings bindings = new SimpleBindings();
        final JScriptConsole jScriptConsole = new JScriptConsole();
        bindings.put("console", jScriptConsole);

        JscriptExecutor.execScript("var a = 42 + 'ggg'; console.log(a);", bindings);
        Assert.assertEquals("42ggg", jScriptConsole.out());
    }
View Full Code Here

            script = interpreteScript(script);

            Bindings bindings = null;
            if(arguments != null)
            {
                bindings = new SimpleBindings(arguments);
            }

            if(bindings != null)
            {
                return getCurrentScriptEngineManager().getEngineByName(language).eval(script, bindings);
View Full Code Here

TOP

Related Classes of javax.script.SimpleBindings

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.