<commit_before>package de.adito.maven.plugin.jarsign;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.jarsigner.*;
import org.apache.maven.shared.utils.cli.CommandLineException;
import org.apache.maven.shared.utils.cli.javatool.JavaToolException;
import org.apache.maven.shared.utils.cli.javatool.JavaToolResult;
import org.codehaus.plexus.digest.Digester;
import org.codehaus.plexus.digest.DigesterException;
import org.codehaus.plexus.logging.console.ConsoleLogger;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Set;

/**
 * @author PaL
 *         Date: 01.02.15
 *         Time. 21:15
 */
@Mojo(name = "sign", requiresDependencyResolution = ResolutionScope.COMPILE)
public class SignMojo extends AbstractMojo
{

  @Component
  private MavenProject project;

  @Parameter(defaultValue = "false", property = "jarsigner.force")
  private boolean forceSign;

  /**
   * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
   */
  @Parameter(property = "jarsigner.alias")
  private String alias;

  /**
   * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
   */
  @Parameter(property = "jarsigner.keystore")
  private String keystore;

  /**
   * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
   */
  @Parameter(property = "jarsigner.storepass")
  private String storepass;

  /**
   * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
   */
  @Parameter(property = "jarsigner.keypass")
  private String keypass;

  /**
   * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
   */
  @Parameter(property = "jarsigner.tsa")
  private String tsa;

  @Component(hint = "sha1")
  private Digester sha1Digester;


  public void execute() throws MojoExecutionException
  {
    try
    {
      DefaultJarSigner jarSigner = new DefaultJarSigner();
      jarSigner.enableLogging(new ConsoleLogger());

      Set<Artifact> artifacts = project.getArtifacts();

      for (Artifact artifact : artifacts)
      {
        File file = artifact.getFile();
        if (forceSign || !_existingChecksumMatchs(file))
        {
          getLog().info("Signing " + file.getAbsolutePath() + ".");

          JarSignerUtil.unsignArchive(file);

          JarSignerSignRequest signRequest = new JarSignerSignRequest();
          _setup(signRequest, file);
          signRequest.setKeypass(keypass);
          signRequest.setTsaLocation(tsa);
          _execute(jarSigner, signRequest);
        }

        JarSignerVerifyRequest verifyRequest = new JarSignerVerifyRequest();
        _setup(verifyRequest, file);
        verifyRequest.setVerbose(true);
        verifyRequest.setArguments("-strict", "-verbose:summary");
        _execute(jarSigner, verifyRequest);

        _installSignChecksum(file);
      }

      getLog().info(artifacts.size() + " jars have been processed.");
    }
    catch (Exception e)
    {
      throw new MojoExecutionException(e.getMessage(), e);
    }
  }


  private void _setup(AbstractJarSignerRequest pRequest, File pFile)
  {
    pRequest.setKeystore(keystore);
    pRequest.setAlias(alias);
    pRequest.setStorepass(storepass);
    pRequest.setArchive(pFile);
  }

  private void _execute(JarSigner pJarSigner, AbstractJarSignerRequest pRequest)
      throws CommandLineException, JavaToolException
  {
    JavaToolResult result = pJarSigner.execute(pRequest);
    CommandLineException executionException = result.getExecutionException();
    if (executionException != null)
      throw executionException;
  }

  private boolean _existingChecksumMatchs(File pFile) throws MojoExecutionException
  {
    String existingChecksum;
    try
    {
      existingChecksum = FileUtils.fileRead(pFile.getAbsoluteFile() + ".signed.sha1");
    }
    catch (IOException e)
    {
      return false;
    }
    return _getChecksum(pFile).equals(existingChecksum);
  }

