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:com.galeoconsulting.leonardinius.api.impl.ScriptServiceImpl.java

private synchronized void init() {
    this.scriptEngineManager = new ScriptEngineManager();
    registeredEngines.clear();
}

From source file:com.intbit.util.ServletUtil.java

public static String getServerName(ServletContext context) {
    try {//from   w w  w.jav  a 2 s  . com
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        String path = context.getRealPath("") + "/js/configurations.js";
        // read script file
        engine.eval(Files.newBufferedReader(Paths.get(path), StandardCharsets.UTF_8));

        Invocable inv = (Invocable) engine;
        // call function from script file
        return inv.invokeFunction("getHost", "").toString();
    } catch (Exception ex) {
        return "http://clients.brndbot.com/BrndBot/";
    }
}

From source file:JrubyTest.java

/**
 * ?Excel????/*from  w ww.  j a  va 2 s  .  c o  m*/
 *
 * @throws Exception
 */
@Test
public void parseTestSpecification() throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("jruby");
    Reader scriptReader = null;
    InputStream in = null;
    try {
        in = getClass().getClassLoader().getResourceAsStream("test-specifications.xls");
        Workbook workbook = load(in);
        Book book = new Book(workbook);
        scriptReader = new InputStreamReader(
                getClass().getClassLoader().getResourceAsStream("test-specification-parser.rb"));
        if (engine instanceof Compilable) {
            CompiledScript script = ((Compilable) engine).compile(scriptReader);
            SimpleBindings bindings = new SimpleBindings();
            bindings.put("@book", book);
            script.eval(bindings);
        }
    } finally {
        IOUtils.closeQuietly(scriptReader);
        IOUtils.closeQuietly(in);
    }

}

From source file:org.tamilunicodeconverter.converter.BaminiConverter.java

public String getUnicodeTextUsingJavaScript(String text) {
    String unicodeText = "";

    try {/*from   w  w w . j  a va 2s .  c o m*/
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        engine.eval(getScriptContent());
        Invocable invokable = (Invocable) engine;
        unicodeText = (String) invokable.invokeFunction("convert", new String[] { text });
    } catch (Exception ex) {
        throw new UnhandledException("Error occured while converting bamini text to unicode", ex);
    }
    return unicodeText;
}

From source file:org.openhab.binding.ebus.internal.parser.EBusConfigurationProvider.java

/**
 * Constructor/*from ww  w .  ja va 2 s.c o m*/
 */
public EBusConfigurationProvider() {
    final ScriptEngineManager mgr = new ScriptEngineManager();

    // load script engine if available
    if (mgr != null) {
        final ScriptEngine engine = mgr.getEngineByName("JavaScript");

        if (engine == null) {
            logger.warn(
                    "Unable to load \"JavaScript\" engine! Skip every eBus value calculated by JavaScript.");

        } else if (engine instanceof Compilable) {
            compEngine = (Compilable) engine;

        }
    }
}

From source file:com.qwazr.webapps.transaction.ControllerManager.java

private ControllerManager(File dataDir) {
    this.dataDir = dataDir;
    ScriptEngineManager manager = new ScriptEngineManager();
    scriptEngine = manager.getEngineByName("nashorn");
    servletMap = new AccessTimeCacheMap<>(3600);
}

From source file:com.jkoolcloud.tnt4j.streams.filters.JavaScriptActivityExpressionFilter.java

@Override
public boolean doFilter(ActivityInfo activityInfo) throws FilterException {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName(JAVA_SCRIPT_LANG);

    if (CollectionUtils.isNotEmpty(exprVars)) {
        Object fValue;//from   w w w.  java  2s. c om
        String fieldName;
        for (String eVar : exprVars) {
            fieldName = eVar.substring(2, eVar.length() - 1);
            fValue = activityInfo.getFieldValue(fieldName);
            fieldName = placeHoldersMap.get(eVar);
            factory.put(StringUtils.isEmpty(fieldName) ? eVar : fieldName, fValue);
        }
    }

    try {
        boolean match = (boolean) engine.eval(StreamsScriptingUtils.addDefaultJSScriptImports(getExpression()));

        boolean filteredOut = isFilteredOut(getHandleType(), match);
        activityInfo.setFiltered(filteredOut);

        return filteredOut;
    } catch (Exception exc) {
        throw new FilterException(StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME,
                "ExpressionFilter.filtering.failed", filterExpression), exc);
    }
}

From source file:com.intuit.tank.project.ExternalScript.java

public ScriptEngine getEngine() {
    return new ScriptEngineManager().getEngineByExtension(FilenameUtils.getExtension(name));
}

From source file:org.zaproxy.VerifyScripts.java

private static Stream<Arguments> scriptsJavaScript() {
    Compilable engine = (Compilable) new ScriptEngineManager().getEngineByName("ECMAScript");
    assertThat(engine).as("ECMAScript script engine exists.").isNotNull();
    return testData(".js", engine);
}

From source file:org.nuxeo.connect.connector.http.proxy.NashornProxyPacResolver.java

@Override
public String[] findPacProxies(String url) {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    ScriptContext context = new SimpleScriptContext();
    engine.setContext(context); // set as default context, for invokeFunctino
    SimpleBindings bindings = new SimpleBindings();
    context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
    try {//from www  .j  av  a  2s .com
        // Register internet functions as Java upcalls
        engine.eval("var NashornProxyPacResolver = Java.type('" + getClass().getName() + "');"
                + "var dnsResolve = NashornProxyPacResolver.dnsResolve;"
                + "var myIpAddress = NashornProxyPacResolver.myIpAddress");
        // Register others pac methods
        engine.eval(getFileReader(PAC_FUNCTIONS_FILE));
        // Register remote pac function
        engine.eval(getRemotePacBodyReader());
        // Call and return pac resolution function
        String proxies = (String) ((Invocable) engine).invokeFunction(EXEC_PAC_FUNC, url, getHost(url));
        return proxies.split(";");
    } catch (IOException | ScriptException | NoSuchMethodException e) {
        log.warn(e, e);
    }
    return null;
}