All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.laskowski.shell.runner.DefaultShellTaskRunner Maven / Gradle / Ivy

Go to download

Library to launch .bat and .sh scripts of your choice with different configurations. Allows you to read tasks and services output from terminal

The newest version!
package io.github.laskowski.shell.runner;

import io.github.laskowski.shell.exceptions.ErrorDetectedException;
import io.github.laskowski.shell.output.OutputListener;
import io.github.laskowski.shell.output.Publisher;
import io.github.laskowski.shell.output.StringSubscriber;
import io.github.laskowski.shell.output.service.ServiceReadyStrategy;
import io.github.laskowski.shell.task.ShellTask;
import io.github.rafal.laskowski.wait.Wait;

import java.util.ArrayList;
import java.util.List;

public class DefaultShellTaskRunner implements ShellTaskRunner {
    private static DefaultShellTaskRunner taskRunner;
    private final List> tasks = new ArrayList<>();

    private DefaultShellTaskRunner() {}

    public synchronized static DefaultShellTaskRunner getInstance() {
        if (taskRunner == null) {
            taskRunner = new DefaultShellTaskRunner();
        }
        return taskRunner;
    }

    @Override
    public void run(ShellTask shellTask, OutputListener outputListener, ServiceReadyStrategy serviceReadyStrategy) {
        Publisher publisher = outputListener.getPublisher();
        StringSubscriber subscriber = outputListener.getSubscriber();
        Thread outputListenerThread = new Thread(() -> {
            Process process = shellTask.getProcess();

            publisher.registerSubscriber(subscriber);
            publisher.startPublishing(process);
        });

        outputListenerThread.start();

        Thread processThread = new Thread(shellTask::start);
        processThread.start();

        new Wait()
                .withTimeout(serviceReadyStrategy.getTimeout())
                .withMessage(serviceReadyStrategy.getTimeoutMessage())
                .until(() -> {
                    try {
                        return subscriber.getLines().stream().anyMatch(serviceReadyStrategy.getReadyPredicate());
                    } catch (ErrorDetectedException e) {
                        return shellTask.getErrorHandler().handle(e);
                    }
                });

        tasks.add(shellTask);
    }

    @Override
    public void stop(ShellTask shellTask) {
        tasks.remove(shellTask);
        shellTask.stop();
    }

    @Override
    public void stopAll() {
        tasks.forEach(ShellTask::stop);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy