/**
* SystemCallJob.java
*
* Helper class to run non java program as a cron jobs under Scheduling Agent.
*
* @author Wahyu Yoga Pratama (wahyu@stee.stengg.com)
*
* @created Feb 08, 2006
* @version $$
*
* HISTORY:
* - 2008/02/06 Created.
*
* TODO:
* -
*/
package tcg.scheduling.cronjob;
import org.apache.log4j.Logger;
import org.apache.log4j.NDC;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import tcg.common.util.LoggerWriter;
import tcg.common.util.StreamGobbler;
import tcg.scheduling.SchedulingAgent;
public class SystemCallJob implements Job
{
private static Logger logger = Logger.getLogger(SchedulingAgent.class.getName());
private static LoggerWriter loggerWriter = new LoggerWriter(logger);
private static final String VERSION = "01.00";
public void execute(JobExecutionContext arg0) throws JobExecutionException
{
NDC.push("SystemCallJob");
String name = "", cmd = "";
try
{
//get the job name
name = arg0.getJobDetail().getName();
//get the cmd to execute
cmd = arg0.getTrigger().getJobDataMap().getString("Arguments");
if (cmd == null || cmd.length() == 0)
{
logger.info("Empty or NULL arguments passed as command!");
return;
}
//execute the command
logger.info(name + " Exec: " + cmd);
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), name + " ERROR", logger);
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), name + " OUTPUT", logger);
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
logger.info(name + " ExitValue: " + exitVal);
}
catch (Exception ex)
{
logger.warn(name + " Exception: " + ex.getMessage());
logger.error("--------------------------------------------");
ex.printStackTrace(loggerWriter);
logger.error("--------------------------------------------");
}
NDC.pop();
}
private static void printVersion()
{
System.out.println("Scheduling - Monitor Cron Version " + VERSION);
}
private static final void printUsage()
{
System.out.println("Scheduling - Monitor Cron Version " + VERSION);
System.out.println("");
System.out.println("Command Line Parameters: ");
System.out.println(" -h | --help Print out this help");
System.out.println(" -V | --version Print out program version");
System.out.println("");
System.out.println("It is not supposed to run from command line.");
System.out.println("Instead it should be run from Scheduling Agent.");
System.out.println("");
System.out.println("It is used to run a job that is not a java class.");
System.out.println("This makes possible to run any executable file as a job.");
System.out.println("");
System.out.println("Component Library Information:");
System.out.println("\t - Oracle JDBC Ver. 10.2");
System.out.println("\t - Quartz Scheduler Ver. 1.6.0");
System.out.println("");
System.out.println("Other Component Information:");
System.out.println("");
}
public static void main(String[] args)
{
//if arguments has "--version", 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;
}
}
}
}