List of usage examples for org.apache.commons.exec CommandLine CommandLine
public CommandLine(final CommandLine other)
From source file:com.github.peterjanes.node.NpmPackMojo.java
/** * * @throws MojoExecutionException if anything unexpected happens. *//*from w w w.j av a 2s . c o m*/ public void execute() throws MojoExecutionException { if (!outputDirectory.exists()) { outputDirectory.mkdirs(); } CommandLine commandLine = new CommandLine(executable); Executor exec = new DefaultExecutor(); exec.setWorkingDirectory(outputDirectory); List commandArguments = new ArrayList(); commandArguments.add("pack"); commandArguments.add("./commonjs"); String[] args = new String[commandArguments.size()]; for (int i = 0; i < commandArguments.size(); i++) { args[i] = (String) commandArguments.get(i); } commandLine.addArguments(args, false); OutputStream stdout = System.out; OutputStream stderr = System.err; try { getLog().debug("Executing command line: " + commandLine); exec.setStreamHandler(new PumpStreamHandler(stdout, stderr, System.in)); int resultCode = exec.execute(commandLine); if (0 != resultCode) { throw new MojoExecutionException( "Result of " + commandLine + " execution is: '" + resultCode + "'."); } Artifact artifact = mavenProject.getArtifact(); String fileName = String.format("%s-%s.tgz", artifact.getArtifactId(), mavenProject.getProperties().getProperty("node.project.version")); artifact.setFile(new File(outputDirectory, fileName)); } catch (ExecuteException e) { throw new MojoExecutionException(EXECUTION_FAILED, e); } catch (IOException e) { throw new MojoExecutionException(EXECUTION_FAILED, e); } }
From source file:net.robyf.dbpatcher.util.MySqlUtil.java
/** * Backs up a MySQL database to a file.// www . ja va 2 s. c om * * @param databaseName The name of the database * @param fileName Backup file to be created * @param username Username of the database user * @param password Password of the database user */ public static void backup(final String databaseName, final String fileName, final String username, final String password) { LogFactory.getLog().log("Backing up " + databaseName + " into " + fileName); CommandLine command = new CommandLine("mysqldump"); addCredentials(command, username, password); command.addArgument(databaseName); try (BufferedReader reader = new BufferedReader(new InputStreamReader(executeAndGetOutput(command))); PrintWriter writer = new PrintWriter(new File(fileName))) { String line = reader.readLine(); while (line != null) { writer.println(line); line = reader.readLine(); } } catch (IOException ioe) { throw new UtilException("Error backing up a database", ioe); } }
From source file:com.github.peterjanes.node.AbstractNpmInstallMojo.java
/** * * @param outputDirectory The working directory for the npm command. * @param npmDependencies A comma-separated list of npm packages to install. * @param extraArguments Extra arguments to provide to the npm command. * @param artifactScope Scope of the artifacts to include. * @throws MojoExecutionException if anything unexpected happens. *//*from w w w . j av a 2 s. co m*/ void execute(File outputDirectory, String npmDependencies, String extraArguments, ResolutionScope artifactScope) throws MojoExecutionException { if (!outputDirectory.exists()) { outputDirectory.mkdirs(); } CommandLine commandLine = new CommandLine(executable); Executor exec = new DefaultExecutor(); exec.setWorkingDirectory(outputDirectory); List commandArguments = new ArrayList(); commandArguments.add("install"); if (null != npmDependencies) { String[] packages = npmDependencies.split(","); for (String pkg : packages) { commandArguments.add(pkg); } } List<Artifact> nodeArtifacts = getNodeArtifacts(artifactScope); for (Artifact artifact : nodeArtifacts) { commandArguments.add(artifact.getFile().getAbsolutePath()); } if (null != extraArguments) { String[] extraArgs = extraArguments.split(" "); for (String extraArg : extraArgs) { commandArguments.add(extraArg); } } String[] args = new String[commandArguments.size()]; for (int i = 0; i < commandArguments.size(); i++) { args[i] = (String) commandArguments.get(i); } commandLine.addArguments(args, false); OutputStream stdout = getLog().isDebugEnabled() ? System.err : new ByteArrayOutputStream(); OutputStream stderr = getLog().isDebugEnabled() ? System.err : new ByteArrayOutputStream(); try { getLog().debug( "Executing command line " + commandLine + " in directory " + outputDirectory.getAbsolutePath()); exec.setStreamHandler(new PumpStreamHandler(stdout, stderr, System.in)); int resultCode = exec.execute(commandLine); if (0 != resultCode) { System.out.println("STDOUT: " + stdout); System.err.println("STDERR: " + stderr); throw new MojoExecutionException( "Result of " + commandLine + " execution is: '" + resultCode + "'."); } } catch (ExecuteException e) { throw new MojoExecutionException(EXECUTION_FAILED, e); } catch (IOException e) { throw new MojoExecutionException(EXECUTION_FAILED, e); } }
From source file:hoot.services.nativeinterfaces.CommandRunnerImpl.java
@Override public CommandResult exec(String[] command) throws IOException { logger.debug("Executing the following command: {}", Arrays.toString(command)); try (OutputStream stdout = new ByteArrayOutputStream(); OutputStream stderr = new ByteArrayOutputStream()) { this.stdout = stdout; this.stderr = stderr; CommandLine cmdLine = new CommandLine(command[0]); for (int i = 1; i < command.length; i++) { cmdLine.addArgument(command[i], false); }/*from ww w . j a va 2 s . c om*/ ExecuteStreamHandler executeStreamHandler = new PumpStreamHandler(stdout, stderr); DefaultExecutor executor = new DefaultExecutor(); this.watchDog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); executor.setWatchdog(this.watchDog); executor.setStreamHandler(executeStreamHandler); int exitValue; try { exitValue = executor.execute(cmdLine); if (executor.isFailure(exitValue) && this.watchDog.killedProcess()) { // it was killed on purpose by the watchdog logger.info("Process for '{}' command was killed!", cmdLine); } } catch (Exception e) { exitValue = -1; logger.warn("Error executing: {}", cmdLine, e); } CommandResult commandResult = new CommandResult(cmdLine.toString(), exitValue, stdout.toString(), stderr.toString()); logger.debug("Finished executing: {}", commandResult); return commandResult; } }
From source file:ch.vorburger.exec.ManagedProcessBuilder.java
public ManagedProcessBuilder(File executable) throws ManagedProcessException { commonsExecCommandLine = new CommandLine(executable); this.environment = initialEnvironment(); }
From source file:com.github.peterjanes.node.NpmTestMojo.java
/** * * @throws MojoExecutionException if anything unexpected happens. *//* w w w. j a v a 2 s . c om*/ public void execute() throws MojoExecutionException { if (!workingDirectory.exists()) { workingDirectory.mkdirs(); } CommandLine commandLine = new CommandLine(executable); Executor exec = new DefaultExecutor(); exec.setWorkingDirectory(workingDirectory); Map env = new HashMap(); try { Map systemEnvVars = EnvironmentUtils.getProcEnvironment(); env.putAll(systemEnvVars); } catch (IOException e) { getLog().error("Could not assign default system enviroment variables.", e); } env.put("NODE_PATH", new File(workingDirectory, "node_modules").getAbsolutePath()); env.put("XUNIT_FILE", outputFile.getAbsolutePath()); List commandArguments = new ArrayList(); commandArguments.add("test"); String[] args = new String[commandArguments.size()]; for (int i = 0; i < commandArguments.size(); i++) { args[i] = (String) commandArguments.get(i); } commandLine.addArguments(args, false); OutputStream stdout = System.out; OutputStream stderr = System.err; try { outputFile.getParentFile().mkdirs(); getLog().debug("Executing command line " + commandLine + " in directory " + workingDirectory.getAbsolutePath()); exec.setStreamHandler(new PumpStreamHandler(stdout, stderr, System.in)); int resultCode = exec.execute(commandLine, env); if (0 != resultCode) { throw new MojoExecutionException( "Result of " + commandLine + " execution is: '" + resultCode + "'."); } } catch (ExecuteException e) { throw new MojoExecutionException(EXECUTION_FAILED, e); } catch (IOException e) { throw new MojoExecutionException(EXECUTION_FAILED, e); } }
From source file:com.demandware.vulnapp.challenge.impl.CommandInjectionChallenge.java
private String getCommandOutput(String command) { String output = null;/*from w ww . ja va 2 s. co m*/ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { CommandLine cmd = new CommandLine("/bin/bash"); String[] args = new String[] { "-c", command }; cmd.addArguments(args, false); PumpStreamHandler psh = new PumpStreamHandler(outputStream); DefaultExecutor exec = new DefaultExecutor(); exec.setStreamHandler(psh); exec.execute(cmd); output = outputStream.toString(); } catch (ExecuteException e) { e.printStackTrace(); output = "Could not execute command"; } catch (IOException e) { e.printStackTrace(); } return output; }
From source file:beans.DeployManagerImpl.java
@Override public WidgetInstance uninstall(ServerNode serverNode) { WidgetInstance widgetInstance = serverNode.getWidgetInstance(); String installName = widgetInstance.getInstallName(); // TODO : maybe we should verify it is installed using the rest client? File script = widgetInstance.getRecipeType() == Recipe.Type.APPLICATION ? conf.cloudify.uninstallApplicationScript : conf.cloudify.uninstallServiceScript; CommandLine cmdLine = new CommandLine(script); cmdLine.addArgument(serverNode.getPublicIP()); cmdLine.addArgument(installName);//from w ww. jav a2 s . c o m logger.info("executing command [{}]", cmdLine); execute(cmdLine, serverNode); return widgetInstance; }
From source file:com.adaptris.hpcc.SprayToThorTest.java
/** * Technically this won't work with dfuplus as it doesn't set <code>recordsize</code>. * @throws Exception//from ww w. j a v a2s .c o m */ @Test public void testLegacyFixedFormat() throws Exception { SprayToThor sprayToThor = new SprayToThor(); sprayToThor.setFormat(SprayToThor.FORMAT.FIXED); CommandLine cmdLine = new CommandLine("/bin/dfuplus"); sprayToThor.addFormatArguments(cmdLine); assertEquals(2, cmdLine.getArguments().length); assertEquals("format=fixed", cmdLine.getArguments()[0]); assertEquals("maxrecordsize=8192", cmdLine.getArguments()[1]); }
From source file:com.tupilabs.pbs.PBS.java
/** * PBS qnodes command.//from w w w . j a va 2 s . c o m * <p> * Get information about the cluster nodes. * * @param name node name * @return list of nodes * @throws PBSException if an error communicating with the PBS occurs */ public static List<Node> qnodes(String name) { final List<Node> nodes; final CommandLine cmdLine = new CommandLine(COMMAND_QNODES); cmdLine.addArgument(PARAMETER_XML); if (StringUtils.isNotBlank(name)) { cmdLine.addArgument(name); } final OutputStream out = new ByteArrayOutputStream(); final OutputStream err = new ByteArrayOutputStream(); DefaultExecuteResultHandler resultHandler; try { resultHandler = execute(cmdLine, null, out, err); resultHandler.waitFor(DEFAULT_TIMEOUT); } catch (ExecuteException e) { throw new PBSException("Failed to execute qnodes command: " + e.getMessage(), e); } catch (IOException e) { throw new PBSException("Failed to execute qnodes command: " + e.getMessage(), e); } catch (InterruptedException e) { throw new PBSException("Failed to execute qnodes command: " + e.getMessage(), e); } final int exitValue = resultHandler.getExitValue(); LOGGER.info("qnodes exit value: " + exitValue); try { nodes = NODE_XML_PARSER.parse(out.toString()); } catch (ParseException pe) { throw new PBSException("Failed to parse node XML: " + pe.getMessage(), pe); } return nodes; }