package SubmitJava;
/*
 * CondorJob.java
 *
 * Created on January 20, 2007, 7:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

import java.io.*;

/** Holds file information for a job as well as arguments to be passed to individual jobs.
 *  Will generate Condor submit files for the jobs to execute the jobs on remote machines.
 *  The jobs are responsible for creating output files which can be reassembled.
 *
 * @author John Morrow
 */
public class CondorJob {
    private String classToRun;
    private String jarFiles[]; //will be null if no required jar files
    private String inputFiles[]; //will be null if no required input files
    private int jobTotal = 0;
    private String jobFilePrefix = "job";
    
    /**     <p>Creates a new instance of CondorJob. </p>
     *      Takes:</br>
     *      className containing main() method to call.</br>
     *      jars[] containing filenames (UNIX paths!) to required .jar files. Pass null if not required.</br>
     *      files[] containing filenames (UNIX paths!) to required files for computation. Pass null if not required.
     **/ 
    public CondorJob(String className, String jars[], String files[]) {
        classToRun = className;
        if(jars != null) {
            jarFiles = new String[jars.length];
            System.arraycopy(jars, 0, jarFiles, 0, jars.length);
        }
        else
            jarFiles = null;
        if(files != null) {
            inputFiles = new String[files.length];
            System.arraycopy(files, 0, inputFiles, 0, files.length);
        }
        else
            inputFiles = null;
    }
    
    /** Will call the main method of class passed in constructor with arguments. */
    public void addJob(String arguments[]) {
        jobTotal++;
        makeSubmitFile(jobTotal, arguments);
    }
    
    /** Sets the prefix for written job files.  E.g. files written will be <prefix>.<# of job>.</br> 
     *  The default prefix is "job". */
    public void setFilePrefix(String prefix) {
        jobFilePrefix = prefix;
    }
        
    
    /** Writes submit file for Job #jobNum as <prefix>.jobNum containing the following information:</br>
     *  Universe                = java</br>
     *  Executable              = classToRun + ".class"</br>
     *  Arguments               = classToRun args[]</br>
     *  Error                   = logs/error.jobNum</br>
     *  Output                  = logs/output.jobNum</br>
     *  Log                     = logs/log.jobNum</br>
     *  should_transfer_files   = YES</br>
     *  when_to_transfer_output = ON_EXIT</br>
     *  jar_files               = jarFiles[] </br>
     *  transfer_input_files    = inputFiles[]</br>
     *  requirements            = (JavaVersion=="1.5.0_06") && (IsDedicated==TRUE)&& (IsInstructional==FALSE) && (RebootedDaily==FALSE) && (IsComputeCluster==TRUE)</br>
     *  Queue
     **/
    public void makeSubmitFile(int jobNum, String args[]) {
        // Stream to write file
        FileOutputStream fout;
        
        try {
            // Open an output stream
            fout = new FileOutputStream(jobFilePrefix + "." + jobNum);
            // Make it into a PrintStream
            PrintStream pout = new PrintStream(fout);
            
            //do some mundane stuff
            pout.println("Universe                = java");
            pout.println("Executable              = " + classToRun + ".class");
            //pass arguments
            pout.print("Arguments                 = " + classToRun);
            for(int i=0; i<args.length; i++)
                pout.print(" "+args[i]);
            pout.println(); 
            pout.println("Error                   = logs/error." + jobNum);
            pout.println("Output                  = logs/output." + jobNum);
            pout.println("Log                     = logs/log." + jobNum);
            pout.println("should_transfer_files   = YES");
            pout.println("when_to_transfer_output = ON_EXIT");
            //pass input files
            if(inputFiles != null) {
                pout.print("transfer_input_files  =");
                        for(int i=0; i<inputFiles.length; i++)
                            pout.print(" "+inputFiles[i]);
                pout.println();
            }
            
            //pass jar files
            if(jarFiles != null) {
                pout.print("jar_files             =");
                        for(int i=0; i<jarFiles.length; i++)
                            pout.print(" "+jarFiles[i]);
                pout.println();
            }
            
            pout.println("requirements            = (JavaVersion==\"1.5.0_06\") && (IsDedicated==TRUE) && (IsInstructional==FALSE) && (RebootedDaily==FALSE) && (IsComputeCluster==TRUE)");
            pout.println("Queue");
            
            // Close our output stream
            fout.close();
        }
        // Catches any error conditions
        catch (IOException e) {
            System.err.println("Unable to write " + jobFilePrefix + "." + jobNum);
            System.out.println(e.getCause());
            System.exit(-1);
        }
    }

 }
