001package org.kuali.ole.sys.context;
002
003import org.apache.commons.lang.StringUtils;
004
005/**
006 * BatchStepTriggerParameters provides parsing and error checking for the arguments needed by the BatchStepTrigger. 
007 * 
008 *  stepName:                args[0]:  String- a comma delimited list of step names (no spaces)
009 *  jobName:                 args[1]:  String- the name of the job in which the steps are being executed
010 *  stepIndex:               args[2]:  String- the index of the step in the job
011 *  batchContainerDirectory: args[3]:  String- the path to the directory in which the semaphore files are created. 
012 *                                             This value needs to match 'staging.directory.sys.batchContainer' in iu/build/configuration.properties
013 *  sleepInterval:           args[4]:  String- the amount of time (in seconds) to wait before looking for the result file from BatchContainerStep  
014 */
015public class BatchStepTriggerParameters {
016        
017        private String[] stepNames;
018        private String jobName;
019        private int stepIndex;
020        private long sleepInterval;
021        
022        private BatchContainerDirectory batchContainerDirectory;
023        
024        /**
025         * @param args
026         *  stepName:                args[0]:  String- a comma delimited list of step names (no spaces)
027         *  jobName:                 args[1]:  String- the name of the job in which the steps are being executed
028         *  stepIndex:               args[2]:  String- the index of the step in the job
029         *  batchContainerDirectory: args[2]:  String- the path to the directory in which the semaphore files are created. 
030         *                                             This value needs to match 'staging.directory.sys.batchContainer' in iu/build/configuration.properties
031         *  sleepInterval:           args[3]:  String- the amount of time (in seconds) to wait before looking for the result file from BatchContainerStep 
032         */
033        protected BatchStepTriggerParameters(String[] args) {
034        if (args.length < 1) {
035            System.err.println("ERROR: You must pass the name of the step to run on the command line.");
036            System.exit(8);
037        } else if (args.length < 2) {
038                System.err.println("ERROR: You must pass the name of the job to run on the command line.");
039                System.exit(8);
040        } else if (args.length < 3) {
041            System.err.println("ERROR: You must pass the index of the step in the job on the command line.");
042            System.exit(8);
043        } else if (args.length < 4) {
044            System.err.println("ERROR: You must pass the path of the directory in which to write and read result files on the command line.");
045            System.exit(8);             
046        } else if (args.length < 5) {
047            System.err.println("ERROR: You must pass the amount of time (in seconds) to sleep while waiting for the step to run on the command line.");
048            System.exit(8);             
049        }
050        
051        if (args[0].indexOf(",") > 0) {
052            stepNames = StringUtils.split(args[0], ",");
053        }
054        else {
055            stepNames = new String[] { args[0] };
056        }
057        
058        jobName = args[1];            
059        stepIndex = Integer.parseInt(args[2]);
060        String directory = args[3];           
061        sleepInterval = Long.parseLong(args[4]) * 1000;        
062        batchContainerDirectory = new BatchContainerDirectory(directory);
063        }
064        
065        /**
066         * @return a comma delimited list of step names (no spaces)
067         */
068        protected String[] getStepNames() {
069                return stepNames;
070        }
071        
072        /**
073         * @return the name of the job in which the step(s) are being executed
074         */
075        protected String getJobName() {
076                return jobName;
077        }
078        
079        /**
080         * @return the index of the step in the job
081         */
082        protected int getStepIndex() {
083            return stepIndex;
084        }
085        
086        /**
087         * @return the amount of time to sleep (in seconds) while waiting for a result file from the BatchContainerStep
088         */
089        protected long getSleepInterval() {
090                return sleepInterval;
091        }
092        
093        /**
094         * @return the directory in which the semaphore files are located
095         */
096        protected BatchContainerDirectory getBatchContainerDirectory() {
097                return batchContainerDirectory;
098        }
099}