package org.jboss.da.scm.impl;

import org.apache.maven.scm.ScmException;
import org.jboss.da.scm.api.SCM;
import org.jboss.da.scm.api.SCMType;

import javax.inject.Inject;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Honza Brázdil <jbrazdil@redhat.com>
 */
public class SCMImpl implements SCM {

    @Inject
    Logger log;

    @Inject
    ScmFacade scm;

    @Inject
    SCMCache cache;

    @Override
    public File cloneRepository(SCMType scmType, String scmUrl, String revision)
            throws ScmException {
        SCMSpecifier spec = new SCMSpecifier(scmType, scmUrl, revision);

        FutureReference fref = cache.get(spec);

        if (fref.shouldIComplete()) {
            try {
                File tempDir = Files.createTempDirectory("cloned_repo").toFile();
                log.debug("Cached repository for {} not found. Cloning to {}", spec, tempDir);
                try {
                    scm.shallowCloneRepository(scmType, scmUrl, revision, tempDir);
                    DirectoryReference ref = new DirectoryReference(tempDir);
                    fref.complete(ref);
                    return tempDir;
                } catch (ScmException ex) {
                    fref.completeExceptionally(ex);
                    try {
                        FileUtils.deleteDirectory(tempDir);
                    } catch (IOException ioex) {
                        
                        log.warn("Temporary directory could not be deleted", ioex);
                    }
                    throw ex;
                }
            } catch (IOException ex) {
                throw new ScmException(
                        "Could not create temp directory for cloning the repository", ex);
            }

        } else {
            try {
                return fref.get(30, TimeUnit.MINUTES).get().orElseThrow(()->new IllegalStateException("Now completed reference has empty file."));
            } catch (InterruptedException | ExecutionException | TimeoutException ex) {
                throw new ScmException("Could not obtain cloned repository.", ex);
            }
        }
    }
}

@@ -1047,13 +1047,12 @@
 log.
-debug
+info
 (%22Ca
@@ -1098,16 +1098,17 @@
 ng to %7B%7D
+.
 %22, spec,
@@ -2024,22 +2024,26 @@
         
-return
+File dir =
  fref.ge
@@ -2159,16 +2159,122 @@
 le.%22));%0A
+                log.info(%22Cached repository for %7B%7D found in %7B%7D.%22, spec, dir);%0A                return dir;%0A
         

Improve loging