Java Code Examples for org.apache.commons.exec.Executor#execute()

The following examples show how to use org.apache.commons.exec.Executor#execute() . 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: __platform-name__Loader.java    From ldbc_graphalytics with Apache License 2.0 6 votes vote down vote up
public int unload(String loadedInputPath) throws Exception {
	String unloaderDir = platformConfig.getUnloaderPath();
	commandLine = new CommandLine(Paths.get(unloaderDir).toFile());

	commandLine.addArgument("--graph-name");
	commandLine.addArgument(formattedGraph.getName());

	commandLine.addArgument("--output-path");
	commandLine.addArgument(loadedInputPath);

	String commandString = StringUtils.toString(commandLine.toStrings(), " ");
	LOG.info(String.format("Execute graph unloader with command-line: [%s]", commandString));

	Executor executor = new DefaultExecutor();
	executor.setStreamHandler(new PumpStreamHandler(System.out, System.err));
	executor.setExitValue(0);

	return executor.execute(commandLine);
}
 
Example 2
Source File: MySqlProcess.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
public static void runShell(final String shellCommand) throws ExecuteException, IOException {
	final Executor executor = new DefaultExecutor();
	final CommandLine commandLine = new CommandLine("/bin/bash");
	commandLine.addArgument("-c");
	commandLine.addArgument(shellCommand, false);
	executor.execute(commandLine);
}
 
Example 3
Source File: __platform-name__Loader.java    From ldbc_graphalytics with Apache License 2.0 5 votes vote down vote up
public int load(String loadedInputPath) throws Exception {
	String loaderDir = platformConfig.getLoaderPath();
	commandLine = new CommandLine(Paths.get(loaderDir).toFile());

	commandLine.addArgument("--graph-name");
	commandLine.addArgument(formattedGraph.getName());
	commandLine.addArgument("--input-vertex-path");
	commandLine.addArgument(formattedGraph.getVertexFilePath());
	commandLine.addArgument("--input-edge-path");
	commandLine.addArgument(formattedGraph.getEdgeFilePath());
	commandLine.addArgument("--output-path");
	commandLine.addArgument(loadedInputPath);
	commandLine.addArgument("--directed");
	commandLine.addArgument(formattedGraph.isDirected() ? "true" : "false");
	commandLine.addArgument("--weighted");
	commandLine.addArgument(formattedGraph.hasEdgeProperties() ? "true" : "false");


	String commandString = StringUtils.toString(commandLine.toStrings(), " ");
	LOG.info(String.format("Execute graph loader with command-line: [%s]", commandString));

	Executor executor = new DefaultExecutor();
	executor.setStreamHandler(new PumpStreamHandler(System.out, System.err));
	executor.setExitValue(0);

	return executor.execute(commandLine);
}
 
Example 4
Source File: __platform-name__Job.java    From ldbc_graphalytics with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the platform job with the pre-defined parameters.
 *
 * @return the exit code
 * @throws IOException if the platform failed to run
 */
public int execute() throws Exception {
	String executableDir = platformConfig.getExecutablePath();
	commandLine = new CommandLine(Paths.get(executableDir).toFile());

	// List of benchmark parameters.
	String jobId = getJobId();
	String logDir = getLogPath();

	// List of dataset parameters.
	String inputPath = getInputPath();
	String outputPath = getOutputPath();

	// List of platform parameters.
	int numMachines = platformConfig.getNumMachines();
	int numThreads = platformConfig.getNumThreads();
	String homeDir = platformConfig.getHomePath();

	appendBenchmarkParameters(jobId, logDir);
	appendAlgorithmParameters();
	appendDatasetParameters(inputPath, outputPath);
	appendPlatformConfigurations(homeDir, numMachines, numThreads);

	String commandString = StringUtils.toString(commandLine.toStrings(), " ");
	LOG.info(String.format("Execute benchmark job with command-line: [%s]", commandString));

	Executor executor = new DefaultExecutor();
	executor.setStreamHandler(new PumpStreamHandler(System.out, System.err));
	executor.setExitValue(0);
	return executor.execute(commandLine);
}
 
