package com.github.aureliano.achmed.resources;

import org.apache.log4j.Logger;

import com.github.aureliano.achmed.AppConfiguration;
import com.github.aureliano.achmed.os.service.IService;
import com.github.aureliano.achmed.resources.properties.IResourceProperties;
import com.github.aureliano.achmed.resources.properties.ServiceProperties;

public class ServiceResource implements IResource {

	private static final Logger logger = Logger.getLogger(ServiceResource.class);
	
	private ServiceProperties properties;
	
	public ServiceResource() {
		super();
	}
	
	public ServiceResource(ServiceProperties properties) {
		this.properties = properties;
	}
	
	public void apply() {
		logger.info(" >>> Apply service resource " + this.properties.getName());
		
		this.properties.configureAttributes();
		 IService service = AppConfiguration.instance().getOperatingSystem().getDefaultServiceManager();
		 service.setServiceProperties(this.properties);
		
		 if (this.properties.isEnsure()) {
			if (!service.isRunning()) {
				service.start();
			}
		} else {
			if (service.isRunning()) {
				service.stop();
			}
		}
		
		throw new UnsupportedOperationException("Not implemented yet.");
	}
	
	public void setProperties(IResourceProperties properties) {
		this.properties = (ServiceProperties) properties;
	}
	
	public IResourceProperties getProperties() {
		return this.properties;
	}

	public ResourceType type() {
		return ResourceType.SERVICE;
	}
}
@@ -792,17 +792,16 @@
 es();%0A%09%09
- 
 IService
@@ -890,17 +890,16 @@
 er();%0A%09%09
- 
 service.
@@ -946,237 +946,63 @@
 %09%0A%09%09
- if (this.properties.isEnsure()) %7B%0A%09%09%09if (!service.isRunning()) %7B%0A%09%09%09%09service.start();%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09if (service.isRunning()) %7B%0A%09%09%09%09service.stop();%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09throw new UnsupportedOperationException(%22Not implemented yet.%22
+this.ensure(service);%0A%09%09this.configureBootstrap(service
 );%0A%09
@@ -1263,9 +1263,456 @@
 ICE;%0A%09%7D%0A
+%09%0A%09private void ensure(IService service) %7B%0A%09%09if (this.properties.isEnsure()) %7B%0A%09%09%09if (!service.isRunning()) %7B%0A%09%09%09%09service.start();%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09if (service.isRunning()) %7B%0A%09%09%09%09service.stop();%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09%0A%09private void configureBootstrap(IService service) %7B%0A%09%09if (this.properties.isEnable() == null) %7B%0A%09%09%09return; %0A%09%09 %7D else if (this.properties.isEnable()) %7B%0A%09%09%09 service.enableBootstrap();%0A%09%09 %7D else %7B%0A%09%09%09 service.disableBootstrap();%0A%09%09 %7D%0A%09%7D%0A
 %7D

Implement service resource.