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;
}
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());
}
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());
}
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());
}
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());
}
Aggregations