Package javax.script

Examples of javax.script.SimpleBindings


            protected ScriptEngine createScriptEngine() {
                return engine;
            }
        };

        EasyMock.expect(engine.createBindings()).andReturn(new SimpleBindings());
        engine.setContext((ScriptContext) EasyMock.anyObject());
        EasyMock.expect(engine.eval((Reader) EasyMock.anyObject())).andThrow(new ScriptException("test exception"));
        EasyMock.replay(engine);

        scriptedView.setEngineName("test-engine");
View Full Code Here


  /**
   * Creates a bindings.
   */
  public Bindings createBindings() {
    return new SimpleBindings();
  }
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

    }

    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

  }

  private Bindings createGlobalBindings() {
    Map<String, Object> map =
      Collections.synchronizedMap(new HashMap<String, Object>());
    return new SimpleBindings(map);
  }
View Full Code Here

        filter(requestUri, SCRIPT.scriptSource, null);

    if(it.hasNext()) {
      NonLiteral scriptResource = (NonLiteral) it.next().getObject();
      try {
        Bindings bindings = new SimpleBindings();
        bindings.put("uriInfo", uriInfo);
        bindings.put("request", request);
        bindings.put("httpHeaders", httpHeaders);
        return scriptExecution.execute(scriptResource, bindings);
      } catch (ScriptException ex) {
        logger.warn("Exception while executing script {}",
            ((UriRef) scriptResource).getUnicodeString());
        throw new WebApplicationException(
View Full Code Here

            throw new ScriptException(e);
        }
    }

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

    public void testEval_bindings() throws Exception {
        logger1.info("eval with bindings");
        System.setProperty("org.jruby.embed.localvariable.behavior", "transient");
        JRubyEngineFactory factory = new JRubyEngineFactory();
        JRubyEngine engine = (JRubyEngine) factory.getScriptEngine();
        Bindings bindings = new SimpleBindings();
        bindings.put("message", "Helloooo Woooorld!");
        engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        String script = "\"I heard, \"#{message}\"\"";
        JRubyCompiledScript instance = (JRubyCompiledScript) engine.compile(script);
        Object expResult = "I heard, Helloooo Woooorld!";
        Object result = instance.eval(bindings);
        // Bug? a local variable isn't shared.
        //assertEquals(expResult, result);

        bindings.put("@message", "Saaaay Heeeey.");
        engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        script = "\"I heard, #{@message}\"";
        instance = (JRubyCompiledScript) engine.compile(script);
        expResult = "I heard, Saaaay Heeeey.";
        result = instance.eval(bindings);
        assertEquals(expResult, result);

        bindings.put("$message", "Hiya, hiya, hiya");
        engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        script = "\"I heard, #{$message}\"";
        instance = (JRubyCompiledScript) engine.compile(script);
        expResult = "I heard, Hiya, hiya, hiya";
        result = instance.eval(bindings);
View Full Code Here

        String script =
            "def get_perimeter(x, y)\n" +
              "x + 2.0 * y + PI / 2.0 * x\n" +
            "end\n" +
            "get_perimeter(1.5, 1.5)";
        Bindings bindings = new SimpleBindings();
        Double expResult = 6.856;
        bindings.put("PI", 3.1415);
        Double result = (Double) instance.eval(script, bindings);
        assertEquals(expResult, result, 0.01);

        instance.getBindings(ScriptContext.ENGINE_SCOPE).clear();
        instance = null;
View Full Code Here

            ScriptEngineManager manager = new ScriptEngineManager();
            instance = manager.getEngineByName("jruby");
        }
        String filename = basedir + "/test/org/jruby/embed/ruby/count_down.rb";
        Reader reader = new FileReader(filename);
        Bindings bindings = new SimpleBindings();
        bindings.put("@month", 6);
        bindings.put("@day", 2);
        instance.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        String result = (String) instance.eval(reader, bindings);
        assertTrue(result.startsWith("Happy") || result.startsWith("You have"));

        instance.getBindings(ScriptContext.ENGINE_SCOPE).clear();
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.