List of usage examples for org.apache.commons.exec DefaultExecutor setExitValue
public void setExitValue(final int value)
From source file:org.cloudifysource.shell.commands.StartLocalCloud.java
private void startLocalAgent(Admin admin) throws ExecuteException, IOException { File binDir = new File(Environment.getHomeDirectory(), "bin"); DefaultExecutor executor = new DefaultExecutor(); executor.setWorkingDirectory(binDir); CommandLine cmdLine = createCommandLine(); executor.setExitValue(0); executor.execute(cmdLine, new ExecuteResultHandler() { @Override// ww w . j a va2 s .co m public void onProcessFailed(ExecuteException e) { logger.log(Level.SEVERE, "Local Cloud Agent terminated unexpectedly", e); } @Override public void onProcessComplete(int arg0) { logger.fine("Local Cloud Agent terminated"); } }); boolean foundLus = admin.getLookupServices().waitFor(1, 1, TimeUnit.MINUTES); if (!foundLus) { throw new java.lang.IllegalStateException("Local Cloud Lookup Service did not start!"); } }
From source file:org.cloudifysource.shell.commands.TestRecipe.java
/** * Execute a command line in with a given map of environment settings. The execution outupt is filtered unless * verbose is set to true./*from ww w . ja v a 2 s . co m*/ * * @param cmdLine * The command to execute * @param env * Environment settings available for the command execution * @return the command's execution exit code, or -2 if the command failed to execute */ private int executeRecipe(final CommandLine cmdLine, final Map<Object, Object> env) { final DefaultExecutor executor = new DefaultExecutor(); // The watchdog will terminate the process if it does not end within the // specified timeout final int externalProcessTimeout = (this.timeout + EXTERNAL_PROCESS_WATCHDOG_ADDITIONAL_TIMEOUT) * 1000; final ExecuteWatchdog watchdog = new TestRecipeWatchdog(externalProcessTimeout); executor.setWatchdog(watchdog); executor.setExitValue(0); int result = -1; PipedInputStream in = null; PipedOutputStream out = null; BufferedReader reader = null; try { in = new PipedInputStream(); out = new PipedOutputStream(in); reader = new BufferedReader(new InputStreamReader(in)); final Thread thread = new Thread(new FilteredOutputHandler(reader, this.verbose)); thread.setDaemon(true); thread.start(); final PumpStreamHandler psh = new PumpStreamHandler(out, out); executor.setStreamHandler(psh); result = executor.execute(cmdLine, env); } catch (final ExecuteException e) { logger.log(Level.SEVERE, "A problem was encountered while executing the recipe: " + e.getMessage(), e); } catch (final IOException e) { logger.log(Level.SEVERE, "An IO Exception was encountered while executing the recipe: " + e.getMessage(), e); result = UNEXPECTED_ERROR_EXIT_CODE; } return result; }
From source file:org.codice.git.GitIntegrationTest.java
private int executeGitCommand(String[] args) throws IOException { int exitValue = 0; List<String> outputLines = null; CommandLine cmdLine = new CommandLine("git"); for (String arg : args) { cmdLine.addArgument(arg);/*ww w .jav a2s. c o m*/ } DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); executor.setWorkingDirectory(trash); CollectingLogOutputStream output = new CollectingLogOutputStream(); PumpStreamHandler pump = new PumpStreamHandler(output, output); executor.setStreamHandler(pump); try { exitValue = executor.execute(cmdLine); outputLines = output.getLines(); } catch (IOException e) { boolean hookRan = false; String errorMessage = ""; // Check if we got the aborted message from the hook - implies it ran successfully outputLines = output.getLines(); if ((outputLines != null) && (outputLines.size() > 0)) { errorMessage = outputLines.get(0); for (String line : outputLines) { if (line.contains("HOOK ABORTED")) { hookRan = true; break; } } } if (hookRan) { LOGGER.debug("Hook ran successfully - returning an error to abort the git command"); } else { LOGGER.warn("Unexpected error during hook processing - first line of output: {}", errorMessage, e); throw e; } exitValue = 1; } for (String line : outputLines) { System.err.println(line); } return exitValue; }
From source file:org.codice.git.hook.Artifact.java
/** * Downloads the artifact using maven.//from w w w . j ava 2s.c o m * * @param settings the maven settings file or "" if using the default one * @param out the output stream where to print messages to the user * @throws IOException if an error occurs */ protected void downloadUsingMaven(String settings, PrintStream out) throws IOException { final CommandLine cmd = new CommandLine(SystemUtils.IS_OS_WINDOWS ? "mvn.cmd" : "mvn"); cmd.addArgument("-f").addArgument(new File(handler.getBasedir(), "pom.xml").getAbsolutePath()); if (StringUtils.isNotEmpty(settings)) { cmd.addArgument("-s").addArgument(settings); } cmd.addArgument("org.apache.maven.plugins:maven-dependency-plugin:3.0.0:copy") .addArgument("-Dartifact=" + mvnInfo) .addArgument("-DoutputDirectory=" + handler.getBasedir().getAbsolutePath()) .addArgument("-Dmdep.stripClassifier=true").addArgument("-Dmdep.stripVersion=true"); if (!install) { cmd.addArgument("-quiet"); } final DefaultExecutor exec = new DefaultExecutor(); exec.setExitValue(0); try { exec.execute(cmd); } catch (IOException e) { LOGGER.log(Level.WARNING, "failed to download blacklist words artifact", e); if (file.exists()) { // ignore the error and continue with the one that is there return; } out.printf("%sFailed to download artifact '%s'; %s.%n", eprefix, mvnInfo, e.getMessage()); throw new IOException("failed to download blacklist words artifact", e); } if (!mvnName.equals(file.getName())) { final File f = new File(handler.getBasedir(), mvnName); out.printf("%sMoving %s to %s.%n", iprefix, mvnName, file); if (!f.renameTo(file)) { LOGGER.log(Level.WARNING, "failed to copy {0} file", file.getName()); if (file.exists()) { // ignore the error and continue with the one that is there return; } out.printf("%sFailed to move %s to %s.%n", eprefix, mvnName, file); throw new IOException("failed to copy " + file.getName() + " file"); } } }
From source file:org.estatio.webapp.services.other.EstatioOtherServices.java
@Programmatic String execute(String command) { int exitValue = 1; CommandLine commandLine = CommandLine.parse(command); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); ExecuteStreamHandler handler = executor.getStreamHandler(); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); PumpStreamHandler psh = new PumpStreamHandler(stdout); executor.setStreamHandler(psh);/* w w w . ja va2 s . co m*/ try { handler.setProcessOutputStream(System.in); } catch (IOException e) { } try { exitValue = executor.execute(commandLine); } catch (ExecuteException e) { return e.getMessage(); } catch (IOException e) { return e.getMessage(); } return stdout.toString(); }
From source file:org.excalibur.service.aws.CommandExample.java
public static void main(String[] args) throws ExecuteException, IOException, InterruptedException { CommandLine command = new CommandLine("/bin/bash"); command.addArgument("-c", false); command.addArgument("iperf3 -t 30 -c iperf.scottlinux.com >> output.txt", false); //Process process = Runtime.getRuntime().exec(new String[]{"bash", "-c", "iperf3 -t 60 -c localhost"}); // System.out.println(new Mirror().on(process).get().field("pid")); //process.waitFor(); // System.out.println(process.exitValue()); // ManagementFactory.getRuntimeMXBean().getName(); // System.out.println(IOUtils.readLines(process.getInputStream())); //String command = "iperf3 -t 30 -c iperf.scottlinux.com"; ExecuteWatchdog watchdog = new ExecuteWatchdog(10); final DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler()); executor.setExitValue(1); executor.execute(command, new ExecuteResultHandler() { @Override//w ww .j a v a2 s .c o m public void onProcessFailed(ExecuteException e) { e.printStackTrace(); } @Override public void onProcessComplete(int exitValue) { System.out.println(exitValue); } }); }
From source file:org.jboss.tools.windup.runtime.WindupRmiClient.java
public void startWindup(final IProgressMonitor monitor, String jreHome) { logInfo("Begin start RHAMT."); //$NON-NLS-1$ monitor.worked(1);//w ww . j av a 2s .com String windupExecutable = WindupRuntimePlugin.computeWindupExecutable(); if (windupExecutable == null) { WindupRuntimePlugin.logErrorMessage("rhamt-cli not specified."); //$NON-NLS-1$ return; } boolean executable = new File(windupExecutable).setExecutable(true); if (!executable) { WindupRuntimePlugin.logErrorMessage("rhamt-cli not executable."); //$NON-NLS-1$ return; } CommandLine cmdLine = CommandLine.parse(windupExecutable); Map<String, String> env = Maps.newHashMap(); for (Map.Entry<String, String> entry : System.getenv().entrySet()) { env.put(entry.getKey(), entry.getValue()); } if (!jreHome.trim().isEmpty()) { env.put(JAVA_HOME, jreHome); } logInfo("Using " + JAVA_HOME + " - " + jreHome); cmdLine.addArgument("--startServer"); //$NON-NLS-1$ cmdLine.addArgument(String.valueOf(getRmiPort())); watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); ExecuteResultHandler handler = new ExecuteResultHandler() { @Override public void onProcessFailed(ExecuteException e) { logInfo("The RHAMT process failed:"); //$NON-NLS-1$ logInfo(e.getMessage()); //$NON-NLS-1$ executionBuilder = null; notifyServerChanged(); } @Override public void onProcessComplete(int exitValue) { logInfo("The RHAMT process has completed."); //$NON-NLS-1$ executionBuilder = null; notifyServerChanged(); } }; DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() { @Override protected void processLine(String line, int logLevel) { logInfo("Message from RHAMT executor: " + line); //$NON-NLS-1$ monitor.worked(1); } })); executor.setWatchdog(watchdog); executor.setExitValue(1); monitor.worked(1); try { logInfo("Starting RHAMT in server mode..."); //$NON-NLS-1$ logInfo("Command-line: " + cmdLine); //$NON-NLS-1$ executor.execute(cmdLine, env, handler); } catch (IOException e) { WindupRuntimePlugin.log(e); } }
From source file:org.jboss.windup.decorator.java.decompiler.BackupOfJadretroDecompilerAdapter.java
private void executeJad(File classLocation, File sourceOutputLocation) { try {/*from w w w. jav a 2 s .co m*/ // Build command array CommandLine cmdLine = new CommandLine(APP_NAME); cmdLine.addArgument("-d"); cmdLine.addArgument("${outputLocation}"); cmdLine.addArgument("-f"); cmdLine.addArgument("-o"); cmdLine.addArgument("-s"); cmdLine.addArgument("java"); cmdLine.addArgument("${classLocation}"); Map<String, Object> argMap = new HashMap<String, Object>(); argMap.put("outputLocation", sourceOutputLocation); argMap.put("classLocation", classLocation); cmdLine.setSubstitutionMap(argMap); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); executor.setWatchdog(watchdog); int exitValue = executor.execute(cmdLine); LOG.debug("Decompiler exited with exit code: " + exitValue); if (!sourceOutputLocation.exists()) { LOG.error("Expected decompiled source: " + sourceOutputLocation.getAbsolutePath() + "; did not find file. This likey means that the decompiler did not successfully decompile the class."); } else { LOG.debug("Decompiled to: " + sourceOutputLocation.getAbsolutePath()); } } catch (IOException e) { throw new FatalWindupException( "Error running " + APP_NAME + " decompiler. Validate that " + APP_NAME + " is on your PATH.", e); } catch (Exception e) { throw new FatalWindupException( "Error running " + APP_NAME + " decompiler. Validate that " + APP_NAME + " is on your PATH.", e); } }
From source file:org.jboss.windup.decorator.java.decompiler.JadretroDecompilerAdapter.java
private void executeJad(File classLocation, File sourceOutputLocation) { try {/*from ww w . j a va 2s. co m*/ // Build command array CommandLine cmdLine = new CommandLine(APP_NAME); cmdLine.addArgument("-d"); cmdLine.addArgument("${outputLocation}"); cmdLine.addArgument("-f"); cmdLine.addArgument("-o"); cmdLine.addArgument("-s"); cmdLine.addArgument("java"); cmdLine.addArgument("${classLocation}"); Map<String, Object> argMap = new HashMap<String, Object>(); argMap.put("outputLocation", sourceOutputLocation); argMap.put("classLocation", classLocation); cmdLine.setSubstitutionMap(argMap); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); executor.setWatchdog(watchdog); int exitValue = executor.execute(cmdLine); LOG.debug("Decompiler exited with exit code: " + exitValue); if (!sourceOutputLocation.exists()) { LOG.error("Expected decompiled source: " + sourceOutputLocation.getAbsolutePath() + "; did not find file. This likey means that the decompiler did not successfully decompile the class."); } else { LOG.debug("Decompiled to: " + sourceOutputLocation.getAbsolutePath()); } } catch (IOException e) { throw new FatalWindupException( "Error running " + APP_NAME + " decompiler. Validate that " + APP_NAME + " is on your PATH.", e); } }
From source file:org.kgi.mybatis.scala.generator.GenerateDaoMojo.java
public void execute() throws MojoExecutionException { String scaladocParamFileName = project.getBuild().getOutputDirectory() + File.separator + "myb-doclet.txt"; try {//ww w .j a v a 2 s. co m File f = outputDirectory; getLog().info("writing generated files to directory:" + outputDirectory.getAbsolutePath()); if (!f.exists()) { f.mkdirs(); } File sourcesDir = new File(project.getBasedir(), "src" + File.separator + "main"); getLog().info("sources located in:" + sourcesDir.getAbsolutePath()); Collection<File> sourceFiles = FileUtils.listFiles(sourcesDir, new String[] { "scala", "java" }, true); PrintWriter scaladocParamFileWriter = new PrintWriter(new FileWriter(scaladocParamFileName)); scaladocParamFileWriter.println("-d"); scaladocParamFileWriter.println("src"); scaladocParamFileWriter.println("-doc-generator"); scaladocParamFileWriter.println("org.kgi.mybatis.scala.generator.doclet.MyBatisMappingDoclet"); for (File sourceFile : sourceFiles) { scaladocParamFileWriter.println(sourceFile.getAbsolutePath()); } scaladocParamFileWriter.flush(); scaladocParamFileWriter.close(); DependencyNode depTree = dependencyGraphBuilder.buildDependencyGraph(project, new ArtifactFilter() { public boolean include(Artifact artifact) { return "jar".equals(artifact.getType()); } }); List deps = collectDependencies(depTree); Iterator depIterator = deps.iterator(); StringBuilder cpBuilder = new StringBuilder(); String docletPath = null; while (depIterator.hasNext()) { Artifact dep = (Artifact) depIterator.next(); String path = System.getProperty("user.home") + File.separator + ".m2" + File.separator + "repository" + File.separator + dep.getGroupId().replace('.', File.separatorChar) + File.separator + dep.getArtifactId() + File.separator + dep.getVersion() + File.separator + dep.getArtifactId() + "-" + dep.getVersion() + "." + dep.getType(); if (cpBuilder.length() > 0) { cpBuilder.append(File.pathSeparator); } cpBuilder.append(path); if ("mybatis-scala-gen-doclet".equals(dep.getArtifactId())) { docletPath = path; } } CommandLine cmdl = new CommandLine("scaladoc"); cmdl.addArgument("-Dmyb-gen-destination=" + outputDirectory.getAbsolutePath()); cmdl.addArgument("-Dmyb-gen-destination-package=" + destinationPackage); cmdl.addArgument("-classpath"); cmdl.addArgument(cpBuilder.toString()); cmdl.addArgument("-toolcp"); cmdl.addArgument(docletPath); cmdl.addArgument("@" + scaladocParamFileName); getLog().info("generation command:\n" + cmdl.toString()); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); executor.execute(cmdl); } catch (Exception e) { getLog().error(e); throw new MojoExecutionException("Problems generating DAO sources" + scaladocParamFileName); } }