ExternalProcessRunner.java

/*-
 * ========================LICENSE_START=================================
 * flyway-core
 * ========================================================================
 * Copyright (C) 2010 - 2026 Red Gate Software Ltd
 * ========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =========================LICENSE_END==================================
 */
package org.flywaydb.core.internal.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import org.flywaydb.core.api.CoreErrorCode;
import org.flywaydb.core.api.FlywayException;

public class ExternalProcessRunner {

    public int run(final String[] command,
        final Consumer<? super String> onStdOut,
        final Consumer<? super String> onStdErr) throws IOException {
        return run(command, null, null, onStdOut, onStdErr);
    }

    public int run(final String[] command,
        final Path workingDirectory,
        final Consumer<? super String> onStdOut,
        final Consumer<? super String> onStdErr) throws IOException {
        return run(command, workingDirectory, null, onStdOut, onStdErr);
    }

    public int run(final String[] command,
        final Path workingDirectory,
        final String stdIn,
        final Consumer<? super String> onStdOut,
        final Consumer<? super String> onStdErr) throws IOException {
        return run(command, workingDirectory, null, stdIn, onStdOut, onStdErr);
    }

    @SuppressWarnings("MethodWithTooManyParameters")
    public int run(final String[] command,
        final Path workingDirectory,
        final Map<String, String> env,
        final String stdIn,
        final Consumer<? super String> onStdOut,
        final Consumer<? super String> onStdErr) throws IOException {
        final var processBuilder = new ProcessBuilder(command);
        if (workingDirectory != null) {
            processBuilder.directory(workingDirectory.toFile());
        }
        if (env != null) {
            processBuilder.environment().putAll(env);
        }
        final var process = processBuilder.start();

        final var executorService = Executors.newFixedThreadPool(2);
        try (final var outStream = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8);
             final var errStream = new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8);
             final var inputSteam = new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8);
             final var stdOut = new Scanner(outStream);
             final var stdErr = new Scanner(errStream)) {
            final var handleStdOut = executorService.submit(() -> {
                while (stdOut.hasNextLine()) {
                    onStdOut.accept(stdOut.nextLine());
                }
            });
            final var handleStdErr = executorService.submit(() -> {
                while (stdErr.hasNextLine()) {
                    onStdErr.accept(stdErr.nextLine());
                }
            });

            if (stdIn != null) {
                inputSteam.write(stdIn);
            }
            inputSteam.close();

            awaitCompletion(handleStdOut);
            awaitCompletion(handleStdErr);
        } finally {
            executorService.shutdown();
        }

        return waitFor(process);
    }

    private static void awaitCompletion(final Future<?> future) {
        try {
            future.get();
        } catch (final InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new FlywayException("Interrupted while reading external process output", e, CoreErrorCode.FAULT);
        } catch (final ExecutionException e) {
            throw new FlywayException("Failed to read external process output", e.getCause(), CoreErrorCode.FAULT);
        }
    }

    private static int waitFor(final Process process) {
        try {
            return process.waitFor();
        } catch (final InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new FlywayException("Interrupted while waiting for external process to complete",
                e,
                CoreErrorCode.FAULT);
        }
    }

    public int run(final ProcessBuilder processBuilder,
        final Path workingDirectory,
        final Map<String, String> env,
        final String stdIn) throws IOException {

        if (workingDirectory != null) {
            processBuilder.directory(workingDirectory.toFile());
        }
        if (env != null) {
            processBuilder.environment().putAll(env);
        }
        final var process = processBuilder.start();

        if (stdIn != null) {
            try (final var inputSteam = new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8)) {
                inputSteam.write(stdIn);
            }
        }

        return waitFor(process);
    }
}