You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2011/10/28 15:18:45 UTC

svn commit: r1190297 - in /maven/plugins/trunk/maven-jarsigner-plugin: ./ src/main/java/org/apache/maven/plugins/jarsigner/ src/main/java/org/apache/maven/shared/ src/main/java/org/apache/maven/shared/jarsigner/

Author: olamy
Date: Fri Oct 28 13:18:44 2011
New Revision: 1190297

URL: http://svn.apache.org/viewvc?rev=1190297&view=rev
Log:
[MJARSIGNER-19] Make mojo code reusable from another mojo + customize the working directory.
Submitted by Tony Chemit.

Added:
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/AbstractJarSignerRequest.java   (with props)
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/CommandLineConfigurationException.java   (with props)
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/DefaultJarSigner.java   (with props)
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/DefaultJarSignerResult.java   (with props)
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSigner.java   (with props)
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerCommandLineBuilder.java   (with props)
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerException.java   (with props)
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerRequest.java   (with props)
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerResult.java   (with props)
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerSignRequest.java   (with props)
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerUtil.java   (with props)
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerVerifyRequest.java   (with props)
Modified:
    maven/plugins/trunk/maven-jarsigner-plugin/pom.xml
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/AbstractJarsignerMojo.java
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerSignMojo.java
    maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerVerifyMojo.java

Modified: maven/plugins/trunk/maven-jarsigner-plugin/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/pom.xml?rev=1190297&r1=1190296&r2=1190297&view=diff
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-jarsigner-plugin/pom.xml Fri Oct 28 13:18:44 2011
@@ -83,6 +83,24 @@ under the License.
     </dependency>
   </dependencies>
 
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.codehaus.plexus</groupId>
+        <artifactId>plexus-maven-plugin</artifactId>
+        <version>1.3.8</version>
+        <executions>
+          <execution>
+            <id>create-component-descriptor</id>
+            <phase>generate-resources</phase>
+            <goals>
+              <goal>descriptor</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
   <profiles>
     <profile>
       <id>run-its</id>

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=1190297&r1=1190296&r2=1190297&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 Fri Oct 28 13:18:44 2011
@@ -19,32 +19,19 @@ package org.apache.maven.plugins.jarsign
  * under the License.
  */
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.text.MessageFormat;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Properties;
-import java.util.ResourceBundle;
-import java.util.zip.ZipInputStream;
-
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.project.MavenProject;
-
+import org.apache.maven.shared.jarsigner.*;
 import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.Os;
 import org.codehaus.plexus.util.StringUtils;
-import org.codehaus.plexus.util.cli.CommandLineException;
-import org.codehaus.plexus.util.cli.CommandLineUtils;
 import org.codehaus.plexus.util.cli.Commandline;
-import org.codehaus.plexus.util.cli.StreamConsumer;
+
+import java.io.File;
+import java.io.IOException;
+import java.text.MessageFormat;
+import java.util.*;
 
 /**
  * Maven Jarsigner Plugin base class.
@@ -66,7 +53,7 @@ public abstract class AbstractJarsignerM
     /**
      * The maximum memory available to the JAR signer, e.g. <code>256M</code>. See <a
      * href="http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html#Xms">-Xmx</a> for more details.
-     * 
+     *
      * @parameter expression="${jarsigner.maxMemory}"
      */
     private String maxMemory;
@@ -80,7 +67,7 @@ public abstract class AbstractJarsignerM
 
     /**
      * The base directory to scan for JAR files using Ant-like inclusion/exclusion patterns.
-     * 
+     *
      * @parameter expression="${jarsigner.archiveDirectory}"
      * @since 1.1
      */
