package nl.jpoint.maven.vertx.utils;

import nl.jpoint.maven.vertx.mojo.DeployConfiguration;
import nl.jpoint.maven.vertx.request.DeployRequest;
import nl.jpoint.maven.vertx.request.Request;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class RequestExecutor {

    private final Log log;

    public RequestExecutor(Log log) {

        this.log = log;
    }

    private void executeRequest(HttpPost postRequest) throws MojoExecutionException {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            try (CloseableHttpResponse response = httpClient.execute(postRequest)) {
                log.info("DeployModuleCommand : Post response status code -> " + response.getStatusLine().getStatusCode());

                if (response.getStatusLine().getStatusCode() != 200) {
                    log.error("DeployModuleCommand : Post response status -> " + response.getStatusLine().getReasonPhrase());
                    throw new MojoExecutionException("Error deploying module. ");
                }
            } catch (IOException e) {
                log.error("testDeployModuleCommand ", e);
                throw new MojoExecutionException("Error deploying module.", e);
            }

        } catch (IOException e) {
            log.error("testDeployModuleCommand ", e);
            throw new MojoExecutionException("Error deploying module.", e);
        }
    }

    public void executeSingleDeployRequest(DeployConfiguration activeConfiguration, Request request) throws MojoExecutionException {
        for (String host : activeConfiguration.getHosts()) {
            HttpPost post = new HttpPost(host + request.getEndpoint());

            ByteArrayInputStream bos = new ByteArrayInputStream(request.toJson().getBytes());
            BasicHttpEntity entity = new BasicHttpEntity();
            entity.setContent(bos);
            entity.setContentLength(request.toJson().getBytes().length);
            post.setEntity(entity);

            this.executeRequest(post);
        }
    }

    public void executeDeployRequests(DeployConfiguration activeConfiguration, DeployRequest deployRequest) throws MojoExecutionException {

        for (String host : activeConfiguration.getHosts()) {

                HttpPost post = new HttpPost(host + deployRequest.getEndpoint());

                ByteArrayInputStream bos = new ByteArrayInputStream(deployRequest.toJson().getBytes());
                BasicHttpEntity entity = new BasicHttpEntity();
                entity.setContent(bos);
                entity.setContentLength(deployRequest.toJson().getBytes().length);
                post.setEntity(entity);
                log.info(deployRequest.toJson());
                this.executeRequest(post);

        }
    }
}

@@ -608,16 +608,147 @@
 ception;
+%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.ScheduledExecutorService;%0Aimport java.util.concurrent.TimeUnit;
 %0A%0Apublic
@@ -949,32 +949,343 @@
 tionException %7B%0A
+%0A        ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();%0A        exec.scheduleAtFixedRate(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                log.info(%22Waiting for deploy request to return...%22);%0A            %7D%0A        %7D, 5, 5, TimeUnit.SECONDS);%0A%0A
         try (Clo
@@ -1426,24 +1426,57 @@
 Request)) %7B%0A
+                exec.shutdown();%0A
             
@@ -2156,32 +2156,32 @@
 eCommand %22, e);%0A
-
             thro
@@ -2232,32 +2232,196 @@
 g module.%22, e);%0A
+        %7D finally %7B%0A            if (!exec.isShutdown()) %7B%0A                log.info(%22Shutdown executor after error%22);%0A                exec.shutdown();%0A            %7D%0A
         %7D%0A    %7D%0A

Add logging during deploy