List of usage examples for org.apache.commons.exec DefaultExecutor setWatchdog
public void setWatchdog(final ExecuteWatchdog watchDog)
From source file:org.wso2.ppaas.configurator.tests.ConfiguratorTestManager.java
/** * Execute shell command//from w w w . j a v a 2 s . com * * @param commandText */ protected int executeCommand(final String commandText, Map<String, String> environment) { final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal(); int result; try { CommandLine commandline = CommandLine.parse(commandText); DefaultExecutor exec = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setWorkingDirectory(new File(ConfiguratorTestManager.class.getResource(PATH_SEP).getPath() + ".." + PATH_SEP + CONFIGURATOR_DIR_NAME)); exec.setStreamHandler(streamHandler); ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT); exec.setWatchdog(watchdog); result = exec.execute(commandline, environment); } catch (Exception e) { log.error(outputStream.toString(), e); throw new RuntimeException(e); } return result; }
From source file:org.zanata.rest.service.VirusScanner.java
/** * Builds an Executor which will output to the specified OutputStream. * <p>/* w ww . j ava 2 s. com*/ * The Executor will be configured to return exit values as int, rather than * throwing ExecuteException. * * @param output * @return a configured Executor */ private Executor buildExecutor(OutputStream output) { DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); executor.setWatchdog(watchdog); ExecuteStreamHandler psh = new PumpStreamHandler(output); executor.setStreamHandler(psh); // We want to handle all exit values directly (not as ExecuteException). executor.setExitValues(null); return executor; }
From source file:sce.ProcessExecutor.java
public String executeProcess(String[] processParameters) throws JobExecutionException { try {// ww w .j a v a 2s .com //Command to be executed CommandLine command = new CommandLine(processParameters[0]); String[] params = new String[processParameters.length - 1]; for (int i = 0; i < processParameters.length - 1; i++) { params[i] = processParameters[i + 1]; } //Adding its arguments command.addArguments(params); //set timeout in seconds ExecuteWatchdog watchDog = new ExecuteWatchdog( this.timeout == 0 ? ExecuteWatchdog.INFINITE_TIMEOUT : this.timeout * 1000); this.watchdog = watchDog; //Result Handler for executing the process in a Asynch way DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); //MyResultHandler resultHandler = new MyResultHandler(); //Using Std out for the output/error stream //ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); //PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); //This is used to end the process when the JVM exits ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer(); //Our main command executor DefaultExecutor executor = new DefaultExecutor(); //Setting the properties executor.setStreamHandler(new PumpStreamHandler(null, null)); executor.setWatchdog(watchDog); //executor.setExitValue(1); // this has to be set if the java code contains System.exit(1) to avoid a FAILED status //Setting the working directory //Use of recursion along with the ls makes this a long running process //executor.setWorkingDirectory(new File("/home")); executor.setProcessDestroyer(processDestroyer); //if set, use the java environment variables when running the command if (!this.environment.equals("")) { Map<String, String> procEnv = EnvironmentUtils.getProcEnvironment(); EnvironmentUtils.addVariableToEnvironment(procEnv, this.environment); //Executing the command executor.execute(command, procEnv, resultHandler); } else { //Executing the command executor.execute(command, resultHandler); } //The below section depends on your need //Anything after this will be executed only when the command completes the execution resultHandler.waitFor(); /*int exitValue = resultHandler.getExitValue(); System.out.println(exitValue); if (executor.isFailure(exitValue)) { System.out.println("Execution failed"); } else { System.out.println("Execution Successful"); } System.out.println(outputStream.toString());*/ //return outputStream.toString(); if (watchdog.killedProcess()) { throw new JobExecutionException("Job Interrupted", new InterruptedException()); } if (executor.isFailure(resultHandler.getExitValue())) { ExecuteException ex = resultHandler.getException(); throw new JobExecutionException(ex.getMessage(), ex); } return "1"; } catch (ExecuteException ex) { throw new JobExecutionException(ex.getMessage(), ex); } catch (IOException | InterruptedException | JobExecutionException ex) { throw new JobExecutionException(ex.getMessage(), ex); } }
From source file:uk.org.sappho.applications.transcript.service.registry.vcs.CommandExecuter.java
public String execute(Command command, File directory) throws TranscriptException { try {/* w w w.jav a 2 s. co m*/ if (logger.isLoggable(Level.INFO)) { logger.info(command.getSafeCommand()); } DefaultExecutor executor = new DefaultExecutor(); DefaultExecuteResultHandler commandResultsHandler = new DefaultExecuteResultHandler(); ByteArrayOutputStream commandOutputStream = new ByteArrayOutputStream(); ByteArrayOutputStream commandErrorStream = new ByteArrayOutputStream(); PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(commandOutputStream, commandErrorStream); executor.setWatchdog(new ExecuteWatchdog(60000)); executor.setStreamHandler(pumpStreamHandler); executor.setWorkingDirectory(directory); executor.execute(command.getCommandLine(), commandResultsHandler); commandResultsHandler.waitFor(); if (commandResultsHandler.getExitValue() != 0) { throw new TranscriptException(commandErrorStream.toString()); } return commandOutputStream.toString(); } catch (Throwable throwable) { if (logger.isLoggable(Level.WARNING)) { logger.warning(throwable.getMessage()); } throw new TranscriptException("Unable to execute system command: " + command.getSafeCommand(), throwable); } }