Search in sources :

Example 1 with Pipeline

use of org.apache.felix.gogo.runtime.Parser.Pipeline 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)

Example 2 with Pipeline

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

the class TestParser method testPipeRedir.

@Test
public void testPipeRedir() {
    Program x = new Parser("abc def|&ghi").program();
    Pipeline p0 = (Pipeline) x.tokens().get(0);
    Statement s00 = (Statement) p0.tokens().get(0);
    assertEquals("|&", p0.tokens().get(1).toString());
    Statement s01 = (Statement) p0.tokens().get(2);
    assertEquals("abc", s00.tokens().get(0).toString());
    assertEquals("def", s00.tokens().get(1).toString());
    assertEquals("ghi", s01.tokens().get(0).toString());
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Pipeline(org.apache.felix.gogo.runtime.Parser.Pipeline) Test(org.junit.Test)

Example 3 with Pipeline

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

the class TestParser method testProgram.

@Test
public void testProgram() {
    Program x = new Parser("abc def|ghi jkl;mno pqr|stu vwx").program();
    Pipeline p0 = (Pipeline) x.tokens().get(0);
    Statement s00 = (Statement) p0.tokens().get(0);
    assertEquals("|", p0.tokens().get(1).toString());
    Statement s01 = (Statement) p0.tokens().get(2);
    assertEquals(";", x.tokens().get(1).toString());
    Pipeline p1 = (Pipeline) x.tokens().get(2);
    Statement s10 = (Statement) p1.tokens().get(0);
    assertEquals("|", p1.tokens().get(1).toString());
    Statement s11 = (Statement) p1.tokens().get(2);
    assertEquals("abc", s00.tokens().get(0).toString());
    assertEquals("def", s00.tokens().get(1).toString());
    assertEquals("ghi", s01.tokens().get(0).toString());
    assertEquals("jkl", s01.tokens().get(1).toString());
    assertEquals("mno", s10.tokens().get(0).toString());
    assertEquals("pqr", s10.tokens().get(1).toString());
    assertEquals("stu", s11.tokens().get(0).toString());
    assertEquals("vwx", s11.tokens().get(1).toString());
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Pipeline(org.apache.felix.gogo.runtime.Parser.Pipeline) Test(org.junit.Test)

Example 4 with Pipeline

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

the class TestParser method testPipeAndOr.

@Test
public void testPipeAndOr() {
    Program x = new Parser("abc|def&&ghi || jkl").program();
    Pipeline p0 = (Pipeline) x.tokens().get(0);
    Statement s00 = (Statement) p0.tokens().get(0);
    assertEquals("|", p0.tokens().get(1).toString());
    Statement s01 = (Statement) p0.tokens().get(2);
    assertEquals("&&", x.tokens().get(1).toString());
    Statement s1 = (Statement) x.tokens().get(2);
    assertEquals("||", x.tokens().get(3).toString());
    Statement s2 = (Statement) x.tokens().get(4);
    assertEquals("abc", s00.tokens().get(0).toString());
    assertEquals("def", s01.tokens().get(0).toString());
    assertEquals("ghi", s1.tokens().get(0).toString());
    assertEquals("jkl", s2.tokens().get(0).toString());
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Pipeline(org.apache.felix.gogo.runtime.Parser.Pipeline) Test(org.junit.Test)

Example 5 with Pipeline

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

the class TestParser method testStatements.

@Test
public void testStatements() {
    Program x = new Parser("abc def|ghi jkl|mno pqr").program();
    Pipeline p0 = (Pipeline) x.tokens().get(0);
    Statement s00 = (Statement) p0.tokens().get(0);
    Statement s01 = (Statement) p0.tokens().get(2);
    Statement s02 = (Statement) p0.tokens().get(4);
    assertEquals("abc", s00.tokens().get(0).toString());
    assertEquals("def", s00.tokens().get(1).toString());
    assertEquals("ghi", s01.tokens().get(0).toString());
    assertEquals("jkl", s01.tokens().get(1).toString());
    assertEquals("mno", s02.tokens().get(0).toString());
    assertEquals("pqr", s02.tokens().get(1).toString());
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Pipeline(org.apache.felix.gogo.runtime.Parser.Pipeline) Test(org.junit.Test)

Aggregations

Pipeline (org.apache.felix.gogo.runtime.Parser.Pipeline)6 Statement (org.apache.felix.gogo.runtime.Parser.Statement)6 Program (org.apache.felix.gogo.runtime.Parser.Program)5 Test (org.junit.Test)5 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 Sequence (org.apache.felix.gogo.runtime.Parser.Sequence)1 Result (org.apache.felix.gogo.runtime.Pipe.Result)1