Search in sources :

Example 1 with Operator

use of org.apache.felix.gogo.runtime.Parser.Operator in project felix by apache.

the class Closure method execute.

@SuppressWarnings("unchecked")
private Object execute(List<Object> values, Channel capturingOutput) throws Exception {
    if (null != values) {
        parmv = new ArrayList<>(values);
        parms = new ArgList(parmv);
    } else if (null != parent) {
        // inherit parent closure parameters
        parmv = parent.parmv != null ? new ArrayList<>(parent.parmv) : null;
        parms = parmv != null ? new ArgList(parmv) : null;
    } else {
        // inherit session parameters
        Object args = session.get("args");
        if (null != args && args instanceof List<?>) {
            parmv = new ArrayList<>((List<Object>) args);
            parms = new ArgList(parmv);
        }
    }
    Result last = null;
    Operator operator = null;
    for (Iterator<Executable> iterator = program.tokens().iterator(); iterator.hasNext(); ) {
        Operator prevOperator = operator;
        Executable executable = iterator.next();
        if (iterator.hasNext()) {
            operator = (Operator) iterator.next();
        } else {
            operator = null;
        }
        if (prevOperator != null) {
            if (Token.eq("&&", prevOperator)) {
                if (!last.isSuccess()) {
                    continue;
                }
            } else if (Token.eq("||", prevOperator)) {
                if (last.isSuccess()) {
                    continue;
                }
            }
        }
        Channel[] streams;
        boolean[] toclose = new boolean[10];
        if (Pipe.getCurrentPipe() != null) {
            streams = Pipe.getCurrentPipe().streams.clone();
        } else {
            streams = new Channel[10];
            System.arraycopy(session.channels, 0, streams, 0, 3);
        }
        if (capturingOutput != null) {
            streams[1] = capturingOutput;
            toclose[1] = true;
        }
        CommandSessionImpl.JobImpl job;
        if (executable instanceof Pipeline) {
            Pipeline pipeline = (Pipeline) executable;
            List<Executable> exec = pipeline.tokens();
            Token s = exec.get(0);
            Token e = exec.get(exec.size() - 1);
            Token t = program.subSequence(s.start - program.start, e.start + e.length - program.start);
            job = session().createJob(t);
            for (int i = 0; i < exec.size(); i++) {
                Statement ex = (Statement) exec.get(i);
                Operator op = i < exec.size() - 1 ? (Operator) exec.get(++i) : null;
                Channel[] nstreams;
                boolean[] ntoclose;
                boolean endOfPipe;
                if (i == exec.size() - 1) {
                    nstreams = streams;
                    ntoclose = toclose;
                    endOfPipe = true;
                } else if (Token.eq("|", op)) {
                    PipedInputStream pis = new PipedInputStream();
                    PipedOutputStream pos = new PipedOutputStream(pis);
                    nstreams = streams.clone();
                    nstreams[1] = Channels.newChannel(pos);
                    ntoclose = toclose.clone();
                    ntoclose[1] = true;
                    streams[0] = Channels.newChannel(pis);
                    toclose[0] = true;
                    endOfPipe = false;
                } else if (Token.eq("|&", op)) {
                    PipedInputStream pis = new PipedInputStream();
                    PipedOutputStream pos = new PipedOutputStream(pis);
                    nstreams = streams.clone();
                    nstreams[1] = nstreams[2] = Channels.newChannel(pos);
                    ntoclose = toclose.clone();
                    ntoclose[1] = ntoclose[2] = true;
                    streams[0] = Channels.newChannel(pis);
                    toclose[0] = true;
                    endOfPipe = false;
                } else {
                    throw new IllegalStateException("Unrecognized pipe operator: '" + op + "'");
                }
                Pipe pipe = new Pipe(this, job, ex, nstreams, ntoclose, endOfPipe);
                job.addPipe(pipe);
            }
        } else {
            job = session().createJob(executable);
            Pipe pipe = new Pipe(this, job, (Statement) executable, streams, toclose, true);
            job.addPipe(pipe);
        }
        // Start pipe in background
        if (operator != null && Token.eq("&", operator)) {
            job.start(Status.Background);
            last = new Result((Object) null);
        } else // Start in foreground and wait for results
        {
            last = job.start(Status.Foreground);
            if (last == null) {
                last = new Result((Object) null);
            } else if (last.exception != null) {
                throw last.exception;
            }
        }
    }
    return last == null ? null : last.result;
}
Also used : Operator(org.apache.felix.gogo.runtime.Parser.Operator) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Channel(java.nio.channels.Channel) ArrayList(java.util.ArrayList) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Result(org.apache.felix.gogo.runtime.Pipe.Result) Pipeline(org.apache.felix.gogo.runtime.Parser.Pipeline) ArrayList(java.util.ArrayList) List(java.util.List) Executable(org.apache.felix.gogo.runtime.Parser.Executable)

Aggregations

PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 Channel (java.nio.channels.Channel)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Executable (org.apache.felix.gogo.runtime.Parser.Executable)1 Operator (org.apache.felix.gogo.runtime.Parser.Operator)1 Pipeline (org.apache.felix.gogo.runtime.Parser.Pipeline)1 Statement (org.apache.felix.gogo.runtime.Parser.Statement)1 Result (org.apache.felix.gogo.runtime.Pipe.Result)1