Java 类org.apache.commons.exec.CommandLine 实例源码

项目:cloud-portal    文件:TerraformService.java   
private CommandLine buildActionCommand(String terraformPath, String action, Map<String, Object> variableMap) {

        CommandLine actionCommand = new CommandLine(terraformPath);
        actionCommand.addArgument(action);

        if (action.equals(Constants.ACTION_DESTROY)) {
            actionCommand.addArgument(FLAG_FORCE);
        }

        actionCommand.addArgument(FLAG_NO_COLOR);

        for (Entry<String, Object> variableEntry : variableMap.entrySet()) {

            String variableName = variableEntry.getKey();
            String variableValue = (String) variableEntry.getValue();
            String variableString = variableName + Constants.CHAR_EQUAL + (variableValue.equals("on") ? "true" : variableValue);

            actionCommand.addArgument(FLAG_VAR);
            actionCommand.addArgument(variableString, false);
        }

        return actionCommand;
    }
项目:stage-job    文件:ScriptUtil.java   
/**
 * 日志文件输出方式
 *
 * 优点:支持将目标数据实时输出到指定日志文件中去
 * 缺点:
 *      标准输出和错误输出优先级固定,可能和脚本中顺序不一致
 *      Java无法实时获取
 *
 * @param command
 * @param scriptFile
 * @param logFile
 * @param params
 * @return
 * @throws IOException
 */
public static int execToFile(String command, String scriptFile, String logFile, String... params) throws IOException {
    // 标准输出:print (null if watchdog timeout)
    // 错误输出:logging + 异常 (still exists if watchdog timeout)
    // 标准输入
    FileOutputStream fileOutputStream = new FileOutputStream(logFile, true);
    PumpStreamHandler streamHandler = new PumpStreamHandler(fileOutputStream, fileOutputStream, null);

    // command
    CommandLine commandline = new CommandLine(command);
    commandline.addArgument(scriptFile);
    if (params!=null && params.length>0) {
        commandline.addArguments(params);
    }

    // exec
    DefaultExecutor exec = new DefaultExecutor();
    exec.setExitValues(null);
    exec.setStreamHandler(streamHandler);
    int exitValue = exec.execute(commandline);  // exit code: 0=success, 1=error
    return exitValue;
}
项目:jekyll2cms    文件:JekyllService.java   
/**
 * Starts the jekyll build process (jekyll build --incremental)
 * @return true, if jekyll build was successful
 */
public boolean startJekyllCI() {
    int exitValue = -1;
    String line = JEKYLL_PATH;
    ByteArrayOutputStream jekyllBuildOutput = new ByteArrayOutputStream();
    CommandLine cmdLine = CommandLine.parse(line);
    cmdLine.addArgument(JEKYLL_OPTION_BUILD);
    cmdLine.addArgument(JEKYLL_OPTION_INCR);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setWorkingDirectory(new File(LOCAL_REPO_PATH));
    PumpStreamHandler streamHandler = new PumpStreamHandler(jekyllBuildOutput);
    executor.setStreamHandler(streamHandler);
    try {
        LOGGER.info("Starting jekyll build");
        exitValue = executor.execute(cmdLine);
        LOGGER.info("Jekyll build command executed");
    } catch (IOException e) {
        LOGGER.error("Error while executing jekyll build. Error message: {}", e.getMessage());
        e.printStackTrace();
        return false;
    }
    printJekyllStatus(exitValue, jekyllBuildOutput.toString());
    return true;
}
项目:commons-sandbox    文件:Tests.java   
public static void main(String[] args) throws Exception {
    String cmd = "/tmp/test.sh";
    CommandLine cmdLine = CommandLine.parse("/bin/bash " + cmd);

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    ByteArrayOutputStream stderr = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout, stderr);

    psh.setStopTimeout(TIMEOUT_FIVE_MINUTES);

    ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT_TEN_MINUTES); // timeout in milliseconds

    Executor executor = new DefaultExecutor();
    executor.setExitValue(0);
    executor.setStreamHandler(psh);
    executor.setWatchdog(watchdog);

    int exitValue = executor.execute(cmdLine, Collections.emptyMap());
    System.out.println(exitValue);
}
项目:xxl-job    文件:ScriptUtil.java   
/**
 * 日志文件输出方式
 *
 * 优点:支持将目标数据实时输出到指定日志文件中去
 * 缺点:
 *      标准输出和错误输出优先级固定,可能和脚本中顺序不一致
 *      Java无法实时获取
 *
 * @param command
 * @param scriptFile
 * @param logFile
 * @param params
 * @return
 * @throws IOException
 */
