Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 407   Methods: 32
NCLOC: 209   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
CommandlineJava.java 68.8% 80.2% 84.4% 78.8%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-2002 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   
 
 55   
 package org.apache.tools.ant.types;
 56   
 
 57   
 import java.util.Enumeration;
 58   
 import java.util.Properties;
 59   
 import java.util.Vector;
 60   
 import org.apache.tools.ant.BuildException;
 61   
 import org.apache.tools.ant.Project;
 62   
 import org.apache.tools.ant.util.JavaEnvUtils;
 63   
 
 64   
 /**
 65   
  * A representation of a Java command line that is nothing more
 66   
  * than a composite of 2 <tt>Commandline</tt>. One is used for the 
 67   
  * vm/options and one for the classname/arguments. It provides 
 68   
  * specific methods for a java command line.
 69   
  *
 70   
  * @author thomas.haas@softwired-inc.com
 71   
  * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
 72   
  */
 73   
 public class CommandlineJava implements Cloneable {
 74   
 
 75   
     /**
 76   
      * commands to the JVM
 77   
      */
 78   
     private Commandline vmCommand = new Commandline();
 79   
     /**
 80   
      * actual java commands
 81   
      */
 82   
     private Commandline javaCommand = new Commandline();
 83   
     /**
 84   
      * properties to add using -D
 85   
      */
 86   
     private SysProperties sysProperties = new SysProperties();
 87   
     private Path classpath = null;
 88   
     private String vmVersion;
 89   
     private String maxMemory = null;
 90   
 
 91   
     /**
 92   
      * Indicate whether it will execute a jar file or not, in this case
 93   
      * the first vm option must be a -jar and the 'executable' is a jar file.
 94   
      */
 95   
      private boolean executeJar  = false;
 96   
 
 97   
     /**
 98   
      * Specialized Environment class for System properties
 99   
      */
 100   
     public static class SysProperties extends Environment implements Cloneable {
 101   
         Properties sys = null;
 102   
 
 103  0
         public String[] getVariables() throws BuildException {
 104  0
             String[] props = super.getVariables();
 105   
       
 106  0
             if (props == null) {
 107  0
               return null;
 108   
             }
 109   
 
 110  0
             for (int i = 0; i < props.length; i++) {
 111  0
                 props[i] = "-D" + props[i];
 112   
             }
 113  0
             return props;
 114   
         }
 115   
 
 116  46
         public int size() {
 117  46
             return variables.size();
 118   
         }
 119   
 
 120  142
         public void setSystem() throws BuildException {
 121  142
             try {
 122  142
                 sys = System.getProperties();
 123  142
                 Properties p = new Properties();
 124  142
                 for (Enumeration e = sys.keys(); e.hasMoreElements();) {
 125  6968
                     Object o = e.nextElement();
 126  6968
                     p.put(o, sys.get(o));
 127   
                 }
 128  142
                 for (Enumeration e = variables.elements(); e.hasMoreElements();) {
 129  409
                     Environment.Variable v = (Environment.Variable) e.nextElement();
 130  409
                     p.put(v.getKey(), v.getValue());
 131   
                 }
 132  142
                 System.setProperties(p);
 133   
             } catch (SecurityException e) {
 134  0
                 throw new BuildException("Cannot modify system properties", e);
 135   
             }
 136   
         }
 137   
 
 138  142
         public void restoreSystem() throws BuildException {
 139  142
             if (sys == null) {
 140  0
                 throw new BuildException("Unbalanced nesting of SysProperties");
 141   
             }
 142   
 
 143  142
             try {
 144  142
                 System.setProperties(sys);
 145  142
                 sys = null;
 146   
             } catch (SecurityException e) {
 147  0
                 throw new BuildException("Cannot modify system properties", e);
 148   
             }
 149   
         }
 150   
 
 151  1
         public Object clone() {
 152  1
             try {
 153  1
                 SysProperties c = (SysProperties) super.clone();
 154  1
                 c.variables = (Vector) variables.clone();
 155  1
                 return c;
 156   
             } catch (CloneNotSupportedException e) {
 157  0
                 return null;
 158   
             }
 159   
         }
 160   
 
 161   
     }
 162   
 
 163   
     /**
 164   
      * constructor uses the VM we are running on now.
 165   
      */
 166  30
     public CommandlineJava() {
 167  30
         setVm(JavaEnvUtils.getJreExecutable("java"));
 168  30
         setVmversion(JavaEnvUtils.getJavaVersion());
 169   
     }
 170   
 
 171  79
     public Commandline.Argument createArgument() {
 172  79
         return javaCommand.createArgument();
 173   
     }
 174   
 
 175  3
     public Commandline.Argument createVmArgument() {
 176  3
         return vmCommand.createArgument();
 177   
     }
 178   
 
 179  4
     public void addSysproperty(Environment.Variable sysp) {
 180  4
         sysProperties.addVariable(sysp);
 181   
     }
 182   
 
 183  30
     public void setVm(String vm) {
 184  30
         vmCommand.setExecutable(vm);
 185   
     }
 186   
 
 187  30
     public void setVmversion(String value) {
 188  30
         vmVersion = value;
 189   
     }
 190   
 
 191   
     /**
 192   
      * set a jar file to execute via the -jar option.
 193   
      * @param jarpathname the pathname of the jar to execute
 194   
      */
 195  3
     public void setJar(String jarpathname){
 196  3
         javaCommand.setExecutable(jarpathname);
 197  3
         executeJar = true;
 198   
     }
 199   
 
 200   
     /**
 201   
      * @return the pathname of the jar file to run via -jar option
 202   
      * or <tt>null</tt> if there is no jar to run.
 203   
      * @see #getClassname()
 204   
      */
 205  25
     public String getJar(){
 206  25
         if (executeJar){
 207  3
             return javaCommand.getExecutable();
 208   
         }
 209  22
         return null;
 210   
     }
 211   
 
 212   
     /**
 213   
      * set the classname to execute
 214   
      * @param classname the fully qualified classname.
 215   
      */
 216  18
     public void setClassname(String classname) {
 217  18
         javaCommand.setExecutable(classname);
 218  18
         executeJar = false;
 219   
     }
 220   
 
 221   
     /**
 222   
      * @return the name of the class to run or <tt>null</tt> if there is no class.
 223   
      * @see #getJar()
 224   
      */
 225  20
     public String getClassname() {
 226  20
         if (!executeJar) {
 227  19
             return javaCommand.getExecutable();
 228   
         }
 229  1
         return null;
 230   
     }
 231   
 
 232  18
     public Path createClasspath(Project p) {
 233  18
         if (classpath == null) {
 234  16
             classpath = new Path(p);
 235   
         }
 236  18
         return classpath;
 237   
     }
 238   
 
 239  0
     public String getVmversion() {
 240  0
         return vmVersion;
 241   
     }
 242   
 
 243   
     /**
 244   
      * get the command line to run a java vm.
 245   
      * @return the list of all arguments necessary to run the vm.
 246   
      */
 247  23
     public String[] getCommandline() {
 248  23
         String[] result = new String[size()];
 249  23
         int pos = 0;
 250  23
         String[] vmArgs = getActualVMCommand().getCommandline();
 251   
         // first argument is the java.exe path...
 252  23
         result[pos++] = vmArgs[0];
 253   
         
 254   
         // next follows the vm options
 255  23
         System.arraycopy(vmArgs, 1, result, pos, vmArgs.length - 1);
 256  23
         pos += vmArgs.length - 1;
 257   
         // properties are part of the vm options...
 258  23
         if (sysProperties.size() > 0) {
 259  0
             System.arraycopy(sysProperties.getVariables(), 0,
 260   
                              result, pos, sysProperties.size());
 261  0
             pos += sysProperties.size();
 262   
         }
 263   
         // classpath is a vm option too..
 264  23
         Path fullClasspath = classpath != null ? classpath.concatSystemClasspath("ignore") : null;
 265  23
         if (fullClasspath != null && fullClasspath.toString().trim().length() > 0) {
 266  21
             result[pos++] = "-classpath";
 267  21
             result[pos++] = fullClasspath.toString();
 268   
         }
 269   
 
 270   
         // JDK usage command line says that -jar must be the first option, as there is
 271   
         // a bug in JDK < 1.4 that forces the jvm type to be specified as the first
 272   
         // option, it is appended here as specified in the docs even though there is
 273   
         // in fact no order.
 274  23
         if (executeJar){
 275  1
             result[pos++] = "-jar";
 276   
         }
 277   
 
 278   
         // this is the classname to run as well as its arguments.
 279   
         // in case of 'executeJar', the executable is a jar file.
 280  23
         System.arraycopy(javaCommand.getCommandline(), 0, 
 281   
                          result, pos, javaCommand.size());
 282   
 
 283  23
         return result;
 284   
     }
 285   
 
 286   
     /**
 287   
      * Specify max memory of the JVM
 288   
      * -mx or -Xmx depending on VM version
 289   
      */
 290  0
     public void setMaxmemory(String max){
 291  0
         this.maxMemory = max;
 292   
     }
 293   
 
 294   
 
 295   
     /**
 296   
      * get a string description
 297   
      * @return the command line as a string
 298   
      */
 299  0
     public String toString() {
 300  0
         return Commandline.toString(getCommandline());
 301   
     }
 302   
 
 303   
     /**
 304   
      * Returns a String that describes the command and arguments
 305   
      * suitable for verbose output before a call to
 306   
      * <code>Runtime.exec(String[])<code>
 307   
      *
 308   
      * @since Ant 1.5
 309   
      */
 310  10
     public String describeCommand() {
 311  10
         return Commandline.describeCommand(getCommandline());
 312   
     }
 313   
 
 314   
     /**
 315   
      * Returns a String that describes the java command and arguments
 316   
      * for in VM executions.
 317   
      *
 318   
      * <p>The class name is the executable in this context.</p>
 319   
      *
 320   
      * @since Ant 1.5
 321   
      */
 322  11
     public String describeJavaCommand() {
 323  11
         return Commandline.describeCommand(getJavaCommand());
 324   
     }
 325   
 
 326  51
     private Commandline getActualVMCommand() {
 327  51
         Commandline actualVMCommand = (Commandline) vmCommand.clone();
 328  51
         if (maxMemory != null) {
 329  0
             if (vmVersion.startsWith("1.1")) {
 330  0
                 actualVMCommand.createArgument().setValue("-mx" + maxMemory);
 331   
             } else {
 332  0
                 actualVMCommand.createArgument().setValue("-Xmx" + maxMemory);
 333   
             }
 334   
         }
 335  51
         return actualVMCommand;
 336   
     }        
 337   
 
 338   
     /**
 339   
      * The size of the java command line.
 340   
      * @return the total number of arguments in the java command line.
 341   
      * @see #getCommandline()
 342   
      */
 343  23
     public int size() {
 344  23
         int size = getActualVMCommand().size() + javaCommand.size() + sysProperties.size();
 345   
         // classpath is "-classpath <classpath>" -> 2 args
 346  23
         Path fullClasspath = classpath != null ? classpath.concatSystemClasspath("ignore") : null;
 347  23
         if (fullClasspath != null && fullClasspath.toString().trim().length() > 0) {
 348  21
             size += 2;
 349   
         }
 350   
         // jar execution requires an additional -jar option
 351  23
         if (executeJar){
 352  1
             size++ ;
 353   
         }
 354  23
         return size;
 355   
     }
 356   
 
 357  22
     public Commandline getJavaCommand() {
 358  22
         return javaCommand;
 359   
     }
 360   
 
 361  5
     public Commandline getVmCommand() {
 362  5
         return getActualVMCommand();
 363   
     }
 364   
 
 365  141
     public Path getClasspath() {
 366  141
         return classpath;
 367   
     }
 368   
 
 369  1
     public void setSystemProperties() throws BuildException {
 370  1
         sysProperties.setSystem();
 371   
     }
 372   
 
 373  1
     public void restoreSystemProperties() throws BuildException {
 374  1
         sysProperties.restoreSystem();
 375   
     }
 376   
 
 377  141
     public SysProperties getSystemProperties() {
 378  141
         return sysProperties;
 379   
     }
 380   
 
 381   
     /**
 382   
      * clone the object; do a deep clone of all fields in the class
 383   
      * @return a CommandlineJava object
 384   
      */
 385  1
     public Object clone() {
 386  1
         CommandlineJava c = new CommandlineJava();
 387  1
         c.vmCommand = (Commandline) vmCommand.clone();
 388  1
         c.javaCommand = (Commandline) javaCommand.clone();
 389  1
         c.sysProperties = (SysProperties) sysProperties.clone();
 390  1
         c.maxMemory = maxMemory;
 391  1
         if (classpath != null) {
 392  0
             c.classpath = (Path) classpath.clone();
 393   
         }
 394  1
         c.vmVersion = vmVersion;
 395  1
         c.executeJar = executeJar;
 396  1
         return c;
 397   
     }
 398   
 
 399   
     /**
 400   
      * Clear out the java arguments.
 401   
      */
 402  0
     public void clearJavaArgs() {
 403  0
         javaCommand.clearArgs();
 404   
     }
 405   
 
 406   
 }
 407