@@ -90,7 +77,7 @@ public abstract class AbstractJarsignerM
      * The Ant-like inclusion patterns used to select JAR files to process. The patterns must be relative to the
      * directory given by the parameter {@link #archiveDirectory}. By default, the pattern
      * <code>&#42;&#42;/&#42;.?ar</code> is used.
-     * 
+     *
      * @parameter
      * @since 1.1
      */
@@ -99,11 +86,11 @@ public abstract class AbstractJarsignerM
     /**
      * The Ant-like exclusion patterns used to exclude JAR files from processing. The patterns must be relative to the
      * directory given by the parameter {@link #archiveDirectory}.
-     * 
+     *
      * @parameter
      * @since 1.1
      */
-    private String[] excludes = {};
+    private String[] excludes = { };
 
     /**
      * List of additional arguments to append to the jarsigner command line.
@@ -121,7 +108,7 @@ public abstract class AbstractJarsignerM
 
     /**
      * Controls processing of the main artifact produced by the project.
-     * 
+     *
      * @parameter expression="${jarsigner.processMainArtifact}" default-value="true"
      * @since 1.1
      */
@@ -130,7 +117,7 @@ public abstract class AbstractJarsignerM
     /**
      * Controls processing of project attachments. If enabled, attached artifacts that are no JAR/ZIP files will be
      * automatically excluded from processing.
-     * 
+     *
      * @parameter expression="${jarsigner.processAttachedArtifacts}" default-value="true"
      * @since 1.1
      */
@@ -138,7 +125,7 @@ public abstract class AbstractJarsignerM
 
     /**
      * Controls processing of project attachments.
-     * 
+     *
      * @parameter expression="${jarsigner.attachments}"
      * @deprecated As of version 1.1 in favor of the new parameter <code>processAttachedArtifacts</code>.
      */
@@ -147,7 +134,7 @@ public abstract class AbstractJarsignerM
     /**
      * A set of artifact classifiers describing the project attachments that should be processed. This parameter is only
      * relevant if {@link #processAttachedArtifacts} is <code>true</code>. If empty, all attachments are included.
-     * 
+     *
      * @parameter
      * @since 1.2
      */
@@ -156,7 +143,7 @@ public abstract class AbstractJarsignerM
     /**
      * A set of artifact classifiers describing the project attachments that should not be processed. This parameter is
      * only relevant if {@link #processAttachedArtifacts} is <code>true</code>. If empty, no attachments are excluded.
-     * 
+     *
      * @parameter
      * @since 1.2
      */
@@ -172,17 +159,23 @@ public abstract class AbstractJarsignerM
     private MavenProject project;
 
     /**
-     * The path to the jarsigner we are going to use.
+     * Location of the working directory.
+     *
+     * @parameter default-value="${project.basedir}"
+     * @since 1.3
+     */
+    private File workingDirectory;
+
+    /**
+     * @component
      */
-    private String executable;
+    private JarSigner jarSigner;
 
     public final void execute()
         throws MojoExecutionException
     {
         if ( !this.skip )
         {
-            this.executable = getExecutable();
-
             int processed = 0;
 
             if ( this.archive != null )
@@ -252,8 +245,8 @@ public abstract class AbstractJarsignerM
                     }
                     catch ( IOException e )
                     {
-                        throw new MojoExecutionException( "Failed to scan archive directory for JARs: "
-                            + e.getMessage(), e );
+                        throw new MojoExecutionException(
+                            "Failed to scan archive directory for JARs: " + e.getMessage(), e );
                     }
 
                     for ( Iterator it = jarFiles.iterator(); it.hasNext(); )
@@ -275,26 +268,20 @@ public abstract class AbstractJarsignerM
     }
 
     /**
-     * Gets the {@code Commandline} to execute for a given Java archive taking a command line prepared for executing
-     * jarsigner.
-     *
-     * @param archive The Java archive to get a {@code Commandline} to execute for.
-     * @param commandLine A {@code Commandline} prepared for executing jarsigner without any arguments.
+     * Creates the jar signer request to be executed.
      *
-     * @return A {@code Commandline} for executing jarsigner with {@code archive}.
-     *
-     * @throws NullPointerException if {@code archive} or {@code commandLine} is {@code null}.
+     * @param archive the archive file to treat by jarsigner
+     * @return the request
+     * @since 1.3
      */
-    protected abstract Commandline getCommandline( final File archive, final Commandline commandLine );
+    protected abstract JarSignerRequest createRequest( File archive );
 
     /**
      * Gets a string representation of a {@code Commandline}.
      * <p>This method creates the string representation by calling {@code commandLine.toString()} by default.</p>
      *
      * @param commandLine The {@code Commandline} to get a string representation of.
-     *
      * @return The string representation of {@code commandLine}.
-     *
      * @throws NullPointerException if {@code commandLine} is {@code null}.
      */
     protected String getCommandlineInfo( final Commandline commandLine )
@@ -311,41 +298,11 @@ public abstract class AbstractJarsignerM
      * Checks whether the specified artifact is a ZIP file.
      *
      * @param artifact The artifact to check, may be <code>null</code>.
-     *
      * @return <code>true</code> if the artifact looks like a ZIP file, <code>false</code> otherwise.
      */
     private boolean isZipFile( final Artifact artifact )
     {
-        return artifact != null && artifact.getFile() != null && isZipFile( artifact.getFile() );
-    }
-
-    /**
-     * Checks whether the specified file is a JAR file. For our purposes, a ZIP file is a ZIP stream with at least one
-     * entry.
-     * 
-     * @param file The file to check, must not be <code>null</code>.
-     * @return <code>true</code> if the file looks like a ZIP file, <code>false</code> otherwise.
-     */
-    private boolean isZipFile( final File file )
-    {
-        try
-        {
-            ZipInputStream zis = new ZipInputStream( new FileInputStream( file ) );
-            try
-            {
-                return zis.getNextEntry() != null;
-            }
-            finally
-            {
-                zis.close();
-            }
-        }
-        catch ( Exception e )
-        {
-            // ignore, will fail below
-        }
-
-        return false;
+        return artifact != null && artifact.getFile() != null && JarSignerUtil.isZipFile( artifact.getFile() );
     }
 
     /**
@@ -353,8 +310,7 @@ public abstract class AbstractJarsignerM
      *
      * @param artifact The artifact to process.
      * @return <code>true</code> if the artifact is a JAR and was processed, <code>false</code> otherwise.
-     *
-     * @throws NullPointerException if {@code artifact} is {@code null}.
+     * @throws NullPointerException   if {@code artifact} is {@code null}.
      * @throws MojoExecutionException if processing {@code artifact} fails.
      */
     private boolean processArtifact( final Artifact artifact )
@@ -390,7 +346,7 @@ public abstract class AbstractJarsignerM
 
     /**
      * Pre-processes a given archive.
-     * 
+     *
      * @param archive The archive to process, must not be <code>null</code>.
      * @throws MojoExecutionException If pre-processing failed.
      */
@@ -402,9 +358,9 @@ public abstract class AbstractJarsignerM
 
     /**
      * Processes a given archive.
-     * 
+     *
      * @param archive The archive to process.
-     * @throws NullPointerException if {@code archive} is {@code null}.
+     * @throws NullPointerException   if {@code archive} is {@code null}.
      * @throws MojoExecutionException if processing {@code archive} fails.
      */
     private void processArchive( final File archive )
@@ -426,167 +382,43 @@ public abstract class AbstractJarsignerM
             getLog().debug( getMessage( "processing", archive ) );
         }
 