public static int execToFile(String command, String scriptFile, String logFile, String... params) throws IOException {
    // 标准输出:print (null if watchdog timeout)
    // 错误输出:logging + 异常 (still exists if watchdog timeout)
    // 标准输入
    FileOutputStream fileOutputStream = new FileOutputStream(logFile, true);
    PumpStreamHandler streamHandler = new PumpStreamHandler(fileOutputStream, fileOutputStream, null);

    // command
    CommandLine commandline = new CommandLine(command);
    commandline.addArgument(scriptFile);
    if (params!=null && params.length>0) {
        commandline.addArguments(params);
    }

    // exec
    DefaultExecutor exec = new DefaultExecutor();
    exec.setExitValues(null);
    exec.setStreamHandler(streamHandler);
    int exitValue = exec.execute(commandline);  // exit code: 0=success, 1=error
    return exitValue;
}
项目:dispatcher    文件:CLikeWorker.java   
public void compile(String cwd, Submission submission) throws RuntimeException {
    CommandLine cmd = new CommandLine(compiler);
    cmd.addArgument("-basedir=" + cwd);
    cmd.addArgument("-compiler=" + realCompiler);
    cmd.addArgument("-filename=" + fileName + "." + suffix);
    cmd.addArgument("-timeout=" + watchdogTimeout);
    cmd.addArgument("-std=" + std);
    logger.info(cmd.toString());

    ByteArrayOutputStream stderr = new ByteArrayOutputStream();
    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(new PumpStreamHandler(null, stderr, null));
    try {
        executor.execute(cmd);
        if (stderr.toString().length() > 0) {
            submission.setStatus(Submission.STATUS_CE);
            submission.setError("Compile error");
            logger.warn(stderr.toString());
            throw new RuntimeException("Sandbox Aborted.");
        }
        logger.info("Compile OK");
    } catch (IOException e) {
        logger.warn("Compile error:\t" + e.getMessage());
        throw new RuntimeException("An Error Occurred.");
    }
}
项目:yadaframework    文件:YadaUtil.java   
/**
 * Esegue un comando di shell
 * @param command comando
 * @param args lista di argomenti (ogni elemento puo' contenere spazi), puo' essere null
 * @param outputStream ByteArrayOutputStream che conterrà l'output del comando
 * @return the error message (will be empty for a return code >0), or null if there was no error
 */
public String exec(String command, List<String> args, ByteArrayOutputStream outputStream) {
    int exitValue=1;
    try {
        CommandLine commandLine = new CommandLine(command);
        if (args!=null) {
            for (String arg : args) {
                commandLine.addArgument(arg, false); // Don't handle quoting
            }
        }
        DefaultExecutor executor = new DefaultExecutor();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        executor.setStreamHandler(streamHandler);
        log.debug("Executing shell command: {}", commandLine);
        exitValue = executor.execute(commandLine);
    } catch (Exception e) {
        log.error("Failed to execute shell command: " + command + " " + args, e);
        return e.getMessage();
    }
    return exitValue>0?"":null;
}
项目:sonarlint-cli    文件:CommandExecutor.java   
public int execute(String[] args, @Nullable Path workingDir, Map<String, String> addEnv) throws IOException {
  if (!Files.isExecutable(file)) {
    Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
    perms.add(PosixFilePermission.OWNER_READ);
    perms.add(PosixFilePermission.OWNER_EXECUTE);
    Files.setPosixFilePermissions(file, perms);
  }

  ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT);
  CommandLine cmd = new CommandLine(file.toFile());
  cmd.addArguments(args);
  DefaultExecutor exec = new DefaultExecutor();
  exec.setWatchdog(watchdog);
  exec.setStreamHandler(createStreamHandler());
  exec.setExitValues(null);
  if (workingDir != null) {
    exec.setWorkingDirectory(workingDir.toFile());
  }
  in.close();
  LOG.info("Executing: {}", cmd.toString());
  Map<String, String> env = new HashMap<>(System.getenv());
  env.putAll(addEnv);
  return exec.execute(cmd, env);
}
项目:reactor    文件:DockerClient.java   
/**
 * Runs docker command asynchronously.
 *
 * @param arguments command line arguments
 * @param out       stdout will be written in to this file
 *
 * @return a handle used to stop the command process
 *
 * @throws IOException when command has error
 */
