org.etlunit.io.ApacheProcessExecutor Maven / Gradle / Ivy
package org.etlunit.io;
import org.apache.commons.exec.*;
import org.etlunit.ProcessDescription;
import org.etlunit.ProcessExecutor;
import org.etlunit.ProcessFacade;
import org.etlunit.util.Incomplete;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class ApacheProcessExecutor implements ProcessExecutor
{
@Incomplete
@Override
public ProcessFacade execute(final ProcessDescription pd)
{
// apache thinks they are too good for process builders
final CommandLine cmdLine = new CommandLine(pd.getCommand());
Iterator it = pd.getArguments().iterator();
// drop the first entry - that is the command
while (it.hasNext())
{
cmdLine.addArgument(it.next());
}
final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
final ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 1000);
Executor executor = new DefaultExecutor();
try
{
final ModifiedPumpStreamHandler modifiedPumpStreamHandler =
new ModifiedPumpStreamHandler(pd.getOutputFile());
executor.setStreamHandler(modifiedPumpStreamHandler);
executor.setWatchdog(watchdog);
if (pd.getWorkingDirectory() != null)
{
executor.setWorkingDirectory(pd.getWorkingDirectory());
}
Map getenv = new HashMap(System.getenv());
getenv.putAll(pd.getEnvironment());
executor.execute(cmdLine, getenv, resultHandler);
return new ProcessFacade()
{
private StringBuffer bufferedInput;
@Override
public ProcessDescription getDescriptor()
{
return pd;
}
@Override
public void waitForCompletion()
{
try
{
resultHandler.waitFor();
}
catch (InterruptedException e)
{
throw new RuntimeException("Error while waiting for process", e);
}
}
@Override
public int getCompletionCode()
{
return resultHandler.getExitValue();
}
@Override
public void kill()
{
watchdog.destroyProcess();
}
@Incomplete
@Override
public Writer getWriter()
{
return modifiedPumpStreamHandler.getProcessInput();
}
@Override
public void waitForStreams()
{
//wait until the streams have been injected
modifiedPumpStreamHandler.waitForStreams();
}
@Incomplete
@Override
public BufferedReader getReader()
{
return new BufferedReader(modifiedPumpStreamHandler.getProcessOutput());
}
@Incomplete
@Override
public BufferedReader getErrorReader()
{
return new BufferedReader(modifiedPumpStreamHandler.getProcessOutput());
}
@Incomplete
@Override
public StringBuffer getInputBuffered() throws IOException
{
if (bufferedInput != null)
{
return bufferedInput;
}
waitForCompletion();
bufferedInput = new StringBuffer();
BufferedReader bread = new BufferedReader(modifiedPumpStreamHandler.getProcessOutput());
char[] buff = new char[4096];
int read = 0;
while ((read = bread.read(buff)) != -1)
{
bufferedInput.append(buff, 0, read);
}
return bufferedInput;
}
@Incomplete
@Override
public StringBuffer getErrorBuffered() throws IOException
{
return getInputBuffered();
}
@Incomplete
@Override
public OutputStream getOutputStream()
{
return modifiedPumpStreamHandler.getProcessInputStream();
}
@Incomplete
@Override
public InputStream getInputStream()
{
return modifiedPumpStreamHandler.getProcessOutputStream();
}
@Incomplete
@Override
public InputStream getErrorStream()
{
return modifiedPumpStreamHandler.getProcessOutputStream();
}
};
}
catch (IOException e)
{
throw new IllegalArgumentException("Error invoking system process", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy