List of usage examples for jdk.nashorn.api.scripting NashornScriptEngineFactory getScriptEngine
public ScriptEngine getScriptEngine(final String... args)
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 *//* ww w . ja va 2s . c om*/ 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; } }