schedin commented on code in PR #13:
URL: 
https://github.com/apache/maven-jarsigner-plugin/pull/13#discussion_r1420597254


##########
src/main/java/org/apache/maven/plugins/jarsigner/JarsignerSignMojo.java:
##########
@@ -126,4 +155,62 @@ protected JarSignerRequest createRequest(File archive) 
throws MojoExecutionExcep
         request.setKeypass(decrypt(keypass));
         return request;
     }
+
+    /**
+     * {@inheritDoc}
+     *
+     * Will retry signing up to maxTries times if it fails.
+     *
+     * @throws MojoExecutionException If all signing attempts fail.
+     */
+    @Override
+    protected void executeJarSigner(JarSigner jarSigner, JarSignerRequest 
request)
+            throws JavaToolException, MojoExecutionException {
+        Commandline commandLine = null;
+        int resultCode = 0;
+        for (int attempt = 0; attempt < maxTries; attempt++) {
+            JavaToolResult result = jarSigner.execute(request);
+            resultCode = result.getExitCode();
+            commandLine = result.getCommandline();
+            if (resultCode == 0) {
+                return;
+            }
+            if (attempt < maxTries - 1) { // If not last attempt
+                waitStrategy.waitAfterFailure(attempt, 
Duration.ofSeconds(maxRetryDelaySeconds));
+            }
+        }
+        throw new MojoExecutionException(getMessage("failure", 
getCommandlineInfo(commandLine), resultCode));
+    }
+
+    /** Set current WaitStrategy. Package private for testing. */
+    void setWaitStrategy(WaitStrategy waitStrategy) {
+        this.waitStrategy = waitStrategy;
+    }
+
+    /** Wait/sleep after a signing failure before the next re-try should 
happen. */
+    @FunctionalInterface
+    interface WaitStrategy {
+        /**
+         * Will be called after a signing failure, if a re-try is about to 
happen. May as a side effect sleep current
+         * thread for some time.
+         * @param attempt the attempt number (0 is the first).
+         * @param maxRetryDelay The maximum duration to sleep (may be zero).
+         * @throws MojoExecutionException If the sleep was interrupted.
+         */
+        void waitAfterFailure(int attempt, Duration maxRetryDelay) throws 
MojoExecutionException;
+    }
+
+    private void defaultWaitStrategy(int attempt, Duration maxRetryDelay) 
throws MojoExecutionException {
+        long delayMillis = (long) (Duration.ofSeconds(1).toMillis() * 
Math.pow(2, attempt));

Review Comment:
   Refactored method to make it testable. Added test case 
`JarsignerSignMojoRetryTest.testDefaultWaitStrategy()` that tests/proves that 
there are no unexpected overflow/wraparound problems (at least not visible from 
the outside).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to