Example 5
Source File: DynamoDBLocal.java    From geowave with Apache License 2.0 5 votes vote down vote up
/**
 * Using apache commons exec for cmd line execution
 *
 * @param command
 * @return exitCode
 * @throws ExecuteException
 * @throws IOException
 * @throws InterruptedException
 */
private void startDynamoLocal() throws ExecuteException, IOException, InterruptedException {
  // java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar
  // -sharedDb
  final CommandLine cmdLine = new CommandLine("java");

  cmdLine.addArgument("-Djava.library.path=" + dynLocalDir + "/DynamoDBLocal_lib");
  cmdLine.addArgument("-jar");
  cmdLine.addArgument(dynLocalDir + "/DynamoDBLocal.jar");
  cmdLine.addArgument("-sharedDb");
  cmdLine.addArgument("-inMemory");
  cmdLine.addArgument("-port");
  cmdLine.addArgument(Integer.toString(port));
  System.setProperty("aws.accessKeyId", "dummy");
  System.setProperty("aws.secretKey", "dummy");

  // Using a result handler makes the emulator run async
  final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

  // watchdog shuts down the emulator, later
  watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
  final Executor executor = new DefaultExecutor();
  executor.setWatchdog(watchdog);
  executor.execute(cmdLine, resultHandler);

  // we need to wait here for a bit, in case the emulator needs to update
  // itself
  Thread.sleep(EMULATOR_SPINUP_DELAY_MS);
}
 
Example 6
Source File: UNIXUtils.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * Change the ownership of a directory (recursively).
 *
 * @param dir      The directory to change the ownership of.
 * @param user     Userid of the user.
 * @param executor the command executor
 * @throws IOException if the operation fails
 */
public static void changeOwnershipOfDirectory(
    final String dir,
    final String user,
    final Executor executor
) throws IOException {

    final CommandLine commandLine = new CommandLine(SUDO)
        .addArgument("chown")
        .addArgument("-R")
        .addArgument(user)
        .addArgument(dir);
    executor.execute(commandLine);
}
 
Example 7
Source File: BenchmarkRunner.java    From trainbenchmark with Eclipse Public License 1.0 4 votes vote down vote up
public static int runPerformanceBenchmark(final BenchmarkConfig bc, final ExecutionConfig ec)
		throws IOException, InterruptedException {
	final Joiner joiner = Joiner.on(", ");
	System.out.println("Running benchmark.");
	System.out.println("Workload: " + bc.getConfigBase().getWorkload());
	System.out.println("Tool: " + bc.getToolName());
	System.out.println("Model: " + bc.getConfigBase().getModelPath());
	System.out.println("Description: " + bc.getDescription());
	System.out.println("Operations: [" + joiner.join(bc.getConfigBase().getOperations()) + "]");
	System.out.println("Execution configuration: " + ec);
	System.out.println("Runs: " + bc.getConfigBase().getRuns());

	final File configFile = File.createTempFile("trainbenchmark-benchmark-", ".conf");
	final String configPath = configFile.getAbsolutePath();
	bc.saveToFile(configPath);

	final String projectName = String.format("trainbenchmark-tool-%s", bc.getProjectName());
	final String jarPath = String.format("../%s/build/libs/%s-1.0.0-SNAPSHOT-fat.jar %s", projectName, projectName,
			configPath);

	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 long timeoutInSeconds = bc.getConfigBase().getTimeout();
	final long timeoutInMilliseconds = timeoutInSeconds * 1000;
	final ExecuteWatchdog watchdog = new ExecuteWatchdog(timeoutInMilliseconds);
	final Executor executor = new DefaultExecutor();
	executor.setWatchdog(watchdog);
	executor.setStreamHandler(new PumpStreamHandler());
	try {
		final int exitValue = executor.execute(cmdLine);
		System.out.println();
		return exitValue;
	} catch (final ExecuteException e) {
		if (watchdog.killedProcess()) {
			System.out.println("Process timed out.");
		} else {
			e.printStackTrace(System.out);
		}
		return e.getExitValue();
	}
}
 
Example 8
Source File: MySqlProcess.java    From trainbenchmark with Eclipse Public License 1.0 4 votes vote down vote up
public static void runScript(final String scriptFile) throws ExecuteException, IOException {
	final Executor executor = new DefaultExecutor();
	final CommandLine commandLine = new CommandLine("/bin/bash");
	commandLine.addArgument(scriptFile, false);
	executor.execute(commandLine);
}
 
Example 9
Source File: UNIXUtils.java    From genie with Apache License 2.0 4 votes vote down vote up
/**
 * Create user on the system.
 *
 * @param user     user id
 * @param group    group id
 * @param executor the command executor
 * @throws IOException If the user or group could not be created.
 */
public static synchronized void createUser(
    final String user,
    @Nullable final String group,
    final Executor executor
) throws IOException {

    // First check if user already exists
    final CommandLine idCheckCommandLine = new CommandLine("id")
        .addArgument("-u")
        .addArgument(user);

    try {
        executor.execute(idCheckCommandLine);
        log.debug("User already exists");
    } catch (final IOException ioe) {
        log.debug("User does not exist. Creating it now.");

        // Determine if the group is valid by checking that its not null and not same as user.
        final boolean isGroupValid = StringUtils.isNotBlank(group) && !group.equals(user);

        // Create the group for the user if its not the same as the user.
        if (isGroupValid) {
            log.debug("Group and User are different so creating group now.");
            final CommandLine groupCreateCommandLine = new CommandLine(SUDO).addArgument("groupadd")
                .addArgument(group);

            // We create the group and ignore the error as it will fail if group already exists.
            // If the failure is due to some other reason, then user creation will fail and we catch that.
            try {
                log.debug("Running command to create group:  [{}]", groupCreateCommandLine);
                executor.execute(groupCreateCommandLine);
            } catch (IOException ioexception) {
                log.debug("Group creation threw an error as it might already exist", ioexception);
            }
        }

        final CommandLine userCreateCommandLine = new CommandLine(SUDO)
            .addArgument("useradd")
            .addArgument(user);
        if (isGroupValid) {
            userCreateCommandLine
                .addArgument("-G")
                .addArgument(group);
        }
        userCreateCommandLine
            .addArgument("-M");

        log.debug("Running command to create user: [{}]", userCreateCommandLine);
        executor.execute(userCreateCommandLine);
    }
}
 
Example 10
Source File: CommandExecutor.java    From ankush with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Exec.
 *
 * @param command the command
 * @param out the out
 * @param err the err
 * @return the int
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws InterruptedException the interrupted exception
 */
public static int exec(String command, OutputStream out, OutputStream err)
		throws IOException, InterruptedException {
	CommandLine cmdLine = CommandLine.parse(command);
	Executor executor = new DefaultExecutor();
	executor.setStreamHandler(new PumpStreamHandler(out, err, null));
	return executor.execute(cmdLine);
}
 
Example 11
Source File: UNIXUtils.java    From genie with Apache License 2.0 3 votes vote down vote up
/**
 * Give write permission to the group owning a given file or directory.
 *
 * @param path     the path
 * @param executor the command executor
 * @throws IOException if the operation fails
 */
public static void makeDirGroupWritable(final String path, final Executor executor) throws IOException {
    log.debug("Adding write permissions for the directory {} for the group.", path);
    final CommandLine commandLIne = new CommandLine(SUDO).addArgument("chmod").addArgument("g+w")
        .addArgument(path);
    executor.execute(commandLIne);
}