You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by be...@apache.org on 2009/07/19 16:47:51 UTC

svn commit: r795550 - in /maven/plugins/trunk/maven-jarsigner-plugin/src: it/type-detection/ main/java/org/apache/maven/plugins/jarsigner/

Author: bentmann
Date: Sun Jul 19 14:47:50 2009
New Revision: 795550

URL: http://svn.apache.org/viewvc?rev=795550&view=rev
Log:
o Decoupled detection of signable artifacts from artifact handler and peeked at file instead

Added:
    maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/
    maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/jar-with-unusual.ext   (with props)
    maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/not-a-jar.properties   (with props)
    maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/pom.xml   (with props)
    maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/verify.bsh   (with props)
Modified:
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/AbstractJarsignerMojo.java

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/jar-with-unusual.ext
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/jar-with-unusual.ext?rev=795550&view=auto
==============================================================================
Binary file - no diff available.

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/jar-with-unusual.ext
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/not-a-jar.properties
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/not-a-jar.properties?rev=795550&view=auto
==============================================================================
    (empty)

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/not-a-jar.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/not-a-jar.properties
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/pom.xml?rev=795550&view=auto
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/pom.xml (added)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/pom.xml Sun Jul 19 14:47:50 2009
@@ -0,0 +1,118 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.jarsigner</groupId>
+  <artifactId>test</artifactId>
+  <version>1.0</version>
+  <packaging>jar</packaging>
+
+  <description>
+    Tests the detection of JAR files (we don't want to sign/verify other file formats).
+  </description>
+
+  <properties>
+    <maven.test.skip>true</maven.test.skip>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>2.0.2</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jar-plugin</artifactId>
+        <version>2.1</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-resources-plugin</artifactId>
+        <version>2.2</version>
+      </plugin>
+      <plugin>
+        <groupId>org.codehaus.mojo</groupId>
+        <artifactId>build-helper-maven-plugin</artifactId>
+        <version>1.2</version>
+        <executions>
+          <execution>
+            <id>attach-artifacts</id>
+            <goals>
+              <goal>attach-artifact</goal>
+            </goals>
+            <configuration>
+              <artifacts>
+                <artifact>
+                  <!--
+                  Note that neither the artifact type nor the file extension indicate the JAR nature of the file, yet
+                  we want it to be processed.
+                  -->
+                  <file>jar-with-unusual.ext</file>
+                  <type>foo</type>
+                  <classifier>a</classifier>
+                </artifact>
+                <artifact>
+                  <file>not-a-jar.properties</file>
+                  <type>bar</type>
+                  <classifier>b</classifier>
+                </artifact>
+              </artifacts>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <version>2.3.1</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jarsigner-plugin</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+          <alias>test-01</alias>
+          <keypass>key-passwd</keypass>
+          <sigfile>TESTING</sigfile>
+        </configuration>
+        <executions>
+          <execution>
+            <id>sign-jars</id>
+            <goals>
+              <goal>sign</goal>
+            </goals>
+          </execution>
+          <execution>
+            <id>verify-jars</id>
+            <goals>
+              <goal>verify</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/verify.bsh
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/verify.bsh?rev=795550&view=auto
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/verify.bsh (added)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/verify.bsh Sun Jul 19 14:47:50 2009
@@ -0,0 +1,24 @@
+import java.io.*;
+import java.util.jar.*;
+
+File attachedJarFile = new File( basedir, "jar-with-unusual.ext" );
+System.out.println( "Checking for existence of " + attachedJarFile );
+if ( !attachedJarFile.isFile() )
+{
+    throw new Exception( "missing " + attachedJarFile );
+}
+
+JarFile attachedJar = new JarFile( attachedJarFile );
+System.out.println( "Checking for existence of " + attachedJarFile.getName() + "!/META-INF/TESTING.SF" );
+if ( attachedJar.getEntry( "META-INF/TESTING.SF" ) == null )
+{
+    throw new Exception( "missing " + attachedJarFile.getName() + "!/META-INF/TESTING.SF" );
+}
+System.out.println( "Checking for existence of " + attachedJarFile.getName() + "!/META-INF/TESTING.DSA" );
+if ( attachedJar.getEntry( "META-INF/TESTING.DSA" ) == null )
+{
+    throw new Exception( "missing " + attachedJarFile.getName() + "!/META-INF/TESTING.DSA" );
+}
+attachedJar.close();
+
+return true;

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/verify.bsh
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/it/type-detection/verify.bsh
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/AbstractJarsignerMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/AbstractJarsignerMojo.java?rev=795550&r1=795549&r2=795550&view=diff
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/AbstractJarsignerMojo.java (original)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/AbstractJarsignerMojo.java Sun Jul 19 14:47:50 2009
@@ -20,12 +20,15 @@
  */
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.text.MessageFormat;
 import java.util.Iterator;
 import java.util.Properties;
 import java.util.ResourceBundle;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.plugin.AbstractMojo;
@@ -131,7 +134,7 @@
                     {
                         this.processArtifact( artifact );
                     }
-                    else if ( this.isJavaLanguageCapable( artifact ) )
+                    else if ( this.isJarFile( artifact ) )
                     {
                         this.getLog().info( this.getMessage( "ignoringAttachment", new Object[]
                             {
@@ -188,10 +191,46 @@
      *
      * @return {@code true} if {@code artifact} is Java language capable; {@code false} if not.
      */
-    private boolean isJavaLanguageCapable( final Artifact artifact )
+    private boolean isJarFile( final Artifact artifact )
     {
-        return artifact != null && artifact.getFile() != null && artifact.getArtifactHandler() != null
-            && "java".equals( artifact.getArtifactHandler().getLanguage() );
+        return artifact != null && artifact.getFile() != null && isJarFile( artifact.getFile() );
+    }
+
+    /**
+     * Checks whether the specified file is a JAR file. For our purposes, a JAR file is a (non-empty) ZIP stream with a
+     * META-INF directory or some class files.
+     * 
+     * @param file The file to check, must not be <code>null</code>.
+     * @return <code>true</code> if the file looks like a JAR file, <code>false</code> otherwise.
+     */
+    private boolean isJarFile( final File file )
+    {
+        try
+        {
+            // NOTE: ZipFile.getEntry() might be shorter but is several factors slower on large files
+
+            ZipInputStream zis = new ZipInputStream( new FileInputStream( file ) );
+            try
+            {
+                for ( ZipEntry ze; ( ze = zis.getNextEntry() ) != null; )
+                {
+                    if ( ze.getName().startsWith( "META-INF/" ) || ze.getName().endsWith( ".class" ) )
+                    {
+                        return true;
+                    }
+                }
+            }
+            finally
+            {
+                zis.close();
+            }
+        }
+        catch ( Exception e )
+        {
+            // ignore, will fail below
+        }
+
+        return false;
     }
 
     /**
@@ -210,7 +249,7 @@
             throw new NullPointerException( "artifact" );
         }
 
-        if ( this.isJavaLanguageCapable( artifact ) )
+        if ( this.isJarFile( artifact ) )
         {
             if ( this.verbose )
             {