Class SystemUtil
- Since:
- 2.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classHolds possible return value and standard output/error when invokingcallAndCaptureOutput(Callable)orrunAndCaptureOutput(Runnable).static classRuntime exception wrapping possible exceptions thrown by invokingCallableby one ofSystemUtilmethods. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> SystemUtil.Captured<T> callAndCaptureOutput(Callable<T> callable) Executes aCallableand return its value along with any standard output or error (i.e., STDOUT/STDERR).static <T> TcallWithProperty(String name, String value, Callable<T> callable) Executes the suppliedCallableafter setting the system property and then resets that system property.static StringgetEnvironment(String name) Gets the value of the specified environment variable matching the exact name, or a supported variant.static StringGets the environment variable or system property matching the exact name, or a supported variant.static StringgetProperty(String name) Gets the value of the system property matching the exact name, or a supported variant.static StringGets the system property or environment variable matching the exact name, or a supported variant.static SystemUtil.Captured<Void> runAndCaptureOutput(Runnable runnable) Executes aRunnableand return any standard output or error (i.e., STDOUT/STDERR).static voidrunWithProperty(String name, String value, Runnable runnable) Executes the suppliedRunnableafter setting the system property and then resets that system property.static <T> SystemUtil.Captured<T> withOutputCapture(Callable<T> callable) Same ascallAndCaptureOutput(Callable)but with no thread-safety guarantee.
-
Method Details
-
withOutputCapture
Same ascallAndCaptureOutput(Callable)but with no thread-safety guarantee. This method is not synchronized.- Type Parameters:
T- the type of the return value- Parameters:
callable- the code to execute- Returns:
- captured return value and standard output/error
- Throws:
SystemUtil.UncheckedCallableException- wrapper around anyCallableexception- Since:
- 3.0.0
-
callAndCaptureOutput
Executes aCallableand return its value along with any standard output or error (i.e., STDOUT/STDERR). While this method is synchronized, nothing prevent other threads from also writing toSystem.outorSystem.err. For this reason this method should not be considered thread-safe.- Type Parameters:
T- the type of the return value- Parameters:
callable- the code to execute- Returns:
- captured return value and standard output/error
- Throws:
SystemUtil.UncheckedCallableException- wrapper around anyCallableexception- Since:
- 3.0.0
-
runAndCaptureOutput
Executes aRunnableand return any standard output or error (i.e., STDOUT/STDERR). While this method is synchronized, nothing prevent other threads from also writing toSystem.outorSystem.err. For this reason this method should not be considered thread-safe.- Parameters:
runnable- the code to execute- Returns:
- captured standard output/error
- Since:
- 3.0.0
-
runWithProperty
Executes the supplied
Runnableafter setting the system property and then resets that system property. This method is synchronized to make sure setting the property does not affect other threads. The system property is restored to its original value (if any) after execution. Useful when the invoked code expects a system property that is configurable and may change from one thread to another.nullhandlingThis method is
null-safe. If the runnable isnull, invoking this method has no effect. If the property name isnull, the runnable is invoked without setting any property beforehand. If the property value isnull, it will temporary clear any existing system property with the same name (if any) for the duration of the execution.- Parameters:
name- system property namevalue- system property valuerunnable- code to run with the system property set
-
callWithProperty
Executes the supplied
Callableafter setting the system property and then resets that system property. This method is synchronized to make sure setting the property does not affect other threads. The system property is restored to its original value (if any) after execution. Useful when the invoked code expects a system property that is configurable and may change from one thread to another.nullhandlingThis method is
null-safe. If the callable isnull, invoking this method returnsnull. If the property name isnull, the runnable is invoked without setting any property beforehand. If the property value isnull, it will temporary clear any existing system property with the same name (if any) for the duration of the execution.- Type Parameters:
T- class type of return value- Parameters:
name- system property namevalue- system property valuecallable- code to run with the system property set- Returns:
- the callable return value
- Throws:
SystemUtil.UncheckedCallableException- wrapper around anyCallableexception
-
getEnvironmentOrProperty
Gets the environment variable or system property matching the exact name, or a supported variant. Same as invokinggetEnvironment(String)and ifnull, invokinggetProperty(String).- Parameters:
name- property or environment name- Returns:
- property or environment value
-
getPropertyOrEnvironment
Gets the system property or environment variable matching the exact name, or a supported variant. Same as invokinggetProperty(String)and ifnull, invokinggetEnvironment(String).- Parameters:
name- environment or property name- Returns:
- environment or property value
-
getEnvironment
Gets the value of the specified environment variable matching the exact name, or a supported variant. First, it looks for a direct environment variable name match, as withSystem.getenv(String). If no matches are found, it will iterate through all environment variables names and compare them all with the requested name, but only after stripping all non alpha-numeric characters and ignoring case.- Parameters:
name- the name of the environment variable- Returns:
- environment variable value
-
getProperty
Gets the value of the system property matching the exact name, or a supported variant. First, it looks for a direct property name match, as withSystem.getProperty(String). If no matches are found, it will iterate through all system property names and compare them all with the requested name, but only after stripping all non alpha-numeric characters and ignoring case.- Parameters:
name- the name of the system property- Returns:
- system property value
-