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

com.prezi.pride.ProcessUtils Maven / Gradle / Ivy

There is a newer version: 0.12.3
Show newest version
package com.prezi.pride;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ProcessUtils {

	private static final Logger logger = LoggerFactory.getLogger(ProcessUtils.class);

	public static Process executeIn(File directory, List commandLine, boolean processOutput, boolean redirectErrorStream) throws IOException {
		List stringCommandLine = new ArrayList();
		for (Object item : commandLine) {
			stringCommandLine.add(String.valueOf(item));
		}
		logger.debug("Executing in {}: {}", directory != null ? directory : System.getProperty("user.dir"), StringUtils.join(stringCommandLine, " "));
		ProcessBuilder builder = new ProcessBuilder(stringCommandLine);
		builder.directory(directory);
		if (redirectErrorStream) {
			builder.redirectErrorStream(true);
		}

		Process process = builder.start();

		if (processOutput) {
			BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
			try {
				String line;
				while ((line = errorReader.readLine()) != null) {
					logger.warn("{}", line);
				}
			} finally {
				errorReader.close();
			}
			BufferedReader outputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			try {
				String line;
				while ((line = outputReader.readLine()) != null) {
					logger.info("{}", line);
				}
			} finally {
				outputReader.close();
			}
		}

		try {
			process.waitFor();
		} catch (InterruptedException e) {
			throw new IOException("Interrupted", e);
		}
		int result = process.exitValue();
		if (result != 0) {
			String output;
			if (!processOutput) {
				List outputLines = new ArrayList();
				outputLines.addAll(IOUtils.readLines(process.getErrorStream()));
				outputLines.addAll(IOUtils.readLines(process.getInputStream()));
				output = ", output:\n" + StringUtils.join(outputLines, '\n');
			} else {
				output = "";
			}
			throw new PrideException("Failed to execute \"" + StringUtils.join(commandLine, " ") + "\" in \"" + directory + "\", exit code: " + result + output);
		}

		return process;
	}

	public static Process executeIn(File directory, List commandLine, boolean processOutput) throws IOException {
		return ProcessUtils.executeIn(directory, commandLine, processOutput, true);
	}

	public static Process executeIn(File directory, List commandLine) throws IOException {
		return ProcessUtils.executeIn(directory, commandLine, true, true);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy