/** * Stringify operator returns the command line as a string. * Parameters are correctly quoted when containing a space or * left untouched if the are already quoted. * * @return the command line as single string */ @Override public String toString() { return "[" + StringUtils.toString(toStrings(), ", ") + "]"; }
public abstract Process exec(final CommandLine cmd, final Map<String, String> env, final File workingDir) throws IOException;
/** * Launches the given command in a new process, in the given working * directory * * @param cmd * the command line to execute as an array of strings * @param env * the environment to set as an array of strings * @param workingDir * the working directory where the command should run * @throws IOException * probably forwarded from Runtime#exec */ @Override public Process exec(final CommandLine cmd, final Map<String, String> env, final File workingDir) throws IOException { final String[] envVars = EnvironmentUtils.toStrings(env); return Runtime.getRuntime().exec(cmd.toStrings(), envVars, workingDir); } }
protected int doExecution(DefaultExecutor executor, CommandLine commandLine, Map<Object, Object> env) { try { final StringJoiner paramsJoiner = new StringJoiner(" "); for (String cmdArg : commandLine.toStrings()) { paramsJoiner.add(cmdArg.replaceAll("(?<=\\.password=)(.*)", "****")); } logger.info("Executing: " + paramsJoiner.toString()); return executor.execute(commandLine, env); } catch (ExecuteException e) { return e.getExitValue(); } catch (Exception e) { throw new MuleControllerException("Error executing [" + commandLine.getExecutable() + " " + commandLine.getArguments() + "]", e); } }
/** * Launches the given command in a new process, in the given working * directory. * * @param cmd * the command line to execute as an array of strings * @param env * the environment to set as an array of strings * @param workingDir * working directory where the command should run * @throws IOException * forwarded from the exec method of the command launcher */ @Override public Process exec(final CommandLine cmd, final Map<String, String> env, final File workingDir) throws IOException { if (workingDir == null) { return exec(cmd, env); } final CommandLine newCmd = new CommandLine("cmd"); newCmd.addArgument("/c"); newCmd.addArguments(cmd.toStrings()); return exec(newCmd, env); } }
/** * Launches the given command in a new process, in the given working * directory. * * @param cmd * the command line to execute as an array of strings * @param env * the environment to set as an array of strings * @param workingDir * working directory where the command should run * @throws IOException * forwarded from the exec method of the command launcher */ @Override public Process exec(final CommandLine cmd, final Map<String, String> env, final File workingDir) throws IOException { if (workingDir == null) { return exec(cmd, env); } // Use cmd.exe to change to the specified directory before running // the command final CommandLine newCmd = new CommandLine("cmd"); newCmd.addArgument("/c"); newCmd.addArguments(cmd.toStrings()); return exec(newCmd, env); } }
protected String displayCommandLine(CommandLine commandLine) { StringBuilder sb = new StringBuilder(); for (String s : commandLine.toStrings()) { sb.append(s); sb.append(" "); } return sb.toString(); }
public static long logStartInfo(CommandLine command, File projectRoot) { log.info("Starting " + fullPath(projectRoot) + "> " + StringUtils.join(command.toStrings(), " ")); return System.currentTimeMillis(); }
private void runNPM(LineConsumer buildLogHandler, Map<String, String> envVarsForApp, String argument) { CommandLine command = new CommandLine(npmExec).addArgument(argument); buildLogHandler.consumeLine("Running " + StringUtils.join(command.toStrings(), " ")); ProcessStarter.run(buildLogHandler, envVarsForApp, command, projectRoot, TimeUnit.MINUTES.toMillis(30)); } }
private static Function<CommandLine, CommandLine> getWindowsShellWrapperFunc() { return (cmd) -> new CommandLine("cmd") .addArgument("/C") .addArguments(cmd.toStrings(), false); }
/** * Launches the Wisdom server in background using the {@literal chameleon.sh} scripts. * This method works only on Linux and Mac OS X. * * @param mojo the mojo * @throws MojoExecutionException if the Wisdom instance cannot be started. */ public void executeInBackground(AbstractWisdomMojo mojo) throws MojoExecutionException { File script = new File(mojo.getWisdomRootDirectory(), "chameleon.sh"); if (!script.isFile()) { throw new MojoExecutionException("The 'chameleon.sh' file does not exist in " + mojo .getWisdomRootDirectory().getAbsolutePath() + " - cannot launch Wisdom"); } // Remove the RUNNING_PID file if exists. File pid = new File(mojo.getWisdomRootDirectory(), "RUNNING_PID"); if (pid.isFile()) { mojo.getLog().info("The RUNNING_PID file is present, deleting it"); FileUtils.deleteQuietly(pid); } // We create a command line, as we are using th toString method on it. CommandLine cmdLine = new CommandLine(script); appendSystemPropertiesToCommandLine(mojo, cmdLine); try { mojo.getLog().info("Launching Wisdom Server using '" + cmdLine.toString() + "'."); // As we know which command line we are executing, we can safely execute the command. Runtime.getRuntime().exec(cmdLine.toStrings(), null, mojo.getWisdomRootDirectory()); //NOSONAR see comment } catch (IOException e) { throw new MojoExecutionException("Cannot execute Wisdom", e); } }
@Override public void start(LineConsumer buildLogHandler, LineConsumer consoleLogHandler, Map<String, String> envVarsForApp, Waiter startupWaiter) throws ProjectCannotStartException { CommandLine command = new CommandLine(executable) .addArgument("run") .addArgument("-p") .addArgument(projectFile.getName()); buildLogHandler.consumeLine("Starting Dotnet app: " + StringUtils.join(command.toStrings(), " ")); watchDog = ProcessStarter.startDaemon(buildLogHandler, consoleLogHandler, envVarsForApp, command, projectRoot, startupWaiter); buildLogHandler.consumeLine("Dotnet app started."); }
/** * Stops a running instance of wisdom using 'chameleon stop'. * * @param mojo the mojo * @throws MojoExecutionException if the instance cannot be stopped */ public void stop(AbstractWisdomMojo mojo) throws MojoExecutionException { File script = new File(mojo.getWisdomRootDirectory(), "chameleon.sh"); if (!script.isFile()) { throw new MojoExecutionException("The 'chameleon.sh' file does not exist in " + mojo .getWisdomRootDirectory().getAbsolutePath() + " - cannot stop the Wisdom instance"); } // If there is a RUNNING_PID file, exit immediately. File pid = new File(mojo.getWisdomRootDirectory(), "RUNNING_PID"); if (!pid.isFile()) { mojo.getLog().info("The RUNNING_PID file does not exist, are you sure Wisdom is running ?"); return; } CommandLine cmdLine = new CommandLine(script); cmdLine.addArgument("stop"); try { mojo.getLog().info("Stopping Wisdom Server using '" + cmdLine.toString() + "'."); // As we know which command line we are executing, we can safely execute the command. Runtime.getRuntime().exec(cmdLine.toStrings(), null, mojo.getWisdomRootDirectory()); //NOSONAR } catch (IOException e) { throw new MojoExecutionException("Cannot stop Wisdom", e); } }
.addArgument("server"); buildLogHandler.consumeLine("Creating virtualenv: " + StringUtils.join(virtualenvCmd.toStrings(), " ")); ProcessStarter.run(buildLogHandler, envVarsForApp, virtualenvCmd, projectRoot, TimeUnit.MINUTES.toMillis(30)); .addArgument(requirements.getAbsolutePath()); buildLogHandler.consumeLine("Installing dependencies: " + StringUtils.join(pipCmd.toStrings(), " ")); ProcessStarter.run(buildLogHandler, envVarsForApp, pipCmd, projectRoot, TimeUnit.MINUTES.toMillis(30)); .addArgument("--app-name=" + envVarsForApp.get("APP_NAME")); //Add app name to command line so ps can identify which app is running buildLogHandler.consumeLine("Starting python app: " + StringUtils.join(command.toStrings(), " "));
for (CommandLine cmd : commands) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); String commandstring = StringUtils.toString(cmd.toStrings(), " "); commandstring, e.toString()); } catch (IOException e) { String executable = cmd.toStrings()[0]; if (executable.equals("wmctrl")) { wmctrlAvail = false;
} catch (IOException e) { throw new RrdException("An error occured while executing '" + StringUtils.join(cmdLine.toStrings(), " ") + "' with stderr: " + stderr.toString(), e); } catch (SAXException | JAXBException e) {
} catch (IOException e) { throw new RrdException("An error occured while executing '" + StringUtils.join(cmdLine.toStrings(), " ") + "' with stderr: " + stderr.toString(), e); } catch (SAXException | JAXBException e) {
public static void run(LineConsumer outputHandler, Map<String, String> envVarsForApp, CommandLine command, File projectRoot, long timeout) throws ProjectCannotStartException { long startTime = logStartInfo(command, projectRoot); ExecuteWatchdog watchDog = new ExecuteWatchdog(timeout); Executor executor = createExecutor(outputHandler, command, projectRoot, watchDog); try { int exitValue = executor.execute(command, envVarsForApp); if (executor.isFailure(exitValue)) { String message = watchDog.killedProcess() ? "Timed out waiting for " + command : "Exit code " + exitValue + " returned from " + command; throw new ProjectCannotStartException(message); } } catch (Exception e) { String message = "Error running: " + fullPath(projectRoot) + "> " + StringUtils.join(command.toStrings(), " "); outputHandler.consumeLine(message); outputHandler.consumeLine(e.toString()); throw new ProjectCannotStartException(message, e); } logEndTime(command, startTime); }
private static Executor createExecutor(LineConsumer consoleLogHandler, CommandLine command, File projectRoot, ExecuteWatchdog watchDog) { Executor executor = new DefaultExecutor(); executor.setWorkingDirectory(projectRoot); executor.setWatchdog(watchDog); executor.setStreamHandler(new PumpStreamHandler(new WriterOutputStream(new WriterToOutputBridge(consoleLogHandler)))); consoleLogHandler.consumeLine(fullPath(executor.getWorkingDirectory()) + "> " + String.join(" ", command.toStrings()) + LINE_SEPARATOR); return executor; }