List of usage examples for javax.script Compilable compile
public CompiledScript compile(Reader script) throws ScriptException;
Reader
) for later execution. From source file:org.netbeans.jcode.parser.ejs.EJSParser.java
public EJSParser() { try {/*from ww w . ja v a2 s.c om*/ engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.eval("var window = this;" + "var console = {\n" + " debug: print,\n" + " warn: print,\n" + " log: print\n" + "};" + "function contains(obj, a) {\n" + " for (var i = 0; i < a.length; i++) {\n" + " if (a[i] === obj) {\n" + " return true;\n" + " }\n" + " }\n" + " return false;\n" + "}"); Compilable compilingEngine = (Compilable) engine; cscript = compilingEngine .compile(new BufferedReader(new InputStreamReader(EJSParser.class.getClassLoader() .getResourceAsStream("org/netbeans/jcode/parser/ejs/resources/ejs.js"), "UTF-8"))); bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); cscript.eval(bindings); } catch (ScriptException | UnsupportedEncodingException ex) { Exceptions.printStackTrace(ex); } }
From source file:prototypes.ws.proxy.soap.configuration.BooleanExecutableExpression.java
protected final void setBody(String body) { this.body = body; try {//w w w . j av a 2 s . c om if (engine instanceof Compilable) { if (script == null) { Compilable compilingEngine = (Compilable) engine; script = compilingEngine.compile(body); } } else { logger.warn("Engine {}-{} cannot compile scripts", engine.getFactory().getEngineName(), engine.getFactory().getEngineVersion()); } } catch (ScriptException ex) { logger.warn(Messages.MSG_ERROR_DETAILS, ex); throw new IllegalArgumentException("Script expression '" + body + "' is not correct.", ex); } }
From source file:org.omegat.gui.scripting.ScriptingTest.java
public void testCompileScripts() throws Exception { File scriptDir = new File(StaticUtils.installDir(), ScriptingWindow.DEFAULT_SCRIPTS_DIR); assertTrue(scriptDir.isDirectory()); for (File f : scriptDir.listFiles()) { if (!f.isFile()) { continue; }/* w w w.j a v a 2s . c om*/ String ext = FilenameUtils.getExtension(f.getName()); ScriptEngine engine = ScriptRunner.MANAGER.getEngineByExtension(ext); if (engine instanceof Compilable) { Compilable cEngine = (Compilable) engine; try (BufferedReader br = Files.newBufferedReader(f.toPath())) { assertNotNull(cEngine.compile(br)); } } } }
From source file:org.trafodion.rest.script.ScriptManager.java
public void runScript(ScriptContext ctx) { String scriptName;/* w w w. j a va2s.c o m*/ if (ctx.getScriptName().length() == 0) scriptName = DEFAULT_SCRIPT_NAME; else if (!ctx.getScriptName().endsWith(".py")) scriptName = ctx.getScriptName() + PYTHON_SUFFIX; else scriptName = ctx.getScriptName(); try { ScriptEngine engine = manager.getEngineByName("python"); Bindings bindings = engine.createBindings(); bindings.put("scriptcontext", ctx); if (engine instanceof Compilable) { CompiledScript script = m.get(scriptName); if (script == null) { LOG.info("Compiling script " + scriptName); Compilable compilingEngine = (Compilable) engine; try { script = compilingEngine.compile(new FileReader(restHome + "/bin/scripts/" + scriptName)); } catch (Exception e) { LOG.warn(e.getMessage()); } m.put(scriptName, script); } script.eval(bindings); } else { try { engine.eval(new FileReader(restHome + "/bin/scripts/" + scriptName), bindings); } catch (Exception e) { LOG.warn(e.getMessage()); } } } catch (javax.script.ScriptException se) { LOG.warn(se.getMessage()); } }
From source file:de.ingrid.iplug.dscmapclient.index.mapper.ScriptedWmsDocumentMapper.java
@Override public ElasticDocument map(SourceRecord record, ElasticDocument doc) throws Exception { if (mappingScript == null) { log.error("Mapping script is not set!"); throw new IllegalArgumentException("Mapping script is not set!"); }//from ww w .j ava 2s . c o m try { if (engine == null) { String scriptName = mappingScript.getFilename(); String extension = scriptName.substring(scriptName.lastIndexOf('.') + 1, scriptName.length()); ScriptEngineManager mgr = new ScriptEngineManager(); engine = mgr.getEngineByExtension(extension); if (compile) { if (engine instanceof Compilable) { Compilable compilable = (Compilable) engine; compiledScript = compilable.compile(new InputStreamReader(mappingScript.getInputStream())); } } } // create utils for script CapabilitiesUtils capabilitiesUtils = new CapabilitiesUtils(); capabilitiesUtils.setUrlStr((String) record.get(SourceRecord.ID)); if (log.isDebugEnabled()) { log.debug("Requesting " + capabilitiesUtils.getUrlStr()); } org.w3c.dom.Document wmsDoc = capabilitiesUtils.requestCaps(); // we check for null and return -> NO Exception, so oncoming URLs are processed if (wmsDoc == null) { log.warn("!!! Problems requesting " + capabilitiesUtils.getUrlStr() + " !!! We return null Document so will not be indexed !"); return null; } //we put the xmlDoc(WMS doc) into the record and thereby pass it to the idf-mapper record.put("WmsDoc", wmsDoc); IndexUtils idxUtils = new IndexUtils(doc); XPathUtils xPathUtils = new XPathUtils(new IDFNamespaceContext()); Bindings bindings = engine.createBindings(); bindings.put("wmsDoc", wmsDoc); bindings.put("log", log); bindings.put("CAP", capabilitiesUtils); bindings.put("IDX", idxUtils); bindings.put("XPATH", xPathUtils); bindings.put("javaVersion", System.getProperty("java.version")); if (compiledScript != null) { compiledScript.eval(bindings); } else { engine.eval(new InputStreamReader(mappingScript.getInputStream()), bindings); } return doc; } catch (Exception e) { log.error("Error mapping source record to lucene document.", e); e.printStackTrace(); throw e; } }
From source file:de.ingrid.iplug.dscmapclient.index.mapper.ScriptedIdfDocumentMapper.java
/** * map in this case means, map the sourcerecord to * an idf document and store it as xml-string *//* w w w . j a v a2 s . c o m*/ public ElasticDocument map(SourceRecord record, ElasticDocument luceneDoc) throws Exception { if (luceneDoc.get("id") == null || luceneDoc.get("serviceUnavailable") != null) { log.warn("!!! No 'id' set in index document (id=" + luceneDoc.get("id") + ") or 'serviceUnavailable' set (serviceUnavailable=" + luceneDoc.get("serviceUnavailable") + ") !!! No IDF possible, we return null Document so will not be indexed !"); return null; } if (mappingScript == null) { log.error("Mapping script is not set!"); throw new IllegalArgumentException("Mapping script is not set!"); } org.w3c.dom.Document w3cDoc = docBuilder.newDocument(); try { if (engine == null) { String scriptName = mappingScript.getFilename(); String extension = scriptName.substring(scriptName.lastIndexOf('.') + 1, scriptName.length()); ScriptEngineManager mgr = new ScriptEngineManager(); engine = mgr.getEngineByExtension(extension); if (compile) { if (engine instanceof Compilable) { Compilable compilable = (Compilable) engine; compiledScript = compilable.compile(new InputStreamReader(mappingScript.getInputStream())); } } } IndexUtils idxUtils = new IndexUtils(luceneDoc); CapabilitiesUtils capUtils = new CapabilitiesUtils(); XPathUtils xPathUtils = new XPathUtils(new IDFNamespaceContext()); DOMUtils domUtils = new DOMUtils(w3cDoc, xPathUtils); XMLUtils xmlUtils = new XMLUtils(); org.w3c.dom.Document wmsDoc = (org.w3c.dom.Document) record.get("WmsDoc"); Bindings bindings = engine.createBindings(); bindings.put("wmsDoc", wmsDoc); bindings.put("CAP", capUtils); bindings.put("XML", xmlUtils); bindings.put("sourceRecord", record); bindings.put("luceneDoc", luceneDoc); bindings.put("w3cDoc", w3cDoc); bindings.put("log", log); bindings.put("IDX", idxUtils); bindings.put("XPATH", xPathUtils); bindings.put("DOM", domUtils); bindings.put("javaVersion", System.getProperty("java.version")); if (compiledScript != null) { compiledScript.eval(bindings); } else { engine.eval(new InputStreamReader(mappingScript.getInputStream()), bindings); } } catch (Exception e) { log.error("Error mapping source record to lucene document."); //e.printStackTrace(); throw e; } return luceneDoc; }
From source file:com.l2jfree.gameserver.scripting.CompiledScriptCache.java
public CompiledScript loadCompiledScript(ScriptEngine engine, File file) throws FileNotFoundException, ScriptException { int len = L2ScriptEngineManager.SCRIPT_FOLDER.getPath().length() + 1; String relativeName = file.getPath().substring(len); CompiledScriptHolder csh = _compiledScriptCache.get(relativeName); if (csh != null && csh.matches(file)) { if (_log.isDebugEnabled()) _log.info("Reusing cached compiled script: " + file); return csh.getCompiledScript(); }// w ww . j a v a2 s . co m if (_log.isDebugEnabled()) _log.info("Compiling script: " + file); Compilable eng = (Compilable) engine; BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); // TODO lock file CompiledScript cs = eng.compile(reader); if (cs instanceof Serializable) { synchronized (_compiledScriptCache) { _compiledScriptCache.put(relativeName, new CompiledScriptHolder(cs, file)); _modified = true; } } return cs; }
From source file:de.ingrid.interfaces.csw.index.impl.ScriptedIDFRecordLuceneMapper.java
/** * Load mapping script from filesystem and compile it if needed. This is * only done once when data is being indexed. *///w ww . j a v a 2 s.c om @Override public void init() { // read mapping file from disk if (configurationProvider != null) { this.mappingScript = configurationProvider.getMappingScript(); } // also compile it again (just once per index generation) String scriptName = this.mappingScript.getName(); String extension = scriptName.substring(scriptName.lastIndexOf('.') + 1, scriptName.length()); ScriptEngineManager mgr = new ScriptEngineManager(); this.engine = mgr.getEngineByExtension(extension); if (this.engine instanceof Compilable) { Compilable compilable = (Compilable) this.engine; try { this.compiledScript = compilable .compile(new InputStreamReader(new FileInputStream(this.mappingScript))); } catch (FileNotFoundException ex) { log.error("Mapping script was not found!", ex); } catch (ScriptException ex) { log.error("Error compiling mapping script!", ex); } } }
From source file:org.xwiki.rendering.macro.script.AbstractJSR223ScriptMacro.java
/** * Return a compiled version of the provided script. * /* w ww . j av a 2 s .co m*/ * @param content the script to compile. * @param engine the script engine. * @return the compiled version of the script. * @throws ScriptException failed to compile the script. */ protected CompiledScript getCompiledScript(String content, Compilable engine) throws ScriptException { // TODO: add caching return engine.compile(content); }
From source file:io.github.jeddict.jcode.parser.ejs.EJSParser.java
private ScriptEngine createEngine() { CompiledScript cscript;/*from www .ja va 2 s .c om*/ Bindings bindings; ScriptEngine scriptEngine = new NashornScriptEngineFactory().getScriptEngine("--language=es6");//engine = new ScriptEngineManager().getEngineByName("nashorn"); try { try { if (base == null) { base = IOUtils.toString(EJSParser.class.getClassLoader() .getResourceAsStream("io/github/jeddict/jcode/parser/ejs/resources/base.js"), "UTF-8"); } if (ejs == null) { ejs = IOUtils.toString(EJSParser.class.getClassLoader() .getResourceAsStream("io/github/jeddict/jcode/parser/ejs/resources/ejs.js"), "UTF-8"); } } catch (IOException ex) { Exceptions.printStackTrace(ex); } scriptEngine.eval(base); Compilable compilingEngine = (Compilable) scriptEngine; cscript = compilingEngine.compile(ejs); bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE); cscript.eval(bindings); scriptEngine.eval(scripts.toString()); for (Map<String, Object> context : contexts) { context.keySet().stream().forEach((key) -> { try { bindings.put(key, context.get(key)); if (context.get(key) instanceof Collection) { scriptEngine.eval(String.format("%s = Java.from(%s);", key, key)); } } catch (ScriptException ex) { Exceptions.printStackTrace(ex); } }); } } catch (ScriptException ex) { Exceptions.printStackTrace(ex); } return scriptEngine; }