public ExecuteWatchdog dockerAsync(final List<Object> arguments, OutputStream out) throws IOException {
    CommandLine cmdLine = new CommandLine(DOCKER);
    arguments.forEach((arg) -> {
        cmdLine.addArgument(arg + "");
    });
    LOG.debug("[{} {}]", cmdLine.getExecutable(), StringUtils.join(cmdLine.getArguments(), " "));
    List<String> output = new ArrayList<>();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    Executor executor = new DefaultExecutor();
    executor.setWatchdog(watchdog);
    PrintWriter writer = new PrintWriter(out);
    ESH esh = new ESH(writer);
    executor.setStreamHandler(esh);
    executor.execute(cmdLine, new DefaultExecuteResultHandler());
    return watchdog;
}
项目:piper    文件:Mediainfo.java   
@Override
public Map<String,Object> handle (Task aTask) throws Exception {
  CommandLine cmd = new CommandLine ("mediainfo");
  cmd.addArgument(aTask.getRequiredString("input"));
  log.debug("{}",cmd);
  DefaultExecutor exec = new DefaultExecutor();
  File tempFile = File.createTempFile("log", null);
  try (PrintStream stream = new PrintStream(tempFile);) {
    exec.setStreamHandler(new PumpStreamHandler(stream));
    exec.execute(cmd);
    return parse(FileUtils.readFileToString(tempFile));
  }
  catch (ExecuteException e) {
    throw new ExecuteException(e.getMessage(),e.getExitValue(), new RuntimeException(FileUtils.readFileToString(tempFile)));
  }
  finally {
    FileUtils.deleteQuietly(tempFile);
  }
}
项目:piper    文件:Ffmpeg.java   
@Override
public Object handle(Task aTask) throws Exception {
  List<String> options = aTask.getList("options", String.class);
  CommandLine cmd = new CommandLine ("ffmpeg");
  options.forEach(o->cmd.addArgument(o));
  log.debug("{}",cmd);
  DefaultExecutor exec = new DefaultExecutor();
  File tempFile = File.createTempFile("log", null);
  try (PrintStream stream = new PrintStream(tempFile);) {
    exec.setStreamHandler(new PumpStreamHandler(stream));
    int exitValue = exec.execute(cmd);
    return exitValue!=0?FileUtils.readFileToString(tempFile):cmd.toString();
  }
  catch (ExecuteException e) {
    throw new ExecuteException(e.getMessage(),e.getExitValue(), new RuntimeException(FileUtils.readFileToString(tempFile)));
  }
  finally {
    FileUtils.deleteQuietly(tempFile);
  }
}
项目:piper    文件:Ffprobe.java   
@Override
public Map<String,Object> handle(Task aTask) throws Exception {
  CommandLine cmd = new CommandLine ("ffprobe");
  cmd.addArgument("-v")
     .addArgument("quiet")
     .addArgument("-print_format")
     .addArgument("json")
     .addArgument("-show_error")
     .addArgument("-show_format")
     .addArgument("-show_streams")
     .addArgument(aTask.getRequiredString("input"));
  log.debug("{}",cmd);
  DefaultExecutor exec = new DefaultExecutor();
  File tempFile = File.createTempFile("log", null);
  try (PrintStream stream = new PrintStream(tempFile);) {
    exec.setStreamHandler(new PumpStreamHandler(stream));
    exec.execute(cmd);
    return parse(FileUtils.readFileToString(tempFile));
  }
  catch (ExecuteException e) {
    throw new ExecuteException(e.getMessage(),e.getExitValue(), new RuntimeException(FileUtils.readFileToString(tempFile)));
  }
  finally {
    FileUtils.deleteQuietly(tempFile);
  }
}
项目:app-runner-router    文件:AppRunnerInstance.java   
public AppRunnerInstance start() {
    File dir = new File("target/e2e/" + id + "/" + System.currentTimeMillis());
    assertTrue(dir.mkdirs());
    int httpPort = getAFreePort();
    int httpsPort = getAFreePort();
    File uberJar = new File(FilenameUtils.separatorsToSystem("target/e2e/" + jarName));
    if (!uberJar.isFile()) {
        throw new RuntimeException("Could not find the app-runner jar. Try running mvn compile first.");
    }

    CommandLine command = new CommandLine("java")
        .addArgument("-Dlogback.configurationFile=" + dirPath(new File("src/test/resources/logback-test.xml")))
        .addArgument("-Dappserver.port=" + httpPort)
        .addArgument("-Dappserver.https.port=" + httpsPort)
        .addArgument("-Dappserver.data.dir=" + dirPath(new File(dir, "data")))
        .addArgument("-Dapprunner.keystore.path=" + dirPath(new File("local/test.keystore")))
        .addArgument("-Dapprunner.keystore.password=password")
        .addArgument("-Dapprunner.keymanager.password=password")
        .addArgument("-jar")
        .addArgument(dirPath(uberJar));
    httpUrl = URI.create("http://localhost:" + httpPort + "/");
    httpsUrl = URI.create("https://localhost:" + httpsPort + "/");
    log.info("Starting " + id + " at " + httpUrl);
    this.killer = new ProcessStarter(log).startDaemon(env, command, dir, Waiter.waitFor(id, httpUrl, 60, TimeUnit.SECONDS));
    return this;
}
项目:app-runner    文件:GradleRunner.java   
private ExecuteWatchdog runJar(InvocationOutputHandler buildLogHandler, InvocationOutputHandler consoleLogHandler, Map<String, String> envVarsForApp, Waiter startupWaiter) {
    Path libsPath = Paths.get(projectRoot.getPath(), "build", "libs");
    File libsFolder = libsPath.toFile();

    // To simplify implementation, now I assume only 1 uberjar named "artifact-version-all.jar" under libs folder
    // As we clean the project every time, I can't foresee any possibility that will mix up other uberjars.
    File[] files = libsFolder.listFiles();

    if(files == null) {
        throw new ProjectCannotStartException(libsFolder.getPath() + " doesn't exist");
    }

    Optional<File> jar = Stream.of(files).filter((f) -> f.getName().contains("all")).findFirst();

    if (!jar.isPresent() || !jar.get().isFile()) {
        throw new ProjectCannotStartException("Could not find the jar file at " + jar.get().getPath());
    }

    CommandLine command = javaHomeProvider.commandLine(envVarsForApp)
        .addArgument("-Djava.io.tmpdir=" + envVarsForApp.get("TEMP"))
        .addArgument("-jar")
        .addArgument(fullPath(jar.get()));

    return ProcessStarter.startDaemon(buildLogHandler, consoleLogHandler, envVarsForApp, command, projectRoot, startupWaiter);
}
项目:distGatling    文件:JarExecutor.java   
private void runCancelJob(Master.Job message) {
    if (getAbortStatus(message.abortUrl, message.trackingId)) {
        CommandLine cmdLine = new CommandLine("/bin/bash");
        cmdLine.addArgument(agentConfig.getJob().getJobArtifact("cancel"));
        cmdLine.addArgument(message.jobId);
        DefaultExecutor killExecutor = new DefaultExecutor();
        ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
        killExecutor.setWatchdog(watchdog);
        try {
            log.info("Cancel command: {}", cmdLine);
            killExecutor.execute(cmdLine);
            TaskEvent taskEvent = (TaskEvent) message.taskEvent;
            String outPath = agentConfig.getJob().getOutPath(taskEvent.getJobName(), message.jobId);
            String errPath = agentConfig.getJob().getErrorPath(taskEvent.getJobName(), message.jobId);
            Worker.Result result = new Worker.Result(-9, agentConfig.getUrl(errPath), agentConfig.getUrl(outPath), null, message);
            getSender().tell(new Worker.WorkFailed(result), getSelf());
        } catch (IOException e) {
            log.error(e, "Error cancelling job");
        }
    }
}
项目:distGatling    文件:JarExecutor.java   
private CommandLine getJarCommand(Master.Job job, TaskEvent taskEvent) {
    CommandLine cmdLine = new CommandLine("java");
    cmdLine.addArgument("-jar");

    //download the data feed
    if(taskEvent.getJobInfo().hasDataFeed) {
        log.info("Downloading from {}  to {}", job.dataFileUrl,agentConfig.getJob().getJobDirectory(job.jobId, DATA,taskEvent.getJobInfo().dataFileName));
        DownloadFile.downloadFile(job.dataFileUrl, agentConfig.getJob().getJobDirectory(job.jobId, DATA,taskEvent.getJobInfo().dataFileName));
        //job data feed  path
    }
    cmdLine.addArgument("-DdataFolder=" + agentConfig.getJob().getJobDirectory(job.jobId,DATA));
    cmdLine.addArgument("-DresultsFolder=" + agentConfig.getJob().getResultPath(job.roleId, job.jobId));
    cmdLine.addArgument("-DnoReports=true");
    //parameters come from the task event
    for (String pair : taskEvent.getParameters()) {
        cmdLine.addArgument(pair);
    }
    log.info("Downloading jar to {} ",agentConfig.getJob().getJobDirectory(job.jobId, SIMULATION, taskEvent.getJobInfo().jarFileName));
    //download the simulation or jar file
    DownloadFile.downloadFile(job.jobFileUrl,agentConfig.getJob().getJobDirectory(job.jobId, SIMULATION, taskEvent.getJobInfo().jarFileName));//.jar

    cmdLine.addArgument(agentConfig.getJob().getJobDirectory(job.jobId, SIMULATION, taskEvent.getJobInfo().jarFileName));//.jar
    return cmdLine;
}
项目:distGatling    文件:ScriptExecutor.java   
private void runCancelJob(Master.Job message) {
    if (getAbortStatus(message.abortUrl, message.trackingId)) {
        CommandLine cmdLine = new CommandLine("/bin/bash");
        cmdLine.addArgument(agentConfig.getJob().getJobArtifact("cancel"));
        cmdLine.addArgument(message.jobId);
        DefaultExecutor killExecutor = new DefaultExecutor();
        ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
        killExecutor.setWatchdog(watchdog);
        try {
            log.info("Cancel command: {}", cmdLine);
            killExecutor.execute(cmdLine);
        } catch (IOException e) {
            log.error(e, "Error cancelling job");
        }
    }
}
项目:web2app    文件:Web2AppMojo.java   
void exec(String action, File dir, CommandLine cmdLine) {
    String label = action + ": " + cmdLine;
    try {
        DefaultExecutor executor = new DefaultExecutor();
        executor.setWorkingDirectory(dir);
        Map<String, String> environment = EnvironmentUtils.getProcEnvironment();
        environment.put("PATH", environment.get("PATH") + ":" + new File(frontendDirectory, "node").getAbsolutePath());
        int exitValue = executor.execute(cmdLine, environment);
        if (exitValue == 0) {
            getLog().info(label + ": OK");
        } else {
            throw new MojoExecutionException("EXEC FAILURE: " + label);
        }
    } catch (RuntimeException re) {
        throw re;
    } catch (Exception e) {
        throw new IllegalStateException("EXEC FAILURE: " + label, e);
    }
}
项目:vespa    文件:ProcessExecutor.java   
/**
 * Executes the given command synchronously.
 *
 * @param command The command to execute.
 * @param processInput Input provided to the process.
 * @return The result of the execution, or empty if the process does not terminate within the timeout set for this executor.
 * @throws IOException if the process execution failed.
 */
public Optional<ProcessResult> execute(String command, String processInput) throws IOException {
    ByteArrayOutputStream processErr = new ByteArrayOutputStream();
    ByteArrayOutputStream processOut = new ByteArrayOutputStream();

    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(createStreamHandler(processOut, processErr, processInput));
    ExecuteWatchdog watchDog = new ExecuteWatchdog(TimeUnit.SECONDS.toMillis(timeoutSeconds));
    executor.setWatchdog(watchDog);
    executor.setExitValues(successExitCodes);

    int exitCode;
    try {
        exitCode = executor.execute(CommandLine.parse(command));
    } catch (ExecuteException e) {
        exitCode = e.getExitValue();
    }
    return (watchDog.killedProcess()) ?
            Optional.empty() : Optional.of(new ProcessResult(exitCode, processOut.toString(), processErr.toString()));
}
项目:gradle-mobile-plugin    文件:BuildTask.java   
private void buildArtifacts() throws IOException {
    CommandLine commandLine = new CommandLine("xcodebuild");
    commandLine.addArgument("-configuration");
    commandLine.addArgument(env.getConfiguration());
    commandLine.addArgument("-sdk");
    commandLine.addArgument(env.getSdk());
    commandLine.addArgument("-target");
    commandLine.addArgument(env.getTarget(), false);
    commandLine.addArgument("DSTROOT="+ PathUtil.getXcodeDstDir().getAbsolutePath());
    commandLine.addArgument("OBJROOT="+PathUtil.getXcodeObjDir().getAbsolutePath());
    commandLine.addArgument("SYMROOT="+new File(PathUtil.getXcodeSymDir(), env.getName()).getAbsolutePath());
    commandLine.addArgument("SHARED_PRECOMPS_DIR="+PathUtil.getXcodeSharedDir().getAbsolutePath());
    ExecResult execResult = ExecUtil.execCommand(commandLine, null, null, true, true);
    FileUtils.writeLines(new File(PathUtil.getBuildlogDir(), this.getName()+"Task.build.log"), execResult.getOutput());
    if (!execResult.isSuccess()) {
        ErrorUtil.errorInTask(this.getName(), execResult.getException());
    }
}
项目:gradle-mobile-plugin    文件:BuildAndroidTask.java   
private void zipalignArtifact() throws IOException {
    logger.info("Zipaligning artifact...");
    File sourceApk = getUnsignedArtifact();
    File targetApk = new File(sourceApk.getParentFile(), sourceApk.getName().substring(0,sourceApk.getName().length()-4)+"-signed.apk");
    CommandLine commandLine = new CommandLine("zipalign");
    commandLine.addArgument("-f");
    commandLine.addArgument("-v");
    commandLine.addArgument("4");
    commandLine.addArgument(sourceApk.getAbsolutePath(), false);
    commandLine.addArgument(targetApk.getAbsolutePath(), false);
    ExecResult execResult = ExecUtil.execCommand(commandLine, null, null, false, false);
    if (!execResult.isSuccess()) {
        throw new GradleException("Zipaligning failed");
    }
    Files.deleteIfExists(sourceApk.toPath());
    FileUtils.copyFile(targetApk, sourceApk);
    logger.info("Zipaligning successful");
}
项目:gradle-mobile-plugin    文件:GitUtil.java   
public static long getCheckedoutCommitNumber(File dir, List<String> excludes) {
    long timestamp = getCheckedoutCommitTimestamp(dir);
    CommandLine command = new CommandLine("git");
    command.addArgument("rev-list");
    command.addArgument("--count");
    for (String exclude : excludes) {
        command.addArgument("--exclude="+exclude, false);
    }
    command.addArgument("--remotes");
    command.addArgument("--before="+timestamp);
    ExecResult execResult = ExecUtil.execCommand(command, dir, null, true, false);
    if (execResult.isSuccess()) {
        return Long.parseLong(execResult.getOutput().get(0));
    } else {
        return 0L;
    }
}
项目:gradle-mobile-plugin    文件:ExecUtil.java   
@Deprecated
public static ExecResult execCommand(CommandLine commandLine, File workDir, int[] exitValues, boolean routeToCapture, boolean routeToStdout) {
    Executor executor = new DefaultExecutor();
    if (workDir != null) {
        executor.setWorkingDirectory(workDir);
    }
    if (exitValues != null) {
        executor.setExitValues(exitValues);
    }
    ExecOutputStream execOutputStream = new ExecOutputStream(routeToCapture, routeToStdout);
    PumpStreamHandler streamHandler = new PumpStreamHandler(execOutputStream);
    executor.setStreamHandler(streamHandler);
    try {
        executor.execute(commandLine);
    } catch (IOException e) {
        return new ExecResult(false, execOutputStream.getOutput(), e);
    }
    return new ExecResult(true, execOutputStream.getOutput(), null);
}
项目:device    文件:ShellCommand.java   
@SuppressWarnings("rawtypes")
public static void execAsync(String display, CommandLine commandline)
        throws ShellCommandException {
    log.debug("executing async command: " + commandline);
    DefaultExecutor exec = new DefaultExecutor();

    ExecuteResultHandler handler = new DefaultExecuteResultHandler();
    PumpStreamHandler streamHandler = new PumpStreamHandler(
            new PritingLogOutputStream());
    exec.setStreamHandler(streamHandler);
    try {
        if (display == null || display.isEmpty()) {
            exec.execute(commandline, handler);
        } else {
            Map env = EnvironmentUtils.getProcEnvironment();
            EnvironmentUtils.addVariableToEnvironment(env, "DISPLAY=:"
                    + display);

            exec.execute(commandline, env, handler);
        }
    } catch (Exception e) {
        throw new ShellCommandException(
                "An error occured while executing shell command: "
                        + commandline, e);
    }
}
项目:device    文件:AbstractDevice.java   
/**
 * run command to get dump file
 */
