package apollo.upload;

import apollo.maven.MavenDeployer;
import apollo.predicators.PomFilePredictor;
import apollo.xml_handlers.XmlReformer;
import com.google.inject.Inject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

public class MavenUploader implements Uploader {

    private final PomFilePredictor pomFilePredictor;
    private final MavenDeployer mavenDeployer;
    private final XmlReformer xmlReformer;
    private static final Logger logger = LogManager.getLogger(MavenUploader.class);
    private static final int maxThreadNo = 8;
    private static ExecutorService executor = Executors.newFixedThreadPool(maxThreadNo);

    @Inject
    public MavenUploader(PomFilePredictor pomFilePredictor, MavenDeployer mavenDeployer, XmlReformer xmlReformer) {
        this.pomFilePredictor = pomFilePredictor;
        this.mavenDeployer = mavenDeployer;
        this.xmlReformer = xmlReformer;
    }

    @Override
    public void uploadToRepository(Path pathToUpload) {

        logger.info("Starting to upload artifacts from " + pathToUpload.toString());
        try (Stream<Path> files = Files.walk(pathToUpload)){
            files.filter(pomFilePredictor).peek(xmlReformer::prepareXmlToDeploy).forEach(path -> {
                Runnable uploadThread = new MavenUploadThread(mavenDeployer, path);
                executor.submit(uploadThread);
            });
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }

        executor.shutdown();
        try {
            if (executor.awaitTermination(5, TimeUnit.DAYS)) {
                logger.info("Executor service has shut down");
            }
        } catch (InterruptedException e) {
            logger.info("Executor service interrupted");
        }
    }
}

@@ -1475,13 +1475,67 @@
 or).
-peek(
+filter(path -%3E %7B%0A                try %7B%0A                    
 xmlR
@@ -1545,10 +1545,9 @@
 rmer
-::
+.
 prep
@@ -1560,16 +1560,235 @@
 ToDeploy
+(path);%0A                    return true;%0A                %7D catch (Exception e) %7B%0A                    logger.error(%22Error uploading %22 + path + %22 : %22 + e);%0A                    return false;%0A                %7D%0A            %7D
 ).forEac

fix bug that would stop processing and skip all next items in the list