1 18 package org.apache.tools.ant.util; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.PrintWriter ; 23 import java.io.FileWriter ; 24 import java.io.BufferedWriter ; 25 import java.util.Vector ; 26 import org.apache.tools.ant.taskdefs.condition.Os; 27 28 34 public final class JavaEnvUtils { 35 36 private JavaEnvUtils() { 37 } 38 39 40 private static final boolean IS_DOS = Os.isFamily("dos"); 41 42 private static final boolean IS_NETWARE = Os.isName("netware"); 43 44 private static final boolean IS_AIX = Os.isName("aix"); 45 46 47 private static final String JAVA_HOME = System.getProperty("java.home"); 48 49 50 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 51 52 53 private static String javaVersion; 54 55 56 private static int javaVersionNumber; 57 58 59 public static final String JAVA_1_0 = "1.0"; 60 61 public static final String JAVA_1_1 = "1.1"; 62 63 public static final String JAVA_1_2 = "1.2"; 64 65 public static final String JAVA_1_3 = "1.3"; 66 67 public static final String JAVA_1_4 = "1.4"; 68 69 public static final String JAVA_1_5 = "1.5"; 70 71 public static final String JAVA_1_6 = "1.6"; 72 73 74 private static boolean kaffeDetected; 75 76 77 private static Vector jrePackages; 78 79 80 static { 81 82 90 try { 91 javaVersion = JAVA_1_0; 92 javaVersionNumber = 10; 93 Class.forName("java.lang.Void"); 94 javaVersion = JAVA_1_1; 95 javaVersionNumber++; 96 Class.forName("java.lang.ThreadLocal"); 97 javaVersion = JAVA_1_2; 98 javaVersionNumber++; 99 Class.forName("java.lang.StrictMath"); 100 javaVersion = JAVA_1_3; 101 javaVersionNumber++; 102 Class.forName("java.lang.CharSequence"); 103 javaVersion = JAVA_1_4; 104 javaVersionNumber++; 105 Class.forName("java.net.Proxy"); 106 javaVersion = JAVA_1_5; 107 javaVersionNumber++; 108 Class.forName("java.util.ServiceLoader"); 109 javaVersion = JAVA_1_6; 110 javaVersionNumber++; 111 } catch (Throwable t) { 112 } 115 kaffeDetected = false; 116 try { 117 Class.forName("kaffe.util.NotImplemented"); 118 kaffeDetected = true; 119 } catch (Throwable t) { 120 } 122 } 123 124 128 public static String getJavaVersion() { 129 return javaVersion; 130 } 131 132 133 139 public static int getJavaVersionNumber() { 140 return javaVersionNumber; 141 } 142 143 153 public static boolean isJavaVersion(String version) { 154 return javaVersion.equals(version); 155 } 156 157 168 public static boolean isAtLeastJavaVersion(String version) { 169 return javaVersion.compareTo(version) >= 0; 170 } 171 172 178 public static boolean isKaffe() { 179 return kaffeDetected; 180 } 181 182 200 public static String getJreExecutable(String command) { 201 if (IS_NETWARE) { 202 return command; 207 } 208 209 File jExecutable = null; 210 211 if (IS_AIX) { 212 jExecutable = findInDir(JAVA_HOME + "/sh", command); 215 } 216 217 if (jExecutable == null) { 218 jExecutable = findInDir(JAVA_HOME + "/bin", command); 219 } 220 221 if (jExecutable != null) { 222 return jExecutable.getAbsolutePath(); 223 } else { 224 return addExtension(command); 228 } 229 } 230 231 241 public static String getJdkExecutable(String command) { 242 if (IS_NETWARE) { 243 return command; 248 } 249 250 File jExecutable = null; 251 252 if (IS_AIX) { 253 jExecutable = findInDir(JAVA_HOME + "/../sh", command); 256 } 257 258 if (jExecutable == null) { 259 jExecutable = findInDir(JAVA_HOME + "/../bin", command); 260 } 261 262 if (jExecutable != null) { 263 return jExecutable.getAbsolutePath(); 264 } else { 265 return getJreExecutable(command); 269 } 270 } 271 272 277 private static String addExtension(String command) { 278 return command + (IS_DOS ? ".exe" : ""); 281 } 282 283 288 private static File findInDir(String dirName, String commandName) { 289 File dir = FILE_UTILS.normalize(dirName); 290 File executable = null; 291 if (dir.exists()) { 292 executable = new File (dir, addExtension(commandName)); 293 if (!executable.exists()) { 294 executable = null; 295 } 296 } 297 return executable; 298 } 299 300 304 305 private static void buildJrePackages() { 306 jrePackages = new Vector (); 307 switch(javaVersionNumber) { 308 case 16: 309 case 15: 310 jrePackages.addElement("com.sun.org.apache"); 312 case 14: 314 if (javaVersionNumber == 14) { 315 jrePackages.addElement("org.apache.crimson"); 316 jrePackages.addElement("org.apache.xalan"); 317 jrePackages.addElement("org.apache.xml"); 318 jrePackages.addElement("org.apache.xpath"); 319 } 320 jrePackages.addElement("org.ietf.jgss"); 321 jrePackages.addElement("org.w3c.dom"); 322 jrePackages.addElement("org.xml.sax"); 323 case 13: 325 jrePackages.addElement("org.omg"); 326 jrePackages.addElement("com.sun.corba"); 327 jrePackages.addElement("com.sun.jndi"); 328 jrePackages.addElement("com.sun.media"); 329 jrePackages.addElement("com.sun.naming"); 330 jrePackages.addElement("com.sun.org.omg"); 331 jrePackages.addElement("com.sun.rmi"); 332 jrePackages.addElement("sunw.io"); 333 jrePackages.addElement("sunw.util"); 334 case 12: 336 jrePackages.addElement("com.sun.java"); 337 jrePackages.addElement("com.sun.image"); 338 case 11: 341 default: 342 jrePackages.addElement("sun"); 344 jrePackages.addElement("java"); 345 jrePackages.addElement("javax"); 346 break; 347 } 348 } 349 350 354 public static Vector getJrePackageTestCases() { 355 Vector tests = new Vector (); 356 tests.addElement("java.lang.Object"); 357 switch(javaVersionNumber) { 358 case 16: 359 case 15: 360 tests.addElement( 361 "com.sun.org.apache.xerces.internal.jaxp.datatype.DatatypeFactoryImpl "); 362 case 14: 364 tests.addElement("sun.audio.AudioPlayer"); 365 if (javaVersionNumber == 14) { 366 tests.addElement("org.apache.crimson.parser.ContentModel"); 367 tests.addElement("org.apache.xalan.processor.ProcessorImport"); 368 tests.addElement("org.apache.xml.utils.URI"); 369 tests.addElement("org.apache.xpath.XPathFactory"); 370 } 371 tests.addElement("org.ietf.jgss.Oid"); 372 tests.addElement("org.w3c.dom.Attr"); 373 tests.addElement("org.xml.sax.XMLReader"); 374 case 13: 376 tests.addElement("org.omg.CORBA.Any"); 377 tests.addElement("com.sun.corba.se.internal.corba.AnyImpl"); 378 tests.addElement("com.sun.jndi.ldap.LdapURL"); 379 tests.addElement("com.sun.media.sound.Printer"); 380 tests.addElement("com.sun.naming.internal.VersionHelper"); 381 tests.addElement("com.sun.org.omg.CORBA.Initializer"); 382 tests.addElement("sunw.io.Serializable"); 383 tests.addElement("sunw.util.EventListener"); 384 case 12: 386 tests.addElement("javax.accessibility.Accessible"); 387 tests.addElement("sun.misc.BASE64Encoder"); 388 tests.addElement("com.sun.image.codec.jpeg.JPEGCodec"); 389 case 11: 391 default: 392 tests.addElement("sun.reflect.SerializationConstructorAccessorImpl"); 394 tests.addElement("sun.net.www.http.HttpClient"); 395 tests.addElement("sun.audio.AudioPlayer"); 396 break; 397 } 398 return tests; 399 } 400 405 public static Vector getJrePackages() { 406 if (jrePackages == null) { 407 buildJrePackages(); 408 } 409 return jrePackages; 410 } 411 412 421 public static File createVmsJavaOptionFile(String [] cmd) 422 throws IOException { 423 File script = FILE_UTILS.createTempFile("ANT", ".JAVA_OPTS", null); 424 PrintWriter out = null; 425 try { 426 out = new PrintWriter (new BufferedWriter (new FileWriter (script))); 427 for (int i = 0; i < cmd.length; i++) { 428 out.println(cmd[i]); 429 } 430 } finally { 431 FileUtils.close(out); 432 } 433 return script; 434 } 435 436 440 public static String getJavaHome() { 441 return JAVA_HOME; 442 } 443 } 444 | Popular Tags |