Search in sources :

Example 81 with ScriptEngine

use of javax.script.ScriptEngine in project meecrowave by apache.

the class MeecrowaveRunMojo method scriptCustomization.

private void scriptCustomization(final List<String> customizers, final String ext, final Map<String, Object> customBindings) {
    if (customizers == null || customizers.isEmpty()) {
        return;
    }
    final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension(ext);
    if (engine == null) {
        throw new IllegalStateException("No engine for " + ext + ". Maybe add the JSR223 implementation as plugin dependency.");
    }
    for (final String js : customizers) {
        try {
            final SimpleBindings bindings = new SimpleBindings();
            bindings.put("project", project);
            engine.eval(new StringReader(js), bindings);
            bindings.putAll(customBindings);
        } catch (final ScriptException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }
}
Also used : ScriptException(javax.script.ScriptException) SimpleBindings(javax.script.SimpleBindings) ScriptEngineManager(javax.script.ScriptEngineManager) StringReader(java.io.StringReader) ScriptEngine(javax.script.ScriptEngine)

Example 82 with ScriptEngine

use of javax.script.ScriptEngine in project metrics by dropwizard.

the class PickledGraphiteTest method setUp.

@Before
public void setUp() throws Exception {
    final AtomicBoolean connected = new AtomicBoolean(true);
    final AtomicBoolean closed = new AtomicBoolean(false);
    when(socket.isConnected()).thenAnswer(invocation -> connected.get());
    when(socket.isClosed()).thenAnswer(invocation -> closed.get());
    doAnswer(invocation -> {
        connected.set(false);
        closed.set(true);
        return null;
    }).when(socket).close();
    when(socket.getOutputStream()).thenReturn(output);
    // Mock behavior of socket.getOutputStream().close() calling socket.close();
    doAnswer(invocation -> {
        invocation.callRealMethod();
        socket.close();
        return null;
    }).when(output).close();
    when(socketFactory.createSocket(any(InetAddress.class), anyInt())).thenReturn(socket);
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
    Compilable compilable = (Compilable) engine;
    try (InputStream is = PickledGraphiteTest.class.getResource("/upickle.py").openStream()) {
        unpickleScript = compilable.compile(new InputStreamReader(is, UTF_8));
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Compilable(javax.script.Compilable) ScriptEngineManager(javax.script.ScriptEngineManager) InetAddress(java.net.InetAddress) ScriptEngine(javax.script.ScriptEngine) Before(org.junit.Before)

Example 83 with ScriptEngine

use of javax.script.ScriptEngine 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 84 with ScriptEngine

use of javax.script.ScriptEngine 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 85 with ScriptEngine

use of javax.script.ScriptEngine in project tomee by apache.

the class OpenEJBScripter method evaluate.

public Object evaluate(final String language, final String script, final ScriptContext context) throws ScriptException {
    if (!ENGINE_FACTORIES.containsKey(language)) {
        throw new IllegalArgumentException("can't find factory for language " + language + ". You probably need to add the jar to openejb libs.");
    }
    ScriptContext executionContext = context;
    if (executionContext == null) {
        executionContext = new SimpleScriptContext();
    }
    // we bind local variables (per execution) every time we execute a script
    bindLocal(executionContext);
    final ScriptEngine engine = engine(language);
    return engine.eval(script, executionContext);
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) ScriptEngine(javax.script.ScriptEngine)

Aggregations

ScriptEngine (javax.script.ScriptEngine)175 ScriptEngineManager (javax.script.ScriptEngineManager)70 ScriptException (javax.script.ScriptException)46 Test (org.junit.Test)29 Bindings (javax.script.Bindings)20 IOException (java.io.IOException)19 Map (java.util.Map)14 Invocable (javax.script.Invocable)13 File (java.io.File)11 FileReader (java.io.FileReader)11 ScriptEngineFactory (javax.script.ScriptEngineFactory)11 HashMap (java.util.HashMap)10 ScriptContext (javax.script.ScriptContext)10 SimpleBindings (javax.script.SimpleBindings)10 InputStreamReader (java.io.InputStreamReader)9 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)9 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)9 PrintWriter (java.io.PrintWriter)7 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)7 JRubyTest (org.enumerable.lambda.support.jruby.JRubyTest)7