Search in sources :

Example 1 with Compilable

use of javax.script.Compilable 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(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            return connected.get();
        }
    });
    when(socket.isClosed()).thenAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            return closed.get();
        }
    });
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            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(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            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;
    unpickleScript = compilable.compile(UNPICKLER_SCRIPT);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Compilable(javax.script.Compilable) ScriptEngineManager(javax.script.ScriptEngineManager) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InetAddress(java.net.InetAddress) ScriptEngine(javax.script.ScriptEngine) Before(org.junit.Before)

Example 2 with Compilable

use of javax.script.Compilable in project dubbo by alibaba.

the class ScriptRouter method route.

@SuppressWarnings("unchecked")
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
    try {
        List<Invoker<T>> invokersCopy = new ArrayList<Invoker<T>>(invokers);
        Compilable compilable = (Compilable) engine;
        Bindings bindings = engine.createBindings();
        bindings.put("invokers", invokersCopy);
        bindings.put("invocation", invocation);
        bindings.put("context", RpcContext.getContext());
        CompiledScript function = compilable.compile(rule);
        Object obj = function.eval(bindings);
        if (obj instanceof Invoker[]) {
            invokersCopy = Arrays.asList((Invoker<T>[]) obj);
        } else if (obj instanceof Object[]) {
            invokersCopy = new ArrayList<Invoker<T>>();
            for (Object inv : (Object[]) obj) {
                invokersCopy.add((Invoker<T>) inv);
            }
        } else {
            invokersCopy = (List<Invoker<T>>) obj;
        }
        return invokersCopy;
    } catch (ScriptException e) {
        //fail then ignore rule .invokers.
        logger.error("route error , rule has been ignored. rule: " + rule + ", method:" + invocation.getMethodName() + ", url: " + RpcContext.getContext().getUrl(), e);
        return invokers;
    }
}
Also used : CompiledScript(javax.script.CompiledScript) ScriptException(javax.script.ScriptException) Invoker(com.alibaba.dubbo.rpc.Invoker) Compilable(javax.script.Compilable) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Bindings(javax.script.Bindings)

Example 3 with Compilable

use of javax.script.Compilable in project es6draft by anba.

the class CompilableTest method setUp.

@Before
public void setUp() {
    manager = new ScriptEngineManager();
    engine = manager.getEngineByName("es6draft");
    assertThat(engine, notNullValue());
    assertThat(engine, instanceOf(Compilable.class));
    compilable = (Compilable) engine;
}
Also used : Compilable(javax.script.Compilable) ScriptEngineManager(javax.script.ScriptEngineManager) Before(org.junit.Before)

Example 4 with Compilable

use of javax.script.Compilable in project jmeter by apache.

the class JSR223TestElement method processFileOrScript.

/**
     * This method will run inline script or file script with special behaviour for file script:
     * - If ScriptEngine implements Compilable script will be compiled and cached
     * - If not if will be run
     * @param scriptEngine ScriptEngine
     * @param bindings {@link Bindings} might be null
     * @return Object returned by script
     * @throws IOException when reading the script fails
     * @throws ScriptException when compiling or evaluation of the script fails
     */
