1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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 * OS support
27 */
28 public class Os {
29
30 /**
31 * The OS Name.
32 */
33 public static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
34
35 /**
36 * The OA architecture.
37 */
38 public static final String OS_ARCH = System.getProperty("os.arch").toLowerCase(Locale.ENGLISH);
39
40 /**
41 * The OS version.
42 */
43 public static final String OS_VERSION = System.getProperty("os.version").toLowerCase(Locale.ENGLISH);
44
45 /**
46 * OS Family
47 */
48 public static final String OS_FAMILY;
49
50 /**
51 * Boolean indicating if the running OS is a Windows system.
52 */
53 public static final boolean IS_WINDOWS;
54
55 /**
56 * OS family that can be tested for. {@value}
57 */
58 private static final String FAMILY_WINDOWS = "windows";
59
60 /**
61 * OS family that can be tested for. {@value}
62 */
63 private static final String FAMILY_WIN9X = "win9x";
64
65 /**
66 * OS family that can be tested for. {@value}
67 */
68 public static final String FAMILY_NT = "winnt";
69
70 /**
71 * OS family that can be tested for. {@value}
72 */
73 private static final String FAMILY_OS2 = "os/2";
74
75 /**
76 * OS family that can be tested for. {@value}
77 */
78 private static final String FAMILY_NETWARE = "netware";
79
80 /**
81 * OS family that can be tested for. {@value}
82 */
83 private static final String FAMILY_DOS = "dos";
84
85 /**
86 * OS family that can be tested for. {@value}
87 */
88 private static final String FAMILY_MAC = "mac";
89
90 /**
91 * OS family that can be tested for. {@value}
92 */
93 private static final String FAMILY_TANDEM = "tandem";
94
95 /**
96 * OS family that can be tested for. {@value}
97 */
98 private static final String FAMILY_UNIX = "unix";
99
100 /**
101 * OS family that can be tested for. {@value}
102 */
103 private static final String FAMILY_OPENVMS = "openvms";
104
105 /**
106 * OS family that can be tested for. {@value}
107 */
108 private static final String FAMILY_ZOS = "z/os";
109
110 /**
111 * OS family that can be tested for. {@value}
112 */
113 private static final String FAMILY_OS390 = "os/390";
114
115 /**
116 * OS family that can be tested for. {@value}
117 */
118 private static final String FAMILY_OS400 = "os/400";
119
120 /**
121 * OpenJDK is reported to call MacOS X "Darwin"
122 *
123 * @see <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=44889">bugzilla issue</a>
124 * @see <a href="https://issues.apache.org/jira/browse/HADOOP-3318">HADOOP-3318</a>
125 */
126 private static final String DARWIN = "darwin";
127
128 static {
129 // Those two public constants are initialized here, as they need all the private constants
130 // above to be initialized first, but the code style imposes the public constants to be
131 // defined above the private ones...
132 OS_FAMILY = getOsFamily();
133 IS_WINDOWS = isFamily(FAMILY_WINDOWS);
134 }
135
136 private Os() {}
137
138 /**
139 * Determines if the OS on which Maven is executing matches the
140 * given OS family.
141 *
142 * @param family the family to check for
143 * @return true if the OS matches
144 *
145 */
146 public static boolean isFamily(String family) {
147 return isFamily(family, OS_NAME);
148 }
149
150 /**
151 * Determines if the OS on which Maven is executing matches the
152 * given OS family derived from the given OS name
153 *
154 * @param family the family to check for
155 * @param actualOsName the OS name to check against
156 * @return true if the OS matches
157 *
158 */
159 public static boolean isFamily(String family, String actualOsName) {
160 // windows probing logic relies on the word 'windows' in the OS
161 boolean isWindows = actualOsName.contains(FAMILY_WINDOWS);
162 boolean is9x = false;
163 boolean isNT = false;
164 if (isWindows) {
165 // there are only four 9x platforms that we look for
166 is9x = (actualOsName.contains("95")
167 || actualOsName.contains("98")
168 || actualOsName.contains("me")
169 // wince isn't really 9x, but crippled enough to
170 // be a muchness. Maven doesnt run on CE, anyway.
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 * Helper method to determine the current OS family.
208 *
209 * @return name of current OS family.
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 }