package com.github.aureliano.achmed.os.service;

import org.apache.log4j.Logger;

import com.github.aureliano.achmed.command.CommandFacade;
import com.github.aureliano.achmed.command.CommandResponse;
import com.github.aureliano.achmed.exception.ServiceResourceException;
import com.github.aureliano.achmed.helper.StringHelper;
import com.github.aureliano.achmed.os.Linux;
import com.github.aureliano.achmed.os.pkg.IPackageManager;
import com.github.aureliano.achmed.types.OperatingSystemFamily;

public abstract class LinuxService extends BaseService {
	
	private static final Logger logger = Logger.getLogger(LinuxService.class);

	private Linux linux;
	
	public LinuxService() {
		this.linux = this.createLinux();
	}

	public CommandResponse start() {
		if (this.isRunning()) {
			logger.debug("Service " + super.properties.getName() + " is already running.");
			return null;
		}
		
		if (StringHelper.isEmpty(super.properties.getBinary())) {
			throw new ServiceResourceException(
				"Binary file of service " + super.properties.getName() + " not provided.");
		} else if (StringHelper.isEmpty(super.properties.getStart())) {
			throw new ServiceResourceException(
					"Start command of service " + super.properties.getName() +
					" was not provided to be used with binary command " + super.properties.getBinary());
		}
		
		return CommandFacade.executeCommand(
			super.properties.getBinary(),
			super.properties.getStart()
		);
	}

	public CommandResponse stop() {
		if (!this.isRunning()) {
			logger.debug("Service " + super.properties.getName() + " is not running.");
			return null;
		}
		
		if (StringHelper.isEmpty(super.properties.getBinary())) {
			throw new ServiceResourceException(
				"Binary file of service " + super.properties.getName() + " not provided.");
		}
		
		if (StringHelper.isEmpty(super.properties.getStop())) {
			Integer pid = this.linux.getPid(super.properties.getPattern());
			CommandResponse res = this.linux.kill(pid);
			if (res.isOK()) {
				return res;
			}
			
			throw new ServiceResourceException(res);
		}
		
		return CommandFacade.executeCommand(
			super.properties.getBinary(),
			super.properties.getStop()
		);
	}

	public CommandResponse restart() {
		CommandResponse res = this.stop();
		if ((res != null) && (!res.isOK())) {
			throw new ServiceResourceException(
				"Could not stop service " + super.properties.getName() + ". Detail: " + res.getError());
		}
		
		return this.start();
	}

	public boolean isRunning() {
		Integer pid = this.linux.getPid(super.properties.getPattern());
		return (pid != null);
	}

	private Linux createLinux() {
		return new Linux() {
			
			public OperatingSystemFamily getOperatingSystemFamily() {
				return null;
			}
			
			public IPackageManager getDefaultPackageManager() {
				return null;
			}
		};
	}
}
@@ -42,16 +42,93 @@
 rvice;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A%0A
 import o
@@ -151,16 +151,16 @@
 Logger;%0A
-
 %0Aimport 
@@ -497,24 +497,99 @@
 ageManager;%0A
+import com.github.aureliano.achmed.resources.properties.ServiceProperties;%0A
 import com.g
@@ -1481,37 +1481,28 @@
 %09%09%0A%09%09return 
-CommandFacade
+this
 .executeComm
@@ -1509,45 +1509,8 @@
 and(
-%0A%09%09%09super.properties.getBinary(),%0A%09%09%09
 supe
@@ -1532,19 +1532,16 @@
 tStart()
-%0A%09%09
 );%0A%09%7D%0A%0A%09
@@ -2165,29 +2165,20 @@
 %09return 
-CommandFacade
+this
 .execute
@@ -2189,45 +2189,8 @@
 and(
-%0A%09%09%09super.properties.getBinary(),%0A%09%09%09
 supe
@@ -2211,19 +2211,16 @@
 etStop()
-%0A%09%09
 );%0A%09%7D%0A%0A%09
@@ -2847,13 +2847,13 @@
 %09%09%7D%0A
-
 %09%09%7D;%0A%09%7D%0A
 %7D
@@ -2851,9 +2851,536 @@
 %09%09%7D;%0A%09%7D%0A
+%09%0A%09private String%5B%5D buildCommand(String binary, String flags, String action) %7B%0A%09%09List%3CString%3E command = new ArrayList%3C%3E();%0A%09%09%0A%09%09command.add(binary);%0A%09%09if (!StringHelper.isEmpty(flags)) %7B%0A%09%09%09command.addAll(Arrays.asList(flags.split(%22%5C%5Cs+%22)));%0A%09%09%7D%0A%09%09command.add(action);%0A%09%09%0A%09%09return command.toArray(new String%5B0%5D);%0A%09%7D%0A%09%0A%09private CommandResponse executeCommand(String action) %7B%0A%09%09return CommandFacade.executeCommand(%0A%09%09%09this.buildCommand(%0A%09%09%09%09super.properties.getBinary(),%0A%09%09%09%09super.properties.getFlags(),%0A%09%09%09%09action%0A%09%09%09)%0A%09%09);%0A%09%7D%0A
 %7D

Add support to execute commands with flags.