Example usage for jdk.nashorn.api.scripting NashornScriptEngineFactory NashornScriptEngineFactory

List of usage examples for jdk.nashorn.api.scripting NashornScriptEngineFactory NashornScriptEngineFactory

Introduction

In this page you can find the example usage for jdk.nashorn.api.scripting NashornScriptEngineFactory NashornScriptEngineFactory.

Prototype

NashornScriptEngineFactory

Source Link

Usage

From source file:org.wicketstuff.nashorn.resource.NashornScriptCallable.java

License:Apache License

@Override
public Object call() throws Exception {
    enableSecurity();/*from w ww  . j  a va  2  s .c  o m*/
    ScriptEngine scriptEngine = new NashornScriptEngineFactory().getScriptEngine(getClassFilter());
    Bindings bindings = scriptEngine.createBindings();
    SimpleScriptContext scriptContext = new SimpleScriptContext();
    scriptContext.setWriter(getWriter());
    scriptContext.setErrorWriter(getErrorWriter());
    setup(getAttributes(), bindings);
    Thread thread = Thread.currentThread();
    threadId = thread.getId();
    bindings.put("nashornResourceReferenceScriptExecutionThread", thread);
    scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
    return scriptEngine.eval(new StringReader(getScript()), scriptContext);
}

From source file:org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilderFactory.java

License:Open Source License

public void init() {

    factory = new NashornScriptEngineFactory();
}

From source file:org.wso2.carbon.uuf.renderablecreator.hbs.internal.serialize.JsonSerializerTest.java

License:Open Source License

private static Bindings executeJavaScript(String jsScript) throws ScriptException {
    NashornScriptEngineFactory scriptEngineFactory = new NashornScriptEngineFactory();
    ScriptEngine engine = scriptEngineFactory.getScriptEngine("-strict", "--optimistic-types");
    engine.eval(jsScript);/*www.j  a va 2s .  co m*/
    return engine.getBindings(ScriptContext.ENGINE_SCOPE);
}

From source file:org.xbib.elasticsearch.script.nashorn.NashornScriptEngineService.java

License:Apache License

@Inject
public NashornScriptEngineService(Settings settings) {
    super(settings);
    // setup the engine to share the definition of the Ecma script built-in objects: aka NashornGlobal.
    //      System.setProperty("nashorn.args", "--global-per-engine");
    //      ScriptEngineManager m = new ScriptEngineManager();
    //      this.engine = m.getEngineByName("nashorn");
    // changing a system property is not allowed by the tests.
    // Use the internal API instead
    NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
    this.engine = factory.getScriptEngine(new String[] { "--global-per-engine" });
}

From source file:se.idsecurity.LDIFTransform.GetTransformer.java

License:Open Source License

/**
 * Attempts to load a JavaScript file that contains code that extends
 * the TransformerCommon abstract class.
 * @param filePath The path to the JavaScript file
 * @param transformFile The properties file
 * @return An object that extends TransformerCommon
 *//*from  w ww.j  a  v  a2  s . c o m*/
private static TransformerCommon getFromJavascript(String filePath, File transformFile) {
    try {
        //https://wiki.openjdk.java.net/display/Nashorn/Nashorn+jsr223+engine+notes
        NashornScriptEngineFactory nsef = new NashornScriptEngineFactory();

        ScriptEngine engine = nsef.getScriptEngine("--language=es6");

        Logger jsLogger = LoggerFactory.getLogger("JS: " + new File(filePath).getName());
        engine.put("logger", jsLogger);

        Bindings bindings = new SimpleBindings();

        engine.getContext().setBindings(bindings, ScriptContext.GLOBAL_SCOPE);

        Object eval = engine.eval(new FileReader(new File(filePath)));

        Invocable invocable = (Invocable) engine;

        Object transformerCommonFromJavaScript = invocable.invokeFunction("getTransformer", transformFile);

        TransformerCommon tc = (TransformerCommon) transformerCommonFromJavaScript;

        return tc;

    } catch (Exception e) {
        logger.error("Couldn't load class " + filePath, e);
        System.exit(1);
        return null;
    }
}