GriffonApplicationUtils.java
001 /*
002  * Copyright 2008-2014 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 
044     /**
045      * True if running Linux, Solaris or MacOSX
046      */
047     private static final boolean isUnix;
048 
049     private static final boolean isLinux;
050     private static final boolean isSolaris;
051     private static final boolean isMacOSX;
052 
053     private static final String osArch;
054     private static final String osName;
055     private static final String osVersion;
056     private static final String javaVersion;
057     private static final boolean is64Bit;
058 
059     private static final boolean isJdk14;
060     private static final boolean isJdk15;
061     private static final boolean isJdk16;
062     private static final boolean isJdk17;
063     private static final boolean isJdk18;
064 
065     public static final String platform;
066     public static final String basePlatform;
067 
068     static {
069         osArch = System.getProperty("os.arch");
070         osName = System.getProperty("os.name");
071         is64Bit = osArch.contains("64");
072 
073         if (osName.contains("Windows")) {
074             basePlatform = "windows";
075             isWindows = true;
076             isLinux = false;
077             isUnix = false;
078             isMacOSX = false;
079             isSolaris = false;
080             if (osName.contains("95")) {
081                 isWindows95 = true;
082                 isWindows98 = false;
083                 isWindowsNT = false;
084                 isWindows2000 = false;
085                 isWindows2003 = false;
086                 isWindowsXP = false;
087                 isWindowsVista = false;
088                 isWindows7 = false;
089                 isWindows8 = false;
090             else if (osName.contains("98")) {
091                 isWindows95 = false;
092                 isWindows98 = true;
093                 isWindowsNT = false;
094                 isWindows2000 = false;
095                 isWindows2003 = false;
096                 isWindowsXP = false;
097                 isWindowsVista = false;
098                 isWindows7 = false;
099                 isWindows8 = false;
100             else if (osName.contains("NT")) {
101                 isWindows95 = false;
102                 isWindows98 = false;
103                 isWindowsNT = false;
104                 isWindows2000 = true;
105                 isWindows2003 = false;
106                 isWindowsXP = false;
107                 isWindowsVista = false;
108                 isWindows7 = false;
109                 isWindows8 = false;
110             else if (osName.contains("2003")) {
111                 isWindows95 = false;
112                 isWindows98 = false;
113                 isWindowsNT = false;
114                 isWindows2000 = false;
115                 isWindows2003 = true;
116                 isWindowsXP = true;
117                 isWindowsVista = false;
118                 isWindows7 = false;
119                 isWindows8 = false;
120             else if (osName.contains("XP")) {
121                 isWindows95 = false;
122                 isWindows98 = false;
123                 isWindowsNT = true;
124                 isWindows2000 = true;
125                 isWindows2003 = true;
126                 isWindowsXP = false;
127                 isWindowsVista = false;
128                 isWindows7 = false;
129                 isWindows8 = false;
130             else if (osName.contains("Vista")) {
131                 isWindows95 = false;
132                 isWindows98 = false;
133                 isWindowsNT = false;
134                 isWindows2000 = false;
135                 isWindows2003 = false;
136                 isWindowsXP = false;
137                 isWindowsVista = true;
138                 isWindows7 = false;
139                 isWindows8 = false;
140             else if (osName.contains("Windows 7")) {
141                 isWindows95 = false;
142                 isWindows98 = false;
143                 isWindowsNT = false;
144                 isWindows2000 = false;
145                 isWindows2003 = false;
146                 isWindowsXP = false;
147                 isWindowsVista = false;
148                 isWindows7 = true;
149                 isWindows8 = false;
150             else if (osName.equals("Windows 8")) {
151                 isWindows95 = false;
152                 isWindows98 = false;
153                 isWindowsNT = false;
154                 isWindows2000 = false;
155                 isWindows2003 = false;
156                 isWindowsXP = false;
157                 isWindowsVista = false;
158                 isWindows7 = false;
159                 isWindows8 = true;
160             else {
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 = false;
170             }
171         else if (osName.contains("Linux")) {
172             basePlatform = "linux";
173             isWindows = false;
174             isLinux = true;
175             isUnix = true;
176             isMacOSX = false;
177             isSolaris = false;
178             isWindows95 = false;
179             isWindows98 = false;
180             isWindowsNT = false;
181             isWindows2000 = false;
182             isWindows2003 = false;
183             isWindowsXP = false;
184             isWindowsVista = false;
185             isWindows7 = false;
186             isWindows8 = false;
187         else if (osName.contains("Solaris"|| osName.contains("SunOS")) {
188             basePlatform = "solaris";
189             isWindows = false;
190             isLinux = false;
191             isUnix = true;
192             isMacOSX = false;
193             isSolaris = true;
194             isWindows95 = false;
195             isWindows98 = false;
196             isWindowsNT = false;
197             isWindows2000 = false;
198             isWindows2003 = false;
199             isWindowsXP = false;
200             isWindowsVista = false;
201             isWindows7 = false;
202             isWindows8 = false;
203         else if (osName.contains("Mac OS")) {
204             basePlatform = "macosx";
205             isWindows = false;
206             isLinux = false;
207             isUnix = true;
208             isMacOSX = true;
209             isSolaris = false;
210             isWindows95 = false;
211             isWindows98 = false;
212             isWindowsNT = false;
213             isWindows2000 = false;
214             isWindows2003 = false;
215             isWindowsXP = false;
216             isWindowsVista = false;
217             isWindows7 = false;
218             isWindows8 = false;
219         else {
220             basePlatform = "unknown";
221             isWindows = false;
222             isLinux = false;
223             isUnix = false;
224             isMacOSX = false;
225             isSolaris = false;
226             isWindows95 = false;
227             isWindows98 = false;
228             isWindowsNT = false;
229             isWindows2000 = false;
230             isWindows2003 = false;
231             isWindowsXP = false;
232             isWindowsVista = false;
233             isWindows7 = false;
234             isWindows8 = false;
235         }
236 
237         osVersion = System.getProperty("os.version");
238         javaVersion = System.getProperty("java.version");
239         String version = javaVersion.substring(03);
240         isJdk14 = true;
241         switch (version) {
242             case "1.8":
243                 isJdk18 = true;
244                 isJdk17 = true;
245                 isJdk16 = true;
246                 isJdk15 = true;
247                 break;
248             case "1.7":
249                 isJdk18 = false;
250                 isJdk17 = true;
251                 isJdk16 = true;
252                 isJdk15 = true;
253                 break;
254             case "1.6":
255                 isJdk18 = false;
256                 isJdk17 = false;
257                 isJdk16 = true;
258                 isJdk15 = true;
259                 break;
260             case "1.5":
261                 isJdk18 = false;
262                 isJdk17 = false;
263                 isJdk16 = false;
264                 isJdk15 = true;
265                 break;
266             default:
267                 isJdk18 = false;
268                 isJdk17 = false;
269                 isJdk16 = false;
270                 isJdk15 = false;
271                 break;
272         }
273 
274         platform = basePlatform + (is64Bit && !isSolaris ? "64" "");
275     }
276 
277     public static boolean isWindows() {
278         return isWindows;
279     }
280 
281     public static boolean isWindows95() {
282         return isWindows95;
283     }
284 
285     public static boolean isWindows98() {
286         return isWindows98;
287     }
288 
289     public static boolean isWindowsNT() {
290         return isWindowsNT;
291     }
292 
293     public static boolean isWindows2000() {
294         return isWindows2000;
295     }
296 
297     public static boolean isWindows2003() {
298         return isWindows2003;
299     }
300 
301     public static boolean isWindowsXP() {
302         return isWindowsXP;
303     }
304 
305     public static boolean isWindowsVista() {
306         return isWindowsVista;
307     }
308 
309     public static boolean isWindows7() {
310         return isWindows7;
311     }
312 
313     public static boolean isWindows8() {
314         return isWindows8;
315     }
316 
317     public static boolean isUnix() {
318         return isUnix;
319     }
320 
321     public static boolean isLinux() {
322         return isLinux;
323     }
324 
325     public static boolean isSolaris() {
326         return isSolaris;
327     }
328 
329     public static boolean isMacOSX() {
330         return isMacOSX;
331     }
332 
333     public static String getOsArch() {
334         return osArch;
335     }
336 
337     public static String getOsName() {
338         return osName;
339     }
340 
341     public static String getOsVersion() {
342         return osVersion;
343     }
344 
345     public static String getJavaVersion() {
346         return javaVersion;
347     }
348 
349     public static boolean is64Bit() {
350         return is64Bit;
351     }
352 
353     public static boolean isJdk14() {
354         return isJdk14;
355     }
356 
357     public static boolean isJdk15() {
358         return isJdk15;
359     }
360 
361     public static boolean isJdk16() {
362         return isJdk16;
363     }
364 
365     public static boolean isJdk17() {
366         return isJdk17;
367     }
368 
369     public static boolean isJdk18() {
370         return isJdk18;
371     }
372 
373     public static String getPlatform() {
374         return platform;
375     }
376 
377     public static String getBasePlatform() {
378         return basePlatform;
379     }
380 
381     public static boolean getIsWindows() {
382         return isWindows;
383     }
384 
385     public static boolean getIsWindows95() {
386         return isWindows95;
387     }
388 
389     public static boolean getIsWindows98() {
390         return isWindows98;
391     }
392 
393     public static boolean getIsWindowsNT() {
394         return isWindowsNT;
395     }
396 
397     public static boolean getIsWindows2000() {
398         return isWindows2000;
399     }
400 
401     public static boolean getIsWindows2003() {
402         return isWindows2003;
403     }
404 
405     public static boolean getIsWindowsXP() {
406         return isWindowsXP;
407     }
408 
409     public static boolean getIsWindowsVista() {
410         return isWindowsVista;
411     }
412 
413     public static boolean getIsWindows7() {
414         return isWindows7;
415     }
416 
417     public static boolean getIsWindows8() {
418         return isWindows8;
419     }
420 
421     public static boolean getIsUnix() {
422         return isUnix;
423     }
424 
425     public static boolean getIsLinux() {
426         return isLinux;
427     }
428 
429     public static boolean getIsSolaris() {
430         return isSolaris;
431     }
432 
433     public static boolean getIsMacOSX() {
434         return isMacOSX;
435     }
436 
437     public static boolean getIs64Bit() {
438         return is64Bit;
439     }
440 
441     public static boolean getIsJdk14() {
442         return isJdk14;
443     }
444 
445     public static boolean getIsJdk15() {
446         return isJdk15;
447     }
448 
449     public static boolean getIsJdk16() {
450         return isJdk16;
451     }
452 
453     public static boolean getIsJdk17() {
454         return isJdk17;
455     }
456 
457     public static boolean getIsJdk18() {
458         return isJdk18;
459     }
460 
461     @Nonnull
462     @SuppressWarnings("ConstantConditions")
463     public static Locale parseLocale(@Nullable String locale) {
464         if (isBlank(locale)) return Locale.getDefault();
465         String[] parts = locale.split("_");
466         switch (parts.length) {
467             case 1:
468                 return new Locale(parts[0]);
469             case 2:
470                 return new Locale(parts[0], parts[1]);
471             case 3:
472                 return new Locale(parts[0], parts[1], parts[2]);
473             default:
474                 return Locale.getDefault();
475         }
476     }
477 }