Package javax.script

Examples of javax.script.SimpleScriptContext


            throw new IllegalArgumentException("can't find factory for language " + language + ". You probably need to add the jar to openejb libs.");
        }

        ScriptContext executionContext = context;
        if (executionContext == null) {
            executionContext = new SimpleScriptContext();
        }

        //we bind local variables (per execution) every time we execute a script
        bindLocal(executionContext);
View Full Code Here


                .getResourceController().getProperties());
            final FreeplaneSecurityManager securityManager = (FreeplaneSecurityManager) System.getSecurityManager();
            final boolean needToSetFinalSecurityManager = securityManager.needToSetFinalSecurityManager();
            final PrintStream oldOut = System.out;
            try {
                final SimpleScriptContext context = createScriptContext(node);
                if (compilationEnabled && engine instanceof Compilable) {
                    compileAndCache((Compilable) engine);
                    if (needToSetFinalSecurityManager)
                        securityManager.setFinalSecurityManager(scriptingSecurityManager);
                    System.setOut(outStream);
View Full Code Here

    private boolean disableScriptCompilation(File scriptFile) {
        return FilenameUtils.isExtension(scriptFile.getName(), ScriptResources.SCRIPT_COMPILATION_DISABLED_EXTENSIONS);
    }

    private SimpleScriptContext createScriptContext(final NodeModel node) {
        final SimpleScriptContext context = new SimpleScriptContext();
        final OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
        context.setWriter(outWriter);
        context.setErrorWriter(outWriter);
        context.setBindings(createBinding(node), javax.script.ScriptContext.ENGINE_SCOPE);
        return context;
    }
View Full Code Here

        final ScriptEngineManager manager = new ScriptEngineManager();
        final ScriptEngine engine = manager.getEngineByName((String) this.options.get("engineName"));

        //new context for the execution of this script
        final ScriptContext newContext = new SimpleScriptContext();

        //creating the bidings object for the current execution
        final Bindings bindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE);

        bindings.put("user", this.userData.user);
        bindings.put("password", this.userData.pass);

        final List<String> myGroups;
View Full Code Here

    protected ScriptContext getScriptContext(Bindings bindings) {
        if (bindings == null) {
            throw new NullPointerException("null bindings in engine scope");
        }

        ScriptContext newContext = new SimpleScriptContext();
        newContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        Bindings global = getBindings(ScriptContext.GLOBAL_SCOPE);
        if (global != null) {
            newContext.setBindings(global, ScriptContext.GLOBAL_SCOPE);
        }
        newContext.setReader(context.getReader());
        newContext.setWriter(context.getWriter());
        newContext.setErrorWriter(context.getErrorWriter());

        return newContext;
    }
View Full Code Here

            "end\n" +
            "def get_perimeter(x, y)\n" +
              "x + 2.0 * y + Math::PI / 2.0 * x\n" +
            "end\n" +
            "norman_window(1, 3)";
        ScriptContext context = new SimpleScriptContext();
        List<Double> expResult = new ArrayList();
        expResult.add(3.392);
        expResult.add(8.571);
        List<Double> result = (List<Double>) instance.eval(script, context);
        for (int i=0; i<result.size(); i++) {
            assertEquals(expResult.get(i), result.get(i), 0.01);
        }

        script =
            "def get_area\n" +
              "$x * $y + Math::PI / 8.0 * $x ** 2.0\n" +
            "end\n" +
            "get_area";
        context.setAttribute("x", 1.0, ScriptContext.ENGINE_SCOPE);
        context.setAttribute("y", 3.0, ScriptContext.ENGINE_SCOPE);
        Double result2 = (Double) instance.eval(script, context);
        assertEquals(expResult.get(0), result2, 0.01);

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

            System.setProperty("org.jruby.embed.localcontext.scope", "singlethread");
            System.setProperty("org.jruby.embed.localvariable.behavior", "transient");
            ScriptEngineManager manager = new ScriptEngineManager();
            instance = manager.getEngineByName("jruby");
        }
        ScriptContext context = new SimpleScriptContext();

        String script =
            "def get_area\n" +
              "@x * @y + Math::PI / 8.0 * @x ** 2.0\n" +
            "end\n" +
            "get_area";
        context.setAttribute("@x", 1.0, ScriptContext.ENGINE_SCOPE);
        context.setAttribute("@y", 3.0, ScriptContext.ENGINE_SCOPE);
        Double expResult = 3.392;
        Double result = (Double) instance.eval(script, context);
        assertEquals(expResult, result, 0.01);

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

            ScriptEngineManager manager = new ScriptEngineManager();
            instance = manager.getEngineByName("jruby");
        }
        String filename = basedir + "/core/src/test/ruby/org/jruby/embed/ruby/list_printer.rb";
        Reader reader = new FileReader(filename);
        ScriptContext context = new SimpleScriptContext();

        String[] big5 = {"Alaska", "Texas", "California", "Montana", "New Mexico"};
        context.setAttribute("@list", Arrays.asList(big5), ScriptContext.ENGINE_SCOPE);
        StringWriter sw = new StringWriter();
        context.setWriter(sw);
        instance.eval(reader, context);
        String expResult = "Alaska >> Texas >> California >> Montana >> New Mexico: 5 in total";
        assertEquals(expResult, sw.toString().trim());

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

    //@Test
    public void testSetContext() {
        logger1.info("setContext");
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine instance = manager.getEngineByName("jruby");
        ScriptContext ctx = new SimpleScriptContext();
        StringWriter sw = new StringWriter();
        sw.write("Have a great summer!");
        ctx.setWriter(sw);
        instance.setContext(ctx);
        ScriptContext result = instance.getContext();
        Writer w = result.getWriter();
        Object expResult = "Have a great summer!";
        assertTrue(sw == result.getWriter());
View Full Code Here

    public void testEval_context() throws Exception {
        logger1.info("eval with context");
        System.setProperty("org.jruby.embed.localvariable.behavior", "transient");
        JRubyEngineFactory factory = new JRubyEngineFactory();
        JRubyEngine engine = (JRubyEngine) factory.getScriptEngine();
        ScriptContext context = new SimpleScriptContext();
        StringWriter writer = new StringWriter();
        StringWriter errorWriter = new StringWriter();
        context.setWriter(writer);
        context.setErrorWriter(errorWriter);

        context.setAttribute("message", "Hello World!!!!!", ScriptContext.ENGINE_SCOPE);
        engine.setContext(context);
        String script = "puts message";
        JRubyCompiledScript instance = (JRubyCompiledScript) engine.compile(script);
        Object expResult = "Hello World!!!!!";
        instance.eval(context);
        Object result = writer.toString().trim();
        assertEquals(expResult, result);
        writer.close();

        writer = new StringWriter();
        context.setWriter(writer);
        context.setAttribute("@message", "Say Hey.", ScriptContext.ENGINE_SCOPE);
        engine.setContext(context);
        script = "puts @message";
        instance = (JRubyCompiledScript) engine.compile(script);
        expResult = "Say Hey.";
        instance.eval(context);
        result = writer.toString().trim();
        assertEquals(expResult, result);

        context.setAttribute("@message", "Yeah!", ScriptContext.ENGINE_SCOPE);
        engine.setContext(context);
        expResult = "Say Hey.\nYeah!";
        instance.eval(context);
        result = writer.toString().trim();
        assertEquals(expResult, result);
        writer.close();

        writer = new StringWriter();
        context.setWriter(writer);
        context.setAttribute("$message", "Hiya.", ScriptContext.ENGINE_SCOPE);
        engine.setContext(context);
        script = "puts $message";
        instance = (JRubyCompiledScript) engine.compile(script);
        expResult = "Hiya.";
        instance.eval(context);
View Full Code Here

TOP

Related Classes of javax.script.SimpleScriptContext

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.