Search in sources :

Example 6 with SimpleScriptContext

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);
    }
}
Also used : AbacPermission(gov.ca.cwds.security.permission.AbacPermission) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Collection(java.util.Collection)

Example 7 with SimpleScriptContext

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;
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings)

Example 8 with SimpleScriptContext

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;
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings)

Example 9 with SimpleScriptContext

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;
}
Also used : CompiledScript(javax.script.CompiledScript) SimpleScriptContext(javax.script.SimpleScriptContext) FileWriter(java.io.FileWriter) Compilable(javax.script.Compilable) ScriptEngineManager(javax.script.ScriptEngineManager) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Reader(java.io.Reader) FileReader(java.io.FileReader) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine) ScriptException(javax.script.ScriptException) IOException(java.io.IOException) Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) ArrayList(java.util.ArrayList) List(java.util.List) FileReader(java.io.FileReader) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 10 with SimpleScriptContext

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;
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) StringReader(java.io.StringReader) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Block(org.xwiki.rendering.block.Block) NewLineBlock(org.xwiki.rendering.block.NewLineBlock)

Aggregations

SimpleScriptContext (javax.script.SimpleScriptContext)74 ScriptContext (javax.script.ScriptContext)59 Bindings (javax.script.Bindings)30 SimpleBindings (javax.script.SimpleBindings)26 Test (org.junit.Test)26 ScriptEngine (javax.script.ScriptEngine)10 ScriptException (javax.script.ScriptException)9 CompiledScript (javax.script.CompiledScript)8 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)7 ScriptEngineManager (javax.script.ScriptEngineManager)6 File (java.io.File)4 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 Reader (java.io.Reader)3 StringReader (java.io.StringReader)3 Invocable (javax.script.Invocable)3 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2