List of usage examples for jdk.nashorn.api.scripting NashornScriptEngineFactory NashornScriptEngineFactory
NashornScriptEngineFactory
From source file:com.autodomum.script.nashorn.NashornScriptComponent.java
License:Open Source License
/** * Replace the current script engine and initialize with a new script * // w w w . jav a2s . c om * @param inputStream * the script * @throws ScriptException */ public void replaceScript(final InputStream inputStream) throws ScriptException { this.scriptEngine = new NashornScriptEngineFactory().getScriptEngine("-strict"); this.scriptEngine.put("eventComponent", this.eventComponent); this.scriptEngine.put("LOG", LOG); this.scriptEngine.eval(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); }
From source file:com.bunjlabs.fuga.templates.TemplateCompiler.java
License:Apache License
/** * Create new template compiler with resource manager * //from ww w . jav a 2 s . c om * @param resourceRepresenter Resource manager */ public TemplateCompiler(ResourceRepresenter resourceRepresenter) { this.resourceRepresenter = resourceRepresenter; this.engine = new NashornScriptEngineFactory().getScriptEngine("--print-no-newline"); }
From source file:com.bytelightning.opensource.pokerface.NashornCompiledScriptTest.java
License:Open Source License
@BeforeClass public static void setUpBeforeClass() throws Exception { engineFactory = new NashornScriptEngineFactory(); engine = engineFactory.getScriptEngine(); String script = new StringBuilder("").append("(function() {").append(" var i = 0;").append(" return {") .append(" setI: function(val) {").append(" i = val;").append(" },") .append(" addTwice: function(amount) {").append(" var x = amount;") .append(" i += amount;") .append(" var shortly_later = new Date()/1000 + Math.random;") .append(" while( (new Date()/1000) < shortly_later) { Math.random() };") .append(" i += x;").append(" },").append(" getI: function() {") .append(" return i;").append(" },").append(" square: function(val) {") .append(" var x = val;") .append(" var shortly_later = new Date()/1000 + Math.random;") .append(" while( (new Date()/1000) < shortly_later) { Math.random() };") .append(" return (val * x);").append(" },").append(" };").append("})();").toString(); compiledScript = ((Compilable) engine).compile(script); }
From source file:com.kise.kairosdb.util.ScriptBuilder.java
License:Apache License
/** * Bundles the script into a function f and builds it into an invocable object. * @param script/*from w ww. j av a 2s . c om*/ * @param arguments * @return null if the script could not be built */ public static Invocable buildScript(String script, String... arguments) { ScriptEngine engine; try { engine = new NashornScriptEngineFactory().getScriptEngine(new String[] { "--no-java" }); engine.eval(bundleAsFonctionF(script, arguments)); } catch (ScriptException e) { return null; } return (Invocable) engine; }
From source file:com.kise.kairosdb.util.ScriptBuilder.java
License:Apache License
public static String getScriptError(String script, String... arguments) { try {/* www. jav a 2 s .c om*/ ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine(new String[] { "--no-java" }); engine.eval(bundleAsFonctionF(script, arguments)); } catch (ScriptException e) { return e.getMessage(); } return null; }
From source file:com.kodcu.boot.SpringAppConfig.java
License:Apache License
@Bean @Lazy/*from w w w . j a va 2 s .co m*/ public NashornScriptEngineFactory nashornScriptEngineFactory() { NashornScriptEngineFactory nashornScriptEngineFactory = new NashornScriptEngineFactory(); return nashornScriptEngineFactory; }
From source file:com.mckoi.webplatform.nashorn.NashornInstanceGlobalFactory.java
License:Open Source License
/** * Initializes the NashornInternal object. * /*from w w w.ja v a 2 s .c o m*/ * @param cl the ClassLoader to use, or null for default. */ public void init(final ClassLoader cl) { // Incase we call 'init' more than once, if (PRIV_nashorn_ctx != null) { return; } // Hacky way to return properties from the Nashorn engine, final Object[] inner_data = new Object[5]; // Run initialization in a privileged security context, AccessController.doPrivileged(new PrivilegedAction<Object>() { @Override public Object run() { // We make a VM static NashornScriptEngine that we use to compile // scripts and fork new JavaScript instances. NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); NashornScriptEngine engine; if (cl == null) { engine = (NashornScriptEngine) factory.getScriptEngine(engine_args); } else { engine = (NashornScriptEngine) factory.getScriptEngine(engine_args, cl); } // We add some functions to the global object, ScriptObjectMirror global_object = (ScriptObjectMirror) engine .getBindings(ScriptContext.ENGINE_SCOPE); // A function that performs startup. We execute this to obtain // information about the engine such as the Global and Context object. // It seems the only way to get at this information is from code // running within the engine. global_object.put("engineStartup", new EngineStartupFunction(inner_data)); // Invoke the 'engineStartup' function to get at the priviledged // information. try { engine.invokeFunction("engineStartup", new Object[0]); } catch (ScriptException | NoSuchMethodException ex) { // Oops, throw new RuntimeException(ex); } finally { // Don't leave this function around, just incase, global_object.delete("engineStartup"); } return null; } }); PRIV_nashorn_ctx = (Context) inner_data[0]; // base_nashorn_global = (Global) inner_data[1]; }
From source file:com.mycompany.script.engine.js.nashorn.JsNashornScriptExecutor.java
/** * ? ?.// w ww. j av a2 s. co m * * @param scriptPath ? ? * @param basePath * @param logger * @param binding * @return */ @Override public ScriptResult execScript(String scriptPath, String basePath, Logger logger, Map<String, Object> binding) { ScriptResult result = new ScriptResult(); NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); ScriptEngine engine = factory.getScriptEngine(); try { ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE); Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE); engineScope.putAll(binding); engineScope.put("logger", logger); engineScope.put("currentDir", new File("").getAbsolutePath()); result.setStart(System.currentTimeMillis()); try (Reader reader = Files.newBufferedReader(new File(basePath, scriptPath).toPath())) { Object value = engine.eval(reader, engineScope); result.setResult(value); } } catch (IOException | ScriptException | RuntimeException ex) { logger.error("", ex); result.setException(ex); } finally { result.setFinish(System.currentTimeMillis()); } return result; }
From source file:com.sk89q.worldedit.scripting.NashornCraftScriptEngine.java
License:Open Source License
@Override public Object evaluate(String script, String filename, Map<String, Object> args) throws Throwable { ClassLoader cl = Fawe.get().getClass().getClassLoader(); Thread.currentThread().setContextClassLoader(cl); synchronized (NashornCraftScriptEngine.class) { if (FACTORY == null) FACTORY = new NashornScriptEngineFactory(); }/*from www. j a v a 2 s . c o m*/ ; ScriptEngine engine = FACTORY.getScriptEngine("--language=es6"); SimpleBindings bindings = new SimpleBindings(); for (Map.Entry<String, Object> entry : args.entrySet()) { bindings.put(entry.getKey(), entry.getValue()); } try { Object result = engine.eval(script, bindings); return result; } catch (Error e) { e.printStackTrace(); throw new ScriptException(e.getMessage()); } catch (Throwable e) { e.printStackTrace(); while (e.getCause() != null) { e = e.getCause(); } if (e instanceof WorldEditException) { throw e; } throw e; } finally { } }
From source file:com.threecrickets.scripturian.adapter.NashornAdapter.java
License:LGPL
/** * Constructor./*from w w w . j a v a2 s .c om*/ * * @throws LanguageAdapterException * In case of an initialization error */ public NashornAdapter() throws LanguageAdapterException { super("Nashorn", Version.version(), "JavaScript", new NashornScriptEngineFactory().getLanguageVersion(), Arrays.asList("js", "javascript", "nashorn"), "js", Arrays.asList("javascript", "js", "nashorn"), "nashorn"); try { System.setProperty("nashorn.persistent.code.cache", getCacheDir().getCanonicalPath()); } catch (IOException x) { throw new LanguageAdapterException(NashornAdapter.class, "Could not access cache directory: " + getCacheDir(), x); } PrintWriter out = new PrintWriter(new ExecutionContextWriter(), true); PrintWriter err = new PrintWriter(new ExecutionContextErrorWriter(), true); // See: jdk.nashorn.internal.runtime.ScriptEnvironment Options options = new Options("nashorn", err); options.set("print.no.newline", true); options.set("persistent.code.cache", true); ErrorManager errors = new ErrorManager(err); context = new Context(options, errors, out, err, getClass().getClassLoader()); }