public final class SystemUtil extends Object
Modifier and Type | Method and Description |
---|---|
static <T> T |
callWithProperty(String name,
String value,
Callable<T> callable)
Executes the supplied
Callable after setting the system
property and then resets that system property. |
static String |
getEnvironment(String name)
Gets the value of the specified environment variable matching the exact
name, or a supported variant.
|
static String |
getEnvironmentOrProperty(String name)
Gets the environment variable or system property matching the exact
name, or a supported variant.
|
static String |
getProperty(String name)
Gets the value of the system property matching the exact name,
or a supported variant.
|
static String |
getPropertyOrEnvironment(String name)
Gets the system property or environment variable matching the exact
name, or a supported variant.
|
static void |
runWithProperty(String name,
String value,
Runnable runnable)
Executes the supplied
Runnable after setting the system
property and then resets that system property. |
public static void runWithProperty(String name, String value, Runnable runnable)
Executes the supplied Runnable
after 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.
null
handling
This method is null
-safe.
If the runnable is null
, invoking this method has no effect.
If the property name is null
, the runnable is
invoked without setting any property beforehand.
If the property value is null
, it will temporary
clear any existing system property with the same name (if any) for
the duration of the execution.
name
- system property namevalue
- system property valuerunnable
- code to run with the system property setpublic static <T> T callWithProperty(String name, String value, Callable<T> callable) throws Exception
Executes the supplied Callable
after 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.
null
handling
This method is null
-safe.
If the callable is null
, invoking this method returns
null
.
If the property name is null
, the runnable is
invoked without setting any property beforehand.
If the property value is null
, it will temporary
clear any existing system property with the same name (if any)
for the duration of the execution.
T
- class type of return valuename
- system property namevalue
- system property valuecallable
- code to run with the system property setException
- any exception the callable may throwpublic static String getEnvironmentOrProperty(String name)
getEnvironment(String)
and if
null
, invoking getProperty(String)
.name
- property or environment namepublic static String getPropertyOrEnvironment(String name)
getProperty(String)
and if
null
, invoking getEnvironment(String)
.name
- environment or property namepublic static String getEnvironment(String name)
System.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.name
- the name of the environment variablepublic static String getProperty(String name)
System.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.name
- the name of the system propertyCopyright © 2008–2023 Norconex Inc.. All rights reserved.