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:utybo.branchingstorytree.swing.impl.XSFClient.java

@Override
public Object invokeScript(String resourceName, String function, XSFBridge bst, BranchingStory story, int line)
        throws BSTException {
    ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
    SimpleBindings binds = new SimpleBindings();
    binds.putAll(story.getRegistry().getAllInt());
    binds.putAll(story.getRegistry().getAllString());
    binds.put("bst", bst);
    scriptEngine.setBindings(binds, ScriptContext.ENGINE_SCOPE);
    try {/*from w  ww . ja  va 2s. c  om*/
        scriptEngine.eval(scripts.get(resourceName));
        return scriptEngine.eval(function + "()");
    } catch (ScriptException e) {
        throw new BSTException(line, "Script exception : " + e.getMessage(), story);
    }
}

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

/**
 * Constructor//from  ww w .java  2s  .com
 */
public EBusConfigurationProvider() {
    final ScriptEngineManager mgr = new ScriptEngineManager();
    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:org.netbeans.jcode.parser.ejs.EJSParser.java

public EJSParser() {

    try {/*w w w .jav  a2s .c o m*/
        engine = new ScriptEngineManager().getEngineByName("nashorn");
        engine.eval("var window = this;" + "var console = {\n" + "  debug: print,\n" + "  warn: print,\n"
                + "  log: print\n" + "};" + "function contains(obj, a) {\n"
                + "    for (var i = 0; i < a.length; i++) {\n" + "        if (a[i] === obj) {\n"
                + "            return true;\n" + "        }\n" + "    }\n" + "    return false;\n" + "}");

        Compilable compilingEngine = (Compilable) engine;
        cscript = compilingEngine
                .compile(new BufferedReader(new InputStreamReader(EJSParser.class.getClassLoader()
                        .getResourceAsStream("org/netbeans/jcode/parser/ejs/resources/ejs.js"), "UTF-8")));
        bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
        cscript.eval(bindings);
    } catch (ScriptException | UnsupportedEncodingException ex) {
        Exceptions.printStackTrace(ex);
    }
}

From source file:org.springframework.integration.scripting.jsr223.AbstractScriptExecutor.java

public AbstractScriptExecutor(String language) {
    Assert.hasText(language, "language must not be empty");
    this.language = language;

    scriptEngine = new ScriptEngineManager().getEngineByName(this.language);

    if (logger.isDebugEnabled()) {

        if (scriptEngine == null) {
            logger.error(invlalidLanguageMessage(this.language));
        } else {//  w ww .  ja v a2  s .c o  m
            logger.debug("using script engine : " + scriptEngine.getFactory().getEngineName());
        }
    }
}

From source file:it.geosolutions.geobatch.action.scripting.ScriptingTest.java

@Test
public void testLoop() throws ScriptException {

    String engineName = "groovy";

    ScriptEngine foundEngine = null;
    // create a script engine manager
    ScriptEngineManager mgr = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = mgr.getEngineFactories();
    System.out.println("FOUND " + factories.size() + " factories");
    for (ScriptEngineFactory sef : factories) {
        System.out.println("FACTORY: " + "'" + sef.getEngineName() + "' " + "'" + sef.getLanguageName() + "' "
                + "'" + sef.getExtensions() + "' " + "'" + sef.getNames() + "' ");
        if (sef.getEngineName().contains(engineName)) {
            foundEngine = sef.getScriptEngine();
        }/*  ww  w.j  a v  a  2  s. c o m*/

    }

    assertNotNull("Can't find engine '" + engineName + "'", foundEngine);
    foundEngine.eval("print('Hello, World')");
}

From source file:com.asual.lesscss.compiler.NashornCompiler.java

public NashornCompiler(LessOptions options, ResourceLoader loader, URL less, URL env, URL engine, URL cssmin,
        URL sourceMap) throws IOException {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine scriptEngine = factory.getEngineByName("nashorn");
    try {/*from w w  w.ja va  2  s .com*/
        scriptEngine.eval(new InputStreamReader(sourceMap.openConnection().getInputStream()));
        scriptEngine.eval(new InputStreamReader(env.openConnection().getInputStream()));
        ScriptObjectMirror lessenv = (ScriptObjectMirror) scriptEngine.get("lessenv");
        lessenv.put("charset", options.getCharset());
        lessenv.put("css", options.isCss());
        lessenv.put("lineNumbers", options.getLineNumbers());
        lessenv.put("optimization", options.getOptimization());
        lessenv.put("sourceMap", options.isSourceMap());
        lessenv.put("sourceMapRootpath", options.getSourceMapRootpath());
        lessenv.put("sourceMapBasepath", options.getSourceMapBasepath());
        lessenv.put("sourceMapURL", options.getSourceMapUrl());
        lessenv.put("loader", loader);
        lessenv.put("paths", options.getPaths());
        scriptEngine.eval(new InputStreamReader(less.openConnection().getInputStream()));
        scriptEngine.eval(new InputStreamReader(cssmin.openConnection().getInputStream()));
        scriptEngine.eval(new InputStreamReader(engine.openConnection().getInputStream()));
        compile = (ScriptObjectMirror) scriptEngine.get("compile");
    } catch (ScriptException e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:org.hbird.business.scripting.bean.ScriptExecutor.java

public ScriptExecutor(ScriptComponent component) {
    this.component = component;

    ScriptEngineManager factory = new ScriptEngineManager();
    engine = factory.getEngineByName(component.format);

    engine.put("output", component.output);

    /** *///w  w  w .j  a va2  s.c o  m
    if (component.script == null || component.script.equals("") == true) {

        /** Default root is the the resource folder of the current project. */
        String root = "src/main/resources/library/";
        if (System.getProperty("hbird.scriptlibrary") != null) {
            root = System.getProperty("hbird.scriptlibrary");
            if (root.endsWith("/") == false) {
                root += "/";
            }
        }

        File file = new File(root + component.getScriptName() + ".js");
        if (file.exists() == false) {
            LOG.error("Failed to find script file '" + file.getAbsolutePath() + "'.");
            LOG.error(
                    "Use the runtime system property '-Dhbird.scriptlibrary=[path]' to point to the script library. Script will not be evaluated.");
        } else {
            try {
                component.script = FileUtils.readFileToString(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.viddu.handlebars.HandlebarsNashorn.java

HandlebarsNashorn(Reader handlebarsLib) throws HandlebarsException {
    ScriptEngineManager engineManager = new ScriptEngineManager();
    this.engine = engineManager.getEngineByName("nashorn");
    try {/*from  www.j a va2s.  c o m*/
        engine.eval(handlebarsLib);
        handlebars = engine.eval("Handlebars");
        JSON = engine.eval("JSON");

        engine.eval("var ScriptUtils = Java.type('jdk.nashorn.api.scripting.ScriptUtils')\n"
                + "var wrapper = function(name, biFunctionInstance){\n"
                + "    Handlebars.registerHelper(name, function(context, options){\n"
                + "        var wrappedContext = ScriptUtils.wrap(context);\n"
                + "        var wrappedOptions = ScriptUtils.wrap(options);\n"
                + "        return biFunctionInstance.apply(wrappedContext, wrappedOptions); \n" + "    }); \n"
                + "}");
    } catch (ScriptException e) {
        logger.debug("Error in script:{}", e);
        throw new HandlebarsException(e);
    }
    this.objectMapper = new ObjectMapper();
    this.objectMapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
    this.mapper = new ObjectMapper();
}

From source file:nz.net.orcon.kanban.automation.plugin.ScriptPlugin.java

@Override
public Map<String, Object> process(Action action, Map<String, Object> context) throws Exception {

    ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");

    for (Entry<String, Object> entry : context.entrySet()) {
        if (StringUtils.isNotBlank(entry.getKey())) {
            engine.put(entry.getKey(), entry.getValue());
        }//w  ww  .j  a va 2 s.  c  om
    }

    if (action.getProperties() != null) {
        for (Entry<String, String> entry : action.getProperties().entrySet()) {
            if (StringUtils.isNotBlank(entry.getKey())) {
                engine.put(entry.getKey(), entry.getValue());
            }
        }
    }

    engine.put("lists", listController);
    engine.put("log", log);

    String script = null;
    if (StringUtils.isNotBlank(action.getResource())) {
        script = resourceController.getResource((String) context.get("boardid"), action.getResource());
    } else {
        script = action.getMethod();
    }

    engine.eval(script);
    Bindings resultBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    for (Entry<String, Object> entry : resultBindings.entrySet()) {
        if (entry.getKey().equals("context") || entry.getKey().equals("print")
                || entry.getKey().equals("println")) {
            continue;
        }
        context.put(entry.getKey(), entry.getValue());
    }
    return context;
}

From source file:org.geoserver.script.ScriptManager.java

public ScriptManager(GeoServerDataDirectory dataDir) {
    this.dataDir = dataDir;
    engineMgr = new ScriptEngineManager();
    sessions = CacheBuilder.newBuilder().maximumSize(10).expireAfterAccess(10, TimeUnit.MINUTES).build();
}