use of javax.script.ScriptEngine in project opennms by OpenNMS.
the class OSGiScriptEngineManager method getEngineByExtension.
public ScriptEngine getEngineByExtension(String extension) {
// TODO this is a hack to deal with context class loader issues
ScriptEngine engine = null;
for (ScriptEngineManager manager : classLoaders.keySet()) {
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(classLoaders.get(manager));
engine = manager.getEngineByExtension(extension);
Thread.currentThread().setContextClassLoader(old);
if (engine != null)
break;
}
return engine;
}
use of javax.script.ScriptEngine in project opennms by OpenNMS.
the class OSGiScriptEngineManager method getEngineByName.
public ScriptEngine getEngineByName(String shortName) {
// TODO this is a hack to deal with context class loader issues
for (ScriptEngineManager manager : classLoaders.keySet()) {
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(classLoaders.get(manager));
ScriptEngine engine = manager.getEngineByName(shortName);
Thread.currentThread().setContextClassLoader(old);
if (engine != null) {
return new OSGiScriptEngine(engine, new OSGiScriptEngineFactory(engine.getFactory(), classLoaders.get(manager)));
}
}
return null;
}
use of javax.script.ScriptEngine in project MantaroBot by Mantaro.
the class OwnerCmd method eval.
@Subscribe
public void eval(CommandRegistry cr) {
// has no state
JavaEvaluator javaEvaluator = new JavaEvaluator();
Map<String, Evaluator> evals = new HashMap<>();
evals.put("js", (event, code) -> {
ScriptEngine script = new ScriptEngineManager().getEngineByName("nashorn");
script.put("mantaro", MantaroBot.getInstance());
script.put("db", MantaroData.db());
script.put("jda", event.getJDA());
script.put("event", event);
script.put("guild", event.getGuild());
script.put("channel", event.getChannel());
try {
return script.eval(String.join("\n", "load(\"nashorn:mozilla_compat.js\");", "imports = new JavaImporter(java.util, java.io, java.net);", "(function() {", "with(imports) {", code, "}", "})()"));
} catch (Exception e) {
return e;
}
});
evals.put("bsh", (event, code) -> {
Interpreter interpreter = new Interpreter();
try {
interpreter.set("mantaro", MantaroBot.getInstance());
interpreter.set("db", MantaroData.db());
interpreter.set("jda", event.getJDA());
interpreter.set("event", event);
interpreter.set("guild", event.getGuild());
interpreter.set("channel", event.getChannel());
return interpreter.eval(String.join("\n", "import *;", code));
} catch (Exception e) {
return e;
}
});
evals.put("java", (event, code) -> {
try {
CompilationResult r = javaEvaluator.compile().addCompilerOptions("-Xlint:unchecked").source("Eval", JAVA_EVAL_IMPORTS + "\n\n" + "public class Eval {\n" + " public static Object run(GuildMessageReceivedEvent event) {\n" + " try {\n" + " return null;\n" + " } finally {\n" + " " + (code + ";").replaceAll(";{2,}", ";") + "\n" + " }\n" + " }\n" + "}").execute();
EvalClassLoader ecl = new EvalClassLoader();
r.getClasses().forEach((name, bytes) -> ecl.define(bytes));
return ecl.loadClass("Eval").getMethod("run", GuildMessageReceivedEvent.class).invoke(null, event);
} catch (CompilationException e) {
StringBuilder sb = new StringBuilder("\n");
if (e.getCompilerOutput() != null)
sb.append(e.getCompilerOutput());
if (!e.getDiagnostics().isEmpty()) {
if (sb.length() > 0)
sb.append("\n\n");
e.getDiagnostics().forEach(d -> sb.append(d).append('\n'));
}
return new Error(sb.toString()) {
@Override
public String toString() {
return getMessage();
}
};
} catch (Exception e) {
return e;
}
});
cr.register("eval", new SimpleCommand(Category.OWNER, CommandPermission.OWNER) {
@Override
protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
Evaluator evaluator = evals.get(args[0]);
if (evaluator == null) {
onHelp(event);
return;
}
String[] values = SPLIT_PATTERN.split(content, 2);
if (values.length < 2) {
onHelp(event);
return;
}
String v = values[1];
Object result = evaluator.eval(event, v);
boolean errored = result instanceof Throwable;
event.getChannel().sendMessage(new EmbedBuilder().setAuthor("Evaluated " + (errored ? "and errored" : "with success"), null, event.getAuthor().getAvatarUrl()).setColor(errored ? Color.RED : Color.GREEN).setDescription(result == null ? "Executed successfully with no objects returned" : ("Executed " + (errored ? "and errored: " : "successfully and returned: ") + result.toString())).setFooter("Asked by: " + event.getAuthor().getName(), null).build()).queue();
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Eval cmd").setDescription("**Evaluates stuff (A: js/bsh)**").build();
}
});
}
use of javax.script.ScriptEngine in project bamboobsc by billchen198318.
the class ScriptExpressionUtils method executeRenjin.
private static void executeRenjin(String scriptExpression, Map<String, Object> results, Map<String, Object> parameters) throws Exception {
ScriptEngine engine = buildRenjinScriptEngine();
if (parameters != null) {
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
engine.put(entry.getKey(), entry.getValue());
}
}
engine.eval(scriptExpression);
if (results != null) {
for (Map.Entry<String, Object> entry : results.entrySet()) {
Object res = engine.get(entry.getKey());
if (res instanceof AbstractAtomicVector) {
if (((AbstractAtomicVector) res).length() > 0) {
entry.setValue(((AbstractAtomicVector) res).getElementAsObject(0));
}
} else {
entry.setValue(res);
}
}
}
}
use of javax.script.ScriptEngine in project bamboobsc by billchen198318.
the class ScriptExpressionUtils method buildRenjinScriptEngine.
public static ScriptEngine buildRenjinScriptEngine() {
if (renjinScriptEngineTL.get() != null) {
return renjinScriptEngineTL.get();
}
ScriptEngine engine = manager.getEngineByName("Renjin");
renjinScriptEngineTL.set(engine);
return engine;
}
Aggregations