1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.util;
18
19 import java.io.File;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Locale;
25 import java.util.Set;
26
27 import org.apache.commons.lang3.SystemUtils;
28
29
30
31
32
33
34 @Deprecated
35 public final class Os {
36
37
38
39
40 public static final OsFamilyhtml#OsFamily">OsFamily OS_FAMILY_WINDOWS = new OsFamily("windows");
41
42
43
44
45 public static final OsFamilyily.html#OsFamily">OsFamily OS_FAMILY_DOS = new OsFamily("dos");
46
47
48
49
50 public static final OsFamilyy.html#OsFamily">OsFamily OS_FAMILY_WINNT = new OsFamily("nt", new OsFamily[] { OS_FAMILY_WINDOWS });
51
52
53
54
55 public static final OsFamilyy.html#OsFamily">OsFamily OS_FAMILY_WIN9X = new OsFamily("win9x",
56 new OsFamily[] { OS_FAMILY_WINDOWS, OS_FAMILY_DOS });
57
58
59
60
61 public static final OsFamilyily.html#OsFamily">OsFamily OS_FAMILY_OS2 = new OsFamily("os/2", new OsFamily[] { OS_FAMILY_DOS });
62
63
64
65
66 public static final OsFamilyhtml#OsFamily">OsFamily OS_FAMILY_NETWARE = new OsFamily("netware");
67
68
69
70
71 public static final OsFamilyly.html#OsFamily">OsFamily OS_FAMILY_UNIX = new OsFamily("unix");
72
73
74
75
76 public static final OsFamilyily.html#OsFamily">OsFamily OS_FAMILY_MAC = new OsFamily("mac");
77
78
79
80
81 public static final OsFamilyily.html#OsFamily">OsFamily OS_FAMILY_OSX = new OsFamily("osx", new OsFamily[] { OS_FAMILY_UNIX, OS_FAMILY_MAC });
82
83 private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.US);
84 private static final String OS_ARCH = System.getProperty("os.arch").toLowerCase(Locale.US);
85 private static final String OS_VERSION = System.getProperty("os.version").toLowerCase(Locale.US);
86 private static final String PATH_SEP = File.pathSeparator;
87 private static final OsFamily OS_FAMILY;
88 private static final OsFamily[] OS_ALL_FAMILIES;
89
90 private static final OsFamilyly.html#OsFamily">OsFamily[] ALL_FAMILIES = new OsFamily[] { OS_FAMILY_DOS, OS_FAMILY_MAC, OS_FAMILY_NETWARE,
91 OS_FAMILY_OS2, OS_FAMILY_OSX, OS_FAMILY_UNIX, OS_FAMILY_WINDOWS, OS_FAMILY_WINNT, OS_FAMILY_WIN9X };
92
93 static {
94 OS_FAMILY = determineOsFamily();
95 OS_ALL_FAMILIES = determineAllFamilies();
96 }
97
98
99
100
101 private Os() {
102 }
103
104
105
106
107
108
109
110 public static boolean isVersion(final String version) {
111 return isOs((OsFamily) null, null, null, version);
112 }
113
114
115
116
117
118
119
120 public static boolean isArch(final String arch) {
121 return isOs((OsFamily) null, null, arch, null);
122 }
123
124
125
126
127
128
129
130 public static boolean isFamily(final String family) {
131 return isOs(family, null, null, null);
132 }
133
134
135
136
137
138
139
140 public static boolean isFamily(final OsFamily family) {
141 return isOs(family, null, null, null);
142 }
143
144
145
146
147
148
149
150
151 public static boolean isName(final String name) {
152 return isOs((OsFamily) null, name, null, null);
153 }
154
155
156
157
158
159
160
161
162
163
164 public static boolean isOs(final String family, final String name, final String arch, final String version) {
165 return isOs(getFamily(family), name, arch, version);
166 }
167
168
169
170
171
172
173
174
175
176
177 public static boolean isOs(final OsFamily family, final String name, final String arch, final String version) {
178 if (family != null || name != null || arch != null || version != null) {
179 final boolean isFamily = familyMatches(family);
180 final boolean isName = nameMatches(name);
181 final boolean isArch = archMatches(arch);
182 final boolean isVersion = versionMatches(version);
183
184 return isFamily && isName && isArch && isVersion;
185 }
186 return false;
187 }
188
189
190
191
192
193
194
195 public static OsFamily getFamily(final String name) {
196 for (final OsFamily osFamily : ALL_FAMILIES) {
197 if (osFamily.getName().equalsIgnoreCase(name)) {
198 return osFamily;
199 }
200 }
201
202 return null;
203 }
204
205 private static boolean versionMatches(final String version) {
206 boolean isVersion = true;
207 if (version != null) {
208 isVersion = version.equalsIgnoreCase(OS_VERSION);
209 }
210 return isVersion;
211 }
212
213 private static boolean archMatches(final String arch) {
214 boolean isArch = true;
215 if (arch != null) {
216 isArch = arch.equalsIgnoreCase(OS_ARCH);
217 }
218 return isArch;
219 }
220
221 private static boolean nameMatches(final String name) {
222 boolean isName = true;
223 if (name != null) {
224 isName = name.equalsIgnoreCase(OS_NAME);
225 }
226 return isName;
227 }
228
229 private static boolean familyMatches(final OsFamily family) {
230 if (family == null) {
231 return false;
232 }
233 for (final OsFamily osFamily : OS_ALL_FAMILIES) {
234 if (family == osFamily) {
235 return true;
236 }
237 }
238 return false;
239 }
240
241 private static OsFamily[] determineAllFamilies() {
242
243 final Set<OsFamily> allFamilies = new HashSet<>();
244 if (OS_FAMILY != null) {
245 final List<OsFamily> queue = new ArrayList<>();
246 queue.add(OS_FAMILY);
247 while (!queue.isEmpty()) {
248 final OsFamily family = queue.remove(0);
249 allFamilies.add(family);
250 final OsFamily[] families = family.getFamilies();
251 Collections.addAll(queue, families);
252 }
253 }
254 return allFamilies.toArray(OsFamily.EMPTY_OS_FAMILY_ARRAY);
255 }
256
257 private static OsFamily determineOsFamily() {
258
259 if (OS_NAME.contains("windows")) {
260 if (OS_NAME.contains("xp") || OS_NAME.contains("2000") || OS_NAME.contains("nt")) {
261 return OS_FAMILY_WINNT;
262 }
263 return OS_FAMILY_WIN9X;
264 }
265 if (OS_NAME.contains("os/2")) {
266 return OS_FAMILY_OS2;
267 }
268 if (OS_NAME.contains("netware")) {
269 return OS_FAMILY_NETWARE;
270 }
271 if (OS_NAME.contains("mac")) {
272 if (OS_NAME.endsWith("x")) {
273 return OS_FAMILY_OSX;
274 }
275 return OS_FAMILY_MAC;
276 }
277 if (PATH_SEP.equals(":")) {
278 return OS_FAMILY_UNIX;
279 }
280 return null;
281 }
282 }