All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.siwenyan.si.SiScriptEngine Maven / Gradle / Ivy

There is a newer version: 1.25.1.0
Show newest version
package com.github.siwenyan.si;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class SiScriptEngine {

    private static final ScriptEngineManager SEM = new ScriptEngineManager();

    private static SiScriptEngine instance;

    public static SiScriptEngine instance() {
        if (null == instance) {
            instance = new SiScriptEngine();
        }
        return instance;
    }

    private ScriptEngine jse;

    private SiScriptEngine() {
        jse = SEM.getEngineByExtension("js");
        try {
            this.init();
        } catch (ScriptException e) {
            throw new RuntimeException(e);
        }
    }

    private void init() throws ScriptException {
        try {
            String name = "side/jse_init.js";
            InputStream is = this.getClass().getClassLoader().getResourceAsStream(name);
            InputStreamReader isr = new InputStreamReader(is);
            this.jse.eval(isr);
        } catch (Exception e) {
            // do nothing
        }
    }

    public Object executeScript(String js) {

        try {
            Object res = this.jse.eval(js);
            return res;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy