Java Code Examples for org.zeroturnaround.exec.ProcessExecutor#command()

The following examples show how to use org.zeroturnaround.exec.ProcessExecutor#command() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProcessExecutorMainTest.java    From zt-exec with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessExecutorCommand() throws Exception {
  // Use timeout in case we get stuck
  List<String> args = new ArrayList<String>() {
    {
      add("java");
      add("-cp");
      add("target/test-classes");
      add(HelloWorld.class.getName());
    }
  };
  ProcessExecutor exec = new ProcessExecutor();
  exec.command(args);
  ProcessResult result = exec.readOutput(true).execute();
  Assert.assertEquals("Hello world!", result.outputUTF8());
}
 
Example 2
Source File: ProcessExecutorMainTest.java    From zt-exec with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessExecutorSetDirectory() throws Exception {
  // Use timeout in case we get stuck
  List<String> args = new ArrayList<String>() {
    {
      add("java");
      add("-cp");
      add("test-classes");
      add(HelloWorld.class.getName());
    }
  };
  ProcessExecutor exec = new ProcessExecutor().directory(new File("target"));
  exec.command(args);
  ProcessResult result = exec.readOutput(true).execute();
  Assert.assertEquals("Hello world!", result.outputUTF8());
}
 
Example 3
Source File: MySQLBackupProcessor.java    From gocd with Apache License 2.0 5 votes vote down vote up
private ProcessExecutor createProcessExecutor(File targetDir, DbProperties dbProperties) {
    ConnectionUrl connectionUrlInstance = ConnectionUrl.getConnectionUrlInstance(dbProperties.url(), dbProperties.connectionProperties());

    LinkedHashMap<String, String> env = new LinkedHashMap<>();
    if (isNotBlank(dbProperties.password())) {
        env.put("MYSQL_PWD", dbProperties.password());
    }
    // override with any user specified environment
    env.putAll(dbProperties.extraBackupEnv());

    ArrayList<String> argv = new ArrayList<>();
    argv.add("mysqldump");


    String dbName = connectionUrlInstance.getDatabase();
    HostInfo mainHost = connectionUrlInstance.getMainHost();

    if (mainHost != null) {
        argv.add("--host=" + mainHost.getHost());
        argv.add("--port=" + mainHost.getPort());
    }
    if (isNotBlank(dbProperties.user())) {
        argv.add("--user=" + dbProperties.user());
    }

    // append any user specified args for mysqldump
    if (isNotBlank(dbProperties.extraBackupCommandArgs())) {
        Collections.addAll(argv, Commandline.translateCommandline(dbProperties.extraBackupCommandArgs()));
    }

    argv.add("--result-file=" + new File(targetDir, "db." + dbName).toString());
    argv.add(connectionUrlInstance.getDatabase());

    ProcessExecutor processExecutor = new ProcessExecutor();
    processExecutor.redirectOutputAlsoTo(Slf4jStream.of(getClass()).asDebug());
    processExecutor.redirectErrorAlsoTo(Slf4jStream.of(getClass()).asDebug());
    processExecutor.environment(env);
    processExecutor.command(argv);
    return processExecutor;
}
 
Example 4
Source File: PostgresqlBackupProcessor.java    From gocd with Apache License 2.0 5 votes vote down vote up
ProcessExecutor createProcessExecutor(File targetDir, DbProperties dbProperties) {
    Properties connectionProperties = dbProperties.connectionProperties();
    Properties pgProperties = Driver.parseURL(dbProperties.url(), connectionProperties);

    ArrayList<String> argv = new ArrayList<>();
    LinkedHashMap<String, String> env = new LinkedHashMap<>();
    if (isNotBlank(dbProperties.password())) {
        env.put("PGPASSWORD", dbProperties.password());
    }

    // override with any user specified environment
    env.putAll(dbProperties.extraBackupEnv());

    String dbName = pgProperties.getProperty("PGDBNAME");
    argv.add("pg_dump");
    argv.add("--no-password");
    argv.add("--host=" + pgProperties.getProperty("PGHOST"));
    argv.add("--port=" + pgProperties.getProperty("PGPORT"));
    if (isNotBlank(dbProperties.user())) {
        argv.add("--username=" + dbProperties.user());
    }
    argv.add(pgProperties.getProperty("PGDBNAME"));
    // append any user specified args for pg_dump
    if (isNotBlank(dbProperties.extraBackupCommandArgs())) {
        Collections.addAll(argv, Commandline.translateCommandline(dbProperties.extraBackupCommandArgs()));
    }
    argv.add("--file=" + new File(targetDir, "db." + dbName).toString());
    ProcessExecutor processExecutor = new ProcessExecutor();
    processExecutor.redirectOutputAlsoTo(Slf4jStream.of(getClass()).asDebug());
    processExecutor.redirectErrorAlsoTo(Slf4jStream.of(getClass()).asDebug());
    processExecutor.environment(env);
    processExecutor.command(argv);
    return processExecutor;
}