use of org.apache.commons.exec.Executor in project oxCore by GluuFederation.
the class ProcessHelper method executeProgram.
/**
* @param printJobTimeout
* the printJobTimeout (ms) before the watchdog terminates the print
* process
* @param printInBackground
* printing done in the background or blocking
* @param streamHandler
* @return a print result handler (implementing a future)
* @throws IOException
* the test failed
*/
public static PrintResultHandler executeProgram(CommandLine commandLine, String workingDirectory, long printJobTimeout, boolean printInBackground, int successExitValue, ExecuteStreamHandler streamHandler) throws IOException {
ExecuteWatchdog watchdog = null;
PrintResultHandler resultHandler;
// Create the executor and consider the successExitValue as success
Executor executor = new DefaultExecutor();
executor.setExitValue(successExitValue);
if (StringHelper.isNotEmpty(workingDirectory)) {
executor.setWorkingDirectory(new File(workingDirectory));
}
// Redirect streams if needed
if (streamHandler != null) {
executor.setStreamHandler(streamHandler);
}
// Create a watchdog if requested
if (printJobTimeout > 0) {
watchdog = new ExecuteWatchdog(printJobTimeout);
executor.setWatchdog(watchdog);
}
// Pass a "ExecuteResultHandler" when doing background printing
if (printInBackground) {
LOG.debug(String.format("Executing non-blocking process %s", commandLine.toString()));
resultHandler = new PrintResultHandler(watchdog);
executor.execute(commandLine, resultHandler);
} else {
LOG.debug(String.format("Executing blocking process %s", commandLine.toString()));
successExitValue = executor.execute(commandLine);
resultHandler = new PrintResultHandler(successExitValue);
}
return resultHandler;
}
use of org.apache.commons.exec.Executor in project jaqy by Teradata.
the class Os method shell.
public void shell(File dir, String cmd) throws Exception {
CommandLine commandLine;
if (s_windows) {
commandLine = new CommandLine("cmd");
commandLine.addArgument("/C");
commandLine.addArgument(cmd, false);
} else {
commandLine = new CommandLine("/bin/bash");
commandLine.addArgument("-c");
commandLine.addArgument(cmd, false);
}
Executor executor = new DefaultExecutor();
executor.setWorkingDirectory(dir);
executor.setStreamHandler(new PumpStreamHandler(System.out, System.out));
executor.execute(commandLine);
}
use of org.apache.commons.exec.Executor in project scala-maven-plugin by davidB.
the class JavaMainCallerByFork method run.
@Override
public boolean run(boolean displayCmd, boolean throwFailure) throws Exception {
List<String> cmd = buildCommand();
displayCmd(displayCmd, cmd);
Executor exec = new DefaultExecutor();
// err and out are redirected to out
if (!_redirectToLog) {
exec.setStreamHandler(new PumpStreamHandler(System.out, System.err, System.in));
} else {
exec.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {
private LevelState _previous = new LevelState();
@Override
protected void processLine(String line, int level) {
try {
_previous = LogProcessorUtils.levelStateOf(line, _previous);
switch(_previous.level) {
case ERROR:
requester.getLog().error(line);
break;
case WARNING:
requester.getLog().warn(line);
break;
default:
requester.getLog().info(line);
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}));
}
CommandLine cl = new CommandLine(cmd.get(0));
for (int i = 1; i < cmd.size(); i++) {
cl.addArgument(cmd.get(i), false);
}
try {
int exitValue = exec.execute(cl);
if (exitValue != 0) {
if (throwFailure) {
throw new MojoFailureException("command line returned non-zero value:" + exitValue);
}
return false;
}
if (!displayCmd)
tryDeleteArgFile(cmd);
return true;
} catch (ExecuteException exc) {
if (throwFailure) {
throw exc;
}
return false;
}
}
use of org.apache.commons.exec.Executor in project BroadleafCommerce by BroadleafCommerce.
the class MvelHelperTest method executeExternalJavaProcess.
protected String executeExternalJavaProcess(Class<?> mainClass) throws IOException {
String classpath = MvelTestUtils.getClassPath();
// See javadoc on MvelOverloadFailureReproduction for description of why we need to execute the test in a new JVM
CommandLine cmdLine = new CommandLine("java");
cmdLine.addArgument("-cp");
cmdLine.addArgument(classpath, true);
cmdLine.addArgument(mainClass.getName());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Executor executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(baos));
try {
executor.execute(cmdLine, new HashMap<String, String>());
} catch (IOException e) {
throw new IOException(new String(baos.toByteArray()));
}
return new String(baos.toByteArray());
}
use of org.apache.commons.exec.Executor in project georocket by georocket.
the class ElasticsearchRunner method runElasticsearch.
/**
* Run Elasticsearch
* @param host the host Elasticsearch should bind to
* @param port the port Elasticsearch should listen on
* @param elasticsearchInstallPath the path where Elasticsearch is installed
* @return an observable that emits exactly one item when Elasticsearch has started
*/
public Observable<Void> runElasticsearch(String host, int port, String elasticsearchInstallPath) {
JsonObject config = vertx.getOrCreateContext().config();
String storage = config.getString(ConfigConstants.STORAGE_FILE_PATH);
String root = storage + "/index";
return vertx.<Void>rxExecuteBlocking(f -> {
log.info("Starting Elasticsearch ...");
// get Elasticsearch executable
String executable = FilenameUtils.separatorsToSystem(elasticsearchInstallPath);
executable = FilenameUtils.concat(executable, "bin");
if (SystemUtils.IS_OS_WINDOWS) {
executable = FilenameUtils.concat(executable, "elasticsearch.bat");
} else {
executable = FilenameUtils.concat(executable, "elasticsearch");
}
// start Elasticsearch
CommandLine cmdl = new CommandLine(executable);
cmdl.addArgument("-Ecluster.name=georocket-cluster");
cmdl.addArgument("-Enode.name=georocket-node");
cmdl.addArgument("-Enetwork.host=" + host);
cmdl.addArgument("-Ehttp.port=" + port);
cmdl.addArgument("-Epath.data=" + root + "/data");
executor = new DefaultExecutor();
executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
executor.setWatchdog(new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT));
// set ES_JAVA_OPTS environment variable if necessary
Map<String, String> env = new HashMap<>(System.getenv());
if (!env.containsKey("ES_JAVA_OPTS")) {
String javaOpts = config.getString(ConfigConstants.INDEX_ELASTICSEARCH_JAVA_OPTS);
if (javaOpts != null) {
env.put("ES_JAVA_OPTS", javaOpts);
}
}
try {
executor.execute(cmdl, env, new DefaultExecuteResultHandler() {
@Override
public void onProcessComplete(final int exitValue) {
log.info("Elasticsearch quit with exit code: " + exitValue);
}
@Override
public void onProcessFailed(final ExecuteException e) {
if (!stopped) {
log.error("Elasticsearch execution failed", e);
}
}
});
f.complete();
} catch (IOException e) {
f.fail(e);
}
}).toObservable();
}
Aggregations