package com.github.aureliano.achmed.command;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.apache.log4j.Logger;

import com.github.aureliano.achmed.exception.ExecResourceException;
import com.github.aureliano.achmed.resources.properties.ExecProperties;

public final class CommandFacade {

	private static final Logger logger = Logger.getLogger(CommandFacade.class);
	private static final long ONE_SECOND = 1000;
	public static final long DEFAULT_TIMEOUT_EXECUTION = 300 * ONE_SECOND;
	
	private CommandFacade() {
		throw new InstantiationError(this.getClass().getName() + " cannot be instantiated.");
	}
	
	public static int executeCommand(CommandBuilder command) {
		int exitStatusCode = 0;
		int tries = 0;
		
		do {
			exitStatusCode = _executeCommand(command);
			tries ++;
			
			logger.debug("Exit status code: " + exitStatusCode);
			logger.debug("Tries: " + tries);
		} while ((tries < command.getTries()) && (exitStatusCode != 0));
		
		return exitStatusCode;
	}
	
	public static int executeCommand(ExecProperties properties) {
		return executeCommand(new CommandBuilder(properties));
	}
	
	private static int _executeCommand(CommandBuilder command) {
		boolean verbose = command.getVerbose() != null ? command.getVerbose() : true;
		CommandRunner runner = new CommandRunner(command.getCommand(), command.getWorkingDir(), verbose);
		
		ExecutorService executor = Executors.newSingleThreadExecutor();
		Future<Integer> future = executor.submit(runner);
		
		try {
			return future.get(command.getTimeout(), TimeUnit.MILLISECONDS);
		} catch (TimeoutException ex) {
			logger.warn("Execution timeout to command: " + command.getCommand());
			throw new ExecResourceException(ex);
		} catch (ExecutionException|InterruptedException ex) {
			throw new ExecResourceException(ex);
		}
	}
}
@@ -39,16 +39,37 @@
 mmand;%0A%0A
+import java.io.File;%0A
 import j
@@ -836,24 +836,317 @@
 ed.%22);%0A%09%7D%0A%09%0A
+%09public static int executeCommand(String cmd) %7B%0A%09%09CommandBuilder command = new CommandBuilder()%0A%09%09%09.withCommand(cmd)%0A%09%09%09.withTimeout(DEFAULT_TIMEOUT_EXECUTION)%0A%09%09%09.withTries(1)%0A%09%09%09.withVerbose(true)%0A%09%09%09.withWorkingDir(new File(%22%22).getAbsolutePath());%0A%09%09%0A%09%09return executeCommand(command);%0A%09%7D%0A%09%0A
 %09public stat

Add method to execute single string command.