Class SystemUtil


  • public final class SystemUtil
    extends Object
    System-related convenience methods.
    Since:
    2.0.0
    Author:
    Pascal Essiembre
    • Method Detail

      • runWithProperty

        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.

        Parameters:
        name - system property name
        value - system property value
        runnable - 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 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.

        Type Parameters:
        T - class type of return value
        Parameters:
        name - system property name
        value - system property value
        callable - 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 invoking getEnvironment(String) and if null, invoking getProperty(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 invoking getProperty(String) and if null, invoking getEnvironment(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 with 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.
        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 with 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.
        Parameters:
        name - the name of the system property
        Returns:
        system property value