use of javax.script.ScriptEngineManager in project Lucee by lucee.
the class Script2 method getScriptEngine.
public ScriptEngine getScriptEngine() throws PageException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName(language);
// get engine by engine name
if (engine == null) {
Iterator<ScriptEngineFactory> it = manager.getEngineFactories().iterator();
ScriptEngineFactory factory;
while (it.hasNext()) {
factory = it.next();
if (language.equalsIgnoreCase(factory.getEngineName())) {
engine = factory.getScriptEngine();
break;
}
}
}
// get engine by language name
if (engine == null) {
Iterator<ScriptEngineFactory> it = manager.getEngineFactories().iterator();
ScriptEngineFactory factory;
while (it.hasNext()) {
factory = it.next();
if (language.equalsIgnoreCase(factory.getEngineName())) {
engine = factory.getScriptEngine();
break;
}
}
}
if (engine == null) {
Iterator<ScriptEngineFactory> it = manager.getEngineFactories().iterator();
ScriptEngineFactory factory;
StringBuilder sb = new StringBuilder();
while (it.hasNext()) {
factory = it.next();
if (sb.length() > 0)
sb.append(',');
sb.append(factory.getEngineName());
}
throw this.engine.getExceptionUtil().createApplicationException("language [" + language + "] is not supported, supported languages are [" + sb + "]");
}
return engine;
}
use of javax.script.ScriptEngineManager in project Lucee by lucee.
the class Main method main.
public static void main(final String[] args) throws Exception {
String lang = "CFML";
String code = null;
String arg;
final String pw = null, key = null;
for (int i = 0; i < args.length; i++) {
arg = args[i];
if ("-l".equals(arg)) {
if (args.length > i + 1)
lang = args[++i].trim();
} else if ("-e".equals(arg))
if (args.length > i + 1)
code = args[++i].trim();
}
final int dialect = CFMLEngine.DIALECT_CFML;
if (code == null)
printUsage("-e is missing", System.err);
final LuceeScriptEngineFactory factory = new LuceeScriptEngineFactory();
System.out.println(factory.getScriptEngine().eval(code));
final ScriptEngine engine = new ScriptEngineManager().getEngineByName(lang);
if (engine == null)
System.out.println("could not load a engine with the name:" + lang);
else
System.out.println(engine.eval(code));
}
use of javax.script.ScriptEngineManager 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.ScriptEngineManager 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.ScriptEngineManager 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);
}
Aggregations