public boolean runtest() {
  cleanTemp();
  CommandLine command = adbCommand("shell", "uiautomator", "runtest",
      "/data/local/tmp/automator.jar", "-c", "com.uia.example.my.test");
  String output = executeCommandQuietly(command);
  log.debug("run test {}", output);

  try {
    // give it a second to recover from the activity start
    Thread.sleep(1000);
  } catch (InterruptedException ie) {
    throw new RuntimeException(ie);
  }
  return output.contains("OK");
}
项目:compose-executor    文件:ProcessUtils.java   
public static int executeCommand(String command,ExecuteWatchdog watchdog) {
    CommandLine cmdLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(new PumpStreamHandler(System.out,System.err, null));
    executor.setExitValues(new int[]{0, 1});
    if(watchdog != null){
        executor.setWatchdog(watchdog);
    }
    int exitValue = 0;
    try {
        exitValue = executor.execute(cmdLine);
    } catch (IOException e) {
        exitValue = 1;
        log.error("error executing command", e);
    }
    return exitValue;
}
项目:project-build-plugin    文件:EngineControl.java   
private CommandLine toEngineCommand(Command command)
{
  String classpath = context.engineClasspathJarPath;
  if (StringUtils.isNotBlank(context.vmOptions.additionalClasspath))
  {
    classpath += File.pathSeparator + context.vmOptions.additionalClasspath;
  }

  File osgiDir = new File(context.engineDirectory, OsgiDir.INSTALL_AREA);

  CommandLine cli = new CommandLine(new File(getJavaExec()))
          .addArgument("-classpath").addArgument(classpath)
          .addArgument("-Divy.engine.testheadless=true")
          .addArgument("-Dosgi.install.area=" + osgiDir.getAbsolutePath());

  if (StringUtils.isNotBlank(context.vmOptions.additionalVmOptions))
  {
    cli.addArguments(context.vmOptions.additionalVmOptions, false);
  }
  cli.addArgument("org.eclipse.equinox.launcher.Main")
          .addArgument("-application").addArgument("ch.ivyteam.ivy.server.exec.engine")
          .addArgument(command.toString());
  return cli;
}
项目:project-build-plugin    文件:EngineControl.java   
/**
 * Run a short living engine command where we expect a process failure as the engine invokes <code>System.exit(-1)</code>.
 * @param statusCmd 
 * @return the output of the engine command.
 */
