java.lang.Object
de.uni_mannheim.informatik.dws.melt.matching_base.external.cli.process.ExternalProcess

public class ExternalProcess extends Object
Handles everything with external process When no ProcessOutputConsumer is added, the default is to discard it.
  • Field Details

    • LOGGER

      private static final org.slf4j.Logger LOGGER
    • OS_NAME

      private static final String OS_NAME
    • IS_LINUX

      private static final boolean IS_LINUX
    • IS_WINDOWS

      private static final boolean IS_WINDOWS
    • workingDirectory

      private File workingDirectory
      The working directory to start the external process.
    • environment

      private Map<String,String> environment
      The variables in the environment which are used when starting an external process.
    • arguments

      private List<ArgumentScope> arguments
      The arguments to start the external process
    • substitutionLookups

      private List<Function<String,String>> substitutionLookups
      the list of function which can substitute the variables in the command line/ arguments
    • timeout

      private long timeout
      Time out for the external process.
    • timeoutTimeUnit

      private TimeUnit timeoutTimeUnit
      Time unit for the process time out.
    • outConsumer

      private List<ProcessOutputConsumer> outConsumer
      processes the strings which are printed on the std::out.
    • errConsumer

      private List<ProcessOutputConsumer> errConsumer
      processes the strings which are printed on the std::err.
    • milliSecondsBetweenSigtermAndSigkill

      private long milliSecondsBetweenSigtermAndSigkill
      The time to wait between
    • timeoutForReadingThreadJoin

      private long timeoutForReadingThreadJoin
      The time to wait to join the reading thread for stdOut and stdErr. Zero to wait forever.
  • Constructor Details

    • ExternalProcess

      public ExternalProcess()
    • ExternalProcess

      public ExternalProcess(List<String> arguments)
  • Method Details

    • run

      public int run() throws TimeoutException
      Run the specified process in a synchronous manner. This means it blocks until the timeout is over or the process is finished.
      Returns:
      the exit status of the process
      Throws:
      TimeoutException - in case the timeout was hit
    • startReadingThread

      private static Thread startReadingThread(InputStream streamToCollect, String threadName, List<ProcessOutputConsumer> consumer)
    • stopReadingThread

      private void stopReadingThread(Thread thread)
    • closeAllProcessStreams

      private void closeAllProcessStreams(Process p)
    • setTimeout

      public void setTimeout(long timeout, TimeUnit timeoutTimeUnit)
      Sets the timeout of a process
      Parameters:
      timeout - the value of the timeout
      timeoutTimeUnit - the time unit of the value
    • getTimeout

      public long getTimeout()
      Rteurns the timput as long. The corresponding time unit can be retrived with getTimeoutTimeUnit().
      Returns:
      the timeout value
    • getTimeoutTimeUnit

      public TimeUnit getTimeoutTimeUnit()
      Return the timput unit
      Returns:
      the timput unit
    • getTimeoutAsText

      public String getTimeoutAsText()
      Returns the timeout as text which includes the timeout value and timeout unit.
      Returns:
      The timeout as text.
    • getTimeoutForReadingThreadJoin

      public long getTimeoutForReadingThreadJoin()
      Gets the timeout to wait for joining the thread which reads the standard out and standard error of the external process.
      Returns:
      the timeout in milliseconds
    • setTimeoutForReadingThreadJoin

      public void setTimeoutForReadingThreadJoin(long timeoutForReadingThreadJoin)
      Sets the timeout to wait for joining the thread which reads the standard out and standard error of the external process. Set it to zero to wait forever.
      Parameters:
      timeoutForReadingThreadJoin - the timeout in milliseconds
    • terminateProcess

      private void terminateProcess(Process process)
      Terminates the process.
      Parameters:
      process - the process to terminate
    • killProcessWithJava

      private void killProcessWithJava(Process p)
    • killAllProcessesWithSameSessionId

      private void killAllProcessesWithSameSessionId(Long pid)
    • getPid

      private static Long getPid(Process process)
      Obtains the process ID given a process.
      Parameters:
      process - The process for which the ID shall be determined.
      Returns:
      Process ID as Long of a given process.
    • getMilliSecondsBetweenSigtermAndSigkill

      public long getMilliSecondsBetweenSigtermAndSigkill()
      Returns the milli seconds between a sigterm and a sigkill when the process is started on linux.
      Returns:
      the milli seconds between a sigterm and a sigkill
    • setMilliSecondsBetweenSigtermAndSigkill

      public void setMilliSecondsBetweenSigtermAndSigkill(long milliSecondsBetweenSigtermAndSigkill)
      Sets the milli seconds between a sigterm and a sigkill when the process is started on linux.
      Parameters:
      milliSecondsBetweenSigtermAndSigkill - the milli seconds between a sigterm and a sigkill
    • addStdOutConsumer

      public void addStdOutConsumer(ProcessOutputConsumer consumer)
      Add the consumer for the std:out processing. The consumer gets a new line whenever the process prints something on std:out.
      Parameters:
      consumer - the consumer to use for std:out
    • clearStdOutConsumers

      public void clearStdOutConsumers()
      Removes all stdout consumers.
    • addStdErrConsumer

      public void addStdErrConsumer(ProcessOutputConsumer consumer)
      Set the consumer for the std:err processing. The consumer gets a new line whenever the process prints something on std:err.
      Parameters:
      consumer - the consumer to use for std:err
    • clearStdErrConsumers

      public void clearStdErrConsumers()
      Removes all stdout consumers.
    • clearEnvironment

      public void clearEnvironment()
      Removes the user defined environment variables.
    • addEnvironmentVariable

      public void addEnvironmentVariable(String key, String value)
      Adds an environemtn varibale to the environment of the started process.
      Parameters:
      key - the key (environment variable name)
      value - the value (environment variable value)
    • addEnvironmentVariableMap

      public void addEnvironmentVariableMap(Map<String,String> map)
      Adds multiple environment variables which are stored in a map.
      Parameters:
      map - the map with additional environment variables
    • addEnvironmentVariableFromCondaActivate

      private void addEnvironmentVariableFromCondaActivate(String firstArgument)
      When starting a python process within a conda virtual environment, it is usually necessary to run conda activate env-name. But since we only have the python executable, we have to modify the path variable as the activate command would do. Call it after all arguments are specified.
      Parameters:
      firstArgument - the first argument which should be the path to the python executable
      See Also:
    • getWorkingDirectory

      public File getWorkingDirectory()
      Returns the working directory which will be used when starting a new process.
      Returns:
      the working directory as a filel object
    • setWorkingDirectory

      public void setWorkingDirectory(File workingDirectory)
      Sets the working directory of the external process. If null, it uses the working directory of the current Java process.
      Parameters:
      workingDirectory - the new working directory
    • addArgument

      public void addArgument(String argument)
      Add one single argument like '-v' or 'test'.
      Parameters:
      argument - the argument
    • addArguments

      public void addArguments(List<String> arguments)
      Add multiple arguments at once. They are all not in a scope.
      Parameters:
      arguments - the arguments
    • addArguments

      public void addArguments(String... arguments)
      Add multiple arguments at once. They are all not in a scope.
      Parameters:
      arguments - the arguments
    • addArgumentScope

      public void addArgumentScope(String argumentLine)
      Adds multiple arguments in one scope. If one argument contains a variable like ${test}, then all arguments in this scope are only added if all variables in this scope can be replaced.
      Parameters:
      argumentLine - the arguments as one string. they will be splitted by whitespaces.
    • addArgumentScope

      public void addArgumentScope(List<String> arguments)
      Adds multiple arguments in one scope. If one argument contains a variable like ${test}, then all arguments in this scope are only added if all variables in this scope can be replaced.
      Parameters:
      arguments - the arguments
    • addArgumentLine

      public void addArgumentLine(String argumentLine)
      Adds an argument line which is one (huge) string containing multiple arguments. The line is splitted by whitespace but quotations are respected. An argument line can contain scopes (scopes are only printed if all variables in a scope can be replaced).
      Parameters:
      argumentLine - the argument line as one string
    • addArgumentLine

      private void addArgumentLine(String argumentLine, boolean inScope)
    • clearSubstitutionLoopkups

      public void clearSubstitutionLoopkups()
      Clear all substitution lookups.
    • addSubstitutionDefaultLookups

      public void addSubstitutionDefaultLookups()
      Adds all default subsitiution lookups:
      • SystemProperties
      • EnvironmentVariables
      • JVMArguments
    • addSubstitutionMap

      public void addSubstitutionMap(Map<String,Object> substitutions)
      Adds substitution map which replaces all variables in the command line with the elements in this map.
      Parameters:
      substitutions - the substitution map
    • addSubstitutionForSystemProperties

      public void addSubstitutionForSystemProperties()
      Adds substitution lookups for system properties. Uusually the following are interesting:
      • line.separator
      • file.separator
      • path.separator
      • user.dir
      • user.home
      • java.io.tmpdir
      All of them has to be written as e.g. ${line.separator}
    • addSubstitutionForEnvironmentVariables

      public void addSubstitutionForEnvironmentVariables()
      Adds substitution lookups for environment variables. E.g. the string ${PATH} will be replaced with the corresponding path variable defined in the system.
    • addSubstitutionForJVMArguments

      public void addSubstitutionForJVMArguments()
      Adds substitution lookups for JVM arguments such as -Xmx or -Xms. The string ${Xmx} is replaced by e.g. -Xmx10G Additionally
    • addSubstitutionFunction

      public void addSubstitutionFunction(Function<String,String> func)
      Add your own substitution function.
      Parameters:
      func - the fucntion which gets a string (the name of the parameter) and returns the value for or null if not available.
    • getArguments

      public List<String> getArguments()
      Returns the substituted command line arguments.
      Returns:
      the command line arguments
    • replaceFileSeparatorChar

      private static String replaceFileSeparatorChar(String text)
      Replace slash and backslash with the platform dependend File.separatorChar.
      Parameters:
      text - the text which should be replaced
      Returns:
      the replaced text
    • parseCommandLine

      private static List<String> parseCommandLine(String commandLine)