package com.github.aureliano.achmed.resources;

import org.apache.log4j.Logger;

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

public class ExecResource implements IResource {

	private static final Logger logger = Logger.getLogger(ExecResource.class);
	
	private ExecProperties properties;
	
	public ExecResource() {
		this.properties = new ExecProperties();
	}
	
	public ExecResource(ExecProperties properties) {
		this.properties = properties;
	}

	public void apply() {
		logger.info(" >>> Apply exec resource with command: " + this.properties.getCommand());
		
		this.properties.configureAttributes();
		int exitStatusCode = 0;
		int tries = 0;
		
		do {
			exitStatusCode = CommandFacade.executeCommand(this.properties);
			tries ++;
			
			logger.debug("Exit status code: " + exitStatusCode);
			logger.debug("Tries: " + tries);
		} while ((tries < this.properties.getTries()) && (exitStatusCode != 0));
		
		if (exitStatusCode != 0) {
			throw new ExecResourceException(this.executionErrorMessage(exitStatusCode));
		}
	}
	
	public void setProperties(IResourceProperties properties) {
		this.properties = (ExecProperties) properties;
	}
	
	public IResourceProperties getProperties() {
		return this.properties;
	}

	public ResourceType type() {
		return ResourceType.EXEC;
	}
	
	private String executionErrorMessage(int exitStatusCode) {
		return String.format(
			"Command [%s] exited with status code [%d]",
			this.properties.getCommand(),
			exitStatusCode
		);
	}
}
@@ -914,52 +914,21 @@
 e = 
-CommandFacade.executeCommand(this.properties
+this.execute(
 );%0A%09
@@ -1485,16 +1485,169 @@
 C;%0A%09%7D%0A%09%0A
+%09private int execute() %7B%0A%09%09try %7B%0A%09%09%09return CommandFacade.executeCommand(this.properties);%0A%09%09%7D catch (ExecResourceException ex) %7B%0A%09%09%09return 100;%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09private

Apply exec resource implementation.