11/*
2- * Copyright (c) 2015, 2021 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2015, 2023 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
4747import java .util .Date ;
4848import java .util .List ;
4949import java .util .Properties ;
50+ import java .util .StringTokenizer ;
5051import java .util .Timer ;
5152import java .util .TimerTask ;
5253import java .util .concurrent .TimeUnit ;
@@ -76,9 +77,10 @@ public ActionHelper(Path workDir, String prefix, Properties properties,
7677 public List <Long > getChildren (HtmlSection section , long pid ) {
7778 String pidStr = "" + pid ;
7879 ProcessBuilder pb = getChildren .prepareProcess (section , this , pidStr );
79- PrintWriter log = getChildren .getSection (section ).getWriter ();
80+ HtmlSection childrenSection = getChildren .getSection (section );
81+ PrintWriter log = childrenSection .getWriter ();
8082 CharArrayWriter writer = new CharArrayWriter ();
81- ExitCode code = run (log , writer , pb , getChildren .getParameters ());
83+ ExitCode code = run (childrenSection , log , writer , pb , getChildren .getParameters ());
8284 Reader output = new CharArrayReader (writer .toCharArray ());
8385
8486 if (!ExitCode .OK .equals (code )) {
@@ -154,8 +156,8 @@ private void addJdks(Path[] jdkPaths) {
154156 }
155157 }
156158
157- private ExitCode run (PrintWriter log , Writer out , ProcessBuilder pb ,
158- ActionParameters params ) {
159+ private ExitCode run (HtmlSection section , PrintWriter log , Writer out , ProcessBuilder pb ,
160+ ActionParameters params ) {
159161 char [] lineChars = new char [40 ];
160162 Arrays .fill (lineChars , '-' );
161163 String line = new String (lineChars );
@@ -199,6 +201,19 @@ private ExitCode run(PrintWriter log, Writer out, ProcessBuilder pb,
199201 log .printf ("%s%n[%tF %<tT] exit code: %d time: %d ms%n%1$s%n" ,
200202 line , new Date (), result .value ,
201203 TimeUnit .NANOSECONDS .toMillis (stopwatch .getElapsedTimeNs ()));
204+ // upon successful completion of the action, generate links to any successArtifacts
205+ // that have been declared for this action
206+ if (ExitCode .OK .equals (result ) && params .successArtifacts != null ) {
207+ final StringTokenizer t = new StringTokenizer (params .successArtifacts , "," );
208+ while (t .hasMoreTokens ()) {
209+ final String artifactPath = t .nextToken ().trim ();
210+ if (artifactPath .isEmpty ()) {
211+ continue ;
212+ }
213+ // create a link to the artifact
214+ section .createLink (artifactPath , artifactPath );
215+ }
216+ }
202217 return result ;
203218 }
204219
@@ -286,11 +301,21 @@ private void exec(HtmlSection section, ProcessBuilder process,
286301 }
287302 PrintWriter sectionWriter = section .getWriter ();
288303 if (params .repeat > 1 ) {
304+ // hold on to the original command and successArtifacts values which potentially
305+ // contain the %iterCount token, since we need to replace it with different value
306+ // on each iteration
307+ String originalSuccessArtifacts = params .successArtifacts ;
308+ List <String > originalCommand = process .command ();
289309 for (int i = 0 , n = params .repeat ; i < n ; ++i ) {
290310 HtmlSection iteration = section .createChildren (
291311 String .format ("iteration_%d" , i ));
292312 PrintWriter writer = iteration .getWriter ();
293- ExitCode exitCode = run (writer , writer , process , params );
313+ // use the original values with the token (if any)
314+ params .successArtifacts = originalSuccessArtifacts ;
315+ process .command (originalCommand );
316+ // replace the %iterCount token (if any)
317+ prepareIteration (i , process , params );
318+ ExitCode exitCode = run (section , writer , writer , process , params );
294319 if (params .stopOnError && !ExitCode .OK .equals (exitCode )) {
295320 sectionWriter .printf (
296321 "ERROR: non zero exit code[%d] -- break." ,
@@ -309,7 +334,26 @@ private void exec(HtmlSection section, ProcessBuilder process,
309334 }
310335 }
311336 } else {
312- run (section .getWriter (), section .getWriter (), process , params );
337+ prepareIteration (0 , process , params );
338+ run (section , section .getWriter (), section .getWriter (), process , params );
339+ }
340+ }
341+
342+ // replaces the occurrences of %iterCount from the process builder command/arguments
343+ // and the action params' "successArtifacts" paths, with the iteration count
344+ private void prepareIteration (int iterationCount , ProcessBuilder pb ,
345+ ActionParameters actionParams ) {
346+ List <String > command = new ArrayList <>();
347+ for (String arg : pb .command ()) {
348+ arg = arg .replaceAll ("%iterCount" , String .valueOf (iterationCount )) ;
349+ command .add (arg );
350+ }
351+ pb .command (command );
352+
353+ String successArtifacts = actionParams .successArtifacts ;
354+ if (successArtifacts != null ) {
355+ actionParams .successArtifacts = successArtifacts .replaceAll ("%iterCount" ,
356+ String .valueOf (iterationCount ));
313357 }
314358 }
315359
0 commit comments