1
2
3
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
32
33
34
35
36 public class SystemInfo {
37
38
39
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
50
51
52
53
54
55
56
57 public SystemInfo() {
58
59 }
60
61
62
63
64
65
66 public static PlatformEnum getCurrentPlatform() {
67 return CURRENT_PLATFORM;
68 }
69
70
71
72
73
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
103
104
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 }