Search in sources :

Example 46 with ScriptEngineManager

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;
}
Also used : ScriptEngineFactory(javax.script.ScriptEngineFactory) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 47 with ScriptEngineManager

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));
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 48 with ScriptEngineManager

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;
}
Also used : SimpleBindings(javax.script.SimpleBindings) ScriptEngineManager(javax.script.ScriptEngineManager) GroovyObject(groovy.lang.GroovyObject) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ScriptEngine(javax.script.ScriptEngine)

Example 49 with ScriptEngineManager

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");
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) ScriptEngineManager(javax.script.ScriptEngineManager) URLClassLoader(java.net.URLClassLoader) File(java.io.File) URL(java.net.URL) ScriptEngine(javax.script.ScriptEngine) ScriptException(javax.script.ScriptException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 50 with ScriptEngineManager

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);
}
Also used : StringWriter(java.io.StringWriter) ScriptEngineManager(javax.script.ScriptEngineManager) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) ScriptEngine(javax.script.ScriptEngine) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

ScriptEngineManager (javax.script.ScriptEngineManager)109 ScriptEngine (javax.script.ScriptEngine)66 Test (org.junit.Test)23 ScriptException (javax.script.ScriptException)20 ScriptEngineFactory (javax.script.ScriptEngineFactory)14 File (java.io.File)12 Invocable (javax.script.Invocable)11 QuickTest (com.hazelcast.test.annotation.QuickTest)10 IOException (java.io.IOException)10 FileReader (java.io.FileReader)9 Bindings (javax.script.Bindings)9 Before (org.junit.Before)9 Map (java.util.Map)8 GroovyScriptEngineFactory (org.codehaus.groovy.jsr223.GroovyScriptEngineFactory)8 Reader (java.io.Reader)7 HashMap (java.util.HashMap)6 SimpleBindings (javax.script.SimpleBindings)6 PrintWriter (java.io.PrintWriter)4 StringWriter (java.io.StringWriter)4 InputStreamReader (java.io.InputStreamReader)3