protected Object processFileOrScript(ScriptEngine scriptEngine, Bindings bindings) throws IOException, ScriptException {
    if (bindings == null) {
        bindings = scriptEngine.createBindings();
    }
    populateBindings(bindings);
    File scriptFile = new File(getFilename());
    // Hack: bsh-2.0b5.jar BshScriptEngine implements Compilable but throws
    // "java.lang.Error: unimplemented"
    boolean supportsCompilable = scriptEngine instanceof Compilable && // NOSONAR // $NON-NLS-1$
    !("bsh.engine.BshScriptEngine".equals(scriptEngine.getClass().getName()));
    try {
        if (!StringUtils.isEmpty(getFilename())) {
            if (scriptFile.exists() && scriptFile.canRead()) {
                if (supportsCompilable) {
                    String cacheKey = // $NON-NLS-1$
                    getScriptLanguage() + "#" + scriptFile.getAbsolutePath() + // $NON-NLS-1$
                    "#" + scriptFile.lastModified();
                    CompiledScript compiledScript = compiledScriptsCache.get(cacheKey);
                    if (compiledScript == null) {
                        synchronized (compiledScriptsCache) {
                            compiledScript = compiledScriptsCache.get(cacheKey);
                            if (compiledScript == null) {
                                // TODO Charset ?
                                try (BufferedReader fileReader = new BufferedReader(new FileReader(scriptFile), (int) scriptFile.length())) {
                                    compiledScript = ((Compilable) scriptEngine).compile(fileReader);
                                    compiledScriptsCache.put(cacheKey, compiledScript);
                                }
                            }
                        }
                    }
                    return compiledScript.eval(bindings);
                } else {
                    // TODO Charset ?
                    try (BufferedReader fileReader = new BufferedReader(new FileReader(scriptFile), (int) scriptFile.length())) {
                        return scriptEngine.eval(fileReader, bindings);
                    }
                }
            } else {
                throw new ScriptException("Script file '" + scriptFile.getAbsolutePath() + "' does not exist or is unreadable for element:" + getName());
            }
        } else if (!StringUtils.isEmpty(getScript())) {
            if (supportsCompilable && !StringUtils.isEmpty(cacheKey)) {
                computeScriptMD5();
                CompiledScript compiledScript = compiledScriptsCache.get(this.scriptMd5);
                if (compiledScript == null) {
                    synchronized (compiledScriptsCache) {
                        compiledScript = compiledScriptsCache.get(this.scriptMd5);
                        if (compiledScript == null) {
                            compiledScript = ((Compilable) scriptEngine).compile(getScript());
                            compiledScriptsCache.put(this.scriptMd5, compiledScript);
                        }
                    }
                }
                return compiledScript.eval(bindings);
            } else {
                return scriptEngine.eval(getScript(), bindings);
            }
        } else {
            throw new ScriptException("Both script file and script text are empty for element:" + getName());
        }
    } catch (ScriptException ex) {
        Throwable rootCause = ex.getCause();
        if (isStopCondition(rootCause)) {
            throw (RuntimeException) ex.getCause();
        } else {
            throw ex;
        }
    }
}
Also used : CompiledScript(javax.script.CompiledScript) ScriptException(javax.script.ScriptException) Compilable(javax.script.Compilable) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 5 with Compilable

use of javax.script.Compilable in project OpenAM by OpenRock.

the class StandardScriptValidator method validateScript.

/**
     * {@inheritDoc}
     */
public List<ScriptError> validateScript(ScriptObject script) {
    Reject.ifNull(script);
    final ScriptEngine scriptEngine = script.getLanguage().getScriptEngine(scriptEngineManager);
    final List<ScriptError> scriptErrorList = new ArrayList<ScriptError>();
    if (scriptEngine instanceof Compilable) {
        try {
            ((Compilable) scriptEngine).compile(script.getScript());
        } catch (ScriptException se) {
            // In an ideal world we would receive all errors in the script upon compilation,
            // but for now we can only produce them one at a time.
            scriptErrorList.addAll(getScriptErrors(script, se));
        }
    }
    return scriptErrorList;
}
Also used : ScriptException(javax.script.ScriptException) Compilable(javax.script.Compilable) ArrayList(java.util.ArrayList) ScriptEngine(javax.script.ScriptEngine)

Aggregations

Compilable (javax.script.Compilable)7 CompiledScript (javax.script.CompiledScript)4 ScriptException (javax.script.ScriptException)4 Bindings (javax.script.Bindings)3 ScriptEngine (javax.script.ScriptEngine)3 ScriptEngineManager (javax.script.ScriptEngineManager)3 BufferedReader (java.io.BufferedReader)2 ArrayList (java.util.ArrayList)2 SimpleBindings (javax.script.SimpleBindings)2 Before (org.junit.Before)2 Invoker (com.alibaba.dubbo.rpc.Invoker)1 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 InetAddress (java.net.InetAddress)1