/**
* StepLaunchApplication.java
*
* A plan step to lauch an external, third-party, application.
*
* @author Wahyu Yoga Pratama (wahyu@stee.stengg.com)
*
* @created Nov 12, 2009
* @version $$
*
* HISTORY:
* - 2009/11/12 Created.
*
* TODO:
*
*/
package tcg.plan.step;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import tcg.plan.PlanStep;
import tcg.common.LoggerManager;
public class StepLaunchApplication extends PlanStep
{
private static String VERSION = "01.01 (20091112)";
String exeFile = "";
//we need to provide main class for two purposes:
//- to print version no and usage
//- to provide legacy way of running step, which is to execute the class on its own JVM
public static void main(String[] args)
{
//if no argument is specified, print usage
if (args.length == 0)
{
printUsage();
return;
}
//if arguments has "--version" or "--help", print version number and quit
for (int i=0; i<args.length; i++)
{
if (args[i].equalsIgnoreCase("--version") || args[i].equalsIgnoreCase("-V"))
{
printVersion();
return;
}
else if (args[i].equalsIgnoreCase("--help") || args[i].equalsIgnoreCase("-h"))
{
printUsage();
return;
}
}
//create the step instance
StepLaunchApplication instance = new StepLaunchApplication();
//reset the logger
instance.logger = LoggerManager.getLogger(instance.getClass().toString());
//execute
instance.execute(args);
}
@Override
protected boolean parseArgument2(String[] args)
{
CommandLineParser cmdLnParser = new BasicParser();
CommandLine cmdLn = null;
Options optArgs = new Options();
Option cmdLineArg1 =
new Option("exeFile", "Target Execution File (required)");
cmdLineArg1.setRequired(true);
cmdLineArg1.setArgs(1);
cmdLineArg1.setArgName("execution-file");
optArgs.addOption(cmdLineArg1);
//parse the arguments
try
{
cmdLn = cmdLnParser.parse(optArgs, args);
}
catch (ParseException pe)
{
logger.error("Can not parse argument: " + pe.getLocalizedMessage());
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "Parameters:", optArgs );
return false;
}
//get the valud
exeFile = cmdLn.getOptionValue("exeFile");
return true;
}
@Override
protected boolean init()
{
//nothing to do here
return true;
}
@Override
protected boolean close()
{
//nothing to do here
return true;
}
@Override
protected boolean running()
{
//validation
if (exeFile.equalsIgnoreCase(""))
{
logger.warn("Target execution is never specified!");
return false;
}
//execute the application
try
{
Runtime.getRuntime().exec(exeFile);
Thread.sleep(1000);
}
catch (Exception ex)
{
logger.error("Failed to execute application: " + exeFile + ". " +
"Exception = " + ex.toString());
return false;
}
return true;
}
@Override
protected void terminate()
{
//This will be called when PlanMaster wants to abruptly terminate the step
//TODO
}
private static void printVersion()
{
//print version here
System.out.println("Plan Launch Application Step Version " + VERSION);
}
private static void printUsage()
{
//print usage here
System.out.println("Plan Launch Application Step Version " + VERSION);
System.out.println("");
System.out.println("Command Line Parameters: ");
System.out.println(" --plan-execution-id <plan-id> Plan Execution ID");
System.out.println(" --executed-node-id <node-id> Executed Node ID");
System.out.println(" --port-no <port-no> Plan Master Port No");
System.out.println(" --agent-port-no <port-no> Plan Agent Port No");
System.out.println(" --execution-file <file-name> Target Execution File");
System.out.println(" -h | --help Print out this help");
System.out.println(" -v | --version Print out program version");
System.out.println("");
}
}