private String executeSynch(CommandLine statusCmd)
{
  String engineOutput = null;
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, System.err);
  Executor executor = createEngineExecutor();
  executor.setStreamHandler(streamHandler);
  executor.setExitValue(-1);
  try
  {
    executor.execute(statusCmd);
  }
  catch (IOException ex)
  { // expected!
  }
  finally
  {
    engineOutput = outputStream.toString();
    IOUtils.closeQuietly(outputStream);
  }
  return engineOutput;
}
项目:poor-man-transcoder    文件:CommandBuilderHelper.java   
protected String prepareSegmentDirectory(CommandLine cmdLine, ITranscoderResource output, TokenReplacer tokenReplacer, String masterWorkingDirectory) throws IOException
{   
    // create sub directory for hls output
    String HLS_SAMPLE_PLAYBACK_TEMPLATE = "hls-index-sample.html";
    String OUTPUT_HLS_PLAYBACK_TEMPLATE = "index.html";
    tokenReplacer.setTokenValue(TokenReplacer.TOKEN.HLS_SAMPLE_PLAYBACK_TEMPLATE, HLS_SAMPLE_PLAYBACK_TEMPLATE);

    String outName = output.getMediaName();
    String outNameWithOutExt = FilenameUtils.removeExtension(outName);

    File sub = new File(masterWorkingDirectory + File.separator + outNameWithOutExt);
    if(!sub.exists()) sub.mkdir();

    File indexTemplateSample = new File(Globals.getEnv(Globals.Vars.TEMPLATE_DIRECTORY) + File.separator + HLS_SAMPLE_PLAYBACK_TEMPLATE);
    File indexTemplate = new File(sub.getAbsolutePath() + File.separator + OUTPUT_HLS_PLAYBACK_TEMPLATE);
    if(indexTemplateSample.exists())  FileUtils.copyFile(indexTemplateSample, indexTemplate);
    logger.info("Copying html template for hls playback into " + sub.getAbsolutePath());

    String relative = new File(masterWorkingDirectory).toURI().relativize(new File(sub.getAbsolutePath()).toURI()).getPath();
    logger.info("Relative path of segment directory " + relative);

    return relative;

}
项目:npm-maven-plugin    文件:ExecMojo.java   
protected CommandLine getNpmCommand()
{
String sNpm = npmHome == null
              ? "npm"
              : new File(npmHome, "npm").getAbsolutePath();

if (isWindows())
    {
    sNpm = sNpm + ".cmd";
    CommandLine cmd = new CommandLine("cmd");
    cmd.addArgument("/c");
    cmd.addArgument(sNpm);
    return cmd;
    }
else
    {
    return new CommandLine(sNpm);
    }
}
项目:commons-dost    文件:DotUtils.java   
/**
 * Verify if dot can be started and print out the version to stdout.
 *
 * @return True if "dot -V" ran successfully, false otherwise
 *
 * @throws IOException If running dot fails.
 */
