use of javax.script.SimpleScriptContext in project perry by ca-cwds.
the class AbacMethodInterceptor method checkPermission.
@SuppressWarnings("unchecked")
private void checkPermission(String permission, Object arg) throws ScriptException {
AbacPermission abacPermission = new AbacPermission(permission);
String selector = abacPermission.getSecuredObject().toString();
int dotIndex = selector.indexOf('.');
String identifier;
if (dotIndex == -1) {
identifier = selector;
} else {
identifier = selector.substring(0, dotIndex);
}
ScriptContext scriptContext = new SimpleScriptContext();
scriptContext.setAttribute(identifier, arg, ScriptContext.ENGINE_SCOPE);
for (Object o : (Collection<Object>) scriptEngine.eval("[" + selector + "].flatten()", scriptContext)) {
abacPermission.setSecuredObject(o);
SecurityUtils.getSubject().checkPermission(abacPermission);
}
}
use of javax.script.SimpleScriptContext 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.SimpleScriptContext 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.SimpleScriptContext 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.SimpleScriptContext in project xwiki-platform by xwiki.
the class PygmentsParser method highlight.
/**
* Return a highlighted version of the provided content.
*
* @param syntaxId the identifier of the source syntax.
* @param code the content to highlight.
* @return the highlighted version of the provided source.
* @throws ScriptException when failed to execute the script
* @throws ParseException when failed to parse the content as plain text
*/
private List<Block> highlight(String syntaxId, String code) throws ScriptException, ParseException {
BlocksGeneratorPygmentsListener listener = new BlocksGeneratorPygmentsListener(this.plainTextParser);
ScriptContext scriptContext = new SimpleScriptContext();
scriptContext.setAttribute(PY_LANGUAGE_VARNAME, syntaxId, ScriptContext.ENGINE_SCOPE);
scriptContext.setAttribute(PY_CODE_VARNAME, code, ScriptContext.ENGINE_SCOPE);
scriptContext.setAttribute(PY_STYLE_VARNAME, this.configuration.getStyle(), ScriptContext.ENGINE_SCOPE);
scriptContext.setAttribute(PY_LISTENER_VARNAME, listener, ScriptContext.ENGINE_SCOPE);
this.engine.eval(this.script, scriptContext);
List<Block> blocks;
if (scriptContext.getAttribute(PY_LEXER_VARNAME) != null) {
blocks = listener.getBlocks();
} else {
blocks = this.plainTextParser.parse(new StringReader(code)).getChildren().get(0).getChildren();
}
return blocks;
}
Aggregations