PageRenderTime 28ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/maven-amps-plugin/src/main/java/com/atlassian/maven/plugins/amps/osgi/ValidateTestManifestMojo.java

https://bitbucket.org/mmeinhold/amps
Java | 103 lines | 84 code | 11 blank | 8 comment | 9 complexity | 425af0018c1d1d0856773d582c2f1e91 MD5 | raw file
  1package com.atlassian.maven.plugins.amps.osgi;
  2
  3import java.io.File;
  4import java.io.FileInputStream;
  5import java.io.IOException;
  6import java.io.InputStream;
  7import java.util.HashMap;
  8import java.util.Map;
  9import java.util.jar.Manifest;
 10
 11import com.atlassian.maven.plugins.amps.AbstractAmpsMojo;
 12
 13import org.apache.commons.io.IOUtils;
 14import org.apache.maven.plugin.MojoExecutionException;
 15import org.apache.maven.plugin.MojoFailureException;
 16import org.apache.maven.plugins.annotations.Mojo;
 17import org.apache.maven.plugins.annotations.Parameter;
 18
 19import aQute.lib.osgi.Constants;
 20
 21import static com.atlassian.maven.plugins.amps.util.FileUtils.file;
 22
 23@Mojo(name = "validate-test-manifest")
 24public class ValidateTestManifestMojo extends AbstractAmpsMojo
 25{
 26    /**
 27     * Whether to skip validation or not
 28     */
 29    @Parameter(property = "manifest.validation.skip")
 30    protected boolean skipManifestValidation = false;
 31
 32    /**
 33     * The BND instructions for the bundle.  We'll only validate the import versions if there was an
 34     * explicit Import-Package list, not if we auto-generated the imports.
 35     */
 36    @Parameter
 37    private Map<String, String> testInstructions = new HashMap<String, String>();
 38
 39    public void execute() throws MojoExecutionException, MojoFailureException
 40    {
 41        if(shouldBuildTestPlugin())
 42        {
 43            final File mfile = file(getMavenContext().getProject().getBuild().getTestOutputDirectory(), "META-INF", "MANIFEST.MF");
 44    
 45            // Only valid if the manifest exists
 46            if (!skipManifestValidation && mfile.exists())
 47            {
 48                getLog().info("Manifest found, validating...");
 49                InputStream mfin = null;
 50                try
 51                {
 52                    checkManifestEndsWithNewLine(mfile);
 53    
 54                    mfin = new FileInputStream(mfile);
 55                    Manifest mf = new Manifest(mfin);
 56                    if (testInstructions.containsKey(Constants.IMPORT_PACKAGE))
 57                    {
 58                        PackageImportVersionValidator validator = new PackageImportVersionValidator(getMavenContext().getProject(),
 59                                getMavenContext().getLog(), getPluginInformation().getId());
 60                        validator.validate(mf.getMainAttributes().getValue(Constants.IMPORT_PACKAGE));
 61                    }
 62                }
 63                catch (IOException e)
 64                {
 65                    throw new MojoExecutionException("Unable to read manifest", e);
 66                }
 67                finally
 68                {
 69                    IOUtils.closeQuietly(mfin);
 70                }
 71                getLog().info("Manifest validated");
 72            }
 73            else
 74            {
 75                getLog().info("No manifest found or validation skip flag specified, skipping validation");
 76            }
 77        }
 78    }
 79
 80    private void checkManifestEndsWithNewLine(final File mfile)
 81            throws IOException, MojoExecutionException, MojoFailureException
 82    {
 83        InputStream is = null;
 84        try
 85        {
 86            is = new FileInputStream(mfile);
 87            final long bytesToSkip = mfile.length() - 1;
 88            long bytesSkipped = is.skip(bytesToSkip);
 89            if (bytesSkipped != bytesToSkip)
 90            {
 91                throw new MojoExecutionException("Could not skip " + bytesToSkip + " bytes reading " + mfile.getAbsolutePath());
 92            }
 93            else if (is.read() != '\n')
 94            {
 95                throw new MojoFailureException("Manifests must end with a new line. " + mfile.getAbsolutePath() + " doesn't.");
 96            }
 97        }
 98        finally
 99        {
100            IOUtils.closeQuietly(is);
101        }
102    }
103}