Search in sources :

Example 11 with Invocable

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);
    }
}
Also used : GeneFeature(io.repseq.core.GeneFeature) InputStreamReader(java.io.InputStreamReader) ScriptEngineManager(javax.script.ScriptEngineManager) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine) Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) BufferedReader(java.io.BufferedReader) GeneType(io.repseq.core.GeneType) Map(java.util.Map)

Example 12 with Invocable

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;
    }
}
Also used : Path(java.nio.file.Path) Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) ScriptEngineManager(javax.script.ScriptEngineManager) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine)

Example 13 with Invocable

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;
}
Also used : ScriptException(javax.script.ScriptException) Invocable(javax.script.Invocable) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File)

Example 14 with Invocable

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();
            }
        }
    }
}
Also used : CompiledScript(javax.script.CompiledScript) Compilable(javax.script.Compilable) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ScriptException(javax.script.ScriptException) ScriptException(javax.script.ScriptException) Invocable(javax.script.Invocable) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) FileReader(java.io.FileReader) File(java.io.File)

Example 15 with Invocable

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;
    }
}
Also used : Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ScriptException(javax.script.ScriptException)

Aggregations

Invocable (javax.script.Invocable)68 ScriptException (javax.script.ScriptException)41 ScriptEngine (javax.script.ScriptEngine)30 ScriptEngineManager (javax.script.ScriptEngineManager)22 IOException (java.io.IOException)19 File (java.io.File)10 InputStreamReader (java.io.InputStreamReader)10 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)10 FileReader (java.io.FileReader)9 Reader (java.io.Reader)9 Compilable (javax.script.Compilable)5 CompiledScript (javax.script.CompiledScript)5 BufferedReader (java.io.BufferedReader)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Bindings (javax.script.Bindings)4 ScriptContext (javax.script.ScriptContext)4 Writer (java.io.Writer)3 SimpleScriptContext (javax.script.SimpleScriptContext)3 ValidationResult (org.apache.nifi.components.ValidationResult)3