001 /*
002 * Copyright 2008-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package griffon.util;
017
018 import javax.annotation.Nonnull;
019 import javax.annotation.Nullable;
020 import java.util.Locale;
021
022 import static griffon.util.GriffonNameUtils.isBlank;
023
024 /**
025 * Assorted utility methods and constants.
026 *
027 * @author Andres Almiray
028 */
029 public final class GriffonApplicationUtils {
030 private GriffonApplicationUtils() {
031 // prevent instantiation
032 }
033
034 private static final boolean isWindows;
035 private static final boolean isWindows95;
036 private static final boolean isWindows98;
037 private static final boolean isWindowsNT;
038 private static final boolean isWindows2000;
039 private static final boolean isWindows2003;
040 private static final boolean isWindowsXP;
041 private static final boolean isWindowsVista;
042 private static final boolean isWindows7;
043 private static final boolean isWindows8;
044 private static final boolean isWindows10;
045
046 /**
047 * True if running Linux, Solaris or MacOSX
048 */
049 private static final boolean isUnix;
050
051 private static final boolean isLinux;
052 private static final boolean isSolaris;
053 private static final boolean isMacOSX;
054
055 private static final String osArch;
056 private static final String osName;
057 private static final String osVersion;
058 private static final String javaVersion;
059 private static final boolean is64Bit;
060
061 private static final boolean isJdk4;
062 private static final boolean isJdk5;
063 private static final boolean isJdk6;
064 private static final boolean isJdk7;
065 private static final boolean isJdk8;
066 private static final boolean isJdk9;
067
068 public static final String platform;
069 public static final String basePlatform;
070
071 static {
072 osArch = System.getProperty("os.arch");
073 osName = System.getProperty("os.name");
074 is64Bit = osArch.contains("64");
075
076 if (osName.contains("Windows")) {
077 basePlatform = "windows";
078 isWindows = true;
079 isLinux = false;
080 isUnix = false;
081 isMacOSX = false;
082 isSolaris = false;
083 if (osName.contains("95")) {
084 isWindows95 = true;
085 isWindows98 = false;
086 isWindowsNT = false;
087 isWindows2000 = false;
088 isWindows2003 = false;
089 isWindowsXP = false;
090 isWindowsVista = false;
091 isWindows7 = false;
092 isWindows8 = false;
093 isWindows10 = false;
094 } else if (osName.contains("98")) {
095 isWindows95 = false;
096 isWindows98 = true;
097 isWindowsNT = false;
098 isWindows2000 = false;
099 isWindows2003 = false;
100 isWindowsXP = false;
101 isWindowsVista = false;
102 isWindows7 = false;
103 isWindows8 = false;
104 isWindows10 = false;
105 } else if (osName.contains("NT")) {
106 isWindows95 = false;
107 isWindows98 = false;
108 isWindowsNT = false;
109 isWindows2000 = true;
110 isWindows2003 = false;
111 isWindowsXP = false;
112 isWindowsVista = false;
113 isWindows7 = false;
114 isWindows8 = false;
115 isWindows10 = false;
116 } else if (osName.contains("2003")) {
117 isWindows95 = false;
118 isWindows98 = false;
119 isWindowsNT = false;
120 isWindows2000 = false;
121 isWindows2003 = true;
122 isWindowsXP = true;
123 isWindowsVista = false;
124 isWindows7 = false;
125 isWindows8 = false;
126 isWindows10 = false;
127 } else if (osName.contains("XP")) {
128 isWindows95 = false;
129 isWindows98 = false;
130 isWindowsNT = true;
131 isWindows2000 = true;
132 isWindows2003 = true;
133 isWindowsXP = false;
134 isWindowsVista = false;
135 isWindows7 = false;
136 isWindows8 = false;
137 isWindows10 = false;
138 } else if (osName.contains("Vista")) {
139 isWindows95 = false;
140 isWindows98 = false;
141 isWindowsNT = false;
142 isWindows2000 = false;
143 isWindows2003 = false;
144 isWindowsXP = false;
145 isWindowsVista = true;
146 isWindows7 = false;
147 isWindows8 = false;
148 isWindows10 = false;
149 } else if (osName.contains("Windows 7")) {
150 isWindows95 = false;
151 isWindows98 = false;
152 isWindowsNT = false;
153 isWindows2000 = false;
154 isWindows2003 = false;
155 isWindowsXP = false;
156 isWindowsVista = false;
157 isWindows7 = true;
158 isWindows8 = false;
159 isWindows10 = false;
160 } else if (osName.equals("Windows 8")) {
161 isWindows95 = false;
162 isWindows98 = false;
163 isWindowsNT = false;
164 isWindows2000 = false;
165 isWindows2003 = false;
166 isWindowsXP = false;
167 isWindowsVista = false;
168 isWindows7 = false;
169 isWindows8 = true;
170 isWindows10 = false;
171 } else if (osName.equals("Windows 8.1") || osName.equals("Windows 10")) {
172 isWindows95 = false;
173 isWindows98 = false;
174 isWindowsNT = false;
175 isWindows2000 = false;
176 isWindows2003 = false;
177 isWindowsXP = false;
178 isWindowsVista = false;
179 isWindows7 = false;
180 isWindows8 = false;
181 isWindows10 = true;
182 } else {
183 isWindows95 = false;
184 isWindows98 = false;
185 isWindowsNT = false;
186 isWindows2000 = false;
187 isWindows2003 = false;
188 isWindowsXP = false;
189 isWindowsVista = false;
190 isWindows7 = false;
191 isWindows8 = false;
192 isWindows10 = false;
193 }
194 } else if (osName.contains("Linux")) {
195 basePlatform = "linux";
196 isWindows = false;
197 isLinux = true;
198 isUnix = true;
199 isMacOSX = false;
200 isSolaris = false;
201 isWindows95 = false;
202 isWindows98 = false;
203 isWindowsNT = false;
204 isWindows2000 = false;
205 isWindows2003 = false;
206 isWindowsXP = false;
207 isWindowsVista = false;
208 isWindows7 = false;
209 isWindows8 = false;
210 isWindows10 = false;
211 } else if (osName.contains("Solaris") || osName.contains("SunOS")) {
212 basePlatform = "solaris";
213 isWindows = false;
214 isLinux = false;
215 isUnix = true;
216 isMacOSX = false;
217 isSolaris = true;
218 isWindows95 = false;
219 isWindows98 = false;
220 isWindowsNT = false;
221 isWindows2000 = false;
222 isWindows2003 = false;
223 isWindowsXP = false;
224 isWindowsVista = false;
225 isWindows7 = false;
226 isWindows8 = false;
227 isWindows10 = false;
228 } else if (osName.contains("Mac OS")) {
229 basePlatform = "macosx";
230 isWindows = false;
231 isLinux = false;
232 isUnix = true;
233 isMacOSX = true;
234 isSolaris = false;
235 isWindows95 = false;
236 isWindows98 = false;
237 isWindowsNT = false;
238 isWindows2000 = false;
239 isWindows2003 = false;
240 isWindowsXP = false;
241 isWindowsVista = false;
242 isWindows7 = false;
243 isWindows8 = false;
244 isWindows10 = false;
245 } else {
246 basePlatform = "unknown";
247 isWindows = false;
248 isLinux = false;
249 isUnix = false;
250 isMacOSX = false;
251 isSolaris = false;
252 isWindows95 = false;
253 isWindows98 = false;
254 isWindowsNT = false;
255 isWindows2000 = false;
256 isWindows2003 = false;
257 isWindowsXP = false;
258 isWindowsVista = false;
259 isWindows7 = false;
260 isWindows8 = false;
261 isWindows10 = false;
262 }
263
264 osVersion = System.getProperty("os.version");
265 javaVersion = System.getProperty("java.version");
266 String version = javaVersion.substring(0, 3);
267 isJdk4 = true;
268
269 if (version.startsWith("9")) {
270 isJdk9 = true;
271 isJdk8 = true;
272 isJdk7 = true;
273 isJdk6 = true;
274 isJdk5 = true;
275 } else {
276 switch (version) {
277 case "1.8":
278 isJdk9 = false;
279 isJdk8 = true;
280 isJdk7 = true;
281 isJdk6 = true;
282 isJdk5 = true;
283 break;
284 case "1.7":
285 isJdk9 = false;
286 isJdk8 = false;
287 isJdk7 = true;
288 isJdk6 = true;
289 isJdk5 = true;
290 break;
291 case "1.6":
292 isJdk9 = false;
293 isJdk8 = false;
294 isJdk7 = false;
295 isJdk6 = true;
296 isJdk5 = true;
297 break;
298 case "1.5":
299 isJdk9 = false;
300 isJdk8 = false;
301 isJdk7 = false;
302 isJdk6 = false;
303 isJdk5 = true;
304 break;
305 default:
306 isJdk9 = false;
307 isJdk8 = false;
308 isJdk7 = false;
309 isJdk6 = false;
310 isJdk5 = false;
311 break;
312 }
313 }
314
315 platform = basePlatform + (is64Bit && !isSolaris ? "64" : "");
316 }
317
318 public static boolean isWindows() {
319 return isWindows;
320 }
321
322 public static boolean isWindows95() {
323 return isWindows95;
324 }
325
326 public static boolean isWindows98() {
327 return isWindows98;
328 }
329
330 public static boolean isWindowsNT() {
331 return isWindowsNT;
332 }
333
334 public static boolean isWindows2000() {
335 return isWindows2000;
336 }
337
338 public static boolean isWindows2003() {
339 return isWindows2003;
340 }
341
342 public static boolean isWindowsXP() {
343 return isWindowsXP;
344 }
345
346 public static boolean isWindowsVista() {
347 return isWindowsVista;
348 }
349
350 public static boolean isWindows7() {
351 return isWindows7;
352 }
353
354 public static boolean isWindows8() {
355 return isWindows8;
356 }
357
358 public static boolean isWindows10() {
359 return isWindows10;
360 }
361
362 public static boolean isUnix() {
363 return isUnix;
364 }
365
366 public static boolean isLinux() {
367 return isLinux;
368 }
369
370 public static boolean isSolaris() {
371 return isSolaris;
372 }
373
374 public static boolean isMacOSX() {
375 return isMacOSX;
376 }
377
378 public static String getOsArch() {
379 return osArch;
380 }
381
382 public static String getOsName() {
383 return osName;
384 }
385
386 public static String getOsVersion() {
387 return osVersion;
388 }
389
390 public static String getJavaVersion() {
391 return javaVersion;
392 }
393
394 public static boolean is64Bit() {
395 return is64Bit;
396 }
397
398 @Deprecated
399 public static boolean isJdk14() {
400 return isJdk4();
401 }
402
403 @Deprecated
404 public static boolean isJdk15() {
405 return isJdk5();
406 }
407
408 @Deprecated
409 public static boolean isJdk16() {
410 return isJdk6();
411 }
412
413 @Deprecated
414 public static boolean isJdk17() {
415 return isJdk7();
416 }
417
418 @Deprecated
419 public static boolean isJdk18() {
420 return isJdk8();
421 }
422
423 public static boolean isJdk4() {
424 return isJdk4;
425 }
426
427 public static boolean isJdk5() {
428 return isJdk5;
429 }
430
431 public static boolean isJdk6() {
432 return isJdk6;
433 }
434
435 public static boolean isJdk7() {
436 return isJdk7;
437 }
438
439 public static boolean isJdk8() {
440 return isJdk8;
441 }
442
443 public static boolean isJdk9() {
444 return isJdk9;
445 }
446
447 public static String getPlatform() {
448 return platform;
449 }
450
451 public static String getBasePlatform() {
452 return basePlatform;
453 }
454
455 public static boolean getIsWindows() {
456 return isWindows;
457 }
458
459 public static boolean getIsWindows95() {
460 return isWindows95;
461 }
462
463 public static boolean getIsWindows98() {
464 return isWindows98;
465 }
466
467 public static boolean getIsWindowsNT() {
468 return isWindowsNT;
469 }
470
471 public static boolean getIsWindows2000() {
472 return isWindows2000;
473 }
474
475 public static boolean getIsWindows2003() {
476 return isWindows2003;
477 }
478
479 public static boolean getIsWindowsXP() {
480 return isWindowsXP;
481 }
482
483 public static boolean getIsWindowsVista() {
484 return isWindowsVista;
485 }
486
487 public static boolean getIsWindows7() {
488 return isWindows7;
489 }
490
491 public static boolean getIsWindows8() {
492 return isWindows8;
493 }
494
495 public static boolean getIsWindows10() {
496 return isWindows10;
497 }
498
499 public static boolean getIsUnix() {
500 return isUnix;
501 }
502
503 public static boolean getIsLinux() {
504 return isLinux;
505 }
506
507 public static boolean getIsSolaris() {
508 return isSolaris;
509 }
510
511 public static boolean getIsMacOSX() {
512 return isMacOSX;
513 }
514
515 public static boolean getIs64Bit() {
516 return is64Bit;
517 }
518
519 @Deprecated
520 public static boolean getIsJdk14() {
521 return isJdk4;
522 }
523
524 @Deprecated
525 public static boolean getIsJdk15() {
526 return isJdk5;
527 }
528
529 @Deprecated
530 public static boolean getIsJdk16() {
531 return isJdk6;
532 }
533
534 @Deprecated
535 public static boolean getIsJdk17() {
536 return isJdk7;
537 }
538
539 @Deprecated
540 public static boolean getIsJdk18() {
541 return isJdk8;
542 }
543
544 public static boolean getIsJdk4() {
545 return isJdk4;
546 }
547
548 public static boolean getIsJdk5() {
549 return isJdk5;
550 }
551
552 public static boolean getIsJdk6() {
553 return isJdk6;
554 }
555
556 public static boolean getIsJdk7() {
557 return isJdk7;
558 }
559
560 public static boolean getIsJdk8() {
561 return isJdk8;
562 }
563
564 public static boolean getIsJdk9() {
565 return isJdk9;
566 }
567
568 @Nonnull
569 @SuppressWarnings("ConstantConditions")
570 public static Locale parseLocale(@Nullable String locale) {
571 if (isBlank(locale)) { return Locale.getDefault(); }
572 String[] parts = locale.split("_");
573 switch (parts.length) {
574 case 1:
575 return new Locale(parts[0]);
576 case 2:
577 return new Locale(parts[0], parts[1]);
578 case 3:
579 return new Locale(parts[0], parts[1], parts[2]);
580 default:
581 return Locale.getDefault();
582 }
583 }
584 }
|