use of org.apache.commons.exec.CommandLine in project jstorm by alibaba.
the class JStormUtils method launchProcess.
/**
* If it is backend, please set resultHandler, such as DefaultExecuteResultHandler
* If it is frontend, ByteArrayOutputStream.toString will return the calling result
* <p/>
* This function will ignore whether the command is successfully executed or not
*
* @param command command to be executed
* @param environment env vars
* @param workDir working directory
* @param resultHandler exec result handler
* @return output stream
* @throws IOException
*/
@Deprecated
public static ByteArrayOutputStream launchProcess(String command, final Map environment, final String workDir, ExecuteResultHandler resultHandler) throws IOException {
String[] cmdlist = command.split(" ");
CommandLine cmd = new CommandLine(cmdlist[0]);
for (String cmdItem : cmdlist) {
if (!StringUtils.isBlank(cmdItem)) {
cmd.addArgument(cmdItem);
}
}
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(0);
if (!StringUtils.isBlank(workDir)) {
executor.setWorkingDirectory(new File(workDir));
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(out, out);
executor.setStreamHandler(streamHandler);
try {
if (resultHandler == null) {
executor.execute(cmd, environment);
} else {
executor.execute(cmd, environment, resultHandler);
}
} catch (ExecuteException ignored) {
}
return out;
}
use of org.apache.commons.exec.CommandLine in project opennms by OpenNMS.
the class SystemReportResourceLocator method slurpOutput.
@Override
public String slurpOutput(final String commandString, final boolean ignoreExitCode) {
final CommandLine command = CommandLine.parse(commandString);
LOG.debug("running: {}", commandString);
final Map<String, String> environment = new HashMap<String, String>(System.getenv());
environment.put("COLUMNS", "2000");
DataInputStream input = null;
PipedInputStream pis = null;
OutputSuckingParser parser = null;
String outputText = null;
final DefaultExecutor executor = new DefaultExecutor();
final PipedOutputStream output = new PipedOutputStream();
final PumpStreamHandler streamHandler = new PumpStreamHandler(output, output);
executor.setWatchdog(new ExecuteWatchdog(m_maxProcessWait));
executor.setStreamHandler(streamHandler);
if (ignoreExitCode) {
executor.setExitValues(null);
}
try {
LOG.trace("executing '{}'", commandString);
pis = new PipedInputStream(output);
input = new DataInputStream(pis);
parser = new OutputSuckingParser(input);
parser.start();
final int exitValue = executor.execute(command, environment);
IOUtils.closeQuietly(output);
parser.join(m_maxProcessWait);
if (!ignoreExitCode && exitValue != 0) {
LOG.debug("error running '{}': exit value was {}", commandString, exitValue);
} else {
outputText = parser.getOutput();
}
LOG.trace("finished '{}'", commandString);
} catch (final Exception e) {
LOG.debug("Failed to run '{}'", commandString, e);
} finally {
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(pis);
}
return outputText;
}
use of org.apache.commons.exec.CommandLine in project opennms by OpenNMS.
the class RScriptExecutor method exec.
/**
* Executes by given script by:
* - Searching both the classpath and the filesystem for the path
* - Copying the script at the given path to a temporary file and
* performing variable substitution with the arguments using Freemarker.
* - Invoking the script with commons-exec
* - Converting the input table to CSV and passing this to the process via stdin
* - Parsing stdout, expecting CSV output, and converting this to an immutable table
*/
public RScriptOutput exec(String script, RScriptInput input) throws RScriptException {
Preconditions.checkNotNull(script, "script argument");
Preconditions.checkNotNull(input, "input argument");
// Grab the script/template
Template template;
try {
template = m_freemarkerConfiguration.getTemplate(script);
} catch (IOException e) {
throw new RScriptException("Failed to read the script.", e);
}
// Create a temporary file
File scriptOnDisk;
try {
scriptOnDisk = File.createTempFile("Rcsript", "R");
scriptOnDisk.deleteOnExit();
} catch (IOException e) {
throw new RScriptException("Failed to create a temporary file.", e);
}
// Perform variable substitution and write the results to the temporary file
try (FileOutputStream fos = new FileOutputStream(scriptOnDisk);
Writer out = new OutputStreamWriter(fos)) {
template.process(input.getArguments(), out);
} catch (IOException | TemplateException e) {
scriptOnDisk.delete();
throw new RScriptException("Failed to process the template.", e);
}
// Convert the input matrix to a CSV string which will be passed to the script via stdin.
// The table may be large, so we try and avoid writing it to disk
StringBuilder inputTableAsCsv;
try {
inputTableAsCsv = toCsv(input.getTable());
} catch (IOException e) {
scriptOnDisk.delete();
throw new RScriptException("Failed to convert the input table to CSV.", e);
}
// Invoke Rscript against the script (located in a temporary file)
CommandLine cmdLine = new CommandLine(RSCRIPT_BINARY);
cmdLine.addArgument(scriptOnDisk.getAbsolutePath());
// Use commons-exec to execute the process
DefaultExecutor executor = new DefaultExecutor();
// Use the CharSequenceInputStream in order to avoid explicitly converting
// the StringBuilder a string and then an array of bytes.
InputStream stdin = new CharSequenceInputStream(inputTableAsCsv, StandardCharsets.UTF_8);
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
executor.setStreamHandler(new PumpStreamHandler(stdout, stderr, stdin));
// Fail if we get a non-zero exit code
executor.setExitValue(0);
// Fail if the process takes too long
ExecuteWatchdog watchdog = new ExecuteWatchdog(SCRIPT_TIMEOUT_MS);
executor.setWatchdog(watchdog);
// Execute
try {
executor.execute(cmdLine);
} catch (IOException e) {
scriptOnDisk.delete();
throw new RScriptException("An error occured while executing Rscript, or the requested script.", inputTableAsCsv.toString(), stderr.toString(), stdout.toString(), e);
}
// Parse and return the results
try {
ImmutableTable<Long, String, Double> table = fromCsv(stdout.toString());
return new RScriptOutput(table);
} catch (Throwable t) {
throw new RScriptException("Failed to parse the script's output.", inputTableAsCsv.toString(), stderr.toString(), stdout.toString(), t);
} finally {
scriptOnDisk.delete();
}
}
use of org.apache.commons.exec.CommandLine in project opennms by OpenNMS.
the class AbstractSystemReportPlugin method getOpenNMSProcesses.
protected Set<Integer> getOpenNMSProcesses() {
LOG.trace("getOpenNMSProcesses()");
final Set<Integer> processes = new HashSet<Integer>();
final String jps = getResourceLocator().findBinary("jps");
LOG.trace("jps = {}", jps);
DataInputStream input = null;
PsParser parser = null;
PipedInputStream pis = null;
PipedOutputStream output = new PipedOutputStream();
DefaultExecutor executor = new DefaultExecutor();
executor.setWatchdog(new ExecuteWatchdog(5000));
if (jps != null) {
CommandLine command = CommandLine.parse(jps + " -v");
PumpStreamHandler streamHandler = new PumpStreamHandler(output, System.err);
try {
LOG.trace("executing '{}'", command);
pis = new PipedInputStream(output);
input = new DataInputStream(pis);
parser = new PsParser(input, "opennms_bootstrap.jar", "status", 0);
parser.start();
executor.setStreamHandler(streamHandler);
int exitValue = executor.execute(command);
IOUtils.closeQuietly(output);
parser.join();
processes.addAll(parser.getProcesses());
LOG.trace("finished '{}'", command);
if (exitValue != 0) {
LOG.debug("error running '{}': exit value was {}", command, exitValue);
}
} catch (final Exception e) {
LOG.debug("Failed to run '{}'", command, e);
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(pis);
IOUtils.closeQuietly(output);
}
}
LOG.trace("looking for ps");
final String ps = getResourceLocator().findBinary("ps");
if (ps != null) {
// try Linux/Mac style
CommandLine command = CommandLine.parse(ps + " aww -o pid -o args");
output = new PipedOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(output, System.err);
try {
LOG.trace("executing '{}'", command);
pis = new PipedInputStream(output);
input = new DataInputStream(pis);
parser = new PsParser(input, "opennms_bootstrap.jar", "status", 0);
parser.start();
executor.setStreamHandler(streamHandler);
int exitValue = executor.execute(command);
IOUtils.closeQuietly(output);
parser.join(MAX_PROCESS_WAIT);
processes.addAll(parser.getProcesses());
LOG.trace("finished '{}'", command);
if (exitValue != 0) {
LOG.debug("error running '{}': exit value was {}", command, exitValue);
}
} catch (final Exception e) {
LOG.debug("error running '{}'", command, e);
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(pis);
IOUtils.closeQuietly(output);
}
if (processes.size() == 0) {
// try Solaris style
command = CommandLine.parse(ps + " -ea -o pid -o args");
output = new PipedOutputStream();
streamHandler = new PumpStreamHandler(output, System.err);
try {
LOG.trace("executing '{}'", command);
pis = new PipedInputStream(output);
input = new DataInputStream(pis);
parser = new PsParser(input, "opennms_bootstrap.jar", "status", 0);
parser.start();
executor.setStreamHandler(streamHandler);
int exitValue = executor.execute(command);
IOUtils.closeQuietly(output);
parser.join(MAX_PROCESS_WAIT);
processes.addAll(parser.getProcesses());
LOG.trace("finished '{}'", command);
if (exitValue != 0) {
LOG.debug("error running '{}': exit value was {}", command, exitValue);
}
} catch (final Exception e) {
LOG.debug("error running '{}'", command, e);
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(pis);
IOUtils.closeQuietly(output);
}
}
}
if (processes.size() == 0) {
LOG.warn("Unable to find any OpenNMS processes.");
}
return processes;
}
use of org.apache.commons.exec.CommandLine in project cloudstack by apache.
the class HeatMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
CommandLine commandLine = new CommandLine("heat");
if (dir != null && !dir.trim().isEmpty()) {
commandLine.addArgument("dir");
commandLine.addArgument(dir);
}
commandLine.addArgument("-gg");
commandLine.addArgument("-cg");
commandLine.addArgument(componentGroup);
commandLine.addArgument("-ke");
commandLine.addArgument("-sfrag");
if (template == null || template.trim().isEmpty()) {
commandLine.addArgument("-template");
commandLine.addArgument("fragment");
} else {
commandLine.addArgument("-template");
commandLine.addArgument(template);
}
if (outputFile != null) {
commandLine.addArgument("-out");
commandLine.addArgument(outputFile.getAbsolutePath());
}
if (directoryName != null) {
commandLine.addArgument("-dr");
commandLine.addArgument(directoryName);
}
if (vars != null) {
commandLine.addArguments(vars, false);
}
DefaultExecutor executor = new DefaultExecutor();
getLog().debug("working directory " + commandLine.toString());
executor.setWorkingDirectory(getWorkingDirectory(workingDirectory));
int exitValue = executor.execute(commandLine);
if (exitValue != 0) {
throw new MojoExecutionException("Problem executing heat, return code " + exitValue);
}
} catch (ExecuteException e) {
throw new MojoExecutionException("Problem executing heat", e);
} catch (IOException e) {
throw new MojoExecutionException("Problem executing heat", e);
}
}
Aggregations