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