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