FS: Extend readPipe() to optionally take additional environment
Change-Id: I4db7763826e4ada92074317d4d1c9a32299f3af8 Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
parent
8ed6b745bb
commit
b8b6357fe6
|
@ -57,6 +57,7 @@
|
||||||
import java.security.PrivilegedAction;
|
import java.security.PrivilegedAction;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
@ -407,13 +408,37 @@ protected static File searchPath(final String path, final String... lookFor) {
|
||||||
* @return the one-line output of the command
|
* @return the one-line output of the command
|
||||||
*/
|
*/
|
||||||
protected static String readPipe(File dir, String[] command, String encoding) {
|
protected static String readPipe(File dir, String[] command, String encoding) {
|
||||||
|
return readPipe(dir, command, encoding, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a command and return a single line of output as a String
|
||||||
|
*
|
||||||
|
* @param dir
|
||||||
|
* Working directory for the command
|
||||||
|
* @param command
|
||||||
|
* as component array
|
||||||
|
* @param encoding
|
||||||
|
* to be used to parse the command's output
|
||||||
|
* @param env
|
||||||
|
* Map of environment variables to be merged with those of the
|
||||||
|
* current process
|
||||||
|
* @return the one-line output of the command
|
||||||
|
* @since 4.0
|
||||||
|
*/
|
||||||
|
protected static String readPipe(File dir, String[] command, String encoding, Map<String, String> env) {
|
||||||
final boolean debug = LOG.isDebugEnabled();
|
final boolean debug = LOG.isDebugEnabled();
|
||||||
try {
|
try {
|
||||||
if (debug) {
|
if (debug) {
|
||||||
LOG.debug("readpipe " + Arrays.asList(command) + "," //$NON-NLS-1$ //$NON-NLS-2$
|
LOG.debug("readpipe " + Arrays.asList(command) + "," //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
+ dir);
|
+ dir);
|
||||||
}
|
}
|
||||||
final Process p = Runtime.getRuntime().exec(command, null, dir);
|
ProcessBuilder pb = new ProcessBuilder(command);
|
||||||
|
pb.directory(dir);
|
||||||
|
if (env != null) {
|
||||||
|
pb.environment().putAll(env);
|
||||||
|
}
|
||||||
|
final Process p = pb.start();
|
||||||
final BufferedReader lineRead = new BufferedReader(
|
final BufferedReader lineRead = new BufferedReader(
|
||||||
new InputStreamReader(p.getInputStream(), encoding));
|
new InputStreamReader(p.getInputStream(), encoding));
|
||||||
p.getOutputStream().close();
|
p.getOutputStream().close();
|
||||||
|
|
Loading…
Reference in New Issue