Class ExternalProcess
java.lang.Object
de.uni_mannheim.informatik.dws.melt.matching_base.external.cli.process.ExternalProcess
Handles everything with external process
When no ProcessOutputConsumer is added, the default is to discard it.
-
Field Summary
Modifier and TypeFieldDescriptionprivate List<ArgumentScope>
The arguments to start the external processThe variables in the environment which are used when starting an external process.private List<ProcessOutputConsumer>
processes the strings which are printed on the std::err.private static final boolean
private static final boolean
private static final org.slf4j.Logger
private long
The time to wait betweenprivate static final String
private List<ProcessOutputConsumer>
processes the strings which are printed on the std::out.the list of function which can substitute the variables in the command line/ argumentsprivate long
Time out for the external process.private long
The time to wait to join the reading thread for stdOut and stdErr.private TimeUnit
Time unit for the process time out.private File
The working directory to start the external process. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addArgument
(String argument) Add one single argument like '-v' or 'test'.void
addArgumentLine
(String argumentLine) Adds an argument line which is one (huge) string containing multiple arguments.private void
addArgumentLine
(String argumentLine, boolean inScope) void
addArguments
(String... arguments) Add multiple arguments at once.void
addArguments
(List<String> arguments) Add multiple arguments at once.void
addArgumentScope
(String argumentLine) Adds multiple arguments in one scope.void
addArgumentScope
(List<String> arguments) Adds multiple arguments in one scope.void
addEnvironmentVariable
(String key, String value) Adds an environemtn varibale to the environment of the started process.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.void
Adds multiple environment variables which are stored in a map.void
addStdErrConsumer
(ProcessOutputConsumer consumer) Set the consumer for the std:err processing.void
addStdOutConsumer
(ProcessOutputConsumer consumer) Add the consumer for the std:out processing.void
Adds all default subsitiution lookups: SystemProperties EnvironmentVariables JVMArgumentsvoid
Adds substitution lookups for environment variables.void
Adds substitution lookups for JVM arguments such as -Xmx or -Xms.void
Adds substitution lookups for system properties.void
Add your own substitution function.void
addSubstitutionMap
(Map<String, Object> substitutions) Adds substitution map which replaces all variables in the command line with the elements in this map.void
Removes the user defined environment variables.void
Removes all stdout consumers.void
Removes all stdout consumers.void
Clear all substitution lookups.private void
Returns the substituted command line arguments.long
Returns the milli seconds between a sigterm and a sigkill when the process is started on linux.private static Long
Obtains the process ID given a process.long
Rteurns the timput as long.Returns the timeout as text which includes the timeout value and timeout unit.long
Gets the timeout to wait for joining the thread which reads the standard out and standard error of the external process.Return the timput unitReturns the working directory which will be used when starting a new process.private void
private void
parseCommandLine
(String commandLine) private static String
Replace slash and backslash with the platform dependend File.separatorChar.int
run()
Run the specified process in a synchronous manner.void
setMilliSecondsBetweenSigtermAndSigkill
(long milliSecondsBetweenSigtermAndSigkill) Sets the milli seconds between a sigterm and a sigkill when the process is started on linux.void
setTimeout
(long timeout, TimeUnit timeoutTimeUnit) Sets the timeout of a processvoid
setTimeoutForReadingThreadJoin
(long timeoutForReadingThreadJoin) Sets the timeout to wait for joining the thread which reads the standard out and standard error of the external process.void
setWorkingDirectory
(File workingDirectory) Sets the working directory of the external process.private static Thread
startReadingThread
(InputStream streamToCollect, String threadName, List<ProcessOutputConsumer> consumer) private void
stopReadingThread
(Thread thread) private void
terminateProcess
(Process process) Terminates the process.
-
Field Details
-
LOGGER
private static final org.slf4j.Logger LOGGER -
OS_NAME
-
IS_LINUX
private static final boolean IS_LINUX -
IS_WINDOWS
private static final boolean IS_WINDOWS -
workingDirectory
The working directory to start the external process. -
environment
The variables in the environment which are used when starting an external process. -
arguments
The arguments to start the external process -
substitutionLookups
the list of function which can substitute the variables in the command line/ arguments -
timeout
private long timeoutTime out for the external process. -
timeoutTimeUnit
Time unit for the process time out. -
outConsumer
processes the strings which are printed on the std::out. -
errConsumer
processes the strings which are printed on the std::err. -
milliSecondsBetweenSigtermAndSigkill
private long milliSecondsBetweenSigtermAndSigkillThe time to wait between -
timeoutForReadingThreadJoin
private long timeoutForReadingThreadJoinThe time to wait to join the reading thread for stdOut and stdErr. Zero to wait forever.
-
-
Constructor Details
-
ExternalProcess
public ExternalProcess() -
ExternalProcess
-
-
Method Details
-
run
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
-
closeAllProcessStreams
-
setTimeout
Sets the timeout of a process- Parameters:
timeout
- the value of the timeouttimeoutTimeUnit
- the time unit of the value
-
getTimeout
public long getTimeout()Rteurns the timput as long. The corresponding time unit can be retrived withgetTimeoutTimeUnit()
.- Returns:
- the timeout value
-
getTimeoutTimeUnit
Return the timput unit- Returns:
- the timput unit
-
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
Terminates the process.- Parameters:
process
- the process to terminate
-
killProcessWithJava
-
killAllProcessesWithSameSessionId
-
getPid
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
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
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
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
Adds multiple environment variables which are stored in a map.- Parameters:
map
- the map with additional environment variables
-
addEnvironmentVariableFromCondaActivate
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
Returns the working directory which will be used when starting a new process.- Returns:
- the working directory as a filel object
-
setWorkingDirectory
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
Add one single argument like '-v' or 'test'.- Parameters:
argument
- the argument
-
addArguments
Add multiple arguments at once. They are all not in a scope.- Parameters:
arguments
- the arguments
-
addArguments
Add multiple arguments at once. They are all not in a scope.- Parameters:
arguments
- the arguments
-
addArgumentScope
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
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
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
-
clearSubstitutionLoopkups
public void clearSubstitutionLoopkups()Clear all substitution lookups. -
addSubstitutionDefaultLookups
public void addSubstitutionDefaultLookups()Adds all default subsitiution lookups:- SystemProperties
- EnvironmentVariables
- JVMArguments
-
addSubstitutionMap
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
-
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
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
Returns the substituted command line arguments.- Returns:
- the command line arguments
-
replaceFileSeparatorChar
Replace slash and backslash with the platform dependend File.separatorChar.- Parameters:
text
- the text which should be replaced- Returns:
- the replaced text
-
parseCommandLine
-