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<>();
}
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);
}
});
}
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;
}
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;
}
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;
}
Aggregations