Class SystemUtil
- java.lang.Object
-
- com.norconex.commons.lang.SystemUtil
-
public final class SystemUtil extends Object
System-related convenience methods.- Since:
- 2.0.0
- Author:
- Pascal Essiembre
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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 StringgetEnvironmentOrProperty(String name)Gets 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 StringgetPropertyOrEnvironment(String name)Gets the system property or environment variable matching the exact name, or a supported variant.static voidrunWithProperty(String name, String value, Runnable runnable)Executes the suppliedRunnableafter setting the system property and then resets that system property.
-
-
-
Method Detail
-
runWithProperty
public static void runWithProperty(String name, String value, Runnable runnable)
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
public static <T> T callWithProperty(String name, String value, Callable<T> callable) throws Exception
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:
Exception- any exception the callable may throw
-
getEnvironmentOrProperty
public static String getEnvironmentOrProperty(String name)
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
public static String getPropertyOrEnvironment(String name)
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
public static String getEnvironment(String name)
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
public static String getProperty(String name)
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
-
-