use of javax.script.SimpleScriptContext in project tomee by apache.
the class OpenEJBScripter method evaluate.
public Object evaluate(final String language, final String script, final ScriptContext context) throws ScriptException {
if (!ENGINE_FACTORIES.containsKey(language)) {
throw new IllegalArgumentException("can't find factory for language " + language + ". You probably need to add the jar to openejb libs.");
}
ScriptContext executionContext = context;
if (executionContext == null) {
executionContext = new SimpleScriptContext();
}
// we bind local variables (per execution) every time we execute a script
bindLocal(executionContext);
final ScriptEngine engine = engine(language);
return engine.eval(script, executionContext);
}
use of javax.script.SimpleScriptContext in project proxyee-down by monkeyWie.
the class ExtensionUtil method invoke.
/**
* 运行一个js方法
*/
public static Object invoke(ExtensionInfo extensionInfo, Event event, Object param, boolean async) throws NoSuchMethodException, ScriptException, FileNotFoundException, InterruptedException {
// 初始化js引擎
ScriptEngine engine = ExtensionUtil.buildExtensionRuntimeEngine(extensionInfo);
Invocable invocable = (Invocable) engine;
// 执行resolve方法
Object result = invocable.invokeFunction(StringUtils.isEmpty(event.getMethod()) ? event.getOn() : event.getMethod(), param);
// 结果为null或者异步调用直接返回
if (result == null || async) {
return result;
}
final Object[] ret = { null };
// 判断是不是返回Promise对象
ScriptContext ctx = new SimpleScriptContext();
ctx.setAttribute("result", result, ScriptContext.ENGINE_SCOPE);
boolean isPromise = (boolean) engine.eval("!!result&&typeof result=='object'&&typeof result.then=='function'", ctx);
if (isPromise) {
// 如果是返回的Promise则等待执行完成
CountDownLatch countDownLatch = new CountDownLatch(1);
invocable.invokeMethod(result, "then", (Function) o -> {
try {
ret[0] = o;
} catch (Exception e) {
LOGGER.error("An exception occurred while resolve()", e);
} finally {
countDownLatch.countDown();
}
return null;
});
invocable.invokeMethod(result, "catch", (Function) o -> {
countDownLatch.countDown();
return null;
});
// 等待解析完成
countDownLatch.await();
} else {
ret[0] = result;
}
return ret[0];
}
use of javax.script.SimpleScriptContext 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.SimpleScriptContext in project keycloak by keycloak.
the class JSPolicyProvider method evaluate.
@Override
public void evaluate(Evaluation evaluation) {
Policy policy = evaluation.getPolicy();
AuthorizationProvider authorization = evaluation.getAuthorizationProvider();
EvaluatableScriptAdapter adapter = evaluatableScript.apply(authorization, policy);
try {
SimpleScriptContext context = new SimpleScriptContext();
context.setAttribute("$evaluation", evaluation, ScriptContext.ENGINE_SCOPE);
adapter.eval(context);
} catch (Exception e) {
throw new RuntimeException("Error evaluating JS Policy [" + policy.getName() + "].", e);
}
}
use of javax.script.SimpleScriptContext in project OpenAM by OpenRock.
the class StandardScriptEvaluator method buildScriptContext.
/**
* Build the script context for evaluating a script, using the given set of variables for the engine scope.
*
* @param engineScope the variable bindings to use for the engine scope.
* @return the configured script context.
*/
private ScriptContext buildScriptContext(Bindings engineScope) {
final ScriptContext context = new SimpleScriptContext();
context.setBindings(engineScope, ScriptContext.ENGINE_SCOPE);
context.setBindings(scriptEngineManager.getBindings(), ScriptContext.GLOBAL_SCOPE);
// Replace reader/writer instances with null versions
context.setReader(null);
// Groovy expects these writers to be non-null, so use the Groovy-supplied NullWriter instance
context.setWriter(NullWriter.DEFAULT);
context.setErrorWriter(NullWriter.DEFAULT);
return context;
}
Aggregations