/**
 * Copyright (c) 2012 Sourcepit.org contributors and others. All rights reserved. This program and the accompanying
 * materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.sourcepit.mavenizor.maven;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;

import org.apache.maven.artifact.Artifact;
import org.slf4j.Logger;
import org.sonatype.aether.util.ChecksumUtils;
import org.sourcepit.common.utils.lang.Exceptions;
import org.sourcepit.mavenizor.maven.ArtifactBundleDistributor.DistributionHandler;

public abstract class AbstractDistributionHandler implements DistributionHandler
{
   final private Logger log;

   public AbstractDistributionHandler(Logger log)
   {
      this.log = log;
   }

   public void distribute(Artifact artifact, boolean forceOverwrite)
   {
      if (forceOverwrite || !existsInTarget(artifact))
      {
         doDistribute(artifact);
      }
      else
      {
         log.info("Skipped distribution of " + artifact + ". It already exists in target.");
      }
   }

   protected Logger getLog()
   {
      return log;
   }

   protected abstract void doDistribute(Artifact artifact);

   protected final boolean existsInTarget(Artifact artifact)
   {
      final String remoteChecksum = getTargetChecksum(artifact);
      if (remoteChecksum == null)
      {
         return false;
      }

      final String localChecksum = getLocalChecksum(artifact);
      if (!localChecksum.equals(remoteChecksum))
      {
         getLog().warn("Target artifact " + artifact + " exists, but with diffrent checksum.");
      }

      return true;
   }

   protected abstract String getLocalChecksum(Artifact artifact);

   protected abstract String getTargetChecksum(Artifact artifact);

   protected static String calc(final File targetFile, String algo)
   {
      try
      {
         final Map<String, Object> checksums = ChecksumUtils.calc(targetFile, Collections.singleton(algo));

         final Object checksum = checksums.values().iterator().next();
         if (checksum instanceof Throwable)
         {
            throw new IllegalStateException((Throwable) checksum);
         }

         return (String) checksum;
      }
      catch (IOException e)
      {
         throw Exceptions.pipe(e);
      }
   }
}

@@ -488,16 +488,64 @@
 tifact;%0A
+import org.apache.maven.artifact.ArtifactUtils;%0A
 import o
@@ -1680,32 +1680,271 @@
 cksum))%0A      %7B%0A
+         if (ArtifactUtils.isSnapshot(artifact.getVersion()))%0A         %7B%0A            getLog().info(%22Target SNAPSHOT artifact %22 + artifact + %22 differs from local artifact.%22);%0A            return false;%0A         %7D%0A         else%0A         %7B%0A   
          getLog(
@@ -2019,24 +2019,35 @@
 hecksum.%22);%0A
+         %7D%0A
       %7D%0A%0A   

Allow re-distribution of snapshot artifacts from target platform