use of javax.script.ScriptContext in project JMRI by JMRI.
the class Jdk9Application method getContext.
private ScriptContext getContext(Object handler) {
Bindings bindings = new SimpleBindings();
// NOI18N
bindings.put("handler", handler);
ScriptContext context = new SimpleScriptContext();
context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
return context;
}
use of javax.script.ScriptContext 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.ScriptContext in project accumulo by apache.
the class ScriptCommand method execute.
@Override
public int execute(String fullCommand, CommandLine cl, Shell shellState) throws Exception {
boolean invoke = false;
ScriptEngineManager mgr = new ScriptEngineManager();
if (cl.hasOption(list.getOpt())) {
listJSREngineInfo(mgr, shellState);
} else if (cl.hasOption(file.getOpt()) || cl.hasOption(script.getOpt())) {
String engineName = DEFAULT_ENGINE;
if (cl.hasOption(engine.getOpt())) {
engineName = cl.getOptionValue(engine.getOpt());
}
ScriptEngine engine = mgr.getEngineByName(engineName);
if (null == engine) {
shellState.printException(new Exception(engineName + " not found"));
return 1;
}
if (cl.hasOption(object.getOpt()) || cl.hasOption(function.getOpt())) {
if (!(engine instanceof Invocable)) {
shellState.printException(new Exception(engineName + " does not support invoking functions or methods"));
return 1;
}
invoke = true;
}
ScriptContext ctx = new SimpleScriptContext();
// Put the following objects into the context so that they
// are available to the scripts
// TODO: What else should go in here?
Bindings b = engine.getBindings(ScriptContext.ENGINE_SCOPE);
b.put("connection", shellState.getConnector());
List<Object> argValues = new ArrayList<>();
if (cl.hasOption(args.getOpt())) {
String[] argList = cl.getOptionValue(args.getOpt()).split(",");
for (String arg : argList) {
String[] parts = arg.split("=");
if (parts.length == 0) {
continue;
} else if (parts.length == 1) {
b.put(parts[0], null);
argValues.add(null);
} else if (parts.length == 2) {
b.put(parts[0], parts[1]);
argValues.add(parts[1]);
}
}
}
ctx.setBindings(b, ScriptContext.ENGINE_SCOPE);
Object[] argArray = argValues.toArray(new Object[argValues.size()]);
Writer writer = null;
if (cl.hasOption(out.getOpt())) {
File f = new File(cl.getOptionValue(out.getOpt()));
writer = new FileWriter(f);
ctx.setWriter(writer);
}
if (cl.hasOption(file.getOpt())) {
File f = new File(cl.getOptionValue(file.getOpt()));
if (!f.exists()) {
if (null != writer) {
writer.close();
}
shellState.printException(new Exception(f.getAbsolutePath() + " not found"));
return 1;
}
Reader reader = new FileReader(f);
try {
engine.eval(reader, ctx);
if (invoke) {
this.invokeFunctionOrMethod(shellState, engine, cl, argArray);
}
} catch (ScriptException ex) {
shellState.printException(ex);
return 1;
} finally {
reader.close();
if (null != writer) {
writer.close();
}
}
} else if (cl.hasOption(script.getOpt())) {
String inlineScript = cl.getOptionValue(script.getOpt());
try {
if (engine instanceof Compilable) {
Compilable compiledEng = (Compilable) engine;
CompiledScript script = compiledEng.compile(inlineScript);
script.eval(ctx);
if (invoke) {
this.invokeFunctionOrMethod(shellState, engine, cl, argArray);
}
} else {
engine.eval(inlineScript, ctx);
if (invoke) {
this.invokeFunctionOrMethod(shellState, engine, cl, argArray);
}
}
} catch (ScriptException ex) {
shellState.printException(ex);
return 1;
} finally {
if (null != writer) {
writer.close();
}
}
}
if (null != writer) {
writer.close();
}
} else {
printHelp(shellState);
}
return 0;
}
use of javax.script.ScriptContext in project smarthome by eclipse.
the class AbstractScriptModuleHandler method setExecutionContext.
/**
* Adds the passed context variables of the rule engine to the context scope of the ScriptEngine, this should be
* updated each time the module is executed
*
* @param engine the scriptengine that is used
* @param context the variables and types to put into the execution context
*/
protected void setExecutionContext(ScriptEngine engine, Map<String, ?> context) {
ScriptContext executionContext = engine.getContext();
// Add the rule's UID to the context and make it available as "ctx".
// Note: We don't use "context" here as it doesn't work on all JVM versions!
final Map<String, Object> contextNew = new HashMap<>(context);
contextNew.put("ruleUID", this.ruleUID);
executionContext.setAttribute("ctx", contextNew, ScriptContext.ENGINE_SCOPE);
// Add the rule's UID to the global namespace.
executionContext.setAttribute("ruleUID", this.ruleUID, ScriptContext.ENGINE_SCOPE);
// add the single context entries without their prefix to the scope
for (Entry<String, ?> entry : context.entrySet()) {
Object value = entry.getValue();
String key = entry.getKey();
int dotIndex = key.indexOf('.');
if (dotIndex != -1) {
key = key.substring(dotIndex + 1);
}
executionContext.setAttribute(key, value, ScriptContext.ENGINE_SCOPE);
}
}
use of javax.script.ScriptContext in project xwiki-platform by xwiki.
the class UntypedEventListener method evaluateVelocity.
private XDOM evaluateVelocity(Event event, Object source, DocumentReference userReference, String templateContent) throws Exception {
ScriptContext currentScriptContext = scriptContextManager.getCurrentScriptContext();
currentScriptContext.setAttribute(EVENT_BINDING_NAME, event, ScriptContext.ENGINE_SCOPE);
currentScriptContext.setAttribute(SOURCE_BINDING_NAME, source, ScriptContext.ENGINE_SCOPE);
try {
Template customTemplate = templateManager.createStringTemplate(templateContent, userReference);
return templateManager.execute(customTemplate);
} finally {
currentScriptContext.removeAttribute(EVENT_BINDING_NAME, ScriptContext.ENGINE_SCOPE);
currentScriptContext.removeAttribute(SOURCE_BINDING_NAME, ScriptContext.ENGINE_SCOPE);
}
}
Aggregations