1 18 19 package org.apache.tools.ant.types; 20 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.Project; 23 import org.apache.tools.ant.util.JavaEnvUtils; 24 25 import java.util.Enumeration ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.ListIterator ; 29 import java.util.Properties ; 30 import java.util.Vector ; 31 32 39 public class CommandlineJava implements Cloneable { 40 41 44 private Commandline vmCommand = new Commandline(); 45 48 private Commandline javaCommand = new Commandline(); 49 52 private SysProperties sysProperties = new SysProperties(); 53 private Path classpath = null; 54 private Path bootclasspath = null; 55 private String vmVersion; 56 private String maxMemory = null; 57 60 private Assertions assertions = null; 61 62 66 private boolean executeJar = false; 67 68 72 private boolean cloneVm = false; 73 74 77 public static class SysProperties extends Environment implements Cloneable { 78 80 Properties sys = null; 81 private Vector propertySets = new Vector (); 83 84 90 public String [] getVariables() throws BuildException { 91 92 List definitions = new LinkedList (); 93 ListIterator list = definitions.listIterator(); 94 addDefinitionsToList(list); 95 if (definitions.size() == 0) { 96 return null; 97 } else { 98 return (String []) definitions.toArray(new String [definitions.size()]); 99 } 100 } 101 102 106 public void addDefinitionsToList(ListIterator listIt) { 107 String [] props = super.getVariables(); 108 if (props != null) { 109 for (int i = 0; i < props.length; i++) { 110 listIt.add("-D" + props[i]); 111 } 112 } 113 Properties propertySetProperties = mergePropertySets(); 114 for (Enumeration e = propertySetProperties.keys(); 115 e.hasMoreElements();) { 116 String key = (String ) e.nextElement(); 117 String value = propertySetProperties.getProperty(key); 118 listIt.add("-D" + key + "=" + value); 119 } 120 } 121 122 127 public int size() { 128 Properties p = mergePropertySets(); 129 return variables.size() + p.size(); 130 } 131 132 137 public void setSystem() throws BuildException { 138 try { 139 sys = System.getProperties(); 140 Properties p = new Properties (); 141 for (Enumeration e = sys.propertyNames(); e.hasMoreElements();) { 142 String name = (String ) e.nextElement(); 143 p.put(name, sys.getProperty(name)); 144 } 145 p.putAll(mergePropertySets()); 146 for (Enumeration e = variables.elements(); e.hasMoreElements();) { 147 Environment.Variable v = (Environment.Variable) e.nextElement(); 148 v.validate(); 149 p.put(v.getKey(), v.getValue()); 150 } 151 System.setProperties(p); 152 } catch (SecurityException e) { 153 throw new BuildException("Cannot modify system properties", e); 154 } 155 } 156 157 162 public void restoreSystem() throws BuildException { 163 if (sys == null) { 164 throw new BuildException("Unbalanced nesting of SysProperties"); 165 } 166 167 try { 168 System.setProperties(sys); 169 sys = null; 170 } catch (SecurityException e) { 171 throw new BuildException("Cannot modify system properties", e); 172 } 173 } 174 175 180 public Object clone() throws CloneNotSupportedException { 181 try { 182 SysProperties c = (SysProperties) super.clone(); 183 c.variables = (Vector ) variables.clone(); 184 c.propertySets = (Vector ) propertySets.clone(); 185 return c; 186 } catch (CloneNotSupportedException e) { 187 return null; 188 } 189 } 190 191 195 public void addSyspropertyset(PropertySet ps) { 196 propertySets.addElement(ps); 197 } 198 199 204 public void addSysproperties(SysProperties ps) { 205 variables.addAll(ps.variables); 206 propertySets.addAll(ps.propertySets); 207 } 208 209 213 private Properties mergePropertySets() { 214 Properties p = new Properties (); 215 for (Enumeration e = propertySets.elements(); 216 e.hasMoreElements();) { 217 PropertySet ps = (PropertySet) e.nextElement(); 218 p.putAll(ps.getProperties()); 219 } 220 return p; 221 } 222 223 } 224 225 228 public CommandlineJava() { 229 setVm(JavaEnvUtils.getJreExecutable("java")); 230 setVmversion(JavaEnvUtils.getJavaVersion()); 231 } 232 233 237 public Commandline.Argument createArgument() { 238 return javaCommand.createArgument(); 239 } 240 241 245 public Commandline.Argument createVmArgument() { 246 return vmCommand.createArgument(); 247 } 248 249 253 public void addSysproperty(Environment.Variable sysp) { 254 sysProperties.addVariable(sysp); 255 } 256 257 261 public void addSyspropertyset(PropertySet sysp) { 262 sysProperties.addSyspropertyset(sysp); 263 } 264 265 270 public void addSysproperties(SysProperties sysp) { 271 sysProperties.addSysproperties(sysp); 272 } 273 274 278 public void setVm(String vm) { 279 vmCommand.setExecutable(vm); 280 } 281 282 286 public void setVmversion(String value) { 287 vmVersion = value; 288 } 289 290 297 public void setCloneVm(boolean cloneVm) { 298 this.cloneVm = cloneVm; 299 } 300 301 305 public Assertions getAssertions() { 306 return assertions; 307 } 308 309 313 public void setAssertions(Assertions assertions) { 314 this.assertions = assertions; 315 } 316 317 321 public void setJar(String jarpathname) { 322 javaCommand.setExecutable(jarpathname); 323 executeJar = true; 324 } 325 326 332 public String getJar() { 333 if (executeJar) { 334 return javaCommand.getExecutable(); 335 } 336 return null; 337 } 338 339 343 public void setClassname(String classname) { 344 javaCommand.setExecutable(classname); 345 executeJar = false; 346 } 347 348 353 public String getClassname() { 354 if (!executeJar) { 355 return javaCommand.getExecutable(); 356 } 357 return null; 358 } 359 360 365 public Path createClasspath(Project p) { 366 if (classpath == null) { 367 classpath = new Path(p); 368 } 369 return classpath; 370 } 371 372 378 public Path createBootclasspath(Project p) { 379 if (bootclasspath == null) { 380 bootclasspath = new Path(p); 381 } 382 return bootclasspath; 383 } 384 385 389 public String getVmversion() { 390 return vmVersion; 391 } 392 393 397 public String [] getCommandline() { 398 List commands = new LinkedList (); 400 final ListIterator listIterator = commands.listIterator(); 401 addCommandsToList(listIterator); 403 return (String []) commands.toArray(new String [commands.size()]); 405 } 406 407 412 private void addCommandsToList(final ListIterator listIterator) { 413 getActualVMCommand().addCommandToList(listIterator); 415 sysProperties.addDefinitionsToList(listIterator); 417 418 if (isCloneVm()) { 419 SysProperties clonedSysProperties = new SysProperties(); 420 PropertySet ps = new PropertySet(); 421 PropertySet.BuiltinPropertySetName sys = 422 new PropertySet.BuiltinPropertySetName(); 423 sys.setValue("system"); 424 ps.appendBuiltin(sys); 425 clonedSysProperties.addSyspropertyset(ps); 426 clonedSysProperties.addDefinitionsToList(listIterator); 427 } 428 Path bcp = calculateBootclasspath(true); 430 if (bcp.size() > 0) { 431 listIterator.add("-Xbootclasspath:" + bcp.toString()); 432 } 433 if (haveClasspath()) { 435 listIterator.add("-classpath"); 436 listIterator.add( 437 classpath.concatSystemClasspath("ignore").toString()); 438 } 439 if (getAssertions() != null) { 441 getAssertions().applyAssertions(listIterator); 442 } 443 if (executeJar) { 448 listIterator.add("-jar"); 449 } 450 javaCommand.addCommandToList(listIterator); 453 } 454 455 460 public void setMaxmemory(String max) { 461 this.maxMemory = max; 462 } 463 464 468 public String toString() { 469 return Commandline.toString(getCommandline()); 470 } 471 472 478 public String describeCommand() { 479 return Commandline.describeCommand(getCommandline()); 480 } 481 482 490 public String describeJavaCommand() { 491 return Commandline.describeCommand(getJavaCommand()); 492 } 493 494 498 protected Commandline getActualVMCommand() { 499 Commandline actualVMCommand = (Commandline) vmCommand.clone(); 500 if (maxMemory != null) { 501 if (vmVersion.startsWith("1.1")) { 502 actualVMCommand.createArgument().setValue("-mx" + maxMemory); 503 } else { 504 actualVMCommand.createArgument().setValue("-Xmx" + maxMemory); 505 } 506 } 507 return actualVMCommand; 508 } 509 510 519 public int size() { 520 int size = getActualVMCommand().size() + javaCommand.size() 521 + sysProperties.size(); 522 if (isCloneVm()) { 524 size += System.getProperties().size(); 525 } 526 if (haveClasspath()) { 528 size += 2; 529 } 530 if (calculateBootclasspath(true).size() > 0) { 532 size++; 533 } 534 if (executeJar) { 536 size++; 537 } 538 if (getAssertions() != null) { 540 size += getAssertions().size(); 541 } 542 return size; 543 } 544 545 549 public Commandline getJavaCommand() { 550 return javaCommand; 551 } 552 553 557 public Commandline getVmCommand() { 558 return getActualVMCommand(); 559 } 560 561 565 public Path getClasspath() { 566 return classpath; 567 } 568 569 573 public Path getBootclasspath() { 574 return bootclasspath; 575 } 576 577 582 public void setSystemProperties() throws BuildException { 583 sysProperties.setSystem(); 584 } 585 586 591 public void restoreSystemProperties() throws BuildException { 592 sysProperties.restoreSystem(); 593 } 594 595 599 public SysProperties getSystemProperties() { 600 return sysProperties; 601 } 602 603 609 public Object clone() throws CloneNotSupportedException { 610 try { 611 CommandlineJava c = (CommandlineJava) super.clone(); 612 c.vmCommand = (Commandline) vmCommand.clone(); 613 c.javaCommand = (Commandline) javaCommand.clone(); 614 c.sysProperties = (SysProperties) sysProperties.clone(); 615 if (classpath != null) { 616 c.classpath = (Path) classpath.clone(); 617 } 618 if (bootclasspath != null) { 619 c.bootclasspath = (Path) bootclasspath.clone(); 620 } 621 if (assertions != null) { 622 c.assertions = (Assertions) assertions.clone(); 623 } 624 return c; 625 } catch (CloneNotSupportedException e) { 626 throw new BuildException(e); 627 } 628 } 629 630 633 public void clearJavaArgs() { 634 javaCommand.clearArgs(); 635 } 636 637 643 protected boolean haveClasspath() { 644 Path fullClasspath = classpath != null 645 ? classpath.concatSystemClasspath("ignore") : null; 646 return fullClasspath != null 647 && fullClasspath.toString().trim().length() > 0; 648 } 649 650 660 protected boolean haveBootclasspath(boolean log) { 661 return calculateBootclasspath(log).size() > 0; 662 } 663 664 671 private Path calculateBootclasspath(boolean log) { 672 if (vmVersion.startsWith("1.1")) { 673 if (bootclasspath != null && log) { 674 bootclasspath.log("Ignoring bootclasspath as " 675 + "the target VM doesn't support it."); 676 } 677 } else { 678 if (bootclasspath != null) { 679 return bootclasspath.concatSystemBootClasspath(isCloneVm() 680 ? "last" 681 : "ignore"); 682 } else if (isCloneVm()) { 683 return Path.systemBootClasspath; 684 } 685 } 686 return new Path(null); 687 } 688 689 695 private boolean isCloneVm() { 696 return cloneVm 697 || "true".equals(System.getProperty("ant.build.clonevm")); 698 } 699 } 700 | Popular Tags |