/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/**
* Wrapper to Java Process API
*
* @author Emilio Monti - emilmont@gmail.com
*/
public class Executor {
private byte[] stdout;
private byte[] stderr;
/**
* Create a new instance of the Executor.
* @param cmd An array containing the command to execute
*/
}
/**
* Create a new instance of the Executor.
* @param cmdList A list containing the command to execute
*/
}
/**
* Create a new instance of the Executor
* @param cmdList A list containing the command to execute
* @param workingDirectory The directory the process should have as the
* working directory
*/
this.workingDirectory = workingDirectory;
}
/**
* Create a new instance of the Executor with specific timeout value.
* @param cmdList A list containing the command to execute
* @param workingDirectory The directory the process should have as the
* working directory
* @param timeout If the command runs longer than the timeout (seconds),
* it will be terminated. If the value is 0, no timer
* will be set up.
*/
this.workingDirectory = workingDirectory;
}
/**
* Create a new instance of the Executor with or without timeout,
* @param cmdList A list containing the command to execute
* @param workingDirectory The directory the process should have as the
* working directory
* @param UseTimeout terminate the process after default timeout or not
*/
this(cmdList, workingDirectory);
if (!UseTimeout) {
this.timeout = 0;
}
}
/**
* Execute the command and collect the output. All exceptions will be
* logged.
*
* @return The exit code of the process
*/
public int exec() {
return exec(true);
}
/**
* Execute the command and collect the output
*
* @param reportExceptions Should exceptions be added to the log or not
* @return The exit code of the process
*/
return ret;
}
/**
* Execute the command and collect the output
*
* @param reportExceptions Should exceptions be added to the log or not
* @param handler The handler to handle data from standard output
* @return The exit code of the process
*/
int ret = -1;
if (workingDirectory != null) {
}
}
} else {
}
"Executing command {0} in directory {1}",
try {
public void run() {
try {
} catch (IOException ex) {
if (reportExceptions) {
"Error during process pipe listening", ex);
}
}
}
});
/*
* Setup timer so if the process get stuck we can terminate it and
* make progress instead of hanging the whole indexer.
*/
if (this.timeout != 0) {
"Terminating process of command {0} in directory {1} " +
"due to timeout {2} seconds",
}
}, this.timeout);
}
"Finished command {0} in directory {1}",
if (this.timeout != 0) {
t.cancel();
}
} catch (IOException e) {
if (reportExceptions) {
}
} catch (InterruptedException e) {
if (reportExceptions) {
}
} finally {
try {
}
} catch (IllegalThreadStateException e) {
}
}
}
.append(" in directory ")
} else {
}
}
}
return ret;
}
/**
* Get the output from the process as a string.
*
* @return The output from the process
*/
}
return ret;
}
/**
* Get a reader to read the output from the process
*
* @return A reader reading the process output
*/
return new InputStreamReader(getOutputStream());
}
/**
* Get an input stream read the output from the process
*
* @return A reader reading the process output
*/
return new ByteArrayInputStream(stdout);
}
/**
* Get the output from the process written to the error stream as a string.
*
* @return The error output from the process
*/
}
return ret;
}
/**
* Get a reader to read the output the process wrote to the error stream.
*
* @return A reader reading the process error stream
*/
return new InputStreamReader(getErrorStream());
}
/**
* Get an input stream to read the output the process wrote to the error stream.
*
* @return An input stream for reading the process error stream
*/
return new ByteArrayInputStream(stderr);
}
/**
* You should use the StreamHandler interface if you would like to process
* the output from a process while it is running
*/
public static interface StreamHandler {
/**
* Process the data in the stream. The processStream function is
* called _once_ during the lifetime of the process, and you should
* process all of the input you want before returning from the function.
*
* @param in The InputStream containing the data
* @throws java.io.IOException if any read error
*/
}
public byte[] getBytes() {
return bytes.toByteArray();
}
byte[] buffer = new byte[8092];
int len;
if (len > 0) {
}
}
}
}
public static void registerErrorHandler() {
+ e.getMessage(), e);
}
});
}
}
}