package org.lapanen.stealth.signing;

import org.lapanen.stealth.Stealth;
import org.lapanen.stealth.maven.artifact.Artifact;
import org.lapanen.stealth.maven.artifact.Dependency;
import org.lapanen.stealth.signing.strategy.SigningStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Optional;

/**
 * Signs the artifact and its dependencies if so configured.
 */
public class ArtifactSigningStealth implements Stealth {

    Logger LOG = LoggerFactory.getLogger(ArtifactSigningStealth.class);

    private final SigningStrategy artifactSigningStrategy;
    private final Optional<SigningStrategy> dependencySigningStrategy;

    private final SignedJarRepo signedJarRepo;

    public ArtifactSigningStealth(final SignedJarRepo signedJarRepo, final SigningStrategy artifactSigningStrategy,
            final SigningStrategy dependencySigningStrategy) {
        this.signedJarRepo = signedJarRepo;
        this.artifactSigningStrategy = artifactSigningStrategy;
        this.dependencySigningStrategy = Optional.fromNullable(dependencySigningStrategy);
    }
    public ArtifactSigningStealth(final SignedJarRepo signedJarRepo, final SigningStrategy artifactSigningStrategy) {
        this(signedJarRepo, artifactSigningStrategy, null);
    }

    @Override
    public void handleArtifact(final Artifact artifact) {
        signIfAppropriate(artifact, artifactSigningStrategy);
        if (dependencySigningStrategy.isPresent()) {
            LOG.debug("Checking dependencies for signing");
            for (final Dependency dependency : artifact.getDependencies()) {
                signIfAppropriate(dependency.getDependency(), dependencySigningStrategy.get());
            }
        }
    }

    private void signIfAppropriate(final Artifact artifact, final SigningStrategy strategy) {
        if (strategy.shouldSign(artifact)) {
            LOG.debug("Signing {}", artifact);
            signedJarRepo.fetchAndSign(artifact);
        } else {
            LOG.debug("Not signing {}", artifact);
        }
    }
}

@@ -31,16 +31,55 @@
 gning;%0A%0A
+import java.util.concurrent.Executor;%0A%0A
 import o
@@ -370,16 +370,61 @@
 ptional;
+%0Aimport com.google.common.base.Preconditions;
 %0A%0A/**%0A *
@@ -548,16 +548,38 @@
  %7B%0A%0A    
+private static final  
 Logger L
@@ -639,16 +639,62 @@
 lass);%0A%0A
+    private Executor signingExecutor = null;%0A%0A
     priv
@@ -740,24 +740,25 @@
 ngStrategy;%0A
+%0A
     private 
@@ -1036,32 +1036,226 @@
 ningStrategy) %7B%0A
+        Preconditions.checkNotNull(signedJarRepo, %22Signed jar repo must not be null%22);%0A        Preconditions.checkNotNull(artifactSigningStrategy, %22Artifact signing strategy must not be null%22);%0A
         this.sig
@@ -1439,24 +1439,25 @@
 egy);%0A    %7D%0A
+%0A
     public A
@@ -2235,77 +2235,411 @@
     
-LOG.debug(%22Signing %7B%7D%22, artifact);%0A            signedJarRepo.fetchAnd
+if (signingExecutor != null) %7B%0A                LOG.debug(%22Delegating signing to executor%22);%0A                signingExecutor.execute(new Runnable() %7B%0A                    @Override%0A                    public void run() %7B%0A                        doSign(artifact);%0A                    %7D%0A                %7D);%0A            %7D else %7B%0A                LOG.debug(%22Signing in the caller's thread%22);%0A                do
 Sign
@@ -2642,32 +2642,46 @@
 Sign(artifact);%0A
+            %7D%0A
         %7D else %7B
@@ -2747,11 +2747,282 @@
  %7D%0A    %7D
+%0A%0A    private void doSign(final Artifact artifact) %7B%0A        LOG.debug(%22Signing %7B%7D%22, artifact);%0A        signedJarRepo.fetchAndSign(artifact);%0A    %7D%0A%0A    public void setSigningExecutor(final Executor signingExecutor) %7B%0A        this.signingExecutor = signingExecutor;%0A    %7D
 %0A%7D%0A

Add possibility to use an executor when signing.