use of javax.script.ScriptEngine in project cas by apereo.
the class ScriptingUtils method executeGroovyScriptEngine.
/**
* Execute inline groovy script engine.
*
* @param <T> the type parameter
* @param script the script
* @param variables the variables
* @param clazz the clazz
* @return the t
*/
public static <T> T executeGroovyScriptEngine(final String script, final Map<String, Object> variables, final Class<T> clazz) {
try {
final ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");
if (engine == null) {
LOGGER.warn("Script engine is not available for Groovy");
return null;
}
final Bindings binding = new SimpleBindings();
if (variables != null && !variables.isEmpty()) {
binding.putAll(variables);
}
if (!binding.containsKey("logger")) {
binding.put("logger", LOGGER);
}
final Object result = engine.eval(script, binding);
if (result != null && !clazz.isAssignableFrom(result.getClass())) {
throw new ClassCastException("Result [" + result + " is of type " + result.getClass() + " when we were expecting " + clazz);
}
return (T) result;
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
return null;
}
use of javax.script.ScriptEngine in project vcell by virtualcell.
the class ComsolServiceScripting method solve.
@Override
public void solve(VCCModel vccModel, File reportFile, File javaFile, File mphFile) {
ClassLoader origClassLoader = null;
File comsolRootDir = VCellConfiguration.getFileProperty(PropertyLoader.comsolRootDir);
File comsolJarDir = VCellConfiguration.getFileProperty(PropertyLoader.comsolJarDir);
if (comsolRootDir == null || !comsolRootDir.exists()) {
throw new RuntimeException("path to COMSOL root directory \"" + comsolRootDir + "\" not found, update preferences (or " + PropertyLoader.comsolRootDir + " in vcell.config)");
}
if (comsolJarDir == null || !comsolJarDir.exists()) {
throw new RuntimeException("path to COMSOL plugin directory \"" + comsolJarDir + "\" not found, update preferences (or " + PropertyLoader.comsolJarDir + " in vcell.config)");
}
try {
origClassLoader = Thread.currentThread().getContextClassLoader();
System.setProperty("cs.root", comsolRootDir.getAbsolutePath());
if (comsolClassloader == null) {
ArrayList<URL> urls = new ArrayList<URL>();
for (File f : comsolJarDir.listFiles()) {
if (f.isFile() && f.getName().endsWith(".jar")) {
try {
urls.add(f.toURI().toURL());
} catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("failed to get URL for comsol dependency " + f.getAbsolutePath());
}
}
}
comsolClassloader = new URLClassLoader(urls.toArray(new URL[0]), ClassLoader.getSystemClassLoader().getParent());
}
Thread.currentThread().setContextClassLoader(comsolClassloader);
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
File jsFile = new File(javaFile.getParentFile(), javaFile.getName().replace(".java", ".js"));
run(engine, vccModel, reportFile, javaFile, mphFile, jsFile);
} catch (Exception e) {
e.printStackTrace(System.err);
} finally {
Thread.currentThread().setContextClassLoader(origClassLoader);
System.out.println("disconnecting");
disconnectComsol();
System.out.println("disconnected");
}
}
use of javax.script.ScriptEngine in project scheduling by ow2-proactive.
the class JavaClassScriptEngineFactoryTest method replication_index.
@Test
public void replication_index() throws Exception {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("java");
StringWriter output = new StringWriter();
StringWriter error = new StringWriter();
engine.getContext().setWriter(output);
engine.getContext().setErrorWriter(new PrintWriter(error));
VariablesMap variables = new VariablesMap();
variables.getInheritedMap().put(SchedulerVars.PA_TASK_REPLICATION.toString(), 42);
engine.getContext().setAttribute("variables", variables, ScriptContext.ENGINE_SCOPE);
Object result = engine.eval(ReturnReplicationIndex.class.getName());
assertEquals(42, result);
}
use of javax.script.ScriptEngine in project scheduling by ow2-proactive.
the class JavaClassScriptEngineFactoryTest method executable_with_localspace.
@Test
public void executable_with_localspace() throws Exception {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("java");
StringWriter output = new StringWriter();
StringWriter error = new StringWriter();
engine.getContext().setWriter(output);
engine.getContext().setErrorWriter(new PrintWriter(error));
String localSpacePath = new File(".").getAbsolutePath();
engine.getContext().setAttribute(SchedulerConstants.DS_SCRATCH_BINDING_NAME, localSpacePath, ScriptContext.ENGINE_SCOPE);
String result = (String) engine.eval(ReturnLocalSpace.class.getName());
assertEquals(result, localSpacePath);
}
use of javax.script.ScriptEngine in project scheduling by ow2-proactive.
the class AbstractIModeCommand method execute.
@Override
public void execute(ApplicationContext currentContext) throws CLIException {
currentContext.setProperty(IMODE, true);
ScriptEngine engine = currentContext.getEngine();
try {
// load supported functions
engine.eval(new InputStreamReader(script()));
} catch (ScriptException error) {
throw new CLIException(CLIException.REASON_OTHER, error);
}
while (!currentContext.getProperty(TERMINATE, Boolean.TYPE, false)) {
try {
String command = readLine(currentContext, "> ");
if (command == null) {
// EOF, exit interactive shell
break;
}
engine.eval(command);
} catch (ScriptException se) {
handleError(String.format("An error occurred while executing the script:"), se, currentContext);
}
}
}
Aggregations