use of org.apache.commons.exec.CommandLine in project camel by apache.
the class DefaultExecCommandExecutor method toCommandLine.
/**
* Transforms an {@link ExecCommand} to a {@link CommandLine}. No quoting fo
* the arguments is used.
*
* @param execCommand a not-null <code>ExecCommand</code> instance.
* @return a {@link CommandLine} object.
*/
protected CommandLine toCommandLine(ExecCommand execCommand) {
notNull(execCommand, "execCommand");
CommandLine cl = new CommandLine(execCommand.getExecutable());
List<String> args = execCommand.getArgs();
for (String arg : args) {
// do not handle quoting here, it is already quoted
cl.addArgument(arg, false);
}
return cl;
}
use of org.apache.commons.exec.CommandLine in project hive by apache.
the class ExecServiceImpl method auxRun.
private ExecBean auxRun(String program, List<String> args, Map<String, String> env) throws NotAuthorizedException, ExecuteException, IOException {
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValues(null);
// Setup stdout and stderr
int nbytes = appConf.getInt(AppConfig.EXEC_MAX_BYTES_NAME, -1);
ByteArrayOutputStream outStream = new MaxByteArrayOutputStream(nbytes);
ByteArrayOutputStream errStream = new MaxByteArrayOutputStream(nbytes);
executor.setStreamHandler(new PumpStreamHandler(outStream, errStream));
// Only run for N milliseconds
int timeout = appConf.getInt(AppConfig.EXEC_TIMEOUT_NAME, 0);
ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
executor.setWatchdog(watchdog);
CommandLine cmd = makeCommandLine(program, args);
LOG.info("Running: " + cmd);
ExecBean res = new ExecBean();
res.exitcode = executor.execute(cmd, execEnv(env));
String enc = appConf.get(AppConfig.EXEC_ENCODING_NAME);
res.stdout = outStream.toString(enc);
res.stderr = errStream.toString(enc);
try {
watchdog.checkException();
} catch (Exception ex) {
LOG.error("Command: " + cmd + " failed. res=" + res, ex);
}
if (watchdog.killedProcess()) {
String msg = " was terminated due to timeout(" + timeout + "ms). See " + AppConfig.EXEC_TIMEOUT_NAME + " property";
LOG.warn("Command: " + cmd + msg + " res=" + res);
res.stderr += " Command " + msg;
}
if (res.exitcode != 0) {
LOG.info("Command: " + cmd + " failed. res=" + res);
}
return res;
}
use of org.apache.commons.exec.CommandLine in project zeppelin by apache.
the class ShellInterpreter method interpret.
@Override
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
LOGGER.debug("Run shell command '" + cmd + "'");
OutputStream outStream = new ByteArrayOutputStream();
CommandLine cmdLine = CommandLine.parse(shell);
// they need to be delimited by '&&' instead
if (isWindows) {
String[] lines = StringUtils.split(cmd, "\n");
cmd = StringUtils.join(lines, " && ");
}
cmdLine.addArgument(cmd, false);
try {
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(contextInterpreter.out, contextInterpreter.out));
executor.setWatchdog(new ExecuteWatchdog(Long.valueOf(getProperty(TIMEOUT_PROPERTY))));
executors.put(contextInterpreter.getParagraphId(), executor);
int exitVal = executor.execute(cmdLine);
LOGGER.info("Paragraph " + contextInterpreter.getParagraphId() + " return with exit value: " + exitVal);
return new InterpreterResult(Code.SUCCESS, outStream.toString());
} catch (ExecuteException e) {
int exitValue = e.getExitValue();
LOGGER.error("Can not run " + cmd, e);
Code code = Code.ERROR;
String message = outStream.toString();
if (exitValue == 143) {
code = Code.INCOMPLETE;
message += "Paragraph received a SIGTERM\n";
LOGGER.info("The paragraph " + contextInterpreter.getParagraphId() + " stopped executing: " + message);
}
message += "ExitValue: " + exitValue;
return new InterpreterResult(code, message);
} catch (IOException e) {
LOGGER.error("Can not run " + cmd, e);
return new InterpreterResult(Code.ERROR, e.getMessage());
} finally {
executors.remove(contextInterpreter.getParagraphId());
}
}
use of org.apache.commons.exec.CommandLine in project elastic-job by dangdangdotcom.
the class TaskLaunchScheduledService method buildCommand.
private Protos.CommandInfo buildCommand(final Protos.CommandInfo.URI uri, final String bootstrapScript, final ShardingContexts shardingContexts, final boolean useDefaultExecutor) {
Protos.CommandInfo.Builder result = Protos.CommandInfo.newBuilder().addUris(uri).setShell(true);
if (useDefaultExecutor) {
CommandLine commandLine = CommandLine.parse(bootstrapScript);
commandLine.addArgument(GsonFactory.getGson().toJson(shardingContexts), false);
result.setValue(Joiner.on(" ").join(commandLine.getExecutable(), Joiner.on(" ").join(commandLine.getArguments())));
} else {
result.setValue(bootstrapScript);
}
return result.build();
}
use of org.apache.commons.exec.CommandLine in project frontend-maven-plugin by eirslett.
the class ProcessExecutor method createCommandLine.
private CommandLine createCommandLine(List<String> command) {
CommandLine commmandLine = new CommandLine(command.get(0));
for (int i = 1; i < command.size(); i++) {
String argument = command.get(i);
commmandLine.addArgument(argument, false);
}
return commmandLine;
}
Aggregations