Example usage for javax.script ScriptEngineManager ScriptEngineManager

List of usage examples for javax.script ScriptEngineManager ScriptEngineManager

Introduction

In this page you can find the example usage for javax.script ScriptEngineManager ScriptEngineManager.

Prototype

public ScriptEngineManager() 

Source Link

Document

The effect of calling this constructor is the same as calling ScriptEngineManager(Thread.currentThread().getContextClassLoader()).

Usage

From source file:cz.vity.freerapid.plugins.services.webshare.PasswordJS.java

public String encryptPassword(String pass, String salt) throws Exception {
    try {/*from   w w  w . jav  a2  s.co  m*/
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        if (engine == null)
            throw new RuntimeException("JavaScript engine not found");
        final String md5enc = (String) engine
                .eval(JavaScript_MD5Crypt + "md5crypt(\"" + pass + "\", \"" + salt + "\")");
        return DigestUtils.shaHex(md5enc);
    } catch (Exception e) {
        throw new ServiceConnectionProblemException("Can't execute javascript");
    }
}

From source file:io.apicurio.hub.core.js.OaiScriptEngineFactory.java

public static final ScriptEngine createScriptEngine(URL... jsUrls) throws Exception {
    logger.debug("Creating and initializing a Nashorn script engine.");
    long start = System.currentTimeMillis();
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    if (engine == null) {
        throw new Exception("Failed to create a Nashorn script engine!");
    }/*w  w w  .  ja  v a 2  s .  c  o m*/

    URL consoleJsUrl = OaiScriptEngineFactory.class.getClassLoader().getResource("js-lib/core-console.js");
    if (consoleJsUrl == null) {
        throw new Exception("Failed to load script: core-console.js");
    }
    URL oaiJsUrl = OaiScriptEngineFactory.class.getClassLoader().getResource("js-lib/OAI.umd.js");
    if (oaiJsUrl == null) {
        throw new Exception("Failed to load script: OAI.umd.js");
    }
    URL oaiCommandsJsUrl = OaiScriptEngineFactory.class.getClassLoader()
            .getResource("js-lib/OAI-commands.umd.js");
    if (oaiCommandsJsUrl == null) {
        throw new Exception("Failed to load script: OAI-commands.umd.js");
    }

    // Load the JS libraries into the engine
    engine.eval(IOUtils.toString(consoleJsUrl));
    engine.eval(IOUtils.toString(oaiJsUrl));
    engine.eval(IOUtils.toString(oaiCommandsJsUrl));
    for (URL jsUrl : jsUrls) {
        engine.eval(IOUtils.toString(jsUrl));
    }

    long end = System.currentTimeMillis();
    logger.debug("Initialized a Nashorn script engine in {} millis.", end - start);

    return engine;

}

From source file:reconf.server.services.JavaScriptEngine.java

public Object eval(String js, Map<String, Object> params, String resultVariableName) {
    try {//from ww  w. j  a  v a 2s.  c  o  m
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        for (Entry<String, Object> each : params.entrySet()) {
            engine.put(each.getKey(), each.getValue());
        }
        engine.eval(js);
        return engine.get(resultVariableName);

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.mycontroller.standalone.scripts.McScriptEngineUtils.java

public static synchronized ScriptEngineManager getScriptEngineManager() {
    if (scriptEngineManager == null) {
        scriptEngineManager = new ScriptEngineManager();
    }//from   www  .ja v a  2 s  .c o  m
    return scriptEngineManager;
}

From source file:jp.toastkid.script.runner.JavaScriptRunner.java

/**
 * init ScriptEngine.
 */
public JavaScriptRunner() {
    engine = new ScriptEngineManager().getEngineByName("javascript");
}

From source file:org.openscore.lang.runtime.configuration.SlangRuntimeSpringConfig.java

@Bean
public ScriptEngine scriptEngine() {
    return new ScriptEngineManager().getEngineByName("python");
}

From source file:org.suren.autotest.web.framework.data.JavaScriptDynamicData.java

@Override
public String getValue(String orginData) {
    String value = null;//from w w  w .  j a  v a  2  s. c  o  m
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
    try {
        Object resObj = engine.eval(orginData);
        if (resObj != null) {
            value = resObj.toString();
        } else {
            value = "js not return!";
        }
    } catch (ScriptException e) {
        value = e.getMessage();
        e.printStackTrace();
    }

    return value;
}

From source file:temp.groovy.java

@Test
public void foo() {
    JexlEngine eng = new JexlEngine();
    eng.setLenient(false);//from ww w.  j  av a 2 s .c o m
    eng.setSilent(false);
    Expression expr = eng.createExpression("a == '9' and a =~ ['7','9']");
    System.out.println(expr.dump());
    JexlContext ctx = new MapContext();
    ctx.set("a", "9");
    Boolean result = (Boolean) expr.evaluate(ctx);
    try {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("jython");

        engine.put("first", 9);
        result = (Boolean) engine.eval("first not in [7,9]");
        result = (Boolean) engine.eval("first in ['7','9']");
        assertEquals(true, result);
        //This next example illustrates calling an invokable function:

        String fact = "def factorial(n) { n == 1 ? 1 : n * factorial(n - 1) }";
        engine.eval(fact);
        Invocable inv = (Invocable) engine;
        Object[] params = { 5 };
        Object oresult = inv.invokeFunction("factorial", params);
        assertEquals(120, oresult);
    } catch (ScriptException | NoSuchMethodException ex) {
        Logger.getLogger(groovy.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:mondrian.util.UtilCompatibleJdk16.java

public <T> T compileScript(Class<T> iface, String script, String engineName) {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName(engineName);
    try {/* w  w w. j a v  a 2s  .  c  o m*/
        engine.eval(script);
        Invocable inv = (Invocable) engine;
        return inv.getInterface(iface);
    } catch (ScriptException e) {
        throw Util.newError(e, "Error while compiling script to implement " + iface + " SPI");
    }
}

From source file:com.quancheng.saluki.core.grpc.router.internal.ScriptRouter.java

public ScriptRouter(String type, String rule) {
    super(rule);//from  w w  w .  j ava  2s .c  o  m
    engine = new ScriptEngineManager().getEngineByName(type);
    if (engine == null && StringUtils.equals(type, "javascript")) {
        engine = new ScriptEngineManager().getEngineByName("js");
    }
    if (engine == null) {
        throw new IllegalStateException("Unsupported route rule type: " + type + ", rule: " + rule);
    }
}