public static boolean checkDot() throws IOException {
    // call graphviz-dot via commons-exec
    CommandLine cmdLine = new CommandLine(DOT_EXE);
    cmdLine.addArgument("-V");
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);
    executor.setStreamHandler(new PumpStreamHandler(System.out, System.err));
    int exitValue = executor.execute(cmdLine);
    if(exitValue != 0) {
        System.err.println("Could not run '" + DOT_EXE + "', had exit value: " + exitValue + "!");
        return false;
    }

    return true;
}
项目:device    文件:AbstractDevice.java   
/**
 * Get crash log from AUT
 *
 * @return empty string if there is no crash log on the device, otherwise returns the stack trace
 * caused by the crash of the AUT
 */
public String getCrashLog() {
  String crashLogFileName = null;
  File crashLogFile = new File(getExternalStoragePath(), crashLogFileName);

  // the "test" utility doesn't exist on all devices so we'll check the
  // output of ls.
  CommandLine directoryListCommand = adbCommand("shell", "ls",
      crashLogFile.getParentFile().getAbsolutePath());
  String directoryList = executeCommandQuietly(directoryListCommand);
  if (directoryList.contains(crashLogFileName)) {
    return executeCommandQuietly(adbCommand("shell", "cat",
        crashLogFile.getAbsolutePath()));
  }

  return "";
}
项目:device    文件:AbstractDevice.java   
/**
 * Push automator.jar to android tmp folder
 *
 * @return push device successful or not
 */
public boolean pushAutomator2Device() {
  InputStream io = AbstractDevice.class.getResourceAsStream("automator.jar");
  File dest = new File(FileUtils.getTempDirectory(), "automator.jar");

  try {
    FileUtils.copyInputStreamToFile(io, dest);
  } catch (IOException e) {
    e.printStackTrace();
  }

  CommandLine pushcommand = adbCommand("push ", dest.getAbsolutePath(), "/data/local/tmp/");
  String outputPush = executeCommandQuietly(pushcommand);
  log.debug("Push automator.jar to device {}", outputPush);

  try {
    // give it a second to recover from the activity start
    Thread.sleep(1000);
  } catch (InterruptedException ie) {
    throw new RuntimeException(ie);
  }
  return outputPush.contains("KB/s");
}
项目:commons-dost    文件:SVNCommands.java   
/**
 * Record a manual merge from one branch to the local working directory.
 *
 * @param directory The local working directory
 * @param branchName The branch that was merged in manually
 * @param revisions The list of merged revisions.
 * @return A stream which provides the output of the "svn merge" command, should be closed by the caller
 * @throws IOException Execution of the SVN sub-process failed or the
 *          sub-process returned a exit value indicating a failure
 */
public static InputStream recordMerge(File directory, String branchName, long... revisions) throws IOException {
    //              svn merge -c 3328 --record-only ^/calc/trunk
    CommandLine cmdLine = new CommandLine(SVN_CMD);
    cmdLine.addArgument(CMD_MERGE);
    addDefaultArguments(cmdLine, null, null);
    cmdLine.addArgument("--record-only");
    cmdLine.addArgument("-c");
    StringBuilder revs = new StringBuilder();
    for (long revision : revisions) {
        revs.append(revision).append(",");
    }
    cmdLine.addArgument(revs.toString());
    // leads to "non-inheritable merges"
    // cmdLine.addArgument("--depth");
    // cmdLine.addArgument("empty");
    cmdLine.addArgument("^" + branchName);
    //cmdLine.addArgument("."); // current dir

    return ExecutionHelper.getCommandResult(cmdLine, directory, 0, 120000);
}
项目:trainbenchmark    文件:GeneratorRunner.java   
public static int run(final GeneratorConfig gc, final ExecutionConfig ec) throws IOException, InterruptedException {
    final File configFile = File.createTempFile("trainbenchmark-generator-", ".conf");
    final String configPath = configFile.getAbsolutePath();
    gc.saveToFile(configPath);

    final String projectName = String.format("trainbenchmark-generator-%s", gc.getProjectName());
    final String jarPath = String.format("../%s/build/libs/%s-1.0.0-SNAPSHOT-fat.jar", projectName, projectName);
    final String javaCommand = String.format("java -Xms%s -Xmx%s -server -jar %s %s", ec.getXms(), ec.getXmx(),
            jarPath, configPath);

    final CommandLine cmdLine = CommandLine.parse(javaCommand);
    final DefaultExecutor executor = new DefaultExecutor();
    try {
        final int exitValue = executor.execute(cmdLine);
        System.out.println();
        return exitValue;
    } catch (final ExecuteException e) {
        e.printStackTrace(System.out);
        return e.getExitValue();
    }
}
项目:commons-dost    文件:SVNCommands.java   
/**
   * Make a branch by calling the "svn cp" operation.
   * 
   * @param base The source of the SVN copy operation
   * @param branch The name and location of the new branch
   * @param revision The revision to base the branch off
   * @param baseUrl       The SVN url to connect to
   * @throws IOException Execution of the SVN sub-process failed or the
   *          sub-process returned a exit value indicating a failure
   */
  public static void copyBranch(String base, String branch, long revision, String baseUrl) throws IOException {
      log.info("Copying branch " + base + AT_REVISION + revision + " to branch " + branch);

      CommandLine cmdLine = new CommandLine(SVN_CMD);
      cmdLine.addArgument("cp");
      addDefaultArguments(cmdLine, null, null);
      if (revision > 0) {
          cmdLine.addArgument("-r" + Long.toString(revision));
      }
      cmdLine.addArgument("-m");
      cmdLine.addArgument("Branch automatically created from " + base + (revision > 0 ? AT_REVISION + revision : ""));
      cmdLine.addArgument(baseUrl + base);
      cmdLine.addArgument(baseUrl + branch);

/*
svn copy -r123 http://svn.example.com/repos/calc/trunk \
        http://svn.example.com/repos/calc/branches/my-calc-branch
         */

      try (InputStream result = ExecutionHelper.getCommandResult(cmdLine, new File("."), 0, 120000)) {
          log.info("Svn-Copy reported:\n" + extractResult(result));
      }
  }
