Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 367   Methods: 9
NCLOC: 165   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JavaEnvUtils.java 68.2% 68.7% 88.9% 70%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.util;
 55   
 
 56   
 import java.io.File;
 57   
 import java.util.Vector;
 58   
 import org.apache.tools.ant.taskdefs.condition.Os;
 59   
 
 60   
 /**
 61   
  * A set of helper methods related to locating executables or checking
 62   
  * conditons of a given Java installation.
 63   
  *
 64   
  * @author Stefan Bodewig
 65   
  *
 66   
  * @since Ant 1.5
 67   
  */
 68   
 public class JavaEnvUtils {
 69   
 
 70   
     /** Are we on a DOS-based system */
 71   
     private static final boolean isDos = Os.isFamily("dos");
 72   
     /** Are we on Novell NetWare */
 73   
     private static final boolean isNetware = Os.isName("netware");
 74   
     /** Are we on AIX */
 75   
     private static final boolean isAix = Os.isName("aix");
 76   
 
 77   
     /** shortcut for System.getProperty("java.home") */
 78   
     private static final String javaHome = System.getProperty("java.home");
 79   
 
 80   
     /** FileUtils instance for path normalization */
 81   
     private static final FileUtils fileUtils = FileUtils.newFileUtils();
 82   
 
 83   
     /** Version of currently running VM. */
 84   
     private static String javaVersion;
 85   
 
 86   
     /** floating version of the JVM */
 87   
     private static int javaVersionNumber;
 88   
 
 89   
     /** Version constant for Java 1.0 */
 90   
     public static final String JAVA_1_0 = "1.0";
 91   
     /** Version constant for Java 1.1 */
 92   
     public static final String JAVA_1_1 = "1.1";
 93   
     /** Version constant for Java 1.2 */
 94   
     public static final String JAVA_1_2 = "1.2";
 95   
     /** Version constant for Java 1.3 */
 96   
     public static final String JAVA_1_3 = "1.3";
 97   
     /** Version constant for Java 1.4 */
 98   
     public static final String JAVA_1_4 = "1.4";
 99   
 
 100   
     /** array of packages in the runtime */
 101   
     private static Vector jrePackages;
 102   
 
 103   
 
 104   
     static {
 105   
 
 106   
         // Determine the Java version by looking at available classes
 107   
         // java.lang.CharSequence was introduced in JDK 1.4
 108   
         // java.lang.StrictMath was introduced in JDK 1.3
 109   
         // java.lang.ThreadLocal was introduced in JDK 1.2
 110   
         // java.lang.Void was introduced in JDK 1.1
 111   
         // Count up version until a NoClassDefFoundError ends the try
 112   
 
 113  1
         try {
 114  1
             javaVersion = JAVA_1_0;
 115  1
             javaVersionNumber=10;
 116  1
             Class.forName("java.lang.Void");
 117  1
             javaVersion = JAVA_1_1;
 118  1
             javaVersionNumber++;
 119  1
             Class.forName("java.lang.ThreadLocal");
 120  1
             javaVersion = JAVA_1_2;
 121  1
             javaVersionNumber++;
 122  1
             Class.forName("java.lang.StrictMath");
 123  1
             javaVersion = JAVA_1_3;
 124  1
             javaVersionNumber++;
 125  1
             Class.forName("java.lang.CharSequence");
 126  1
             javaVersion = JAVA_1_4;
 127  1
             javaVersionNumber++;
 128   
         } catch (Throwable t) {
 129   
             // swallow as we've hit the max class version that
 130   
             // we have
 131   
         }
 132   
     }
 133   
 
 134   
     /**
 135   
      * Returns the version of Java this class is running under.
 136   
      * @return the version of Java as a String, e.g. "1.1"
 137   
      */
 138  635
     public static String getJavaVersion() {
 139  635
         return javaVersion;
 140   
     }
 141   
 
 142   
     /**
 143   
      * Compares the current Java version to the passed in String -
 144   
      * assumes the argument is one of the constants defined in this
 145   
      * class.
 146   
      * @return true if the version of Java is the same as the given
 147   
      * version.
 148   
      * @since Ant 1.5
 149   
      */
 150  1714
     public static boolean isJavaVersion(String version) {
 151  1714
         return javaVersion == version;
 152   
     }
 153   
 
 154   
     /**
 155   
      * Finds an executable that is part of a JRE installation based on
 156   
      * the java.home system property.
 157   
      *
 158   
      * <p><code>java</code>, <code>keytool</code>,
 159   
      * <code>policytool</code>, <code>orbd</code>, <code>rmid</code>,
 160   
      * <code>rmiregistry</code>, <code>servertool</code> and
 161   
      * <code>tnameserv</code> are JRE executables on Sun based
 162   
      * JRE's.</p>
 163   
      *
 164   
      * <p>You typically find them in <code>JAVA_HOME/jre/bin</code> if
 165   
      * <code>JAVA_HOME</code> points to your JDK installation.  JDK
 166   
      * &lt; 1.2 has them in the same directory as the JDK
 167   
      * executables.</p>
 168   
      *
 169   
      * @since Ant 1.5
 170   
      */
 171  37
     public static String getJreExecutable(String command) {
 172  37
         if (isNetware) {
 173   
             // Extrapolating from:
 174   
             // "NetWare may have a "java" in that directory, but 99% of
 175   
             // the time, you don't want to execute it" -- Jeff Tulley
 176   
             // <JTULLEY@novell.com>
 177  0
             return command;
 178   
         }
 179   
 
 180  37
         File jExecutable = null;
 181   
 
 182  37
         if (isAix) {
 183   
             // On IBM's JDK 1.2 the directory layout is different, 1.3 follows
 184   
             // Sun's layout.
 185  0
             jExecutable = findInDir(javaHome + "/sh", command);
 186   
         }
 187   
 
 188  37
         if (jExecutable == null) {
 189  37
             jExecutable = findInDir(javaHome + "/bin", command);
 190   
         }
 191   
 
 192  37
         if (jExecutable != null) {
 193  35
             return jExecutable.getAbsolutePath();
 194   
         } else {
 195   
             // Unfortunately on Windows java.home doesn't always refer
 196   
             // to the correct location, so we need to fall back to
 197   
             // assuming java is somewhere on the PATH.
 198  2
             return addExtension(command);
 199   
         }
 200   
     }
 201   
 
 202   
     /**
 203   
      * Finds an executable that is part of a JDK installation based on
 204   
      * the java.home system property.
 205   
      *
 206   
      * <p>You typically find them in <code>JAVA_HOME/bin</code> if
 207   
      * <code>JAVA_HOME</code> points to your JDK installation.</p>
 208   
      *
 209   
      * @since Ant 1.5
 210   
      */
 211  8
     public static String getJdkExecutable(String command) {
 212  8
         if (isNetware) {
 213   
             // Extrapolating from:
 214   
             // "NetWare may have a "java" in that directory, but 99% of
 215   
             // the time, you don't want to execute it" -- Jeff Tulley
 216   
             // <JTULLEY@novell.com>
 217  0
             return command;
 218   
         }
 219   
 
 220  8
         File jExecutable = null;
 221   
 
 222  8
         if (isAix) {
 223   
             // On IBM's JDK 1.2 the directory layout is different, 1.3 follows
 224   
             // Sun's layout.
 225  0
             jExecutable = findInDir(javaHome + "/../sh", command);
 226   
         }
 227   
 
 228  8
         if (jExecutable == null) {
 229  8
             jExecutable = findInDir(javaHome + "/../bin", command);
 230   
         }
 231   
 
 232  8
         if (jExecutable != null) {
 233  7
             return jExecutable.getAbsolutePath();
 234   
         } else {
 235   
             // fall back to JRE bin directory, also catches JDK 1.0 and 1.1
 236   
             // where java.home points to the root of the JDK and Mac OS X where
 237   
             // the whole directory layout is different from Sun's
 238  1
             return getJreExecutable(command);
 239   
         }
 240   
     }
 241   
 
 242   
     /**
 243   
      * Adds a system specific extension to the name of an executable.
 244   
      *
 245   
      * @since Ant 1.5
 246   
      */
 247  47
     private static String addExtension(String command) {
 248   
         // This is the most common extension case - exe for windows and OS/2,
 249   
         // nothing for *nix.
 250  47
         return command + (isDos ? ".exe" : "");
 251   
     }
 252   
 
 253   
     /**
 254   
      * Look for an executable in a given directory.
 255   
      *
 256   
      * @return null if the executable cannot be found.
 257   
      */
 258  45
     private static File findInDir(String dirName, String commandName) {
 259  45
         File dir = fileUtils.normalize(dirName);
 260  45
         File executable = null;
 261  45
         if (dir.exists()) {
 262  45
             executable = new File(dir, addExtension(commandName));
 263  45
             if (!executable.exists()) {
 264  3
                 executable = null;
 265   
             }
 266   
         }
 267  45
         return executable;
 268   
     }
 269   
 
 270   
     /**
 271   
      * demand creation of the package list.
 272   
      * When you add a new package, add a new test below
 273   
      */
 274   
 
 275  1
     private static void buildJrePackages() {
 276  1
         jrePackages=new Vector();
 277  1
         switch(javaVersionNumber) {
 278   
             case 14:
 279  1
                 jrePackages.addElement("org.apache.crimson");
 280  1
                 jrePackages.addElement("org.apache.xalan");
 281  1
                 jrePackages.addElement("org.apache.xml");
 282  1
                 jrePackages.addElement("org.apache.xpath");
 283  1
                 jrePackages.addElement("org.ietf.jgss");
 284  1
                 jrePackages.addElement("org.w3c.dom");
 285  1
                 jrePackages.addElement("org.xml.sax");
 286   
                 // fall through
 287   
             case 13:
 288  1
                 jrePackages.addElement("org.omg");
 289  1
                 jrePackages.addElement("com.sun.corba");
 290  1
                 jrePackages.addElement("com.sun.jndi");
 291  1
                 jrePackages.addElement("com.sun.media");
 292  1
                 jrePackages.addElement("com.sun.naming");
 293  1
                 jrePackages.addElement("com.sun.org.omg");
 294  1
                 jrePackages.addElement("com.sun.rmi");
 295  1
                 jrePackages.addElement("sunw.io");
 296  1
                 jrePackages.addElement("sunw.util");
 297   
                 // fall through
 298   
             case 12:
 299  1
                 jrePackages.addElement("com.sun.java");
 300  1
                 jrePackages.addElement("com.sun.image");
 301   
                 // are there any here that we forgot?
 302   
                 // fall through
 303   
             case 11:
 304   
             default:
 305   
                 //things like sun.reflection, sun.misc, sun.net
 306  1
                 jrePackages.addElement("sun");
 307  1
                 jrePackages.addElement("java");
 308  1
                 jrePackages.addElement("javax");
 309  1
                 break;
 310   
         }
 311   
     }
 312   
 
 313   
     /**
 314   
      * Testing helper method; kept here for unification of changes.
 315   
      */
 316  0
     public static Vector getJrePackageTestCases() {
 317  0
         Vector tests=new Vector();
 318  0
         tests.addElement("java.lang.Object");
 319  0
         switch(javaVersionNumber) {
 320   
             case 14:
 321  0
                 tests.addElement("sun.audio.AudioPlayer");
 322  0
                 tests.addElement("org.apache.crimson.parser.ContentModel");
 323  0
                 tests.addElement("org.apache.xalan.processor.ProcessorImport");
 324  0
                 tests.addElement("org.apache.xml.utils.URI");
 325  0
                 tests.addElement("org.apache.xpath.XPathFactory");
 326  0
                 tests.addElement("org.ietf.jgss.Oid");
 327  0
                 tests.addElement("org.w3c.dom.Attr");
 328  0
                 tests.addElement("org.xml.sax.XMLReader");
 329   
                 // fall through
 330   
             case 13:
 331  0
                 tests.addElement("org.omg.CORBA.Any");
 332  0
                 tests.addElement("com.sun.corba.se.internal.corba.AnyImpl");
 333  0
                 tests.addElement("com.sun.jndi.ldap.LdapURL");
 334  0
                 tests.addElement("com.sun.media.sound.Printer");
 335  0
                 tests.addElement("com.sun.naming.internal.VersionHelper");
 336  0
                 tests.addElement("com.sun.org.omg.CORBA.Initializer");
 337  0
                 tests.addElement("sunw.io.Serializable");
 338  0
                 tests.addElement("sunw.util.EventListener");
 339   
                 // fall through
 340   
             case 12:
 341  0
                 tests.addElement("javax.accessibility.Accessible");
 342  0
                 tests.addElement("sun.misc.BASE64Encoder");
 343  0
                 tests.addElement("com.sun.image.codec.jpeg.JPEGCodec");
 344   
                 // fall through
 345   
             case 11:
 346   
             default:
 347   
                 //things like sun.reflection, sun.misc, sun.net
 348  0
                 tests.addElement("sun.reflect.SerializationConstructorAccessorImpl");
 349  0
                 tests.addElement("sun.net.www.http.HttpClient");
 350  0
                 tests.addElement("sun.audio.AudioPlayer");
 351  0
                 break;
 352   
         }
 353  0
         return tests;
 354   
     }
 355   
     /**
 356   
      * get a vector of strings of packages built into
 357   
      * that platforms runtime jar(s)
 358   
      * @return list of packages
 359   
      */
 360  144
     public static Vector getJrePackages() {
 361  144
         if(jrePackages==null) {
 362  1
             buildJrePackages();
 363   
         }
 364  144
         return jrePackages;
 365   
     }
 366   
 }
 367