1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.exec;
18
19 import java.io.File;
20 import java.util.Locale;
21
22 /**
23 * Condition that tests the OS type.
24 *
25 * Copied and adapted from Apache Ant 1.9.6 from org.apache.tools.ant.taskdefs.condition.OS.
26 */
27 public final class OS {
28
29 /**
30 * OS family that can be tested for. {@value}
31 */
32 public static final String FAMILY_9X = "win9x";
33 /**
34 * OS family that can be tested for. {@value}
35 */
36 public static final String FAMILY_DOS = "dos";
37 /**
38 * OS family that can be tested for. {@value}
39 */
40 public static final String FAMILY_MAC = "mac";
41 /**
42 * OS family that can be tested for. {@value}
43 */
44 public static final String FAMILY_NETWARE = "netware";
45 /**
46 * OS family that can be tested for. {@value}
47 */
48 public static final String FAMILY_NT = "winnt";
49 /**
50 * OS family that can be tested for. {@value}
51 */
52 public static final String FAMILY_OS2 = "os/2";
53 /**
54 * OS family that can be tested for. {@value}
55 */
56 public static final String FAMILY_OS400 = "os/400";
57 /**
58 * OS family that can be tested for. {@value}
59 */
60 public static final String FAMILY_TANDEM = "tandem";
61 /**
62 * OS family that can be tested for. {@value}
63 */
64 public static final String FAMILY_UNIX = "unix";
65 /**
66 * OS family that can be tested for. {@value}
67 */
68 public static final String FAMILY_VMS = "openvms";
69 /**
70 * OS family that can be tested for. {@value}
71 */
72 public static final String FAMILY_WINDOWS = "windows";
73 /**
74 * OS family that can be tested for. {@value}
75 */
76 public static final String FAMILY_ZOS = "z/os";
77
78 private static final String DARWIN = "darwin";
79
80 private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ROOT);
81 private static final String OS_ARCH = System.getProperty("os.arch").toLowerCase(Locale.ROOT);
82 private static final String OS_VERSION = System.getProperty("os.version").toLowerCase(Locale.ROOT);
83 private static final String PATH_SEP = File.pathSeparator;
84
85 /**
86 * Tests whether the OS on which commons-exec is executing matches the given OS architecture.
87 *
88 * @param arch the OS architecture to check for.
89 * @return whether if the OS matches.
90 */
91 public static boolean isArch(final String arch) {
92 return isOs(null, null, arch, null);
93 }
94
95 /**
96 * Tests whether the OS on which commons-exec is executing matches the given OS family.
97 *
98 * @param family the family to check for.
99 * @return whether if the OS matches.
100 */
101 private static boolean isFamily(final String family) {
102 return isOs(family, null, null, null);
103 }
104
105 /**
106 * Tests whether the OS is in the DOS family.
107 *
108 * @return whether the OS is in the DOS family.
109 */
110 public static boolean isFamilyDOS() {
111 return isFamily(FAMILY_DOS);
112 }
113
114 /**
115 * Tests whether the OS is in the Mac family.
116 *
117 * @return whether the OS is in the Mac family.
118 */
119 public static boolean isFamilyMac() {
120 return isFamily(FAMILY_MAC);
121 }
122
123 /**
124 * Tests whether the OS is in the Netware family.
125 *
126 * @return whether the OS is in the Netware family.
127 */
128 public static boolean isFamilyNetware() {
129 return isFamily(FAMILY_NETWARE);
130 }
131
132 /**
133 * Tests whether the OS is in the OpenVMS family.
134 *
135 * @return whether the OS is in the OpenVMS family.
136 */
137 public static boolean isFamilyOpenVms() {
138 return isFamily(FAMILY_VMS);
139 }
140
141 /**
142 * Tests whether the OS is in the OS/2 family.
143 *
144 * @return whether the OS is in the OS/2 family.
145 */
146 public static boolean isFamilyOS2() {
147 return isFamily(FAMILY_OS2);
148 }
149
150 /**
151 * Tests whether the OS is in the OS/400 family.
152 *
153 * @return whether the OS is in the OS/400 family.
154 */
155 public static boolean isFamilyOS400() {
156 return isFamily(FAMILY_OS400);
157 }
158
159 /**
160 * Tests whether the OS is in the Tandem family.
161 *
162 * @return whether the OS is in the Tandem family.
163 */
164 public static boolean isFamilyTandem() {
165 return isFamily(FAMILY_TANDEM);
166 }
167
168 /**
169 * Tests whether the OS is in the Unix family.
170 *
171 * @return whether the OS is in the Unix family.
172 */
173 public static boolean isFamilyUnix() {
174 return isFamily(FAMILY_UNIX);
175 }
176
177 /**
178 * Tests whether the OS is in the Windows 9x family.
179 *
180 * @return whether the OS is in the Windows 9x family.
181 */
182 public static boolean isFamilyWin9x() {
183 return isFamily(FAMILY_9X);
184 }
185
186 /**
187 * Tests whether the OS is in the Windows family.
188 *
189 * @return whether the OS is in the Windows family.
190 */
191 public static boolean isFamilyWindows() {
192 return isFamily(FAMILY_WINDOWS);
193 }
194
195 /**
196 * Tests whether the OS is in the Windows NT family.
197 *
198 * @return whether the OS is in the Windows NT family.
199 */
200 public static boolean isFamilyWinNT() {
201 return isFamily(FAMILY_NT);
202 }
203
204 /**
205 * Tests whether the OS is in the z/OS family.
206 *
207 * @return whether the OS is in the z/OS family.
208 */
209 public static boolean isFamilyZOS() {
210 return isFamily(FAMILY_ZOS);
211 }
212
213 /**
214 * Tests whether if the OS on which commons-exec is executing matches the given OS name.
215 *
216 * @param name the OS name to check for.
217 * @return whether the OS matches.
218 */
219 public static boolean isName(final String name) {
220 return isOs(null, name, null, null);
221 }
222
223 /**
224 * Tests whether the OS on which commons-exec is executing matches the given OS family, name, architecture and version.
225 *
226 * @param family The OS family.
227 * @param name The OS name.
228 * @param arch The OS architecture.
229 * @param version The OS version.
230 * @return whether the OS matches.
231 */
232 public static boolean isOs(final String family, final String name, final String arch, final String version) {
233 boolean retValue = false;
234 if (family != null || name != null || arch != null || version != null) {
235 boolean isFamily = true;
236 boolean isName = true;
237 boolean isArch = true;
238 boolean isVersion = true;
239 if (family != null) {
240 // Windows probing logic relies on the word 'windows' in the OS
241 final boolean isWindows = OS_NAME.contains(FAMILY_WINDOWS);
242 boolean is9x = false;
243 boolean isNT = false;
244 if (isWindows) {
245 // there are only four 9x platforms that we look for
246 is9x = OS_NAME.contains("95") || OS_NAME.contains("98") || OS_NAME.contains("me")
247 // Windows CE isn't really 9x, but crippled enough to
248 // be a muchness. Ant doesn't run on CE, anyway.
249 || OS_NAME.contains("ce");
250 isNT = !is9x;
251 }
252 switch (family) {
253 case FAMILY_WINDOWS:
254 isFamily = isWindows;
255 break;
256 case FAMILY_9X:
257 isFamily = isWindows && is9x;
258 break;
259 case FAMILY_NT:
260 isFamily = isWindows && isNT;
261 break;
262 case FAMILY_OS2:
263 isFamily = OS_NAME.contains(FAMILY_OS2);
264 break;
265 case FAMILY_NETWARE:
266 isFamily = OS_NAME.contains(FAMILY_NETWARE);
267 break;
268 case FAMILY_DOS:
269 isFamily = PATH_SEP.equals(";") && !isFamily(FAMILY_NETWARE);
270 break;
271 case FAMILY_MAC:
272 isFamily = OS_NAME.contains(FAMILY_MAC) || OS_NAME.contains(DARWIN);
273 break;
274 case FAMILY_TANDEM:
275 isFamily = OS_NAME.contains("nonstop_kernel");
276 break;
277 case FAMILY_UNIX:
278 isFamily = PATH_SEP.equals(":") && !isFamily(FAMILY_VMS) && (!isFamily(FAMILY_MAC) || OS_NAME.endsWith("x") || OS_NAME.contains(DARWIN));
279 break;
280 case FAMILY_ZOS:
281 isFamily = OS_NAME.contains(FAMILY_ZOS) || OS_NAME.contains("os/390");
282 break;
283 case FAMILY_OS400:
284 isFamily = OS_NAME.contains(FAMILY_OS400);
285 break;
286 case FAMILY_VMS:
287 isFamily = OS_NAME.contains(FAMILY_VMS);
288 break;
289 default:
290 throw new IllegalArgumentException("Don\'t know how to detect OS family \"" + family + "\"");
291 }
292 }
293 if (name != null) {
294 isName = name.equals(OS_NAME);
295 }
296 if (arch != null) {
297 isArch = arch.equals(OS_ARCH);
298 }
299 if (version != null) {
300 isVersion = version.equals(OS_VERSION);
301 }
302 retValue = isFamily && isName && isArch && isVersion;
303 }
304 return retValue;
305 }
306
307 /**
308 * Tests whether the OS on which commonss-exec is executing matches the given OS version.
309 *
310 * @param version the OS version to check for.
311 * @return whether if the OS matches.
312 */
313 public static boolean isVersion(final String version) {
314 return isOs(null, null, null, version);
315 }
316
317 /**
318 * Avoids instances.
319 */
320 private OS() {
321 }
322 }