Class SystemUtilities

java.lang.Object
com.cedarsoftware.util.SystemUtilities

public final class SystemUtilities extends Object
Utility class providing common system-level operations and information gathering capabilities. This class offers static methods for accessing and managing system resources, environment settings, and runtime information.

Key Features:

  • System environment and property access
  • Memory usage monitoring and management
  • Network interface information retrieval
  • Process management and identification
  • Runtime environment analysis
  • Temporary file management

Usage Examples:


 // Get system environment variable with fallback to system property
 String configPath = SystemUtilities.getExternalVariable("CONFIG_PATH");

 // Check available system resources
 int processors = SystemUtilities.getAvailableProcessors();
 MemoryInfo memory = SystemUtilities.getMemoryInfo();

 // Get network configuration
 List<NetworkInfo> networks = SystemUtilities.getNetworkInterfaces();
 

All methods in this class are thread-safe unless otherwise noted. The class cannot be instantiated and provides only static utility methods.

Author:
John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

License

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
See Also:
  • Field Details

    • OS_NAME

      public static final String OS_NAME
    • JAVA_VERSION

      public static final String JAVA_VERSION
    • USER_HOME

      public static final String USER_HOME
    • TEMP_DIR

      public static final String TEMP_DIR
  • Method Details

    • getExternalVariable

      public static String getExternalVariable(String var)
      Fetch value from environment variable and if not set, then fetch from System properties. If neither available, return null.
      Parameters:
      var - String key of variable to return
    • getAvailableProcessors

      public static int getAvailableProcessors()
      Get available processors, considering Docker container limits
    • getMemoryInfo

      public static SystemUtilities.MemoryInfo getMemoryInfo()
      Get current JVM memory usage information
    • getSystemLoadAverage

      public static double getSystemLoadAverage()
      Get system load average over last minute
      Returns:
      load average or -1.0 if not available
    • isJavaVersionAtLeast

      public static boolean isJavaVersionAtLeast(int major, int minor)
      Check if running on specific Java version or higher
    • getCurrentProcessId

      public static long getCurrentProcessId()
      Get process ID of current JVM
      Returns:
      process ID for the current Java process
    • createTempDirectory

      public static File createTempDirectory(String prefix) throws IOException
      Create temporary directory that will be deleted on JVM exit
      Throws:
      IOException
    • getSystemTimeZone

      public static TimeZone getSystemTimeZone()
      Get system timezone, considering various sources
    • hasAvailableMemory

      public static boolean hasAvailableMemory(long requiredBytes)
      Check if enough memory is available
    • getEnvironmentVariables

      public static Map<String,String> getEnvironmentVariables(Predicate<String> filter)
      Get all environment variables with optional filtering
    • getNetworkInterfaces

      public static List<SystemUtilities.NetworkInfo> getNetworkInterfaces() throws SocketException
      Get network interface information
      Throws:
      SocketException
    • addShutdownHook

      public static void addShutdownHook(Runnable hook)
      Add shutdown hook with safe execution