Search in sources :

Example 1 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project cas by apereo.

the class ScriptedRegisteredServiceAttributeReleasePolicy method getAttributesInternal.

@Override
protected Map<String, Object> getAttributesInternal(final Map<String, Object> attributes, final RegisteredService service) {
    try {
        String engineName = null;
        if (this.scriptFile.endsWith(".py")) {
            engineName = "python";
        } else if (this.scriptFile.endsWith(".js")) {
            engineName = "js";
        } else if (this.scriptFile.endsWith(".groovy")) {
            engineName = "groovy";
        }
        final ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
        if (engine == null || StringUtils.isBlank(engineName)) {
            LOGGER.warn("Script engine is not available for [{}]", engineName);
        } else {
            final File theScriptFile = ResourceUtils.getResourceFrom(this.scriptFile).getFile();
            if (theScriptFile.exists()) {
                LOGGER.debug("Created python object instance from class [{}]", theScriptFile.getCanonicalPath());
                final Object[] args = { attributes, LOGGER };
                LOGGER.debug("Executing python script's run method, with parameters [{}]", args);
                engine.eval(new FileReader(theScriptFile));
                final Invocable invocable = (Invocable) engine;
                final Map<String, Object> personAttributesMap = (Map<String, Object>) invocable.invokeFunction("run", args);
                LOGGER.debug("Final set of attributes determined by the script are [{}]", personAttributesMap);
                return personAttributesMap;
            }
            LOGGER.warn("Python script [{}] does not exist, or cannot be loaded", scriptFile);
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return new HashMap<>();
}
Also used : Invocable(javax.script.Invocable) HashMap(java.util.HashMap) ScriptEngineManager(javax.script.ScriptEngineManager) FileReader(java.io.FileReader) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) ScriptEngine(javax.script.ScriptEngine)

Example 2 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project jOOQ by jOOQ.

the class NashornTest method testScripts.

@Test
public void testScripts() throws Exception {
    Stream.of(new File(getClass().getResource("/org/jooq/example/test").toURI()).listFiles((dir, name) -> name.endsWith(".js"))).forEach(file -> {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("nashorn");
        Bindings bindings = engine.getBindings(ENGINE_SCOPE);
        bindings.put("connection", connection);
        try {
            engine.eval(new FileReader(file));
        } catch (Exception e) {
            throw new RuntimeException("Error while running " + file, e);
        }
    });
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) FileReader(java.io.FileReader) File(java.io.File) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine) Test(org.junit.Test)

Example 3 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project opennms by OpenNMS.

the class OSGiScriptEngineManager method getEngineByMimeType.

public ScriptEngine getEngineByMimeType(String mimeType) {
    //TODO this is a hack to deal with context class loader issues
    ScriptEngine engine = null;
    for (ScriptEngineManager manager : classLoaders.keySet()) {
        ClassLoader old = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(classLoaders.get(manager));
        engine = manager.getEngineByMimeType(mimeType);
        Thread.currentThread().setContextClassLoader(old);
        if (engine != null)
            break;
    }
    return engine;
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 4 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project opennms by OpenNMS.

the class OSGiScriptEngineManager method getEngineByExtension.

public ScriptEngine getEngineByExtension(String extension) {
    //TODO this is a hack to deal with context class loader issues
    ScriptEngine engine = null;
    for (ScriptEngineManager manager : classLoaders.keySet()) {
        ClassLoader old = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(classLoaders.get(manager));
        engine = manager.getEngineByExtension(extension);
        Thread.currentThread().setContextClassLoader(old);
        if (engine != null)
            break;
    }
    return engine;
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 5 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project POL-POM-5 by PlayOnLinux.

the class NashornEngineFactory method createEngine.

NashornEngine createEngine() {
    final Set<List<String>> includedScripts = new HashSet<>();
    final NashornEngine nashornEngine = new NashornEngine(new ScriptEngineManager().getEngineByName("nashorn"));
    nashornEngine.eval(new InputStreamReader(getClass().getResourceAsStream("utils.js")), this::throwException);
    nashornEngine.put("Bean", (Function<String, Object>) title -> applicationContext.getBean(title), this::throwException);
    nashornEngine.put("SetupWizard", (Function<String, UiSetupWizardImplementation>) (name) -> {
        final UiSetupWizardImplementation uiSetupWizardImplementation = uiSetupWizardFactory.create(name);
        nashornEngine.addErrorHandler(e -> uiSetupWizardImplementation.close());
        return uiSetupWizardImplementation;
    }, this::throwException);
    nashornEngine.put("EngineProgressUi", (Function<String, UiProgressWizardImplementation>) (name) -> {
        final UiProgressWizardImplementation uiProgressWizardImplementation = uiProgressWizardFactory.create(name);
        nashornEngine.addErrorHandler(e -> uiProgressWizardImplementation.close());
        return uiProgressWizardImplementation;
    }, this::throwException);
    nashornEngine.put("include", (Consumer<ScriptObjectMirror>) args -> {
        final String[] arguments = args.to(String[].class);
        final String script = scriptFetcher.getScript(arguments);
        if (script == null) {
            throwException(new ScriptException(Arrays.asList(arguments).toString() + " is not found"));
        }
        if (includedScripts.add(Arrays.asList(arguments))) {
            nashornEngine.eval("//# sourceURL=" + Arrays.asList(arguments).toString() + "\n" + script, this::throwException);
        }
    }, this::throwException);
    return nashornEngine;
}
Also used : Arrays(java.util.Arrays) UiProgressWizardFactory(org.phoenicis.scripts.wizard.UiProgressWizardFactory) ScriptException(org.phoenicis.scripts.interpreter.ScriptException) UiSetupWizardFactory(org.phoenicis.scripts.wizard.UiSetupWizardFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ScriptEngineManager(javax.script.ScriptEngineManager) Set(java.util.Set) ScriptFetcher(org.phoenicis.scripts.interpreter.ScriptFetcher) ApplicationContext(org.springframework.context.ApplicationContext) InputStreamReader(java.io.InputStreamReader) Function(java.util.function.Function) UiSetupWizardImplementation(org.phoenicis.scripts.wizard.UiSetupWizardImplementation) HashSet(java.util.HashSet) Consumer(java.util.function.Consumer) List(java.util.List) UiProgressWizardImplementation(org.phoenicis.scripts.wizard.UiProgressWizardImplementation) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InputStreamReader(java.io.InputStreamReader) UiSetupWizardImplementation(org.phoenicis.scripts.wizard.UiSetupWizardImplementation) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptException(org.phoenicis.scripts.interpreter.ScriptException) UiProgressWizardImplementation(org.phoenicis.scripts.wizard.UiProgressWizardImplementation) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

ScriptEngineManager (javax.script.ScriptEngineManager)231 ScriptEngine (javax.script.ScriptEngine)158 ScriptException (javax.script.ScriptException)62 Test (org.junit.Test)32 IOException (java.io.IOException)31 ScriptEngineFactory (javax.script.ScriptEngineFactory)26 File (java.io.File)21 Bindings (javax.script.Bindings)21 Invocable (javax.script.Invocable)21 Reader (java.io.Reader)18 FileReader (java.io.FileReader)15 SimpleBindings (javax.script.SimpleBindings)15 Map (java.util.Map)14 InputStreamReader (java.io.InputStreamReader)13 HashMap (java.util.HashMap)12 Compilable (javax.script.Compilable)11 QuickTest (com.hazelcast.test.annotation.QuickTest)10 CompiledScript (javax.script.CompiledScript)10 Before (org.junit.Before)10 URL (java.net.URL)8