use of javax.script.Invocable in project mixcr by milaboratory.
the class AFilter method build.
public static AFilter build(String filterCode) {
try {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
Reader reader = null;
String script = null;
try {
reader = new BufferedReader(new InputStreamReader(AFilter.class.getClassLoader().getResourceAsStream("js/filter_init.js")));
char[] buffer = new char[1024];
int size;
StringBuilder sb = new StringBuilder();
while ((size = reader.read(buffer)) >= 0) sb.append(buffer, 0, size);
script = sb.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
int filterBegin = filterCode.lastIndexOf(';') + 1;
script = script.replace("/*CODE*/", filterCode.substring(0, filterBegin));
script = script.replace("/*FILTER*/", filterCode.substring(filterBegin));
engine.eval(script);
for (Map.Entry<GeneFeature, String> entry : GeneFeature.getNameByFeature().entrySet()) engine.put(entry.getValue(), entry.getKey());
for (GeneType geneType : GeneType.values()) engine.put(new String(new char[] { geneType.getLetter() }), geneType);
Invocable inv = (Invocable) engine;
return new AFilter(engine, inv);
} catch (ScriptException e) {
throw new IllegalArgumentException(e);
}
}
use of javax.script.Invocable in project LogHub by fbacchella.
the class Script method configure.
@SuppressWarnings("unchecked")
@Override
public boolean configure(Properties properties) {
synchronized (this) {
if (factory == null) {
factory = new ScriptEngineManager(properties.classloader);
}
}
try {
Path scriptp = Paths.get(script);
String mimeType = Helpers.getMimeType(scriptp.toString());
if (mimeType == null) {
mimeType = Files.probeContentType(scriptp);
}
ScriptEngine engine = null;
if (mimeType != null) {
engine = factory.getEngineByMimeType(mimeType);
}
if (engine == null) {
int p = script.lastIndexOf(".");
String extension = script.substring(p + 1);
engine = factory.getEngineByExtension(extension);
}
if (engine == null) {
logger.error("langage not found for script {}", script);
return false;
}
final ScriptEngine logengine = engine;
logger.debug("script language is {}", () -> logengine.getFactory().getLanguageName());
if (!(engine instanceof Invocable)) {
logger.error("engine for langage {} is not invocable", () -> logengine.getFactory().getLanguageName());
return false;
}
Reader r = getScriptReader();
inv = (Invocable) engine;
settings = (Map<String, String>) engine.eval(r);
if (settings == null) {
settings = (Map<String, String>) engine.get("settings");
}
if (settings.containsKey("configure")) {
inv.invokeFunction(settings.get("configure"), properties);
}
return super.configure(properties);
} catch (IOException e) {
logger.error("Finding script {} failed: {}", script, e);
return false;
} catch (ScriptException e) {
logger.error("execution of script {} failed: {}", script, e);
logger.throwing(Level.DEBUG, e);
return false;
} catch (NoSuchMethodException e) {
return false;
} catch (ClassCastException e) {
logger.error("script {} didn't return a configuration map");
return false;
}
}
use of javax.script.Invocable in project HeavenMS by ronancpl.
the class AbstractScriptManager method getInvocable.
protected Invocable getInvocable(String path, MapleClient c) {
path = "scripts/" + path;
engine = null;
if (c != null) {
engine = c.getScriptEngine(path);
}
if (engine == null) {
File scriptFile = new File(path);
if (!scriptFile.exists()) {
return null;
}
engine = sem.getEngineByName("javascript");
if (c != null) {
c.setScriptEngine(path, engine);
}
try (FileReader fr = new FileReader(scriptFile)) {
if (ServerConstants.JAVA_8) {
engine.eval("load('nashorn:mozilla_compat.js');");
}
engine.eval(fr);
} catch (final ScriptException | IOException t) {
FilePrinter.printError(FilePrinter.INVOCABLE + path.substring(12, path.length()), t, path);
return null;
}
}
return (Invocable) engine;
}
use of javax.script.Invocable in project HeavenMS by ronancpl.
the class MapScriptManager method getMapScript.
public void getMapScript(MapleClient c, String scriptName, boolean firstUser) {
if (scripts.containsKey(scriptName)) {
try {
scripts.get(scriptName).invokeFunction("start", new MapScriptMethods(c));
} catch (final ScriptException | NoSuchMethodException e) {
e.printStackTrace();
}
return;
}
String type = firstUser ? "onFirstUserEnter/" : "onUserEnter/";
File scriptFile = new File("scripts/map/" + type + scriptName + ".js");
if (!scriptExists(scriptName, firstUser)) {
return;
}
FileReader fr = null;
ScriptEngine portal = sef.getScriptEngine();
try {
fr = new FileReader(scriptFile);
CompiledScript compiled = ((Compilable) portal).compile(fr);
compiled.eval();
final Invocable script = ((Invocable) portal);
scripts.put(scriptName, script);
script.invokeFunction("start", new MapScriptMethods(c));
} catch (final UndeclaredThrowableException | ScriptException ute) {
FilePrinter.printError(FilePrinter.MAP_SCRIPT + type + scriptName + ".txt", ute);
} catch (final Exception e) {
FilePrinter.printError(FilePrinter.MAP_SCRIPT + type + scriptName + ".txt", e);
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
use of javax.script.Invocable in project HeavenMS by ronancpl.
the class NPCScriptManager method start.
public boolean start(MapleClient c, int npc, int oid, String fileName, MapleCharacter chr) {
try {
NPCConversationManager cm = new NPCConversationManager(c, npc, oid, fileName);
if (cms.containsKey(c)) {
dispose(c);
}
if (c.canClickNPC()) {
cms.put(c, cm);
Invocable iv = null;
if (fileName != null) {
iv = getInvocable("npc/" + fileName + ".js", c);
}
if (iv == null) {
iv = getInvocable("npc/" + npc + ".js", c);
}
if (iv == null || NPCScriptManager.getInstance() == null) {
dispose(c);
return false;
}
engine.put("cm", cm);
scripts.put(c, iv);
c.setClickedNPC();
try {
iv.invokeFunction("start");
} catch (final NoSuchMethodException nsme) {
try {
iv.invokeFunction("start", chr);
} catch (final NoSuchMethodException nsma) {
nsma.printStackTrace();
}
}
} else {
c.announce(MaplePacketCreator.enableActions());
}
return true;
} catch (final UndeclaredThrowableException | ScriptException ute) {
FilePrinter.printError(FilePrinter.NPC + npc + ".txt", ute);
dispose(c);
return false;
} catch (final Exception e) {
FilePrinter.printError(FilePrinter.NPC + npc + ".txt", e);
dispose(c);
return false;
}
}
Aggregations