-        Commandline commandLine = new Commandline();
-
-        commandLine.setExecutable( this.executable );
-
-        commandLine.setWorkingDirectory( this.project.getBasedir() );
-
-        if ( this.verbose )
-        {
-            commandLine.createArg().setValue( "-verbose" );
-        }
-
-        if ( StringUtils.isNotEmpty( maxMemory ) )
-        {
-            commandLine.createArg().setValue( "-J-Xmx" + maxMemory );
-        }
-
-        if ( this.arguments != null )
-        {
-            commandLine.addArguments( this.arguments );
-        }
-
-        commandLine = getCommandline( archive, commandLine );
+        JarSignerRequest request = createRequest( archive );
+        request.setArchive( archive );
+        request.setWorkingDirectory( workingDirectory );
+        request.setMaxMemory( maxMemory );
+        request.setArguments( arguments );
 
         try
         {
-            if ( getLog().isDebugEnabled() )
-            {
-                getLog().debug( getMessage( "command", getCommandlineInfo( commandLine ) ) );
-            }
+            JarSignerResult result = jarSigner.execute( request );
 
-            final int result = CommandLineUtils.executeCommandLine( commandLine,
-                new InputStream()
-            {
-
-                public int read()
-                {
-                    return -1;
-                }
-
-            }, new StreamConsumer()
-            {
-
-                public void consumeLine( final String line )
-                {
-                    if ( verbose )
-                    {
-                        getLog().info( line );
-                    }
-                    else
-                    {
-                        getLog().debug( line );
-                    }
-                }
-
-            }, new StreamConsumer()
-            {
-
-                public void consumeLine( final String line )
-                {
-                    getLog().warn( line );
-                }
-
-            } );
-
-            if ( result != 0 )
-            {
-                throw new MojoExecutionException( getMessage( "failure", getCommandlineInfo( commandLine ),
-                                                              new Integer( result ) ) );
-            }
-        }
-        catch ( CommandLineException e )
-        {
-            throw new MojoExecutionException( getMessage( "commandLineException", getCommandlineInfo( commandLine ) ),
-                                              e );
-        }
-    }
-
-    /**
-     * Locates the executable for the jarsigner tool.
-     * 
-     * @return The executable of the jarsigner tool, never <code>null<code>.
-     */
-    private String getExecutable()
-    {
-        String command = "jarsigner" + ( Os.isFamily( Os.FAMILY_WINDOWS ) ? ".exe" : "" );
+            Commandline commandLine = result.getCommandline();
 
-        String executable =
-            findExecutable( command, System.getProperty( "java.home" ), new String[] { "../bin", "bin", "../sh" } );
+            int resultCode = result.getExitCode();
 
-        if ( executable == null )
-        {
-            try
+            if ( resultCode != 0 )
             {
-                Properties env = CommandLineUtils.getSystemEnvVars();
-
-                String[] variables = { "JDK_HOME", "JAVA_HOME" };
-
-                for ( int i = 0; i < variables.length && executable == null; i++ )
-                {
-                    executable =
-                        findExecutable( command, env.getProperty( variables[i] ), new String[] { "bin", "sh" } );
-                }
+                throw new MojoExecutionException(
+                    getMessage( "failure", getCommandlineInfo( commandLine ), new Integer( resultCode ) ) );
             }
-            catch ( IOException e )
-            {
-                if ( getLog().isDebugEnabled() )
-                {
-                    getLog().warn( "Failed to retrieve environment variables, cannot search for " + command, e );
-                }
-                else
-                {
-                    getLog().warn( "Failed to retrieve environment variables, cannot search for " + command );
-                }
-            }
-        }
 
-        if ( executable == null )
-        {
-            executable = command;
         }
-
-        return executable;
-    }
-
-    /**
-     * Finds the specified command in any of the given sub directories of the specified JDK/JRE home directory.
-     * 
-     * @param command The command to find, must not be <code>null</code>.
-     * @param homeDir The home directory to search in, may be <code>null</code>.
-     * @param subDirs The sub directories of the home directory to search in, must not be <code>null</code>.
-     * @return The (absolute) path to the command if found, <code>null</code> otherwise.
-     */
-    private String findExecutable( String command, String homeDir, String[] subDirs )
-    {
-        if ( StringUtils.isNotEmpty( homeDir ) )
+        catch ( JarSignerException e )
         {
-            for ( int i = 0; i < subDirs.length; i++ )
-            {
-                File file = new File( new File( homeDir, subDirs[i] ), command );
-
-                if ( file.isFile() )
-                {
-                    return file.getAbsolutePath();
-                }
-            }
+            throw new MojoExecutionException( getMessage( "commandLineException", e.getMessage() ), e );
         }
-
-        return null;
     }
 
     /**
      * Gets a message for a given key from the resource bundle backing the implementation.
      *
-     * @param key The key of the message to return.
+     * @param key  The key of the message to return.
      * @param args Arguments to format the message with or {@code null}.
-     *
      * @return The message with key {@code key} from the resource bundle backing the implementation.
-     *
      * @throws NullPointerException if {@code key} is {@code null}.
-     * @throws java.util.MissingResourceException if there is no message available matching {@code key} or accessing
-     * the resource bundle fails.
+     * @throws java.util.MissingResourceException
+     *                              if there is no message available matching {@code key} or accessing
+     *                              the resource bundle fails.
      */
     private String getMessage( final String key, final Object[] args )
     {
@@ -605,12 +437,12 @@ public abstract class AbstractJarsignerM
 
     private String getMessage( final String key, final Object arg )
     {
-        return getMessage( key, new Object[] { arg } );
+        return getMessage( key, new Object[]{ arg } );
     }
 
     private String getMessage( final String key, final Object arg1, final Object arg2 )
     {
-        return getMessage( key, new Object[] { arg1, arg2 } );
+        return getMessage( key, new Object[]{ arg1, arg2 } );
     }
 
 }

Modified: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerSignMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerSignMojo.java?rev=1190297&r1=1190296&r2=1190297&view=diff
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerSignMojo.java (original)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerSignMojo.java Fri Oct 28 13:18:44 2011
@@ -19,22 +19,16 @@ package org.apache.maven.plugins.jarsign
  * under the License.
  */
 
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
-import java.util.zip.ZipOutputStream;
-
 import org.apache.maven.plugin.MojoExecutionException;
-import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.IOUtil;
+import org.apache.maven.shared.jarsigner.JarSignerRequest;
+import org.apache.maven.shared.jarsigner.JarSignerSignRequest;
+import org.apache.maven.shared.jarsigner.JarSignerUtil;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.cli.Commandline;
 
+import java.io.File;
+import java.io.IOException;
+
 /**
  * Signs a project artifact and attachments using jarsigner.
  *
@@ -85,21 +79,21 @@ public class JarsignerSignMojo
 
     /**
      * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
-     * 
+     *
      * @parameter expression="${jarsigner.providerName}"
      */
     private String providerName;
 
     /**
      * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
-     * 
+     *
      * @parameter expression="${jarsigner.providerClass}"
      */
     private String providerClass;
 
     /**
      * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
-     * 
+     *
      * @parameter expression="${jarsigner.providerArg}"
      */
     private String providerArg;
@@ -115,74 +109,12 @@ public class JarsignerSignMojo
     /**
      * Indicates whether existing signatures should be removed from the processed JAR files prior to signing them. If
      * enabled, the resulting JAR will appear as being signed only once.
-     * 
+     *
      * @parameter expression="${jarsigner.removeExistingSignatures}" default-value="false"
      * @since 1.1
      */
     private boolean removeExistingSignatures;
 
