1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.utils;
20
21 import java.io.File;
22 import java.util.Locale;
23 import java.util.stream.Stream;
24
25
26
27
28 public class Os {
29
30
31
32
33 public static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
34
35
36
37
38 public static final String OS_ARCH = System.getProperty("os.arch").toLowerCase(Locale.ENGLISH);
39
40
41
42
43 public static final String OS_VERSION = System.getProperty("os.version").toLowerCase(Locale.ENGLISH);
44
45
46
47
48 public static final String OS_FAMILY;
49
50
51
52
53 public static final boolean IS_WINDOWS;
54
55
56
57
58 private static final String FAMILY_WINDOWS = "windows";
59
60
61
62
63 private static final String FAMILY_WIN9X = "win9x";
64
65
66
67
68 public static final String FAMILY_NT = "winnt";
69
70
71
72
73 private static final String FAMILY_OS2 = "os/2";
74
75
76
77
78 private static final String FAMILY_NETWARE = "netware";
79
80
81
82
83 private static final String FAMILY_DOS = "dos";
84
85
86
87
88 private static final String FAMILY_MAC = "mac";
89
90
91
92
93 private static final String FAMILY_TANDEM = "tandem";
94
95
96
97
98 private static final String FAMILY_UNIX = "unix";
99
100
101
102
103 private static final String FAMILY_OPENVMS = "openvms";
104
105
106
107
108 private static final String FAMILY_ZOS = "z/os";
109
110
111
112
113 private static final String FAMILY_OS390 = "os/390";
114
115
116
117
118 private static final String FAMILY_OS400 = "os/400";
119
120
121
122
123
124
125
126 private static final String DARWIN = "darwin";
127
128 static {
129
130
131
132 OS_FAMILY = getOsFamily();
133 IS_WINDOWS = isFamily(FAMILY_WINDOWS);
134 }
135
136 private Os() {}
137
138
139
140
141
142
143
144
145
146 public static boolean isFamily(String family) {
147 return isFamily(family, OS_NAME);
148 }
149
150
151
152
153
154
155
156
157
158
159 public static boolean isFamily(String family, String actualOsName) {
160
161 boolean isWindows = actualOsName.contains(FAMILY_WINDOWS);
162 boolean is9x = false;
163 boolean isNT = false;
164 if (isWindows) {
165
166 is9x = (actualOsName.contains("95")
167 || actualOsName.contains("98")
168 || actualOsName.contains("me")
169
170
171 || actualOsName.contains("ce"));
172 isNT = !is9x;
173 }
174 switch (family) {
175 case FAMILY_WINDOWS:
176 return isWindows;
177 case FAMILY_WIN9X:
178 return isWindows && is9x;
179 case FAMILY_NT:
180 return isWindows && isNT;
181 case FAMILY_OS2:
182 return actualOsName.contains(FAMILY_OS2);
183 case FAMILY_NETWARE:
184 return actualOsName.contains(FAMILY_NETWARE);
185 case FAMILY_DOS:
186 return File.pathSeparatorChar == ';' && !isFamily(FAMILY_NETWARE, actualOsName) && !isWindows;
187 case FAMILY_MAC:
188 return actualOsName.contains(FAMILY_MAC) || actualOsName.contains(DARWIN);
189 case FAMILY_TANDEM:
190 return actualOsName.contains("nonstop_kernel");
191 case FAMILY_UNIX:
192 return File.pathSeparatorChar == ':'
193 && !isFamily(FAMILY_OPENVMS, actualOsName)
194 && (!isFamily(FAMILY_MAC, actualOsName) || actualOsName.endsWith("x"));
195 case FAMILY_ZOS:
196 return actualOsName.contains(FAMILY_ZOS) || actualOsName.contains(FAMILY_OS390);
197 case FAMILY_OS400:
198 return actualOsName.contains(FAMILY_OS400);
199 case FAMILY_OPENVMS:
200 return actualOsName.contains(FAMILY_OPENVMS);
201 default:
202 return actualOsName.contains(family.toLowerCase(Locale.US));
203 }
204 }
205
206
207
208
209
210
211 private static String getOsFamily() {
212 return Stream.of(
213 FAMILY_DOS,
214 FAMILY_MAC,
215 FAMILY_NETWARE,
216 FAMILY_NT,
217 FAMILY_OPENVMS,
218 FAMILY_OS2,
219 FAMILY_OS400,
220 FAMILY_TANDEM,
221 FAMILY_UNIX,
222 FAMILY_WIN9X,
223 FAMILY_WINDOWS,
224 FAMILY_ZOS)
225 .filter(Os::isFamily)
226 .findFirst()
227 .orElse(null);
228 }
229 }