1 18 19 package org.apache.tools.ant.types; 20 21 import java.io.File ; 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 import java.util.Locale ; 25 import java.util.Stack ; 26 import java.util.Vector ; 27 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.PathTokenizer; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.types.resources.Union; 32 import org.apache.tools.ant.types.resources.FileResourceIterator; 33 import org.apache.tools.ant.util.FileUtils; 34 import org.apache.tools.ant.util.JavaEnvUtils; 35 36 63 64 public class Path extends DataType implements Cloneable , ResourceCollection { 65 67 68 public static Path systemClasspath = 69 new Path(null, System.getProperty("java.class.path")); 70 71 72 77 public static Path systemBootClasspath = 78 new Path(null, System.getProperty("sun.boot.class.path")); 79 80 private static final Iterator EMPTY_ITERATOR 81 = Collections.EMPTY_SET.iterator(); 82 83 85 88 public class PathElement implements ResourceCollection { 89 private String [] parts; 90 91 96 public void setLocation(File loc) { 97 parts = new String [] {translateFile(loc.getAbsolutePath())}; 98 } 99 100 105 public void setPath(String path) { 106 parts = Path.translatePath(getProject(), path); 107 } 108 109 114 public String [] getParts() { 115 return parts; 116 } 117 118 122 public Iterator iterator() { 123 return new FileResourceIterator(null, parts); 124 } 125 126 130 public boolean isFilesystemOnly() { 131 return true; 132 } 133 134 138 public int size() { 139 return parts == null ? 0 : parts.length; 140 } 141 142 } 143 144 private Union union = null; 145 146 152 public Path(Project p, String path) { 153 this(p); 154 createPathElement().setPath(path); 155 } 156 157 161 public Path(Project project) { 162 setProject(project); 163 } 164 165 171 public void setLocation(File location) throws BuildException { 172 checkAttributesAllowed(); 173 createPathElement().setLocation(location); 174 } 175 176 181 public void setPath(String path) throws BuildException { 182 checkAttributesAllowed(); 183 createPathElement().setPath(path); 184 } 185 186 194 public void setRefid(Reference r) throws BuildException { 195 if (union != null) { 196 throw tooManyAttributes(); 197 } 198 super.setRefid(r); 199 } 200 201 206 public PathElement createPathElement() throws BuildException { 207 if (isReference()) { 208 throw noChildrenAllowed(); 209 } 210 PathElement pe = new PathElement(); 211 add(pe); 212 return pe; 213 } 214 215 220 public void addFileset(FileSet fs) throws BuildException { 221 if (fs.getProject() == null) { 222 fs.setProject(getProject()); 223 } 224 add(fs); 225 } 226 227 232 public void addFilelist(FileList fl) throws BuildException { 233 if (fl.getProject() == null) { 234 fl.setProject(getProject()); 235 } 236 add(fl); 237 } 238 239 244 public void addDirset(DirSet dset) throws BuildException { 245 if (dset.getProject() == null) { 246 dset.setProject(getProject()); 247 } 248 add(dset); 249 } 250 251 257 public void add(Path path) throws BuildException { 258 if (path == this) { 259 throw circularReference(); 260 } 261 if (path.getProject() == null) { 262 path.setProject(getProject()); 263 } 264 add((ResourceCollection) path); 265 } 266 267 272 public void add(ResourceCollection c) { 273 checkChildrenAllowed(); 274 if (c == null) { 275 return; 276 } 277 if (union == null) { 278 union = new Union(); 279 union.setProject(getProject()); 280 union.setCache(false); 281 } 282 union.add(c); 283 setChecked(false); 284 } 285 286 291 public Path createPath() throws BuildException { 292 Path p = new Path(getProject()); 293 add(p); 294 return p; 295 } 296 297 301 public void append(Path other) { 302 if (other == null) { 303 return; 304 } 305 add(other); 306 } 307 308 314 public void addExisting(Path source) { 315 addExisting(source, false); 316 } 317 318 326 public void addExisting(Path source, boolean tryUserDir) { 327 String [] list = source.list(); 328 File userDir = (tryUserDir) ? new File (System.getProperty("user.dir")) 329 : null; 330 331 for (int i = 0; i < list.length; i++) { 332 File f = resolveFile(getProject(), list[i]); 333 334 if (tryUserDir && !f.exists()) { 337 f = new File (userDir, list[i]); 338 } 339 if (f.exists()) { 340 setLocation(f); 341 } else { 342 log("dropping " + f + " from path as it doesn't exist", 343 Project.MSG_VERBOSE); 344 } 345 } 346 } 347 348 352 public String [] list() { 353 if (isReference()) { 354 return ((Path) getCheckedRef()).list(); 355 } 356 return assertFilesystemOnly(union) == null 357 ? new String [0] : union.list(); 358 } 359 360 365 public String toString() { 366 return isReference() ? getCheckedRef().toString() 367 : union == null ? "" : union.toString(); 368 } 369 370 376 public static String [] translatePath(Project project, String source) { 377 final Vector result = new Vector (); 378 if (source == null) { 379 return new String [0]; 380 } 381 PathTokenizer tok = new PathTokenizer(source); 382 StringBuffer element = new StringBuffer (); 383 while (tok.hasMoreTokens()) { 384 String pathElement = tok.nextToken(); 385 try { 386 element.append(resolveFile(project, pathElement).getPath()); 387 } catch (BuildException e) { 388 project.log("Dropping path element " + pathElement 389 + " as it is not valid relative to the project", 390 Project.MSG_VERBOSE); 391 } 392 for (int i = 0; i < element.length(); i++) { 393 translateFileSep(element, i); 394 } 395 result.addElement(element.toString()); 396 element = new StringBuffer (); 397 } 398 String [] res = new String [result.size()]; 399 result.copyInto(res); 400 return res; 401 } 402 403 409 public static String translateFile(String source) { 410 if (source == null) { 411 return ""; 412 } 413 final StringBuffer result = new StringBuffer (source); 414 for (int i = 0; i < result.length(); i++) { 415 translateFileSep(result, i); 416 } 417 return result.toString(); 418 } 419 420 428 protected static boolean translateFileSep(StringBuffer buffer, int pos) { 429 if (buffer.charAt(pos) == '/' || buffer.charAt(pos) == '\\') { 430 buffer.setCharAt(pos, File.separatorChar); 431 return true; 432 } 433 return false; 434 } 435 436 440 public synchronized int size() { 441 if (isReference()) { 442 return ((Path) getCheckedRef()).size(); 443 } 444 dieOnCircularReference(); 445 return union == null ? 0 : assertFilesystemOnly(union).size(); 446 } 447 448 452 public Object clone() { 453 try { 454 Path result = (Path) super.clone(); 455 result.union = union == null ? union : (Union) union.clone(); 456 return result; 457 } catch (CloneNotSupportedException e) { 458 throw new BuildException(e); 459 } 460 } 461 462 469 protected synchronized void dieOnCircularReference(Stack stk, Project p) 470 throws BuildException { 471 if (isChecked()) { 472 return; 473 } 474 if (isReference()) { 475 super.dieOnCircularReference(stk, p); 476 } else { 477 if (union != null) { 478 stk.push(union); 479 invokeCircularReferenceCheck(union, stk, p); 480 stk.pop(); 481 } 482 setChecked(true); 483 } 484 } 485 486 489 private static File resolveFile(Project project, String relativeName) { 490 return FileUtils.getFileUtils().resolveFile( 491 (project == null) ? null : project.getBaseDir(), relativeName); 492 } 493 494 500 public Path concatSystemClasspath() { 501 return concatSystemClasspath("last"); 502 } 503 504 511 public Path concatSystemClasspath(String defValue) { 512 return concatSpecialPath(defValue, Path.systemClasspath); 513 } 514 515 522 public Path concatSystemBootClasspath(String defValue) { 523 return concatSpecialPath(defValue, Path.systemBootClasspath); 524 } 525 526 531 private Path concatSpecialPath(String defValue, Path p) { 532 Path result = new Path(getProject()); 533 534 String order = defValue; 535 if (getProject() != null) { 536 String o = getProject().getProperty("build.sysclasspath"); 537 if (o != null) { 538 order = o; 539 } 540 } 541 if (order.equals("only")) { 542 result.addExisting(p, true); 544 545 } else if (order.equals("first")) { 546 result.addExisting(p, true); 548 result.addExisting(this); 549 550 } else if (order.equals("ignore")) { 551 result.addExisting(this); 553 554 } else { 555 if (!order.equals("last")) { 557 log("invalid value for build.sysclasspath: " + order, 558 Project.MSG_WARN); 559 } 560 result.addExisting(this); 561 result.addExisting(p, true); 562 } 563 return result; 564 } 565 566 569 public void addJavaRuntime() { 570 if (JavaEnvUtils.isKaffe()) { 571 File kaffeShare = new File (System.getProperty("java.home") 574 + File.separator + "share" 575 + File.separator + "kaffe"); 576 if (kaffeShare.isDirectory()) { 577 FileSet kaffeJarFiles = new FileSet(); 578 kaffeJarFiles.setDir(kaffeShare); 579 kaffeJarFiles.setIncludes("*.jar"); 580 addFileset(kaffeJarFiles); 581 } 582 } else if ("GNU libgcj".equals(System.getProperty("java.vm.name"))) { 583 addExisting(systemBootClasspath); 584 } 585 586 if (System.getProperty("java.vendor").toLowerCase(Locale.US).indexOf("microsoft") >= 0) { 587 FileSet msZipFiles = new FileSet(); 590 msZipFiles.setDir(new File (System.getProperty("java.home") 591 + File.separator + "Packages")); 592 msZipFiles.setIncludes("*.ZIP"); 593 addFileset(msZipFiles); 594 } else { 595 addExisting(new Path(null, 597 System.getProperty("java.home") 598 + File.separator + "lib" 599 + File.separator + "rt.jar")); 600 addExisting(new Path(null, 603 System.getProperty("java.home") 604 + File.separator + "jre" 605 + File.separator + "lib" 606 + File.separator + "rt.jar")); 607 608 String [] secJars = {"jce", "jsse"}; 610 for (int i = 0; i < secJars.length; i++) { 611 addExisting(new Path(null, 612 System.getProperty("java.home") 613 + File.separator + "lib" 614 + File.separator + secJars[i] + ".jar")); 615 addExisting(new Path(null, 616 System.getProperty("java.home") 617 + File.separator + ".." 618 + File.separator + "Classes" 619 + File.separator + secJars[i] + ".jar")); 620 } 621 622 String [] ibmJars 625 = {"core", "graphics", "security", "server", "xml"}; 626 for (int i = 0; i < ibmJars.length; i++) { 627 addExisting(new Path(null, 628 System.getProperty("java.home") 629 + File.separator + "lib" 630 + File.separator + ibmJars[i] + ".jar")); 631 } 632 633 addExisting(new Path(null, 635 System.getProperty("java.home") 636 + File.separator + ".." 637 + File.separator + "Classes" 638 + File.separator + "classes.jar")); 639 addExisting(new Path(null, 640 System.getProperty("java.home") 641 + File.separator + ".." 642 + File.separator + "Classes" 643 + File.separator + "ui.jar")); 644 } 645 } 646 647 654 public void addExtdirs(Path extdirs) { 655 if (extdirs == null) { 656 String extProp = System.getProperty("java.ext.dirs"); 657 if (extProp != null) { 658 extdirs = new Path(getProject(), extProp); 659 } else { 660 return; 661 } 662 } 663 664 String [] dirs = extdirs.list(); 665 for (int i = 0; i < dirs.length; i++) { 666 File dir = resolveFile(getProject(), dirs[i]); 667 if (dir.exists() && dir.isDirectory()) { 668 FileSet fs = new FileSet(); 669 fs.setDir(dir); 670 fs.setIncludes("*"); 671 addFileset(fs); 672 } 673 } 674 } 675 676 682 public final synchronized Iterator iterator() { 683 if (isReference()) { 684 return ((Path) getCheckedRef()).iterator(); 685 } 686 dieOnCircularReference(); 687 return union == null ? EMPTY_ITERATOR 688 : assertFilesystemOnly(union).iterator(); 689 } 690 691 695 public synchronized boolean isFilesystemOnly() { 696 if (isReference()) { 697 return ((Path) getCheckedRef()).isFilesystemOnly(); 698 } 699 dieOnCircularReference(); 700 assertFilesystemOnly(union); 701 return true; 702 } 703 704 710 protected ResourceCollection assertFilesystemOnly(ResourceCollection rc) { 711 if (rc != null && !(rc.isFilesystemOnly())) { 712 throw new BuildException(getDataTypeName() 713 + " allows only filesystem resources."); 714 } 715 return rc; 716 } 717 } 718 | Popular Tags |