Class Job

java.lang.Object
edu.rit.pj.job.Job
All Implemented Interfaces:
Externalizable, Serializable, Runnable

public class Job extends Object implements Runnable, Externalizable
Class Job encapsulates a job and its attributes. A job is typically created by a JobGenerator. The job's attributes are:
  • Job number. A JobGenerator assigns a unique job number to each job it creates.
  • Description. An arbitrary string. The Runner program prints the job number and description whenever a job is started.
  • Fully-qualified name of the main program class. To run a job, the main program class's public static void main(String[]) method is called.
  • List of argument strings for the main() method.
  • Standard input redirection. The standard input may be redirected from a file. The job redirects the per-thread standard input stream in class Stdio. For redirection to work, the main() method must use Stdio.in() for the standard input.
  • Standard output redirection. The standard output may be redirected to a file, either overwriting or appending to the file. The job redirects the per-thread standard output stream in class Stdio. For redirection to work, the main() method must use Stdio.out() for the standard output.
  • Standard error redirection. The standard error may be redirected to a file, either overwriting or appending to the file. The standard error may also be redirected to the same place as the standard output. The job redirects the per-thread standard error stream in class Stdio. For redirection to work, the main() method must use Stdio.err() for the standard error.
Version:
09-Oct-2010
Author:
Alan Kaminsky
See Also:
  • Constructor Details

    • Job

      public Job()
      Construct a new uninitialized job. This constructor is for use only by object deserialization.
    • Job

      public Job(int theJobNumber, String theDescription, String theMainClassName)
      Construct a new job. The job has the given job number and description. The job will run the public static void main(String[]) method in the given class.
      Parameters:
      theJobNumber - Job number.
      theDescription - Job description.
      theMainClassName - Fully qualified name of main class.
      Throws:
      NullPointerException - (unchecked exception) Thrown if theMainClassName is null.
  • Method Details

    • getJobNumber

      public int getJobNumber()
      Returns this job's number.
      Returns:
      Job number.
    • getDescription

      public String getDescription()
      Returns this job's description.
      Returns:
      Job description.
    • addArgument

      public void addArgument(String arg)
      Add the given argument string to this job.
      Parameters:
      arg - Argument string.
      Throws:
      NullPointerException - (unchecked exception) Thrown if arg is null.
    • stdinFromFile

      public void stdinFromFile(File file)
      Read this job's standard input from the given file. It is an error if the file does not exist.
      Parameters:
      file - File.
      Throws:
      NullPointerException - (unchecked exception) Thrown if file is null.
    • stdoutToFile

      public void stdoutToFile(File file)
      Store this job's standard output in the given file. The file is created if it does not exist. The file is overwritten if it does exist.
      Parameters:
      file - File.
      Throws:
      NullPointerException - (unchecked exception) Thrown if file is null.
    • stdoutAppendToFile

      public void stdoutAppendToFile(File file)
      Append this job's standard output to the end of the given file. The file is created if it does not exist.
      Parameters:
      file - File.
      Throws:
      NullPointerException - (unchecked exception) Thrown if file is null.
    • stderrToFile

      public void stderrToFile(File file)
      Store this job's standard error in the given file. The file is created if it does not exist. The file is overwritten if it does exist.
      Parameters:
      file - File.
      Throws:
      NullPointerException - (unchecked exception) Thrown if file is null.
    • stderrAppendToFile

      public void stderrAppendToFile(File file)
      Append this job's standard error to the end of the given file. The file is created if it does not exist.
      Parameters:
      file - File.
      Throws:
      NullPointerException - (unchecked exception) Thrown if file is null.
    • stderrToStdout

      public void stderrToStdout()
      Redirect this job's standard error to the same place as this job's standard output.
    • run

      public void run()
      Run this job.
      1. The per-thread standard input, standard output, and standard error in class Stdio are redirected as necessary.
      2. This job's main class is found, using the calling thread's context class loader.
      3. The main class's public static void main(String[]) method is found.
      4. The main() method is called, passing in this job's arguments.

      If an I/O error occurs during Step 1, a RuntimeException wrapping an IOException is thrown. If any exception is thrown during Steps 2 through 4, an exception stack trace is printed on the per-thread standard error (which may have been redirected), but the exception is not propagated.

      Specified by:
      run in interface Runnable
    • writeExternal

      public void writeExternal(ObjectOutput out) throws IOException
      Write this job to the given object output stream.
      Specified by:
      writeExternal in interface Externalizable
      Throws:
      IOException - Thrown if an I/O error occurred.
    • readExternal

      public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
      Read this job from the given object input stream.
      Specified by:
      readExternal in interface Externalizable
      Throws:
      IOException - Thrown if an I/O error occurred.
      ClassNotFoundException - Thrown if an object's class could not be found.