-    protected Commandline getCommandline( final File archive, final Commandline commandLine )
-    {
-        if ( archive == null )
-        {
-            throw new NullPointerException( "archive" );
-        }
-        if ( commandLine == null )
-        {
-            throw new NullPointerException( "commandLine" );
-        }
-
-        if ( !StringUtils.isEmpty( this.keystore ) )
-        {
-            commandLine.createArg().setValue( "-keystore" );
-            commandLine.createArg().setValue( this.keystore );
-        }
-        if ( !StringUtils.isEmpty( this.storepass ) )
-        {
-            commandLine.createArg().setValue( "-storepass" );
-            commandLine.createArg().setValue( this.storepass );
-        }
-        if ( !StringUtils.isEmpty( this.keypass ) )
-        {
-            commandLine.createArg().setValue( "-keypass" );
-            commandLine.createArg().setValue( this.keypass );
-        }
-        if ( !StringUtils.isEmpty( this.storetype ) )
-        {
-            commandLine.createArg().setValue( "-storetype" );
-            commandLine.createArg().setValue( this.storetype );
-        }
-        if ( !StringUtils.isEmpty( this.providerName ) )
-        {
-            commandLine.createArg().setValue( "-providerName" );
-            commandLine.createArg().setValue( this.providerName );
-        }
-        if ( !StringUtils.isEmpty( this.providerClass ) )
-        {
-            commandLine.createArg().setValue( "-providerClass" );
-            commandLine.createArg().setValue( this.providerClass );
-        }
-        if ( !StringUtils.isEmpty( this.providerArg ) )
-        {
-            commandLine.createArg().setValue( "-providerArg" );
-            commandLine.createArg().setValue( this.providerArg );
-        }
-        if ( !StringUtils.isEmpty( this.sigfile ) )
-        {
-            commandLine.createArg().setValue( "-sigfile" );
-            commandLine.createArg().setValue( this.sigfile );
-        }
-
-        commandLine.createArg().setFile( archive );
-
-        if ( !StringUtils.isEmpty( this.alias ) )
-        {
-            commandLine.createArg().setValue( this.alias );
-        }
-
-        return commandLine;
-    }
-
     protected String getCommandlineInfo( final Commandline commandLine )
     {
         String commandLineInfo = commandLine != null ? commandLine.toString() : null;
@@ -201,103 +133,30 @@ public class JarsignerSignMojo
     {
         if ( removeExistingSignatures )
         {
-            unsignArchive( archive );
-        }
-    }
-
-    /**
-     * Removes any existing signatures from the specified JAR file. We will stream from the input JAR directly to the
-     * output JAR to retain as much metadata from the original JAR as possible.
-     * 
-     * @param jarFile The JAR file to unsign, must not be <code>null</code>.
-     * @throws MojoExecutionException If the unsigning failed.
-     */
-    private void unsignArchive( final File jarFile )
-        throws MojoExecutionException
-    {
-        if ( getLog().isDebugEnabled() )
-        {
-            getLog().debug( "Unsigning " + jarFile );
-        }
-
-        File unsignedFile = new File( jarFile.getAbsolutePath() + ".unsigned" );
-
-        ZipInputStream zis = null;
-        ZipOutputStream zos = null;
-        try
-        {
-            zis = new ZipInputStream( new BufferedInputStream( new FileInputStream( jarFile ) ) );
-            zos = new ZipOutputStream( new BufferedOutputStream( new FileOutputStream( unsignedFile ) ) );
-
-            for ( ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry() )
+            try
             {
-                if ( isSignatureFile( ze.getName() ) )
-                {
-                    if ( getLog().isDebugEnabled() )
-                    {
-                        getLog().debug( "  Removing " + ze.getName() );
-                    }
-
-                    continue;
-                }
-
-                zos.putNextEntry( ze );
-
-                IOUtil.copy( zis, zos );
+                JarSignerUtil.unsignArchive( archive );
             }
-
-        }
-        catch ( IOException e )
-        {
-            throw new MojoExecutionException( "Failed to unsign archive " + jarFile + ": " + e.getMessage(), e );
-        }
-        finally
-        {
-            IOUtil.close( zis );
-            IOUtil.close( zos );
-        }
-
-        try
-        {
-            FileUtils.rename( unsignedFile, jarFile );
-        }
-        catch ( IOException e )
-        {
-            throw new MojoExecutionException( "Failed to unsign archive " + jarFile + ": " + e.getMessage(), e );
-        }
-    }
-
-    /**
-     * Checks whether the specified JAR file entry denotes a signature-related file, i.e. matches
-     * <code>META-INF/*.SF</code>, <code>META-INF/*.DSA</code> or <code>META-INF/*.RSA</code>.
-     * 
-     * @param entryName The name of the JAR file entry to check, must not be <code>null</code>.
-     * @return <code>true</code> if the entry is related to a signature, <code>false</code> otherwise.
-     */
-    private boolean isSignatureFile( String entryName )
-    {
-        if ( entryName.regionMatches( true, 0, "META-INF", 0, 8 ) )
-        {
-            entryName = entryName.replace( '\\', '/' );
-
-            if ( entryName.indexOf( '/' ) == 8 && entryName.lastIndexOf( '/' ) == 8 )
+            catch ( IOException e )
             {
-                if ( entryName.regionMatches( true, entryName.length() - 3, ".SF", 0, 3 ) )
-                {
-                    return true;
-                }
-                if ( entryName.regionMatches( true, entryName.length() - 4, ".DSA", 0, 4 ) )
-                {
-                    return true;
-                }
-                if ( entryName.regionMatches( true, entryName.length() - 4, ".RSA", 0, 4 ) )
-                {
-                    return true;
-                }
+                throw new MojoExecutionException( "Failed to unsign archive " + archive + ": " + e.getMessage(), e );
             }
         }
+    }
 
-        return false;
+    protected JarSignerRequest createRequest( File archive )
+    {
+        JarSignerSignRequest request = new JarSignerSignRequest();
+        request.setAlias( alias );
+        request.setKeypass( keypass );
+        request.setKeystore( keystore );
+        request.setProviderArg( providerArg );
+        request.setProviderClass( providerClass );
+        request.setProviderName( providerName );
+        request.setSigfile( sigfile );
+        request.setStorepass( storepass );
+        request.setStoretype( storetype );
+        return request;
     }
 
 }

Modified: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerVerifyMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerVerifyMojo.java?rev=1190297&r1=1190296&r2=1190297&view=diff
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerVerifyMojo.java (original)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/plugins/jarsigner/JarsignerVerifyMojo.java Fri Oct 28 13:18:44 2011
@@ -19,9 +19,10 @@ package org.apache.maven.plugins.jarsign
  * under the License.
  */
 
-import java.io.File;
+import org.apache.maven.shared.jarsigner.JarSignerRequest;
+import org.apache.maven.shared.jarsigner.JarSignerVerifyRequest;
 
-import org.codehaus.plexus.util.cli.Commandline;
+import java.io.File;
 
 /**
  * Checks the signatures of a project artifact and attachments using jarsigner.
@@ -43,27 +44,11 @@ public class JarsignerVerifyMojo
      */
     private boolean certs;
 
