use of javax.script.Invocable in project proxyee-down by monkeyWie.
the class JavascriptEngine method main.
public static void main(String[] args) throws ScriptException, NoSuchMethodException, JsonProcessingException, InterruptedException {
ScriptEngine engine = buildEngine();
Invocable invocable = (Invocable) engine;
engine.eval("load('E:/study/extensions/bilibili-helper/dist/hook.js')");
HttpRequestForm requestForm = new HttpRequestForm();
requestForm.setUrl("https://www.bilibili.com/video/av34765642");
Object result = invocable.invokeFunction("error");
ScriptContext ctx = new SimpleScriptContext();
ctx.setAttribute("result", result, ScriptContext.ENGINE_SCOPE);
System.out.println(engine.eval("!!result&&typeof result=='object'&&typeof result.then=='function'", ctx));
}
use of javax.script.Invocable in project proxyee-down by monkeyWie.
the class JavascriptEngine method buildEngine.
public static ScriptEngine buildEngine() throws ScriptException, NoSuchMethodException {
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine(new SafeClassFilter());
Window window = new Window();
Object global = engine.eval("this");
Object jsObject = engine.eval("Object");
Invocable invocable = (Invocable) engine;
invocable.invokeMethod(jsObject, "bindProperties", global, window);
engine.eval("var window = this");
return engine;
}
use of javax.script.Invocable in project rivescript-java by aichaos.
the class Jsr223ScriptingHandler method call.
/**
* {@inheritDoc}
*/
@Override
public String call(RiveScript rs, String name, String[] fields) {
String result = null;
if (scriptEngine == null) {
logger.warn("Cannot call macro '{}' as no script engine was found for '{}'", name, engineName);
} else {
long startTime = System.currentTimeMillis();
// Invoke the function in the script engine.
try {
Invocable invocable = (Invocable) scriptEngine;
Object value = invocable.invokeFunction(resolveFunctionName(name), rs, fields);
if (value != null) {
result = value.toString();
}
if (logger.isDebugEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
if (result == null) {
logger.debug("Returned null from macro '{}' in {} ms", name, elapsedTime);
} else {
logger.debug("Returned '{}' from macro '{}' in {} ms", result, name, elapsedTime);
}
}
} catch (ScriptException e) {
logger.error("Error invoking function for macro '{}'", name, e);
} catch (NoSuchMethodException e) {
logger.error("Error invoking function for macro '{}'", name, e);
}
}
return result;
}
use of javax.script.Invocable in project spring-framework by spring-projects.
the class ScriptTemplateView method renderMergedOutputModel.
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
ScriptEngine engine = getEngine();
Invocable invocable = (Invocable) engine;
String url = getUrl();
String template = getTemplate(url);
Function<String, String> templateLoader = path -> {
try {
return getTemplate(path);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
};
RenderingContext context = new RenderingContext(this.getApplicationContext(), this.locale, templateLoader, url);
Object html;
if (this.renderObject != null) {
Object thiz = engine.eval(this.renderObject);
html = invocable.invokeMethod(thiz, this.renderFunction, template, model, context);
} else {
html = invocable.invokeFunction(this.renderFunction, template, model, context);
}
response.getWriter().write(String.valueOf(html));
} catch (ScriptException ex) {
throw new ServletException("Failed to render script template", new StandardScriptEvalException(ex));
}
}
use of javax.script.Invocable in project lucene-solr by apache.
the class StatelessScriptUpdateProcessorFactory method initEngines.
//================================================ Helper Methods ==================================================
/**
* Initializes a list of script engines - an engine per script file.
*
* @param req The solr request.
* @param rsp The solr response
* @return The list of initialized script engines.
*/
private List<EngineInfo> initEngines(SolrQueryRequest req, SolrQueryResponse rsp) throws SolrException {
List<EngineInfo> scriptEngines = new ArrayList<>();
ScriptEngineManager scriptEngineManager = new ScriptEngineManager(resourceLoader.getClassLoader());
scriptEngineManager.put("logger", log);
scriptEngineManager.put("req", req);
scriptEngineManager.put("rsp", rsp);
if (params != null) {
scriptEngineManager.put("params", params);
}
for (ScriptFile scriptFile : scriptFiles) {
ScriptEngine engine = null;
if (null != engineName) {
engine = scriptEngineManager.getEngineByName(engineName);
if (engine == null) {
String details = getSupportedEngines(scriptEngineManager, false);
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "No ScriptEngine found by name: " + engineName + (null != details ? " -- supported names: " + details : ""));
}
} else {
engine = scriptEngineManager.getEngineByExtension(scriptFile.getExtension());
if (engine == null) {
String details = getSupportedEngines(scriptEngineManager, true);
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "No ScriptEngine found by file extension: " + scriptFile.getFileName() + (null != details ? " -- supported extensions: " + details : ""));
}
}
if (!(engine instanceof Invocable)) {
String msg = "Engine " + ((null != engineName) ? engineName : ("for script " + scriptFile.getFileName())) + " does not support function invocation (via Invocable): " + engine.getClass().toString() + " (" + engine.getFactory().getEngineName() + ")";
log.error(msg);
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, msg);
}
if (scriptEngineCustomizer != null) {
scriptEngineCustomizer.customize(engine);
}
scriptEngines.add(new EngineInfo((Invocable) engine, scriptFile));
try {
Reader scriptSrc = scriptFile.openReader(resourceLoader);
try {
engine.eval(scriptSrc);
} catch (ScriptException e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unable to evaluate script: " + scriptFile.getFileName(), e);
} finally {
IOUtils.closeQuietly(scriptSrc);
}
} catch (IOException ioe) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unable to evaluate script: " + scriptFile.getFileName(), ioe);
}
}
return scriptEngines;
}
Aggregations