use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class ComposerCommandExecutor method execute.
public static void execute(String[] commandLine, File workDir) throws TimeoutException, IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder(commandLine).redirectErrorStream(true).directory(workDir);
try (WebsocketMessageConsumer<ComposerOutput> websocketMessageConsumer = new WebsocketMessageConsumer<>(Constants.COMPOSER_CHANNEL_NAME)) {
websocketMessageConsumer.consume(new ComposerOutputImpl(String.join(" ", commandLine), ComposerOutput.State.START));
LineConsumer lineConsumer = new AbstractLineConsumer() {
@Override
public void writeLine(String line) throws IOException {
websocketMessageConsumer.consume(new ComposerOutputImpl(line, ComposerOutput.State.IN_PROGRESS));
}
};
// process will be stopped after timeout
Watchdog watcher = new Watchdog(10, TimeUnit.MINUTES);
try {
final Process process = pb.start();
final ValueHolder<Boolean> isTimeoutExceeded = new ValueHolder<>(false);
watcher.start(() -> {
isTimeoutExceeded.set(true);
ProcessUtil.kill(process);
});
// consume logs until process ends
ProcessUtil.process(process, lineConsumer);
process.waitFor();
websocketMessageConsumer.consume(new ComposerOutputImpl("Done", ComposerOutput.State.DONE));
if (isTimeoutExceeded.get()) {
LOG.error("Command time expired : command-line " + Arrays.toString(commandLine));
websocketMessageConsumer.consume(new ComposerOutputImpl("Installing dependencies time expired", ComposerOutput.State.ERROR));
throw new TimeoutException();
} else if (process.exitValue() != 0) {
LOG.error("Command failed : command-line " + Arrays.toString(commandLine));
websocketMessageConsumer.consume(new ComposerOutputImpl("Error occurred", ComposerOutput.State.ERROR));
throw new IOException("Process failed. Exit code " + process.exitValue() + " command-line : " + Arrays.toString(commandLine));
}
} finally {
watcher.stop();
}
}
}
use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class AntUtils method getAntEnvironmentInformation.
public static Map<String, String> getAntEnvironmentInformation() throws IOException {
final Map<String, String> versionInfo = new HashMap<>();
final LineConsumer cmdOutput = new LineConsumer() {
boolean end = false;
@Override
public void writeLine(String line) throws IOException {
if (line.isEmpty()) {
end = true;
}
if (end) {
return;
}
String key = null;
int keyEnd = 0;
int valueStart = 0;
final int l = line.length();
while (keyEnd < l) {
if (line.charAt(keyEnd) == ':') {
valueStart = keyEnd + 1;
break;
}
keyEnd++;
}
if (keyEnd > 0) {
key = line.substring(0, keyEnd);
}
if (key != null) {
while (valueStart < l && Character.isWhitespace(line.charAt(valueStart))) {
valueStart++;
}
if ("Ant version".equals(key)) {
int valueEnd = line.indexOf("compiled on", valueStart);
final String value = line.substring(valueStart, valueEnd).trim();
versionInfo.put(key, value);
} else {
final String value = line.substring(valueStart);
versionInfo.put(key, value);
}
}
}
@Override
public void close() throws IOException {
}
};
readAntEnvironmentInformation(cmdOutput);
return versionInfo;
}
use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class ConcurrentCompositeLineConsumerTest method shouldIgnoreWriteToSubConsumersAfterCloseWasCalled.
@Test
public void shouldIgnoreWriteToSubConsumersAfterCloseWasCalled() throws Exception {
// given
WaitingAnswer<Void> waitingAnswer = new WaitingAnswer<>();
doAnswer(waitingAnswer).when(lineConsumer2).close();
executor.execute(() -> concurrentCompositeLineConsumer.close());
waitingAnswer.waitAnswerCall(1, TimeUnit.SECONDS);
// when
concurrentCompositeLineConsumer.writeLine("Test line");
waitingAnswer.completeAnswer();
// then
awaitFinalization();
for (LineConsumer consumer : subConsumers) {
verify(consumer, never()).writeLine(anyString());
}
}
use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class ConcurrentCompositeLineConsumerTest method shouldWriteMessageIntoEachConsumer.
@Test
public void shouldWriteMessageIntoEachConsumer() throws Exception {
// given
final String message = "Test line";
// when
concurrentCompositeLineConsumer.writeLine(message);
// then
for (LineConsumer subConsumer : subConsumers) {
verify(subConsumer).writeLine(eq(message));
}
}
use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class ConcurrentCompositeLineConsumerTest method shouldBeAbleToWriteIntoSubConsumersSimultaneously.
@Test
public void shouldBeAbleToWriteIntoSubConsumersSimultaneously() throws Exception {
// given
final String message1 = "Message 1";
final String message2 = "Message 2";
WaitingAnswer<Void> waitingAnswer = new WaitingAnswer<>();
doAnswer(waitingAnswer).when(lineConsumer2).writeLine(eq(message1));
executor.execute(() -> concurrentCompositeLineConsumer.writeLine(message1));
waitingAnswer.waitAnswerCall(1, TimeUnit.SECONDS);
// when
concurrentCompositeLineConsumer.writeLine(message2);
waitingAnswer.completeAnswer();
// then
awaitFinalization();
for (LineConsumer consumer : subConsumers) {
verify(consumer).writeLine(eq(message1));
verify(consumer).writeLine(eq(message2));
}
}
Aggregations