View Javadoc
1   /*
2    * Copyright 2016-2023 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi;
6   
7   import static oshi.util.Memoizer.memoize;
8   
9   import java.util.function.Supplier;
10  
11  import com.sun.jna.Platform;
12  
13  import oshi.hardware.HardwareAbstractionLayer;
14  import oshi.hardware.platform.linux.LinuxHardwareAbstractionLayer;
15  import oshi.hardware.platform.mac.MacHardwareAbstractionLayer;
16  import oshi.hardware.platform.unix.aix.AixHardwareAbstractionLayer;
17  import oshi.hardware.platform.unix.freebsd.FreeBsdHardwareAbstractionLayer;
18  import oshi.hardware.platform.unix.openbsd.OpenBsdHardwareAbstractionLayer;
19  import oshi.hardware.platform.unix.solaris.SolarisHardwareAbstractionLayer;
20  import oshi.hardware.platform.windows.WindowsHardwareAbstractionLayer;
21  import oshi.software.os.OperatingSystem;
22  import oshi.software.os.linux.LinuxOperatingSystem;
23  import oshi.software.os.mac.MacOperatingSystem;
24  import oshi.software.os.unix.aix.AixOperatingSystem;
25  import oshi.software.os.unix.freebsd.FreeBsdOperatingSystem;
26  import oshi.software.os.unix.openbsd.OpenBsdOperatingSystem;
27  import oshi.software.os.unix.solaris.SolarisOperatingSystem;
28  import oshi.software.os.windows.WindowsOperatingSystem;
29  
30  /**
31   * System information. This is the main entry point to OSHI.
32   * <p>
33   * This object provides getters which instantiate the appropriate platform-specific implementations of
34   * {@link oshi.software.os.OperatingSystem} (software) and {@link oshi.hardware.HardwareAbstractionLayer} (hardware).
35   */
36  public class SystemInfo {
37  
38      // The platform isn't going to change, and making this static enables easy
39      // access from outside this class
40      private static final PlatformEnum CURRENT_PLATFORM = PlatformEnum.getValue(Platform.getOSType());
41  
42      private static final String NOT_SUPPORTED = "Operating system not supported: ";
43  
44      private final Supplier<OperatingSystem> os = memoize(SystemInfo::createOperatingSystem);
45  
46      private final Supplier<HardwareAbstractionLayer> hardware = memoize(SystemInfo::createHardware);
47  
48      /**
49       * Create a new instance of {@link SystemInfo}. This is the main entry point to OSHI and provides access to
50       * cross-platform code.
51       * <p>
52       * Platform-specific Hardware and Software objects are retrieved via memoized suppliers. To conserve memory at the
53       * cost of additional processing time, create a new version of SystemInfo() for subsequent calls. To conserve
54       * processing time at the cost of additional memory usage, re-use the same {@link SystemInfo} object for future
55       * queries.
56       */
57      public SystemInfo() {
58          // Intentionally empty, here to enable the constructor javadoc.
59      }
60  
61      /**
62       * Gets the {@link PlatformEnum} value representing this system.
63       *
64       * @return Returns the current platform
65       */
66      public static PlatformEnum getCurrentPlatform() {
67          return CURRENT_PLATFORM;
68      }
69  
70      /**
71       * Creates a new instance of the appropriate platform-specific {@link oshi.software.os.OperatingSystem}.
72       *
73       * @return A new instance of {@link oshi.software.os.OperatingSystem}.
74       */
75      public OperatingSystem getOperatingSystem() {
76          return os.get();
77      }
78  
79      private static OperatingSystem createOperatingSystem() {
80          switch (CURRENT_PLATFORM) {
81          case WINDOWS:
82              return new WindowsOperatingSystem();
83          case LINUX:
84          case ANDROID:
85              return new LinuxOperatingSystem();
86          case MACOS:
87              return new MacOperatingSystem();
88          case SOLARIS:
89              return new SolarisOperatingSystem();
90          case FREEBSD:
91              return new FreeBsdOperatingSystem();
92          case AIX:
93              return new AixOperatingSystem();
94          case OPENBSD:
95              return new OpenBsdOperatingSystem();
96          default:
97              throw new UnsupportedOperationException(NOT_SUPPORTED + CURRENT_PLATFORM.getName());
98          }
99      }
100 
101     /**
102      * Creates a new instance of the appropriate platform-specific {@link oshi.hardware.HardwareAbstractionLayer}.
103      *
104      * @return A new instance of {@link oshi.hardware.HardwareAbstractionLayer}.
105      */
106     public HardwareAbstractionLayer getHardware() {
107         return hardware.get();
108     }
109 
110     private static HardwareAbstractionLayer createHardware() {
111         switch (CURRENT_PLATFORM) {
112         case WINDOWS:
113             return new WindowsHardwareAbstractionLayer();
114         case LINUX:
115         case ANDROID:
116             return new LinuxHardwareAbstractionLayer();
117         case MACOS:
118             return new MacHardwareAbstractionLayer();
119         case SOLARIS:
120             return new SolarisHardwareAbstractionLayer();
121         case FREEBSD:
122             return new FreeBsdHardwareAbstractionLayer();
123         case AIX:
124             return new AixHardwareAbstractionLayer();
125         case OPENBSD:
126             return new OpenBsdHardwareAbstractionLayer();
127         default:
128             throw new UnsupportedOperationException(NOT_SUPPORTED + CURRENT_PLATFORM.getName());
129         }
130     }
131 }