Class ProcessLauncher

All Implemented Interfaces:
Runnable, EventListener, IdleStateListener, ProcessWrapperEventListener

public class ProcessLauncher extends AbstractProcessLauncher implements ProcessWrapperEventListener, IdleStateListener

This class is intended as a controller for a subprocess, to enable stopping and restarting it when requested.

It performs the following operations:

  • open a server socket the driver will listen to (port number is dynamically attributed)
  • Start the subprocess, sending the server socket port number as an argument
  • Wait for the subprocess to exit
  • If the subprocess exit code is equal to 2, the subprocess is restarted, otherwise this process exits as well
Author:
Laurent Cohen
  • Constructor Details

    • ProcessLauncher

      public ProcessLauncher(String mainClass, boolean idleModeSupported)
      Initialize this process launcher.
      Parameters:
      mainClass - the fully qualified name of the main class of the sub process to launch.
      idleModeSupported - whether idle mode can be used on the process.
  • Method Details

    • run

      public void run()
      Start the socket listener and the subprocess.
      Specified by:
      run in interface Runnable
    • startProcess

      public void startProcess() throws Exception
      Start the sub-process.
      Throws:
      Exception - if any error occurs.
    • createProcessWrapper

      protected ProcessWrapper createProcessWrapper(Process p)
      Create a process wrapper around the specified process, to capture its console output and prevent it from blocking.
      Parameters:
      p - the process whose output is to be captured.
      Returns:
      a ProcessWrapper instance.
    • onProcessExit

      protected boolean onProcessExit(int n)
      Called when the subprocess has exited with exit value n. This allows for printing the residual output (both standard and error) to this pJVM's console and log file, in order to get additional information if a problem occurred.
      Specified by:
      onProcessExit in class AbstractProcessLauncher
      Parameters:
      n - the exit value of the subprocess.
      Returns:
      true if this launcher is to be terminated, false if it should re-launch the subprocess.
    • getOutput

      public String getOutput(Process process, String streamType)
      Get the output of the driver process.
      Parameters:
      process - the process to get the standard or error output from.
      streamType - determines whether to obtain the standard or error output.
      Returns:
      the output as a string.
    • 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.
    • outputStreamAltered

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

      public void idleStateChanged(IdleStateEvent event)
      Description copied from interface: IdleStateListener
      Called when the idle state of the system has changed.
      Specified by:
      idleStateChanged in interface IdleStateListener
      Parameters:
      event - the event encapsulating the state change.