org.netarchivesuite.heritrix3wrapper.CommandLauncher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of heritrix3-wrapper Show documentation
Show all versions of heritrix3-wrapper Show documentation
Wrapper library for unpacking and communicating with Heritrix 3.
package org.netarchivesuite.heritrix3wrapper;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;
public class CommandLauncher {
public ProcessBuilder processBuilder;
public Map env;
public Process process;
protected CommandLauncher() {
}
public static CommandLauncher getInstance() {
return new CommandLauncher();
}
public void init(File basedir, String[] cmd) {
processBuilder = new ProcessBuilder(cmd);
processBuilder.directory(basedir);
env = processBuilder.environment();
}
public void init(File basedir, List cmd) {
processBuilder = new ProcessBuilder(cmd);
processBuilder.directory(basedir);
env = processBuilder.environment();
}
public void start(LaunchResultHandlerAbstract resultHandler) throws IOException {
process = processBuilder.start();
Thread waitForThread = new Thread(new WaitForThread(resultHandler));
waitForThread.start();
BufferedReader outputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
Thread outputSinkThread = new Thread(new OutputSinkThread(outputReader, resultHandler));
outputSinkThread.start();
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
Thread errorSinkThread = new Thread(new ErrorSinkThread(errorReader, resultHandler));
errorSinkThread.start();
}
protected class WaitForThread implements Runnable {
LaunchResultHandlerAbstract resultHandler;
public WaitForThread(LaunchResultHandlerAbstract resultHandler) {
this.resultHandler = resultHandler;
}
@Override
public void run() {
Integer exitValue = null;
try {
exitValue = process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (resultHandler != null) {
resultHandler.exitValue(exitValue);
}
}
}
protected class OutputSinkThread implements Runnable {
LaunchResultHandlerAbstract resultHandler;
BufferedReader reader;
protected OutputSinkThread(BufferedReader reader, LaunchResultHandlerAbstract resultHandler) {
this.reader = reader;
this.resultHandler = resultHandler;
}
@Override
public void run() {
String line;
try {
while ((line = reader.readLine()) != null) {
if (resultHandler != null) {
resultHandler.output(line);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (resultHandler != null) {
resultHandler.closeOutput();
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
protected class ErrorSinkThread implements Runnable {
LaunchResultHandlerAbstract resultHandler;
BufferedReader reader;
protected ErrorSinkThread(BufferedReader reader, LaunchResultHandlerAbstract resultHandler) {
this.reader = reader;
this.resultHandler = resultHandler;
}
@Override
public void run() {
String line;
try {
while ((line = reader.readLine()) != null) {
if (resultHandler != null) {
resultHandler.error(line);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (resultHandler != null) {
resultHandler.closeError();
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}