-    protected Commandline getCommandline( final File archive, final Commandline commandLine )
+    protected JarSignerRequest createRequest( File archive )
     {
-        if ( archive == null )
-        {
-            throw new NullPointerException( "archive" );
-        }
-        if ( commandLine == null )
-        {
-            throw new NullPointerException( "commandLine" );
-        }
-
-        commandLine.createArg( true ).setValue( "-verify" );
-
-        if ( this.certs )
-        {
-            commandLine.createArg().setValue( "-certs" );
-        }
-
-        commandLine.createArg().setFile( archive );
-
-        return commandLine;
+        JarSignerVerifyRequest request = new JarSignerVerifyRequest();
+        request.setCerts( certs );
+        return request;
     }
 
 }

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/AbstractJarSignerRequest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/AbstractJarSignerRequest.java?rev=1190297&view=auto
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/AbstractJarSignerRequest.java (added)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/AbstractJarSignerRequest.java Fri Oct 28 13:18:44 2011
@@ -0,0 +1,90 @@
+package org.apache.maven.shared.jarsigner;
+
+import java.io.File;
+
+/**
+ * Specifies the commons parameters used to control a jar signer invocation.
+ *
+ * @author tchemit <ch...@codelutin.com>
+ * @version $Id$
+ * @since 1.0
+ */
+public abstract class AbstractJarSignerRequest
+    implements JarSignerRequest
+{
+    /**
+     * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
+     */
+    private boolean verbose;
+
+    /**
+     * The maximum memory available to the JAR signer, e.g. <code>256M</code>. See <a
+     * href="http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html#Xms">-Xmx</a> for more details.
+     */
+    private String maxMemory;
+
+    /**
+     * List of additional arguments to append to the jarsigner command line.
+     */
+    private String[] arguments;
+
+    /**
+     * Location of the working directory.
+     */
+    private File workingDirectory;
+
+    /**
+     * Archive to treat.
+     */
+    private File archive;
+
+    public boolean isVerbose()
+    {
+        return verbose;
+    }
+
+    public String getMaxMemory()
+    {
+        return maxMemory;
+    }
+
+    public String[] getArguments()
+    {
+        return arguments;
+    }
+
+    public File getWorkingDirectory()
+    {
+        return workingDirectory;
+    }
+
+    public File getArchive()
+    {
+        return archive;
+    }
+
+    public void setVerbose( boolean verbose )
+    {
+        this.verbose = verbose;
+    }
+
+    public void setMaxMemory( String maxMemory )
+    {
+        this.maxMemory = maxMemory;
+    }
+
+    public void setArguments( String[] arguments )
+    {
+        this.arguments = arguments;
+    }
+
+    public void setWorkingDirectory( File workingDirectory )
+    {
+        this.workingDirectory = workingDirectory;
+    }
+
+    public void setArchive( File archive )
+    {
+        this.archive = archive;
+    }
+}

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/AbstractJarSignerRequest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/AbstractJarSignerRequest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/CommandLineConfigurationException.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/CommandLineConfigurationException.java?rev=1190297&view=auto
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/CommandLineConfigurationException.java (added)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/CommandLineConfigurationException.java Fri Oct 28 13:18:44 2011
@@ -0,0 +1,38 @@
+package org.apache.maven.shared.jarsigner;
+
+/**
+ * Signals an error during the construction of the command line used to invoke jar signer.
+ *
+ * @author tchemit <ch...@codelutin.com>
+ * @version $Id$
+ * @since 1.0
+ */
+public class CommandLineConfigurationException
+    extends Exception
+{
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * Creates a new exception using the specified detail message and cause.
+     *
+     * @param message The detail message for this exception, may be <code>null</code>.
+     * @param cause   The nested exception, may be <code>null</code>.
+     */
+    public CommandLineConfigurationException( String message, Throwable cause )
+    {
+        super( message, cause );
+    }
+
+    /**
+     * Creates a new exception using the specified detail message.
+     *
+     * @param message The detail message for this exception, may be <code>null</code>.
+     */
+    public CommandLineConfigurationException( String message )
+    {
+        super( message );
+    }
+
+}
+

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/CommandLineConfigurationException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/CommandLineConfigurationException.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/DefaultJarSigner.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/DefaultJarSigner.java?rev=1190297&view=auto
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/DefaultJarSigner.java (added)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/DefaultJarSigner.java Fri Oct 28 13:18:44 2011
@@ -0,0 +1,207 @@
+package org.apache.maven.shared.jarsigner;
+
+import org.codehaus.plexus.logging.AbstractLogEnabled;
+import org.codehaus.plexus.util.Os;
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.cli.CommandLineException;
+import org.codehaus.plexus.util.cli.CommandLineUtils;
+import org.codehaus.plexus.util.cli.Commandline;
+import org.codehaus.plexus.util.cli.StreamConsumer;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+/**
+ * Default implementation of component {@link JarSigner}.
+ *
+ * @author tchemit <ch...@codelutin.com>
+ * @version $Id$
+ * @plexus.component role="org.apache.maven.shared.jarsigner.JarSigner" role-hint="default"
+ * @since 1.0
+ */
+public class DefaultJarSigner
+    extends AbstractLogEnabled
+    implements JarSigner
+{
+
+    /**
+     * The location of the jarSigner executable file.
+     */
+    protected String jarSignerFile;
+
+    public JarSignerResult execute( JarSignerRequest request )
+        throws JarSignerException
+    {
+
+        if ( jarSignerFile == null )
+        {
+
+            // find the jar singer to use
+            try
+            {
+                jarSignerFile = findJarSignerExecutable();
+            }
+            catch ( IOException e )
+            {
+                throw new JarSignerException( "Error finding jar signer executable. Reason: " + e.getMessage(), e );
+            }
+        }
+
+        // creates the command line
+        Commandline cli = createCommandLine( request );
+
+        // execute it
+        return executeCommandLine( cli, request );
+    }
+
+    protected Commandline createCommandLine( JarSignerRequest request )
+        throws JarSignerException
+    {
+        JarSignerCommandLineBuilder cliBuilder = new JarSignerCommandLineBuilder();
+        cliBuilder.setLogger( getLogger() );
+        cliBuilder.setJarSignerFile( jarSignerFile );
+        Commandline cli;
+        try
+        {
+            cli = cliBuilder.build( request );
+        }
+        catch ( CommandLineConfigurationException e )
+        {
+            throw new JarSignerException( "Error configuring command-line. Reason: " + e.getMessage(), e );
+        }
+        return cli;
+    }
+
+    protected JarSignerResult executeCommandLine( Commandline cli, JarSignerRequest request )
+    {
+        if ( getLogger().isDebugEnabled() )
+        {
+            getLogger().debug( "Executing: " + cli );
+        }
+
+        final boolean verbose = request.isVerbose();
+
+        InputStream systemIn = new InputStream()
+        {
+
+            public int read()
+            {
+                return -1;
+            }
+
+        };
+        StreamConsumer systemOut = new StreamConsumer()
+        {
+
+            public void consumeLine( final String line )
+            {
+                if ( verbose )
+                {
+                    getLogger().info( line );
+                }
+                else
+                {
+                    getLogger().debug( line );
+                }
+            }
+
+        };
+
+        StreamConsumer systemErr = new StreamConsumer()
+        {
+
+            public void consumeLine( final String line )
+            {
+                getLogger().warn( line );
+            }
+
+        };
+
+        DefaultJarSignerResult result = new DefaultJarSignerResult();
+        result.setCommandline( cli );
+
+        try
+        {
+            int resultCode = CommandLineUtils.executeCommandLine( cli, systemIn, systemOut, systemErr );
+
+            result.setExitCode( resultCode );
+        }
+        catch ( CommandLineException e )
+        {
+            result.setExecutionException( e );
+        }
+
+        return result;
+    }
+
+    protected String findJarSignerExecutable()
+        throws IOException
+    {
+        String command = "jarsigner" + ( Os.isFamily( Os.FAMILY_WINDOWS ) ? ".exe" : "" );
+
+        String executable =
+            findExecutable( command, System.getProperty( "java.home" ), new String[]{ "../bin", "bin", "../sh" } );
+
+        if ( executable == null )
+        {
+            try
+            {
+                Properties env = CommandLineUtils.getSystemEnvVars();
+
+                String[] variables = { "JDK_HOME", "JAVA_HOME" };
+
+                for ( int i = 0; i < variables.length && executable == null; i++ )
+                {
+                    executable =
+                        findExecutable( command, env.getProperty( variables[i] ), new String[]{ "bin", "sh" } );
+                }
+            }
+            catch ( IOException e )
+            {
+                if ( getLogger().isDebugEnabled() )
+                {
+                    getLogger().warn( "Failed to retrieve environment variables, cannot search for " + command, e );
+                }
+                else
+                {
+                    getLogger().warn( "Failed to retrieve environment variables, cannot search for " + command );
+                }
+            }
+        }
+
+        if ( executable == null )
+        {
+            executable = command;
+        }
+
+        return executable;
+    }
+
+    /**
+     * Finds the specified command in any of the given sub directories of the specified JDK/JRE home directory.
+     *
+     * @param command The command to find, must not be <code>null</code>.
+     * @param homeDir The home directory to search in, may be <code>null</code>.
+     * @param subDirs The sub directories of the home directory to search in, must not be <code>null</code>.
+     * @return The (absolute) path to the command if found, <code>null</code> otherwise.
+     */
+    protected String findExecutable( String command, String homeDir, String[] subDirs )
+    {
+        if ( StringUtils.isNotEmpty( homeDir ) )
+        {
+            for ( int i = 0; i < subDirs.length; i++ )
+            {
+                File file = new File( new File( homeDir, subDirs[i] ), command );
+
+                if ( file.isFile() )
+                {
+                    return file.getAbsolutePath();
+                }
+            }
+        }
+
+        return null;
+    }
+}

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/DefaultJarSigner.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/DefaultJarSigner.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/DefaultJarSignerResult.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/DefaultJarSignerResult.java?rev=1190297&view=auto
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/DefaultJarSignerResult.java (added)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/DefaultJarSignerResult.java Fri Oct 28 13:18:44 2011
@@ -0,0 +1,77 @@
+package org.apache.maven.shared.jarsigner;
+
+import org.codehaus.plexus.util.cli.CommandLineException;
+import org.codehaus.plexus.util.cli.Commandline;
+
+/**
+ * Describes the result of a JarSigner invocation.
+ *
+ * @author tchemit <ch...@codelutin.com>
+ * @version $Id$
+ * @since 1.0
+ */
+public class DefaultJarSignerResult
+    implements JarSignerResult
+{
+
+    /**
+     * The exception that prevented to execute the command line, will be <code>null</code> if jarSigner could be
+     * successfully started.
+     */
+    private CommandLineException executionException;
+
+    /**
+     * The exit code reported by the Maven invocation.
+     */
+    private int exitCode = Integer.MIN_VALUE;
+
+    private Commandline commandline;
+
+    /**
+     * Creates a new invocation result
+     */
+    DefaultJarSignerResult()
+    {
+        // hide constructor
+    }
+
+    public int getExitCode()
+    {
+        return exitCode;
+    }
+
+    public Commandline getCommandline()
+    {
+        return commandline;
+    }
+
+    public CommandLineException getExecutionException()
+    {
+        return executionException;
+    }
+
+    /**
+     * Sets the exit code reported by the Jarsigner invocation.
+     *
+     * @param exitCode The exit code reported by the JarSigner invocation.
+     */
+    void setExitCode( int exitCode )
+    {
+        this.exitCode = exitCode;
+    }
+
+    /**
+     * Sets the exception that prevented to execute the command line.
+     *
+     * @param executionException The exception that prevented to execute the command line, may be <code>null</code>.
+     */
+    void setExecutionException( CommandLineException executionException )
+    {
+        this.executionException = executionException;
+    }
+
+    void setCommandline( Commandline commandline )
+    {
+        this.commandline = commandline;
+    }
+}

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/DefaultJarSignerResult.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/DefaultJarSignerResult.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSigner.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSigner.java?rev=1190297&view=auto
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSigner.java (added)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSigner.java Fri Oct 28 13:18:44 2011
@@ -0,0 +1,23 @@
+package org.apache.maven.shared.jarsigner;
+
+/**
+ * Provides a facade to invoke JarSigner tool.
+ *
+ * @author tchemit <ch...@codelutin.com>
+ * @version $Id$
+ * @since 1.0
+ */
+public interface JarSigner
+{
+
+    /**
+     * Executes JarSigner tool using the parameters specified by the given invocation request.
+     *
+     * @param request The invocation request to execute, must not be <code>null</code>.
+     * @return The result of the JarSigner invocation, never <code>null</code>.
+     * @throws JarSignerException if something fails while init the command
+     */
+    JarSignerResult execute( JarSignerRequest request )
+        throws JarSignerException;
+
+}

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSigner.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSigner.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerCommandLineBuilder.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerCommandLineBuilder.java?rev=1190297&view=auto
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerCommandLineBuilder.java (added)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerCommandLineBuilder.java Fri Oct 28 13:18:44 2011
@@ -0,0 +1,177 @@
+package org.apache.maven.shared.jarsigner;
+
+import org.codehaus.plexus.logging.Logger;
+import org.codehaus.plexus.logging.console.ConsoleLogger;
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.cli.Commandline;
+
+import java.io.IOException;
+
+/**
+ * To build the command line for a given {@link JarSignerRequest}.
+ *
+ * @author tchemit <ch...@codelutin.com>
+ * @version $Id$
+ * @since 1.0
+ */
+public class JarSignerCommandLineBuilder
+{
+    private static final Logger DEFAULT_LOGGER = new ConsoleLogger( 0, JarSignerCommandLineBuilder.class.getName() );
+
+    private Logger logger = DEFAULT_LOGGER;
+
+    private String jarSignerFile;
+
+    public Commandline build( JarSignerRequest request )
+        throws CommandLineConfigurationException
+    {
+        try
+        {
+            checkRequiredState();
+        }
+        catch ( IOException e )
+        {
+            throw new CommandLineConfigurationException( e.getMessage(), e );
+        }
+
+        Commandline cli = new Commandline();
+
+        cli.setExecutable( jarSignerFile );
+
+        cli.setWorkingDirectory( request.getWorkingDirectory() );
+
+        if ( request.isVerbose() )
+        {
+            cli.createArg().setValue( "-verbose" );
+        }
+
+        String maxMemory = request.getMaxMemory();
+        if ( StringUtils.isNotEmpty( maxMemory ) )
+        {
+            cli.createArg().setValue( "-J-Xmx" + maxMemory );
+        }
+
+        String[] arguments = request.getArguments();
+        if ( arguments != null )
+        {
+            cli.addArguments( arguments );
+        }
+
+        if ( request instanceof JarSignerSignRequest )
+        {
+            build( (JarSignerSignRequest) request, cli );
+        }
+
+        if ( request instanceof JarSignerVerifyRequest )
+        {
+            build( (JarSignerVerifyRequest) request, cli );
+        }
+
+        return cli;
+    }
+
+    public void setLogger( Logger logger )
+    {
+        this.logger = logger;
+    }
+
+    public void setJarSignerFile( String jarSignerFile )
+    {
+        this.jarSignerFile = jarSignerFile;
+    }
+
+    protected void checkRequiredState()
+        throws IOException
+    {
+        if ( logger == null )
+        {
+            throw new IllegalStateException( "A logger instance is required." );
+        }
+
+        if ( jarSignerFile == null )
+        {
+            throw new IllegalStateException( "A jarSigner file is required." );
+        }
+    }
+
+    protected void build( JarSignerSignRequest request, Commandline cli )
+    {
+        String keystore = request.getKeystore();
+        if ( !StringUtils.isEmpty( keystore ) )
+        {
+            cli.createArg().setValue( "-keystore" );
+            cli.createArg().setValue( keystore );
+        }
+
+        String storepass = request.getStorepass();
+        if ( !StringUtils.isEmpty( storepass ) )
+        {
+            cli.createArg().setValue( "-storepass" );
+            cli.createArg().setValue( storepass );
+        }
+
+        String keypass = request.getKeypass();
+        if ( !StringUtils.isEmpty( keypass ) )
+        {
+            cli.createArg().setValue( "-keypass" );
+            cli.createArg().setValue( keypass );
+        }
+
+        String storetype = request.getStoretype();
+        if ( !StringUtils.isEmpty( storetype ) )
+        {
+            cli.createArg().setValue( "-storetype" );
+            cli.createArg().setValue( storetype );
+        }
+
+        String providerName = request.getProviderName();
+        if ( !StringUtils.isEmpty( providerName ) )
+        {
+            cli.createArg().setValue( "-providerName" );
+            cli.createArg().setValue( providerName );
+        }
+
+        String providerClass = request.getProviderClass();
+        if ( !StringUtils.isEmpty( providerClass ) )
+        {
+            cli.createArg().setValue( "-providerClass" );
+            cli.createArg().setValue( providerClass );
+        }
+
+        String providerArg = request.getProviderArg();
+        if ( !StringUtils.isEmpty( providerArg ) )
+        {
+            cli.createArg().setValue( "-providerArg" );
+            cli.createArg().setValue( providerArg );
+        }
+
+        String sigfile = request.getSigfile();
+        if ( !StringUtils.isEmpty( sigfile ) )
+        {
+            cli.createArg().setValue( "-sigfile" );
+            cli.createArg().setValue( sigfile );
+        }
+
+        cli.createArg().setFile( request.getArchive() );
+
+        String alias = request.getAlias();
+        if ( !StringUtils.isEmpty( alias ) )
+        {
+            cli.createArg().setValue( alias );
+        }
+    }
+
+    protected Commandline build( JarSignerVerifyRequest request, Commandline cli )
+        throws CommandLineConfigurationException
+    {
+        cli.createArg( true ).setValue( "-verify" );
+
+        if ( request.isCerts() )
+        {
+            cli.createArg().setValue( "-certs" );
+        }
+
+        cli.createArg().setFile( request.getArchive() );
+        return cli;
+    }
+}

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerCommandLineBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerCommandLineBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerException.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerException.java?rev=1190297&view=auto
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerException.java (added)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerException.java Fri Oct 28 13:18:44 2011
@@ -0,0 +1,40 @@
+package org.apache.maven.shared.jarsigner;
+
+/**
+ * Signals an error during the construction of the command line used to invoke jar signer, e.g. illegal invocation arguments.
+ * This should not be confused with a failure of the invoked JarSigner build itself which will be reported by means of a
+ * non-zero exit code.
+ *
+ * @author tchemit <ch...@codelutin.com>
+ * @version $Id$
+ * @see JarSignerResult#getExitCode()
+ * @since 1.0
+ */
+public class JarSignerException
+    extends Exception
+{
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * Creates a new exception using the specified detail message and cause.
+     *
+     * @param message The detail message for this exception, may be <code>null</code>.
+     * @param cause   The nested exception, may be <code>null</code>.
+     */
+    public JarSignerException( String message, Throwable cause )
+    {
+        super( message, cause );
+    }
+
+    /**
+     * Creates a new exception using the specified detail message.
+     *
+     * @param message The detail message for this exception, may be <code>null</code>.
+     */
+    public JarSignerException( String message )
+    {
+        super( message );
+    }
+
+}

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerException.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerRequest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerRequest.java?rev=1190297&view=auto
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerRequest.java (added)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerRequest.java Fri Oct 28 13:18:44 2011
@@ -0,0 +1,33 @@
+package org.apache.maven.shared.jarsigner;
+
+import java.io.File;
+
+/**
+ * Specifies the common parameters used to control a JarSigner tool invocation.
+ *
+ * @author tchemit <ch...@codelutin.com>
+ * @version $Id$
+ * @since 1.0
+ */
+public interface JarSignerRequest
+{
+    boolean isVerbose();
+
+    String getMaxMemory();
+
+    String[] getArguments();
+
+    File getWorkingDirectory();
+
+    File getArchive();
+
+    void setVerbose( boolean verbose );
+
+    void setMaxMemory( String maxMemory );
+
+    void setArguments( String[] arguments );
+
+    void setWorkingDirectory( File workingDirectory );
+
+    void setArchive( File archive );
+}

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerRequest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerRequest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerResult.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerResult.java?rev=1190297&view=auto
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerResult.java (added)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerResult.java Fri Oct 28 13:18:44 2011
@@ -0,0 +1,38 @@
+package org.apache.maven.shared.jarsigner;
+
+import org.codehaus.plexus.util.cli.CommandLineException;
+import org.codehaus.plexus.util.cli.Commandline;
+
+/**
+ * Describes the result of a JarSigner invocation.
+ *
+ * @author tchemit <ch...@codelutin.com>
+ * @version $Id$
+ * @since 1.0
+ */
+public interface JarSignerResult
+{
+
+    /**
+     * Gets the command line used.
+     *
+     * @return The command line used
+     */
+    Commandline getCommandline();
+
+    /**
+     * Gets the exception that possibly occurred during the execution of the command line.
+     *
+     * @return The exception that prevented to invoke Jarsigner or <code>null</code> if the command line was successfully
+     *         processed by the operating system.
+     */
+    CommandLineException getExecutionException();
+
+    /**
+     * Gets the exit code from the JarSigner invocation. A non-zero value indicates a build failure. <strong>Note:</strong>
+     * This value is undefined if {@link #getExecutionException()} reports an exception.
+     *
+     * @return The exit code from the Jarsigner invocation.
+     */
+    int getExitCode();
+}

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerResult.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerResult.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerSignRequest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerSignRequest.java?rev=1190297&view=auto
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerSignRequest.java (added)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerSignRequest.java Fri Oct 28 13:18:44 2011
@@ -0,0 +1,147 @@
+package org.apache.maven.shared.jarsigner;
+
+/**
+ * Specifies the parameters used to control a jar signer sign operation invocation.
+ *
+ * @author tchemit <ch...@codelutin.com>
+ * @version $Id$
+ * @since 1.0
+ */
+public class JarSignerSignRequest
+    extends AbstractJarSignerRequest
+{
+    /**
+     * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
+     */
+    private String keystore;
+
+    /**
+     * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
+     */
+    private String storepass;
+
+    /**
+     * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
+     */
+    private String keypass;
+
+    /**
+     * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
+     */
+    private String sigfile;
+
+    /**
+     * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
+     */
+    private String storetype;
+
+    /**
+     * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
+     */
+    private String providerName;
+
+    /**
+     * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
+     */
+    private String providerClass;
+
+    /**
+     * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
+     */
+    private String providerArg;
+
+    /**
+     * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
+     */
+    private String alias;
+
+    public String getKeystore()
+    {
+        return keystore;
+    }
+
+    public String getStorepass()
+    {
+        return storepass;
+    }
+
+    public String getKeypass()
+    {
+        return keypass;
+    }
+
+    public String getSigfile()
+    {
+        return sigfile;
+    }
+
+    public String getStoretype()
+    {
+        return storetype;
+    }
+
+    public String getProviderName()
+    {
+        return providerName;
+    }
+
+    public String getProviderClass()
+    {
+        return providerClass;
+    }
+
+    public String getProviderArg()
+    {
+        return providerArg;
+    }
+
+    public String getAlias()
+    {
+        return alias;
+    }
+
+    public void setKeystore( String keystore )
+    {
+        this.keystore = keystore;
+    }
+
+    public void setStorepass( String storepass )
+    {
+        this.storepass = storepass;
+    }
+
+    public void setKeypass( String keypass )
+    {
+        this.keypass = keypass;
+    }
+
+    public void setSigfile( String sigfile )
+    {
+        this.sigfile = sigfile;
+    }
+
+    public void setStoretype( String storetype )
+    {
+        this.storetype = storetype;
+    }
+
+    public void setProviderName( String providerName )
+    {
+        this.providerName = providerName;
+    }
+
+    public void setProviderClass( String providerClass )
+    {
+        this.providerClass = providerClass;
+    }
+
+    public void setProviderArg( String providerArg )
+    {
+        this.providerArg = providerArg;
+    }
+
+    public void setAlias( String alias )
+    {
+        this.alias = alias;
+    }
+}

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerSignRequest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerSignRequest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerUtil.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerUtil.java?rev=1190297&view=auto
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerUtil.java (added)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerUtil.java Fri Oct 28 13:18:44 2011
@@ -0,0 +1,130 @@
+package org.apache.maven.shared.jarsigner;
+
+import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.IOUtil;
+
+import java.io.*;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+import java.util.zip.ZipOutputStream;
+
+/**
+ * Usuful methods.
+ *
+ * @author tchemit <ch...@codelutin.com>
+ * @version $Id$
+ * @since 1.0
+ */
+public class JarSignerUtil
+{
+
+    private JarSignerUtil()
+    {
+        // static class
+    }
+
+    /**
+     * Checks whether the specified file is a JAR file. For our purposes, a ZIP file is a ZIP stream with at least one
+     * entry.
+     *
+     * @param file The file to check, must not be <code>null</code>.
+     * @return <code>true</code> if the file looks like a ZIP file, <code>false</code> otherwise.
+     */
+    public static boolean isZipFile( final File file )
+    {
+        try
+        {
+            ZipInputStream zis = new ZipInputStream( new FileInputStream( file ) );
+            try
+            {
+                return zis.getNextEntry() != null;
+            }
+            finally
+            {
+                zis.close();
+            }
+        }
+        catch ( Exception e )
+        {
+            // ignore, will fail below
+        }
+
+        return false;
+    }
+
+    /**
+     * Removes any existing signatures from the specified JAR file. We will stream from the input JAR directly to the
+     * output JAR to retain as much metadata from the original JAR as possible.
+     *
+     * @param jarFile The JAR file to unsign, must not be <code>null</code>.
+     * @throws java.io.IOException
+     */
+    public static void unsignArchive( File jarFile )
+        throws IOException
+    {
+
+        File unsignedFile = new File( jarFile.getAbsolutePath() + ".unsigned" );
+
+        ZipInputStream zis = null;
+        ZipOutputStream zos = null;
+        try
+        {
+            zis = new ZipInputStream( new BufferedInputStream( new FileInputStream( jarFile ) ) );
+            zos = new ZipOutputStream( new BufferedOutputStream( new FileOutputStream( unsignedFile ) ) );
+
+            for ( ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry() )
+            {
+                if ( isSignatureFile( ze.getName() ) )
+                {
+
+                    continue;
+                }
+
+                zos.putNextEntry( ze );
+
+                IOUtil.copy( zis, zos );
+            }
+
+        }
+        finally
+        {
+            IOUtil.close( zis );
+            IOUtil.close( zos );
+        }
+
+        FileUtils.rename( unsignedFile, jarFile );
+
+    }
+
+    /**
+     * Checks whether the specified JAR file entry denotes a signature-related file, i.e. matches
+     * <code>META-INF/*.SF</code>, <code>META-INF/*.DSA</code> or <code>META-INF/*.RSA</code>.
+     *
+     * @param entryName The name of the JAR file entry to check, must not be <code>null</code>.
+     * @return <code>true</code> if the entry is related to a signature, <code>false</code> otherwise.
+     */
+    private static boolean isSignatureFile( String entryName )
+    {
+        if ( entryName.regionMatches( true, 0, "META-INF", 0, 8 ) )
+        {
+            entryName = entryName.replace( '\\', '/' );
+
+            if ( entryName.indexOf( '/' ) == 8 && entryName.lastIndexOf( '/' ) == 8 )
+            {
+                if ( entryName.regionMatches( true, entryName.length() - 3, ".SF", 0, 3 ) )
+                {
+                    return true;
+                }
+                if ( entryName.regionMatches( true, entryName.length() - 4, ".DSA", 0, 4 ) )
+                {
+                    return true;
+                }
+                if ( entryName.regionMatches( true, entryName.length() - 4, ".RSA", 0, 4 ) )
+                {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+}

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerUtil.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerVerifyRequest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerVerifyRequest.java?rev=1190297&view=auto
==============================================================================
--- maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerVerifyRequest.java (added)
+++ maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerVerifyRequest.java Fri Oct 28 13:18:44 2011
@@ -0,0 +1,27 @@
+package org.apache.maven.shared.jarsigner;
+
+/**
+ * Specifies the parameters used to control a jar signer verify operation invocation.
+ *
+ * @author tchemit <ch...@codelutin.com>
+ * @version $Id$
+ * @since 1.0
+ */
+public class JarSignerVerifyRequest
+    extends AbstractJarSignerRequest
+{
+    /**
+     * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
+     */
+    private boolean certs;
+
+    public boolean isCerts()
+    {
+        return certs;
+    }
+
+    public void setCerts( boolean certs )
+    {
+        this.certs = certs;
+    }
+}

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerVerifyRequest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-jarsigner-plugin/src/main/java/org/apache/maven/shared/jarsigner/JarSignerVerifyRequest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision