`GriffonApplicationUtils.java`
`001 /* 002 * Copyright 2008-2012 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 042 /** 043 * True if running Linux, Solaris or MacOSX 044 */ 045 private static final boolean isUnix; 046 047 private static final boolean isLinux; 048 private static final boolean isSolaris; 049 private static final boolean isMacOSX; 050 051 private static final String osArch; 052 private static final String osName; 053 private static final String osVersion; 054 private static final String javaVersion; 055 private static final boolean is64Bit; 056 057 private static final boolean isJdk14; 058 private static final boolean isJdk15; 059 private static final boolean isJdk16; 060 private static final boolean isJdk17; 061 062 public static final String platform; 063 064 static { 065 osArch = System.getProperty("os.arch"); 066 osName = System.getProperty("os.name"); 067 068 if (osName.contains("Windows")) { 069 platform = "windows"; 070 isWindows = true; 071 isLinux = false; 072 isUnix = false; 073 isMacOSX = false; 074 isSolaris = false; 075 if (osName.contains("95")) { 076 isWindows95 = true; 077 isWindows98 = false; 078 isWindowsNT = false; 079 isWindows2000 = false; 080 isWindows2003 = false; 081 isWindowsXP = false; 082 isWindowsVista = false; 083 isWindows7 = false; 084 } else if (osName.contains("98")) { 085 isWindows95 = false; 086 isWindows98 = true; 087 isWindowsNT = false; 088 isWindows2000 = false; 089 isWindows2003 = false; 090 isWindowsXP = false; 091 isWindowsVista = false; 092 isWindows7 = false; 093 } else if (osName.contains("NT")) { 094 isWindows95 = false; 095 isWindows98 = false; 096 isWindowsNT = false; 097 isWindows2000 = true; 098 isWindows2003 = false; 099 isWindowsXP = false; 100 isWindowsVista = false; 101 isWindows7 = false; 102 } else if (osName.contains("2003")) { 103 isWindows95 = false; 104 isWindows98 = false; 105 isWindowsNT = false; 106 isWindows2000 = false; 107 isWindows2003 = true; 108 isWindowsXP = true; 109 isWindowsVista = false; 110 isWindows7 = false; 111 } else if (osName.contains("XP")) { 112 isWindows95 = false; 113 isWindows98 = false; 114 isWindowsNT = true; 115 isWindows2000 = true; 116 isWindows2003 = true; 117 isWindowsXP = false; 118 isWindowsVista = false; 119 isWindows7 = false; 120 } else if (osName.contains("Vista")) { 121 isWindows95 = false; 122 isWindows98 = false; 123 isWindowsNT = false; 124 isWindows2000 = false; 125 isWindows2003 = false; 126 isWindowsXP = false; 127 isWindowsVista = true; 128 isWindows7 = false; 129 } else if (osName.contains("Windows 7")) { 130 isWindows95 = false; 131 isWindows98 = false; 132 isWindowsNT = false; 133 isWindows2000 = false; 134 isWindows2003 = false; 135 isWindowsXP = false; 136 isWindowsVista = false; 137 isWindows7 = true; 138 } else { 139 isWindows95 = false; 140 isWindows98 = false; 141 isWindowsNT = false; 142 isWindows2000 = false; 143 isWindows2003 = false; 144 isWindowsXP = false; 145 isWindowsVista = false; 146 isWindows7 = false; 147 } 148 } else if (osName.contains("Linux")) { 149 platform = "linux"; 150 isWindows = false; 151 isLinux = true; 152 isUnix = true; 153 isMacOSX = false; 154 isSolaris = false; 155 isWindows95 = false; 156 isWindows98 = false; 157 isWindowsNT = false; 158 isWindows2000 = false; 159 isWindows2003 = false; 160 isWindowsXP = false; 161 isWindowsVista = false; 162 isWindows7 = false; 163 } else if (osName.contains("Solaris") || osName.contains("SunOS")) { 164 platform = "solaris"; 165 isWindows = false; 166 isLinux = false; 167 isUnix = true; 168 isMacOSX = false; 169 isSolaris = true; 170 isWindows95 = false; 171 isWindows98 = false; 172 isWindowsNT = false; 173 isWindows2000 = false; 174 isWindows2003 = false; 175 isWindowsXP = false; 176 isWindowsVista = false; 177 isWindows7 = false; 178 } else if (osName.contains("Mac OS")) { 179 platform = "macosx"; 180 isWindows = false; 181 isLinux = false; 182 isUnix = true; 183 isMacOSX = true; 184 isSolaris = false; 185 isWindows95 = false; 186 isWindows98 = false; 187 isWindowsNT = false; 188 isWindows2000 = false; 189 isWindows2003 = false; 190 isWindowsXP = false; 191 isWindowsVista = false; 192 isWindows7 = false; 193 } else { 194 platform = "unknown"; 195 isWindows = false; 196 isLinux = false; 197 isUnix = false; 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 } 209 210 osVersion = System.getProperty("os.version"); 211 javaVersion = System.getProperty("java.version"); 212 String version = javaVersion.substring(0, 3); 213 isJdk14 = true; 214 if (version.equals("1.7")) { 215 isJdk17 = true; 216 isJdk16 = true; 217 isJdk15 = true; 218 } else if (version.equals("1.6")) { 219 isJdk17 = false; 220 isJdk16 = true; 221 isJdk15 = true; 222 } else if (version.equals("1.5")) { 223 isJdk17 = false; 224 isJdk16 = false; 225 isJdk15 = true; 226 } else { 227 228 isJdk17 = false; 229 isJdk16 = false; 230 isJdk15 = false; 231 } 232 233 is64Bit = osArch.contains("64"); 234 } 235 236 public static boolean isWindows() { 237 return isWindows; 238 } 239 240 public static boolean isWindows95() { 241 return isWindows95; 242 } 243 244 public static boolean isWindows98() { 245 return isWindows98; 246 } 247 248 public static boolean isWindowsNT() { 249 return isWindowsNT; 250 } 251 252 public static boolean isWindows2000() { 253 return isWindows2000; 254 } 255 256 public static boolean isWindows2003() { 257 return isWindows2003; 258 } 259 260 public static boolean isWindowsXP() { 261 return isWindowsXP; 262 } 263 264 public static boolean isWindowsVista() { 265 return isWindowsVista; 266 } 267 268 public static boolean isWindows7() { 269 return isWindows7; 270 } 271 272 public static boolean isUnix() { 273 return isUnix; 274 } 275 276 public static boolean isLinux() { 277 return isLinux; 278 } 279 280 public static boolean isSolaris() { 281 return isSolaris; 282 } 283 284 public static boolean isMacOSX() { 285 return isMacOSX; 286 } 287 288 public static String getOsArch() { 289 return osArch; 290 } 291 292 public static String getOsName() { 293 return osName; 294 } 295 296 public static String getOsVersion() { 297 return osVersion; 298 } 299 300 public static String getJavaVersion() { 301 return javaVersion; 302 } 303 304 public static boolean is64Bit() { 305 return is64Bit; 306 } 307 308 public static boolean isJdk14() { 309 return isJdk14; 310 } 311 312 public static boolean isJdk15() { 313 return isJdk15; 314 } 315 316 public static boolean isJdk16() { 317 return isJdk16; 318 } 319 320 public static boolean isJdk17() { 321 return isJdk17; 322 } 323 324 public static String getPlatform() { 325 return platform; 326 } 327 328 public static boolean getIsWindows() { 329 return isWindows; 330 } 331 332 public static boolean getIsWindows95() { 333 return isWindows95; 334 } 335 336 public static boolean getIsWindows98() { 337 return isWindows98; 338 } 339 340 public static boolean getIsWindowsNT() { 341 return isWindowsNT; 342 } 343 344 public static boolean getIsWindows2000() { 345 return isWindows2000; 346 } 347 348 public static boolean getIsWindows2003() { 349 return isWindows2003; 350 } 351 352 public static boolean getIsWindowsXP() { 353 return isWindowsXP; 354 } 355 356 public static boolean getIsWindowsVista() { 357 return isWindowsVista; 358 } 359 360 public static boolean getIsWindows7() { 361 return isWindows7; 362 } 363 364 public static boolean getIsUnix() { 365 return isUnix; 366 } 367 368 public static boolean getIsLinux() { 369 return isLinux; 370 } 371 372 public static boolean getIsSolaris() { 373 return isSolaris; 374 } 375 376 public static boolean getIsMacOSX() { 377 return isMacOSX; 378 } 379 380 public static boolean getIs64Bit() { 381 return is64Bit; 382 } 383 384 public static boolean getIsJdk14() { 385 return isJdk14; 386 } 387 388 public static boolean getIsJdk15() { 389 return isJdk15; 390 } 391 392 public static boolean getIsJdk16() { 393 return isJdk16; 394 } 395 396 public static boolean getIsJdk17() { 397 return isJdk17; 398 } 399 400 public static MetaClass metaClassOf(Object obj) { 401 if (obj == null) return null; 402 if (GriffonArtifact.class.isAssignableFrom(obj.getClass())) { 403 return ((GriffonArtifact) obj).getGriffonClass().getMetaClass(); 404 } else if (GroovyObject.class.isAssignableFrom(obj.getClass())) { 405 return ((GroovyObject) obj).getMetaClass(); 406 } 407 return GroovySystem.getMetaClassRegistry().getMetaClass(obj.getClass()); 408 } 409 }`