List of usage examples for org.apache.commons.exec DefaultExecutor DefaultExecutor
public DefaultExecutor()
From source file:org.kitodo.imagemanagementmodule.ConvertRunner.java
/** * Executes the ImageMagick command using Apache Commons Exec. * * @param commandLine/*from w w w.j a va 2 s . c o m*/ * command line to execute * @throws IOException * if I/O fails */ void run(IMOperation commandLine) throws IOException { Executor executor = new DefaultExecutor(); OutputStream outAndErr = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(outAndErr)); long timeoutMillis = 1000 * Config.getIntParameter("ImageManagementModule.timeoutSec", DEFAULT_TIMEOUT_MINS); executor.setWatchdog(new ExecuteWatchdog(timeoutMillis)); CommandLine command; try { String sshHosts = Config.getParameter("ImageManagementModule.sshHosts"); command = new CommandLine("ssh"); String[] hosts = sshHosts.split(","); String host = hosts[RANDOMNESS_GENERATOR.nextInt(hosts.length)]; command.addArgument(host, false); command.addArgument(convertCommand + ' ' + commandLine.toString(), false); } catch (NoSuchElementException e) { logger.trace("SSH not configured.", e); command = new CommandLine(convertCommand); command.addArguments(commandLine.toString()); } try { logger.debug("Executing: {}", command); logger.trace("Timeout: {} mins", timeoutMillis / 60000d); executor.execute(command); logger.debug("Command output:{}{}", System.lineSeparator(), outAndErr.toString()); } catch (IOException | RuntimeException e) { logger.error("Command output:{}{}", System.lineSeparator(), outAndErr.toString()); throw e; } }
From source file:org.kualigan.maven.plugins.api.DefaultPrototypeHelper.java
protected int executeCommandLine(final String... args) throws ExecuteException, IOException { final Executor exec = new DefaultExecutor(); final Map enviro = new HashMap(); try {/* w w w . ja v a2 s.c o m*/ final Properties systemEnvVars = CommandLineUtils.getSystemEnvVars(); enviro.putAll(systemEnvVars); } catch (IOException x) { getCaller().getLog().error("Could not assign default system enviroment variables.", x); } final File workingDirectory = new File(System.getProperty("java.io.tmpdir")); final org.apache.commons.exec.CommandLine commandLine = new org.apache.commons.exec.CommandLine(args[0]); for (int i = 1; i < args.length; i++) { commandLine.addArgument(args[i]); } exec.setStreamHandler(new PumpStreamHandler(System.out, System.err, System.in)); exec.setWorkingDirectory(workingDirectory); return exec.execute(commandLine, enviro); }
From source file:org.mail.bridge.FolderMonitor.java
public synchronized void runScriptAgainstReceivedFiles(List<File> inboxFiles) { if (config.getInboxScript().isEmpty() || Utils.isEmpty(inboxFiles)) return;//from ww w . j a v a 2 s .c om LOG.debug("Run script '{}' against files {}", config.getInboxScript(), inboxFiles); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { CommandLine cmd = CommandLine.parse(config.getInboxScript()); for (File file : inboxFiles) cmd.addArgument(file.getName(), true); DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(out)); executor.setWatchdog(new ExecuteWatchdog(SCRIPT_TIMEOUT)); Map<String, String> environment = EnvironmentUtils.getProcEnvironment(); environment.putAll(config.asEnvironmentMap()); executor.setWorkingDirectory(new File(System.getProperty("user.dir"))); executor.execute(cmd, environment); LOG.info("Script '{}' successfully finished", config.getInboxScript()); LOG.debug("Script output:\n{}", out.toString()); } catch (ExecuteException e) { LOG.error(e.getMessage(), e); LOG.error("\nScript '{}' output:\n{}", config.getInboxScript(), out.toString()); int c = config.getInboxScriptStopCode(); if (c != 0 && c == e.getExitValue()) postMessage(new Main.StopMessage( String.format("Script '%s' exited with code %d that is configured as stop code", config.getInboxScript(), c))); } catch (IOException e) { LOG.error(e.getMessage(), e); LOG.error("\nScript '{}' output:\n{}", config.getInboxScript(), out.toString()); } }
From source file:org.moe.cli.utils.Utils.java
public static String[] execute(File dir, String command, Map<String, String> environment) { Map<String, String> current = null; try {/*from ww w .j a v a 2s . c o m*/ current = EnvironmentUtils.getProcEnvironment(); } catch (IOException e1) { e1.printStackTrace(); } if (environment != null && current != null) { current.putAll(environment); } CommandLine cmdLine = CommandLine.parse(command); DefaultExecutor executor = new DefaultExecutor(); executor.setWorkingDirectory(dir); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); ByteArrayOutputStream stderr = new ByteArrayOutputStream(); PumpStreamHandler stHandler = new PumpStreamHandler(stdout, stderr); executor.setStreamHandler(stHandler); int exitValue = Executor.INVALID_EXITVALUE; try { exitValue = executor.execute(cmdLine, current); } catch (IOException e) { } return new String[] { stdout.toString(), executor.isFailure(exitValue) ? stderr.toString() : null }; }
From source file:org.moneta.DropwizardContractTest.java
@BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("Java Temp Dir: " + System.getProperty("java.io.tmpdir")); executor = new DefaultExecutor(); resultHandler = new DefaultExecuteResultHandler(); String javaHome = System.getProperty("java.home"); String userDir = System.getProperty("user.dir"); executor.setStreamHandler(new PumpStreamHandler(System.out)); watchdog = new ExecuteWatchdog(10000); executor.setWatchdog(watchdog);/* ww w . j av a 2 s .c o m*/ executor.execute(new CommandLine( javaHome + SystemUtils.FILE_SEPARATOR + "bin" + SystemUtils.FILE_SEPARATOR + "java.exe") .addArgument("-version")); executor.execute( new CommandLine( javaHome + SystemUtils.FILE_SEPARATOR + "bin" + SystemUtils.FILE_SEPARATOR + "java.exe") .addArgument("-jar") .addArgument(userDir + "/../moneta-dropwizard/target/moneta-dropwizard-" + ContractTestSuite.getProjectVersion() + ".jar") .addArgument("server").addArgument( "src/main/resources/dropwizard/moneta-dropwizard.yaml"), resultHandler); Thread.sleep(3000); System.out.println("Test sequence starting...."); }
From source file:org.ms123.common.management.ManagementServiceImpl.java
private int exec(String line, ByteArrayOutputStream outputStream, ByteArrayOutputStream outputErr, int[] exitValues) throws Exception { CommandLine cmdLine = CommandLine.parse(line); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValues(exitValues);/*from ww w .ja va 2s .c om*/ PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, outputErr); executor.setStreamHandler(streamHandler); int exitValue = executor.execute(cmdLine); return exitValue; }
From source file:org.mule.test.infrastructure.process.Controller.java
private int executeSyncCommand(String command, String[] args, Map<Object, Object> newEnv, int timeout) throws MuleControllerException { CommandLine commandLine = new CommandLine(muleBin); commandLine.addArgument(command);//from www .j a v a 2 s . co m commandLine.addArguments(args); DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog); executor.setStreamHandler(new PumpStreamHandler()); return doExecution(executor, commandLine, newEnv); }
From source file:org.mule.test.infrastructure.process.MuleUtils.java
public static int executeCommand(String command, String... envVars) throws IOException { CommandLine cmdLine = CommandLine.parse(command); DefaultExecutor executor = new DefaultExecutor(); Map<String, String> env = addEnvProperties(envVars); ExecuteWatchdog watchDog = new ExecuteWatchdog(TIMEOUT); executor.setWatchdog(watchDog);/*w w w. j a va2s . c om*/ executor.setStreamHandler(new PumpStreamHandler()); int result = executor.execute(cmdLine, env); if (executor.isFailure(result)) { if (watchDog.killedProcess()) { throw new RuntimeException("Reached timeout while running: " + cmdLine); } throw new RuntimeException("Process failed with return code [" + result + "]: " + cmdLine); } return result; }
From source file:org.mule.test.infrastructure.process.UnixController.java
@Override public int getProcessId() { Map<Object, Object> newEnv = this.copyEnvironmentVariables(); DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog);//w w w.j av a 2 s . c o m ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); executor.setStreamHandler(streamHandler); if (this.doExecution(executor, new CommandLine(this.muleBin).addArgument("status"), newEnv) == 0) { Matcher matcher = STATUS_PATTERN.matcher(outputStream.toString()); if (matcher.find()) { return Integer.parseInt(matcher.group(1)); } else { throw new MuleControllerException("bin/mule status didn't return the expected pattern: " + STATUS); } } else { throw new MuleControllerException("Mule ESB is not running"); } }
From source file:org.mule.tooling.jubula.cliexecutor.internal.DefaultCliExecutor.java
@Override public int run(final File executable, final String... params) { try {//from ww w . j a v a 2 s .c om final DefaultExecutor executor = new DefaultExecutor(); final CommandLine command = CommandLine.parse(executable.getAbsolutePath()); command.addArguments(params, true); return executor.execute(command); } catch (final ExecuteException e) { throw new RuntimeException(e); } catch (final IOException e) { throw new RuntimeException(e); } }