1212import com .google .inject .Injector ;
1313import com .google .inject .Key ;
1414import com .google .inject .name .Names ;
15- import org .seedstack .seed .cli .spi .CliOption ;
16- import org .seedstack .seed .cli .spi .CommandLineHandler ;
1715import org .apache .commons .lang .StringUtils ;
16+ import org .seedstack .seed .cli .api .CliCommand ;
17+ import org .seedstack .seed .cli .api .CliOption ;
18+ import org .seedstack .seed .cli .api .CommandLineHandler ;
1819import org .slf4j .Logger ;
1920import org .slf4j .LoggerFactory ;
2021import org .springframework .batch .core .BatchStatus ;
3132import java .util .Map ;
3233
3334/**
34- * This {@link CommandLineHandler} can launch Spring Batch jobs with the following arguments:
35+ * This {@link CommandLineHandler} is launched with the run-job command. It support the following arguments:
3536 *
3637 * <ul>
37- * <li>-j job-name</li>
38- * <li>-l job-launcher-name</li>
39- * <li>-P job-parameter-name=job-parameter-value</li>
38+ * <li>-j job-name</li>
39+ * <li>-l job-launcher-name</li>
40+ * <li>-P job-parameter-name=job-parameter-value</li>
4041 * </ul>
4142 *
4243 * @author epo.jemba@ext.mpsa.com
44+ * @author adrien.lauer@mpsa.com
4345 */
46+ @ CliCommand (value = "run-job" , description = "Launch Spring Batch jobs" )
4447public class SpringBatchCommandLineHandler implements CommandLineHandler {
4548
4649 private static final String DEFAULT_JOB_LAUNCHER_NAME = "jobLauncher" ;
@@ -49,23 +52,18 @@ public class SpringBatchCommandLineHandler implements CommandLineHandler {
4952
5053 private static final Logger LOGGER = LoggerFactory .getLogger (SpringBatchCommandLineHandler .class );
5154
52- @ CliOption (opt = "l" , longOpt = "jobLauncher" , optionalArg = true , description = "Spring Job Launcher Name " )
55+ @ CliOption (name = "l" , longName = "jobLauncher" , valueCount = 1 , description = "Job launcher name to use " )
5356 String optionJobLauncherName ;
5457
55- @ CliOption (opt = "j" , longOpt = "job" , optionalArg = true , description = "Spring Job Name " )
58+ @ CliOption (name = "j" , longName = "job" , valueCount = 1 , description = "Job name to launch " )
5659 String optionJobName ;
5760
58- @ CliOption (opt = "P" , longOpt = "jobParameter" , args = true , valueSeparator = '=' , description = "Spring Job parameter" )
59- String [] optionJobParameters ;
61+ @ CliOption (name = "P" , longName = "jobParameter" , valueCount = - 1 , description = "A job parameter" )
62+ Map < String , String > optionJobParameters ;
6063
6164 @ Inject
6265 Injector injector ;
6366
64- @ Override
65- public String name () {
66- return "spring-batch-cli-handler" ;
67- }
68-
6967 @ Override
7068 public Integer call () throws JobExecutionException {
7169 Integer batchExitStatus = 0 ;
@@ -89,25 +87,17 @@ public Integer call() throws JobExecutionException {
8987 }
9088
9189 private JobParameters getJobParameters () {
92- JobParameters jobParameters ;
93- Map <String , JobParameter > mapJobParameter = optionParameters (optionJobParameters );
94- if (mapJobParameter != null && !mapJobParameter .isEmpty ()) {
95- jobParameters = new JobParameters (mapJobParameter );
96- } else {
97- jobParameters = new JobParameters ();
98- }
99- return jobParameters ;
100- }
90+ if (optionJobParameters != null && !optionJobParameters .isEmpty ()) {
91+ Map <String , JobParameter > mapJobParameter = new HashMap <String , JobParameter >();
10192
102- private Map <String , JobParameter > optionParameters (String [] optJobParameters ) {
103- Map <String , JobParameter > mapJobParameter = null ;
104- if (optJobParameters != null && optJobParameters .length > 0 && (optJobParameters .length % 2 ) == 0 ) {
105- mapJobParameter = new HashMap <String , JobParameter >();
106- for (int i = 0 ; i < optJobParameters .length ; i = i + 2 ) {
107- mapJobParameter .put (optJobParameters [i ], new JobParameter (optJobParameters [i + 1 ]));
93+ for (Map .Entry <String , String > stringJobParameterEntry : optionJobParameters .entrySet ()) {
94+ mapJobParameter .put (stringJobParameterEntry .getKey (), new JobParameter (stringJobParameterEntry .getValue ()));
10895 }
96+
97+ return new JobParameters (mapJobParameter );
98+ } else {
99+ return new JobParameters ();
109100 }
110- return mapJobParameter ;
111101 }
112102
113103 private Job getJob () {
0 commit comments