use of javax.script.Bindings in project sling by apache.
the class EsxScriptEngine method eval.
@Override
public Object eval(Reader reader, ScriptContext context) throws ScriptException {
log.debug("starting to eval ESX Script");
Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
SlingScriptHelper scriptHelper = (SlingScriptHelper) bindings.get("sling");
Resource scriptResource = scriptHelper.getScript().getScriptResource();
Resource resource = scriptHelper.getRequest().getResource();
ModuleScript moduleScript = new ModuleScript(ModuleScript.JS_FILE, scriptResource);
//public Module (EsxScriptEngineFactory factory, Resource resource, ModuleScript moduleScript, String id, Module parent, SlingScriptHelper scriptHelper) throws ScriptException {
Module module = new Module((EsxScriptEngineFactory) getFactory(), resource, moduleScript, scriptResource.getPath(), null, scriptHelper, Module.LOADER_JS);
try {
Object moduleResults = module.require(scriptResource.getPath());
String result = ((EsxScriptEngineFactory) getFactory()).getNashornEngine().eval("if(exports.render && typeof exports.render === 'function') { exports.render('test'); }" + " else if(typeof exports === 'class') { new exports().render('function') } else {" + "'You need to define either a render function or export an object with a render method'; }", module).toString();
context.getWriter().write(result);
} catch (IOException ex) {
throw new ScriptException(ex);
}
return null;
}
use of javax.script.Bindings 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.Bindings in project metrics by dropwizard.
the class PickledGraphiteTest method unpickleOutput.
private String unpickleOutput() throws Exception {
StringBuilder results = new StringBuilder();
// the charset is important. if the GraphitePickleReporter and this test
// don't agree, the header is not always correctly unpacked.
String payload = output.toString("UTF-8");
PyList result = new PyList();
int nextIndex = 0;
while (nextIndex < payload.length()) {
Bindings bindings = new SimpleBindings();
bindings.put("payload", payload.substring(nextIndex));
unpickleScript.eval(bindings);
result.addAll(result.size(), (PyList) bindings.get("metrics"));
nextIndex += (Integer) bindings.get("batchLength");
}
for (Object aResult : result) {
PyTuple datapoint = (PyTuple) aResult;
String name = datapoint.get(0).toString();
PyTuple valueTuple = (PyTuple) datapoint.get(1);
Object timestamp = valueTuple.get(0);
Object value = valueTuple.get(1);
results.append(name).append(" ").append(value).append(" ").append(timestamp).append("\n");
}
return results.toString();
}
use of javax.script.Bindings in project Lucee by lucee.
the class ScriptEngineImpl method getContext.
private ScriptContext getContext(Bindings b) {
ScriptContext def = getContext();
SimpleScriptContext custom = new SimpleScriptContext();
Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE);
if (gs != null)
custom.setBindings(gs, ScriptContext.GLOBAL_SCOPE);
custom.setBindings(b, ScriptContext.ENGINE_SCOPE);
custom.setReader(def.getReader());
custom.setWriter(def.getWriter());
custom.setErrorWriter(def.getErrorWriter());
return custom;
}
use of javax.script.Bindings 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;
}
Aggregations