项目:zeppelin    文件:ShellInterpreter.java   
public void createSecureConfiguration() throws InterpreterException {
  Properties properties = getProperties();
  CommandLine cmdLine = CommandLine.parse(shell);
  cmdLine.addArgument("-c", false);
  String kinitCommand = String.format("kinit -k -t %s %s",
      properties.getProperty("zeppelin.shell.keytab.location"),
      properties.getProperty("zeppelin.shell.principal"));
  cmdLine.addArgument(kinitCommand, false);
  DefaultExecutor executor = new DefaultExecutor();
  try {
    executor.execute(cmdLine);
  } catch (Exception e) {
    LOGGER.error("Unable to run kinit for zeppelin user " + kinitCommand, e);
    throw new InterpreterException(e);
  }
}
项目:event-store-maven-plugin    文件:EventStoreDownloadMojo.java   
private static void applyFileMode(final File file, final FileMode fileMode)
        throws MojoExecutionException {

    if (OS.isFamilyUnix() || OS.isFamilyMac()) {
        final String smode = fileMode.toChmodStringFull();
        final CommandLine cmdLine = new CommandLine("chmod");
        cmdLine.addArgument(smode);
        cmdLine.addArgument(file.getAbsolutePath());
        final Executor executor = new DefaultExecutor();
        try {
            final int result = executor.execute(cmdLine);
            if (result != 0) {
                throw new MojoExecutionException("Error # " + result + " while trying to set mode \""
                        + smode + "\" for file: " + file.getAbsolutePath());
            }
        } catch (final IOException ex) {
            throw new MojoExecutionException("Error while trying to set mode \"" + smode + "\" for file: "
                    + file.getAbsolutePath(), ex);
        }
    } else {
        file.setReadable(fileMode.isUr() || fileMode.isGr() || fileMode.isOr());
        file.setWritable(fileMode.isUw() || fileMode.isGw() || fileMode.isOw());
        file.setExecutable(fileMode.isUx() || fileMode.isGx() || fileMode.isOx());
    }
}
项目:greenpepper    文件:ShadingTest.java   
@Test
public void testCommonsCodecsConflicts() throws IOException, VerificationException {
    SetupContent setupContent = new SetupContent().invoke();
    String shadedJar = setupContent.getShadedJar();
    String issue24POM = setupContent.getIssue24POM();
    String issue24TestFile = setupContent.getIssue24TestFile();
    File cwdFile = setupContent.getCwdFile();


    String line = String.format("java -jar %s -v -p %s %s %s", shadedJar, issue24POM, issue24TestFile,
            StringUtils.join(new String[] {cwdFile.getAbsolutePath(), "testCommonsCodecsConflicts.html"}, File.separator));
    CommandLine command = CommandLine.parse(line);
    DefaultExecutor executor = new DefaultExecutor();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream err = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(out, err));
    executor.execute(command);
}
项目:greenpepper    文件:ShadingTest.java   
@Test
public void shouldNotOverwriteInputFile() throws IOException, VerificationException {
    SetupContent setupContent = new SetupContent().invoke();
    String shadedJar = setupContent.getShadedJar();
    String issue24POM = setupContent.getIssue24POM();
    String issue24TestFile = setupContent.getIssue24TestFile();
    File specFile = new File(issue24TestFile);
    File specBackupFile = new File(issue24TestFile + ".orig");
    FileUtils.copyFile(specFile, specBackupFile);

    String line = String.format("java -jar %s -v -p %s %s", shadedJar, issue24POM, issue24TestFile);
    CommandLine command = CommandLine.parse(line);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setWorkingDirectory(specFile.getParentFile());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream err = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(out, err));
    executor.execute(command);

    assertTrue("The specification should not be overriden by the output", FileUtils.contentEquals(specFile,specBackupFile));
}