Class CommandLineTask<T>

java.lang.Object
org.jppf.node.protocol.AbstractTask<T>
org.jppf.node.protocol.CommandLineTask<T>
Type Parameters:
T - the type of results returned by this task.
All Implemented Interfaces:
Serializable, Runnable, EventListener, Interruptibility, PositionalElement<Task<T>>, Task<T>, ProcessWrapperEventListener

public abstract class CommandLineTask<T> extends AbstractTask<T> implements ProcessWrapperEventListener
Instances of this class encapsulate the execution of an external process, program or shell script.
This task starts and external process using command line arguments, environment variables, and a list of input and/or output files to use or generated by the external process.
This task also captures the standard and error output (i.e. equivalent to System.out and System.err) of the external process.
Author:
Laurent Cohen
See Also:
  • Constructor Details

    • CommandLineTask

      public CommandLineTask()
      Default constructor.
    • CommandLineTask

      public CommandLineTask(String... commands)
      Create an instance of this class and set the parameters of the external process or script to launch.
      Parameters:
      commands - the list of command-line arguments.
    • CommandLineTask

      public CommandLineTask(Map<String,String> env, String startDir, String... commands)
      Create an instance of this class and set the parameters of the external process or script to launch.
      Parameters:
      env - the environment variables to set.
      startDir - the directory to start the command in.
      commands - the list of command-line arguments.
  • Method Details

    • launchProcess

      public int launchProcess() throws Exception
      Run the external process or script.
      Returns:
      the exit code returned by the sub-process.
      Throws:
      Exception - if an error occurs.
    • isCaptureOutput

      public boolean isCaptureOutput()
      Determines whether the process output is captured.
      Returns:
      true if the output is captured, false otherwise.
    • setCaptureOutput

      public CommandLineTask<T> setCaptureOutput(boolean captureOutput)
      Specifies whether the process output is captured.
      Parameters:
      captureOutput - true if the output is captured, false otherwise.
      Returns:
      this task, for method chaining.
    • getStandardOutput

      public String getStandardOutput()
      Get the content of the standard output for the process.
      Returns:
      the output as a string.
    • getErrorOutput

      public String getErrorOutput()
      Get the content of the error output for the process.
      Returns:
      the output as a string.
    • getCommandList

      public List<String> getCommandList()
      Get the list of command-line arguments.
      Returns:
      a list of arguments as strings.
    • setCommandList

      public CommandLineTask<T> setCommandList(List<String> commandList)
      Set the list of command-line arguments.
      Parameters:
      commandList - a list of arguments as strings.
      Returns:
      this task, for method chaining.
    • setCommandList

      public CommandLineTask<T> setCommandList(String... commands)
      Set the list of command-line arguments.
      Parameters:
      commands - a list of arguments as strings.
      Returns:
      this task, for method chaining.
    • getEnv

      public Map<String,String> getEnv()
      Get the environment variables to set.
      Returns:
      a map of variable names to their corresponding values.
    • setEnv

      public CommandLineTask<T> setEnv(Map<String,String> env)
      Get the environment variables to set.
      Parameters:
      env - a map of variable names to their corresponding values.
      Returns:
      this task, for method chaining.
    • getStartDir

      public String getStartDir()
      Get the directory to start the command in.
      Returns:
      the start directory as a string.
    • setStartDir

      public CommandLineTask<T> setStartDir(String startDir)
      Set the directory to start the command in.
      Parameters:
      startDir - the start directory as a string.
      Returns:
      this task, for method chaining.
    • outputStreamAltered

      public void outputStreamAltered(ProcessWrapperEvent event)
      Notification that the process has written to its output stream.
      Specified by:
      outputStreamAltered in interface ProcessWrapperEventListener
      Parameters:
      event - encapsulates the output stream's content.
    • errorStreamAltered

      public void errorStreamAltered(ProcessWrapperEvent event)
      Notification that the process has written to its error stream.
      Specified by:
      errorStreamAltered in interface ProcessWrapperEventListener
      Parameters:
      event - encapsulate the error stream's content.
    • getExitCode

      public int getExitCode()
      Get the exit code returned by the sub-process.
      Returns:
      the value of the exit code returned by the sub-process. A negative value indicates the process was never launched or never returned.
    • getProcess

      public Process getProcess()
      Get the process that is launched by this task.
      Returns:
      a Process instance.