List of usage examples for org.apache.commons.exec Executor execute
int execute(CommandLine command) throws ExecuteException, IOException;
From source file:org.rhq.server.control.ControlCommand.java
protected boolean isUnixPidRunning(String pid) { Executor executor = new DefaultExecutor(); executor.setWorkingDirectory(getBinDir()); PumpStreamHandler streamHandler = new PumpStreamHandler(createNullOutputStream(), createNullOutputStream()); executor.setStreamHandler(streamHandler); org.apache.commons.exec.CommandLine commandLine; commandLine = new org.apache.commons.exec.CommandLine("kill").addArgument("-0").addArgument(pid); boolean isRunning = true; // assume it is running try {/* www . j a v a2 s .c o m*/ int code = executor.execute(commandLine); if (code != 0) { isRunning = false; } } catch (ExecuteException ee) { log.debug("kill -0 for pid [" + pid + "] threw exception with exit value [" + ee.getExitValue() + "]"); if (ee.getExitValue() == 1) { // return code 1 means process does not exist isRunning = false; } } catch (IOException e) { log.error("Checking for running process failed. Will assume it is running. Error: " + e.getMessage()); } log.debug("unix pid [" + pid + "] " + ((isRunning) ? "is" : "is NOT") + " running"); return isRunning; }
From source file:org.rhq.storage.installer.StorageInstaller.java
protected void exec(Executor executor, org.apache.commons.exec.CommandLine cmdLine) throws IOException { executor.execute(cmdLine); }
From source file:org.stanwood.media.info.MediaFileInfoFetcher.java
private String getCommandOutput(boolean captureStdout, boolean captureStderr, boolean failOnExitCode, String command, Object... args) throws StanwoodException { CommandLine cmdLine = new CommandLine(command); for (Object arg : args) { if (arg instanceof File) { cmdLine.addArgument(((File) arg).getAbsolutePath(), false); } else if (arg instanceof String) { cmdLine.addArgument((String) arg, false); }//from w w w . j av a 2s. c o m } if (log.isDebugEnabled()) { log.debug("About to execute: " + cmdLine.toString()); //$NON-NLS-1$ } Executor exec = new DefaultExecutor(); exec.setExitValues(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, -1 }); try { ByteArrayOutputStream capture = new ByteArrayOutputStream(); OutputStream out; if (captureStdout) { out = capture; } else { out = new LoggerOutputStream(Level.INFO); } OutputStream err; ; if (captureStderr) { err = capture; } else { err = new LoggerOutputStream(Level.ERROR); } exec.setStreamHandler(new PumpStreamHandler(out, err)); int exitCode = exec.execute(cmdLine); if (failOnExitCode && exitCode != 0) { log.error(capture.toString()); throw new StanwoodException(MessageFormat .format(Messages.getString("MediaFileInfoFetcher.NON_ZERO"), exitCode, cmdLine.toString())); //$NON-NLS-1$ } return capture.toString(); } catch (IOException e) { throw new StanwoodException(MessageFormat .format(Messages.getString("MediaFileInfoFetcher.UnableExecuteSysCmd"), cmdLine.toString()), e); //$NON-NLS-1$ } }
From source file:org.zanata.rest.service.VirusScanner.java
private void doScan(File file, String documentName) { Stopwatch stop = Stopwatch.createStarted(); CommandLine cmdLine = buildCommandLine(file); ByteArrayOutputStream scannerOutput = new ByteArrayOutputStream(); Executor executor = buildExecutor(scannerOutput); try {/*from ww w . j av a 2 s . co m*/ int exitValue = executor.execute(cmdLine); log.debug("{} to scan file: \'{}\'", stop, documentName); handleResult(exitValue, documentName, scannerOutput); } catch (IOException e) { // perhaps the antivirus executable was not found... // we omit the stack exception, because it tends to be uninteresting // in this case String msg = "error executing " + SCANNER_NAME + ", unable to scan file \'" + documentName + "\' for viruses: " + e.getMessage(); if (SCANNER_SET) { throw new RuntimeException(msg); } log.error(msg); } }
From source file:org.zanata.sync.jobs.utils.ProcessUtils.java
public static List<String> runNativeCommand(Path workingDir, long timeoutInMilli, String... commands) { Preconditions.checkArgument(commands != null && commands.length > 0, "You must provide commands to run"); CommandLine commandLine = CommandLine.parse(commands[0]); ImmutableList<String> args = ImmutableList.copyOf(commands).subList(1, commands.length); for (String arg : args) { commandLine.addArgument(arg);/*from w w w . j a v a2 s . co m*/ } Executor executor = new DefaultExecutor(); ImmutableList.Builder<String> output = ImmutableList.builder(); executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() { @Override protected void processLine(String line, int logLevel) { log.info(line); output.add(line); } })); ExecuteWatchdog watchDog = new ExecuteWatchdog(timeoutInMilli); executor.setWatchdog(watchDog); executor.setWorkingDirectory(workingDir.toFile()); executor.setProcessDestroyer(PROCESS_DESTROYER); try { int exitCode = executor.execute(commandLine); if (Execute.isFailure(exitCode) && watchDog.killedProcess()) { // it was killed on purpose by the watchdog log.error("process {} taking too long to run and killed by watchdog", commandLine); } } catch (IOException e) { log.error("error running:{}", commandLine); throw Throwables.propagate(e); } return output.build(); }
From source file:pl.net.ptak.InstallShieldBuildMojo.java
/** * Verifies that configuration is satisfied to bild the project and builds it. * /* w w w .j a v a 2s .c o m*/ * @throws MojoExecutionException when plugin is misconfigured * @throws MojoFailureException when plugin execution result was other than expected * @see org.apache.maven.plugin.AbstractMojo#execute() */ public void execute() throws MojoExecutionException, MojoFailureException { if (!OS.isFamilyWindows()) { throw new MojoExecutionException("This plugin is for Windows systems only"); } if (!installshieldOutputDirectory.exists()) { installshieldOutputDirectory.mkdirs(); } if (installshieldProjectFile == null || !installshieldProjectFile.exists()) { if (failWhenNoInstallshieldFile) { getLog().error(String.format("IS Project File available: %b", installshieldProjectFile.exists())); throw new MojoFailureException("InstallShield project file not found"); } else { getLog().info("IS Project File not found. IS build skipped"); } } else { String canonicalProjectFilePath = resolveCanonicalPath(installshieldProjectFile); getLog().info(String.format("About to build file %s", canonicalProjectFilePath)); String canonicalOutputDirectoryPath = resolveCanonicalPath(installshieldOutputDirectory); getLog().info(String.format("Output will be placed in %s", canonicalOutputDirectoryPath)); CommandLine installshieldCommandLine = new CommandLine(installshieldExecutable); addCmdLnArguments(installshieldCommandLine, "-p", canonicalProjectFilePath); addCmdLnArguments(installshieldCommandLine, "-b", canonicalOutputDirectoryPath); if (usePomVersion && null != version && !version.isEmpty()) { addCmdLnArguments(installshieldCommandLine, "-y", version); } if (null != productConfiguration && !productConfiguration.isEmpty()) { addCmdLnArguments(installshieldCommandLine, "-a", productConfiguration); } if (null != productRelease && !productRelease.isEmpty()) { addCmdLnArguments(installshieldCommandLine, "-r", productRelease); } if (null != properties && !properties.isEmpty()) { for (Entry<String, String> entry : properties.entrySet()) { addCmdLnArguments(installshieldCommandLine, "-z", String.format("%s=%s", entry.getKey(), entry.getValue())); } } if (null != pathVariables && !pathVariables.isEmpty()) { for (Entry<String, String> entry : pathVariables.entrySet()) { addCmdLnArguments(installshieldCommandLine, "-l", String.format("%s=%s", entry.getKey(), entry.getValue())); } } Executor exec = new DefaultExecutor(); getLog().debug( String.format("IS Build Command to be executed: %s", installshieldCommandLine.toString())); try { int exitCode = exec.execute(installshieldCommandLine); getLog().debug(String.format("IS build exit code: %d", exitCode)); if (exitCode != 0) { throw new MojoFailureException("Failed to build IS project"); } } catch (IOException e) { String errorMessage = "Failed to execute InstallShield build"; getLog().error(errorMessage); getLog().debug("Details to failure: ", e); throw new MojoFailureException(errorMessage); } } }
From source file:processing.app.linux.Platform.java
@Override public Map<String, Object> resolveDeviceAttachedTo(String serial, Map<String, TargetPackage> packages, String devicesListOutput) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Executor executor = new ExternalProcessExecutor(baos); try {//from w w w .j a v a 2 s . c o m CommandLine toDevicePath = CommandLine.parse("udevadm info -q path -n " + serial); executor.execute(toDevicePath); String devicePath = new String(baos.toByteArray()); baos.reset(); CommandLine commandLine = CommandLine.parse("udevadm info --query=property -p " + devicePath); executor.execute(commandLine); String vidPid = new UDevAdmParser().extractVIDAndPID(new String(baos.toByteArray())); if (vidPid == null) { return super.resolveDeviceAttachedTo(serial, packages, devicesListOutput); } return super.resolveDeviceByVendorIdProductId(packages, vidPid); } catch (IOException e) { return super.resolveDeviceAttachedTo(serial, packages, devicesListOutput); } }
From source file:processing.app.macosx.Platform.java
private void discoverRealOsArch() throws IOException { CommandLine uname = CommandLine.parse("uname -m"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(baos, null)); executor.execute(uname); osArch = StringUtils.trim(new String(baos.toByteArray())); }
From source file:processing.app.windows.Platform.java
@Override public String preListAllCandidateDevices() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Executor executor = new ExternalProcessExecutor(baos); try {//from w w w . ja v a2s . com String listComPorts = new File(System.getProperty("user.dir"), "hardware/tools/listComPorts.exe") .getCanonicalPath(); CommandLine toDevicePath = CommandLine.parse(listComPorts); executor.execute(toDevicePath); return new String(baos.toByteArray()); } catch (Throwable e) { return super.preListAllCandidateDevices(); } }