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);
}
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;
}
}
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;
}
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;
}
}
}
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;
}
Aggregations