  private String _getChecksum(File pFile) throws MojoExecutionException
  {
    getLog().debug("Calculating " + sha1Digester.getAlgorithm() + " checksum for " + pFile);
    try
    {
      return sha1Digester.calc(pFile);
    }
    catch (DigesterException e)
    {<commit_msg>Fail when jarsigner returns non-null exit code.<commit_after>package de.adito.maven.plugin.jarsign;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.jarsigner.*;
import org.apache.maven.shared.utils.cli.CommandLineException;
import org.apache.maven.shared.utils.cli.javatool.JavaToolException;
import org.apache.maven.shared.utils.cli.javatool.JavaToolResult;
import org.codehaus.plexus.digest.Digester;
import org.codehaus.plexus.digest.DigesterException;
import org.codehaus.plexus.logging.console.ConsoleLogger;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Set;

/**
 * @author PaL
 *         Date: 01.02.15
 *         Time. 21:15
 */
@Mojo(name = "sign", requiresDependencyResolution = ResolutionScope.COMPILE)
public class SignMojo extends AbstractMojo
{

  @Component
  private MavenProject project;

  @Component(hint = "sha1")
  private Digester sha1Digester;

  /**
   * If <i>true</i> all jars are signed no matter whether already signed or not.
   */
  @Parameter(defaultValue = "false", property = "jarsigner.force")
  private boolean forceSign;

  /**
   * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
   */
  @Parameter(property = "jarsigner.alias")
  private String alias;

  /**
   * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
   */
  @Parameter(property = "jarsigner.keystore")
  private String keystore;

  /**
   * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
   */
  @Parameter(property = "jarsigner.storepass")
  private String storepass;

  /**
   * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
   */
  @Parameter(property = "jarsigner.keypass")
  private String keypass;

  /**
   * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
   */
  @Parameter(property = "jarsigner.tsa")
  private String tsa;


  public void execute() throws MojoExecutionException
  {
    try
    {
      DefaultJarSigner jarSigner = new DefaultJarSigner();
      jarSigner.enableLogging(new ConsoleLogger());

      Set<Artifact> artifacts = project.getArtifacts();
      int signCount = 0;

      for (Artifact artifact : artifacts)
      {
        File file = artifact.getFile();
        if (forceSign || !_existingChecksumMatchs(file))
        {
          getLog().info("Signing " + file.getAbsolutePath() + ".");

          JarSignerUtil.unsignArchive(file);

          JarSignerSignRequest signRequest = new JarSignerSignRequest();
          _setup(signRequest, file);
          signRequest.setKeypass(keypass);
          signRequest.setTsaLocation(tsa);
          _execute(jarSigner, signRequest);

          _installSignChecksum(file);
          signCount++;
        }

        JarSignerVerifyRequest verifyRequest = new JarSignerVerifyRequest();
        _setup(verifyRequest, file);
        verifyRequest.setVerbose(true);
        verifyRequest.setArguments("-strict", "-verbose:summary");
        _execute(jarSigner, verifyRequest);
      }

      getLog().info(signCount + " jars have been resigned.");
      getLog().info(artifacts.size() + " jars have been verified.");
    }
    catch (CommandLineException | IOException | JavaToolException e)
    {
      throw new MojoExecutionException(e.getMessage(), e);
    }
  }


  private void _setup(AbstractJarSignerRequest pRequest, File pFile)
  {
    pRequest.setKeystore(keystore);
    pRequest.setAlias(alias);
    pRequest.setStorepass(storepass);
    pRequest.setArchive(pFile);
  }

  private void _execute(JarSigner pJarSigner, AbstractJarSignerRequest pRequest)
      throws CommandLineException, JavaToolException, MojoExecutionException
  {
    JavaToolResult result = pJarSigner.execute(pRequest);
    CommandLineException executionException = result.getExecutionException();
    if (executionException != null)
      throw executionException;
    int exitCode = result.getExitCode();
    if (exitCode != 0)
      throw new MojoExecutionException("Wrong exit code '" + exitCode + "'. Jar signing or verifying failed");
  }

  private boolean _existingChecksumMatchs(File pFile) throws MojoExecutionException
  {
    String existingChecksum;
    try
    {
      existingChecksum = FileUtils.fileRead(pFile.getAbsoluteFile() + ".signed.sha1");
    }
    catch (IOException e)
    {
      return false;
    }
    return _getChecksum(pFile).equals(existingChecksum);
  }

  private String _getChecksum(File pFile) throws MojoExecutionException
  {
    getLog().debug("Calculating " + sha1Digester.getAlgorithm() + " checksum for " + pFile);
    try
    {
      return sha1Digester.calc(pFile);
    }
    catch (DigesterException e)
    {
{'fraction_non_alphanumeric': 0.12050466918338963, 'fraction_numerical': 0.004172461752433936, 'mean_word_length': 3.710809546092653, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2493392', 'n_tokens_mistral': 3207, 'n_tokens_neox': 3071, 'n_words': 526}