| 1 18 19 package org.apache.tools.ant; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.EOFException ; 24 import java.io.InputStream ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.Modifier ; 27 import java.util.Collections ; 28 import java.util.Enumeration ; 29 import java.util.Hashtable ; 30 import java.util.Iterator ; 31 import java.util.Properties ; 32 import java.util.Stack ; 33 import java.util.Vector ; 34 import java.util.Set ; 35 import java.util.HashSet ; 36 import java.util.HashMap ; 37 import java.util.Map ; 38 import java.util.WeakHashMap ; 39 import org.apache.tools.ant.input.DefaultInputHandler; 40 import org.apache.tools.ant.input.InputHandler; 41 import org.apache.tools.ant.helper.DefaultExecutor; 42 import org.apache.tools.ant.types.FilterSet; 43 import org.apache.tools.ant.types.FilterSetCollection; 44 import org.apache.tools.ant.types.Description; 45 import org.apache.tools.ant.types.Path; 46 import org.apache.tools.ant.types.Resource; 47 import org.apache.tools.ant.types.ResourceFactory; 48 import org.apache.tools.ant.types.resources.FileResource; 49 import org.apache.tools.ant.util.FileUtils; 50 import org.apache.tools.ant.util.JavaEnvUtils; 51 import org.apache.tools.ant.util.StringUtils; 52 53 64 public class Project implements ResourceFactory { 65 private static final String LINE_SEP = System.getProperty("line.separator"); 66 67 68 public static final int MSG_ERR = 0; 69 70 public static final int MSG_WARN = 1; 71 72 public static final int MSG_INFO = 2; 73 74 public static final int MSG_VERBOSE = 3; 75 76 public static final int MSG_DEBUG = 4; 77 78 82 private static final String VISITING = "VISITING"; 83 87 private static final String VISITED = "VISITED"; 88 89 95 public static final String JAVA_1_0 = JavaEnvUtils.JAVA_1_0; 96 102 public static final String JAVA_1_1 = JavaEnvUtils.JAVA_1_1; 103 109 public static final String JAVA_1_2 = JavaEnvUtils.JAVA_1_2; 110 116 public static final String JAVA_1_3 = JavaEnvUtils.JAVA_1_3; 117 123 public static final String JAVA_1_4 = JavaEnvUtils.JAVA_1_4; 124 125 126 public static final String TOKEN_START = FilterSet.DEFAULT_TOKEN_START; 127 128 public static final String TOKEN_END = FilterSet.DEFAULT_TOKEN_END; 129 130 131 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 132 133 134 private String name; 135 136 private String description; 137 138 139 140 private Hashtable references = new AntRefTable(); 141 142 143 private HashMap idReferences = new HashMap (); 144 145 146 private Project parentIdProject = null; 147 148 149 private String defaultTarget; 150 151 152 private Hashtable targets = new Hashtable (); 153 154 private FilterSet globalFilterSet = new FilterSet(); 155 { 156 globalFilterSet.setProject(this); 158 } 159 160 165 private FilterSetCollection globalFilters 166 = new FilterSetCollection(globalFilterSet); 167 168 169 private File baseDir; 170 171 172 private Vector listeners = new Vector (); 173 174 178 private ClassLoader coreLoader = null; 179 180 181 private Map threadTasks = Collections.synchronizedMap(new WeakHashMap ()); 182 183 184 private Map threadGroupTasks 185 = Collections.synchronizedMap(new WeakHashMap ()); 186 187 190 private InputHandler inputHandler = null; 191 192 195 private InputStream defaultInputStream = null; 196 197 200 private boolean keepGoingMode = false; 201 202 205 private boolean loggingMessage = false; 206 207 212 public void setInputHandler(InputHandler handler) { 213 inputHandler = handler; 214 } 215 216 225 public void setDefaultInputStream(InputStream defaultInputStream) { 226 this.defaultInputStream = defaultInputStream; 227 } 228 229 235 public InputStream getDefaultInputStream() { 236 return defaultInputStream; 237 } 238 239 245 public InputHandler getInputHandler() { 246 return inputHandler; 247 } 248 249 252 public Project() { 253 inputHandler = new DefaultInputHandler(); 254 } 255 256 263 public Project createSubProject() { 264 Project subProject = null; 265 try { 266 subProject = (Project) (getClass().newInstance()); 267 } catch (Exception e) { 268 subProject = new Project(); 269 } 270 initSubProject(subProject); 271 return subProject; 272 } 273 274 278 public void initSubProject(Project subProject) { 279 ComponentHelper.getComponentHelper(subProject) 280 .initSubProject(ComponentHelper.getComponentHelper(this)); 281 subProject.setDefaultInputStream(getDefaultInputStream()); 282 subProject.setKeepGoingMode(this.isKeepGoingMode()); 283 subProject.setExecutor(getExecutor().getSubProjectExecutor()); 284 } 285 286 294 public void init() throws BuildException { 295 initProperties(); 296 297 ComponentHelper.getComponentHelper(this).initDefaultDefinitions(); 298 } 299 300 305 public void initProperties() throws BuildException { 306 setJavaVersionProperty(); 307 setSystemProperties(); 308 setPropertyInternal(MagicNames.ANT_VERSION, Main.getAntVersion()); 309 setAntLib(); 310 } 311 312 private void setAntLib() { 313 File antlib = org.apache.tools.ant.launch.Locator.getClassSource( 314 Project.class); 315 if (antlib != null) { 316 setPropertyInternal(MagicNames.ANT_LIB, antlib.getAbsolutePath()); 317 } 318 } 319 327 public AntClassLoader createClassLoader(Path path) { 328 return new AntClassLoader( 329 getClass().getClassLoader(), this, path); 330 } 331 332 341 public AntClassLoader createClassLoader( 342 ClassLoader parent, Path path) { 343 return new AntClassLoader(parent, this, path); 344 } 345 346 353 public void setCoreLoader(ClassLoader coreLoader) { 354 this.coreLoader = coreLoader; 355 } 356 357 365 public ClassLoader getCoreLoader() { 366 return coreLoader; 367 } 368 369 376 public synchronized void addBuildListener(BuildListener listener) { 377 if (listeners.contains(listener)) { 379 return; 380 } 381 Vector newListeners = getBuildListeners(); 384 newListeners.addElement(listener); 385 listeners = newListeners; 386 } 387 388 395 public synchronized void removeBuildListener(BuildListener listener) { 396 Vector newListeners = getBuildListeners(); 399 newListeners.removeElement(listener); 400 listeners = newListeners; 401 } 402 403 408 public Vector getBuildListeners() { 409 return (Vector ) listeners.clone(); 410 } 411 412 417 418 public void log(String message) { 419 log(message, MSG_INFO); 420 } 421 422 427 public void log(String message, int msgLevel) { 428 log(message, null, msgLevel); 429 } 430 431 438 public void log(String message, Throwable throwable, int msgLevel) { 439 fireMessageLogged(this, message, throwable, msgLevel); 440 } 441 442 448 public void log(Task task, String message, int msgLevel) { 449 fireMessageLogged(task, message, null, msgLevel); 450 } 451 452 460 public void log(Task task, String message, Throwable throwable, int msgLevel) { 461 fireMessageLogged(task, message, throwable, msgLevel); 462 } 463 464 471 public void log(Target target, String message, int msgLevel) { 472 log(target, message, null, msgLevel); 473 } 474 475 484 public void log(Target target, String message, Throwable throwable, 485 int msgLevel) { 486 fireMessageLogged(target, message, throwable, msgLevel); 487 } 488 489 494 public FilterSet getGlobalFilterSet() { 495 return globalFilterSet; 496 } 497 498 506 public void setProperty(String name, String value) { 507 PropertyHelper.getPropertyHelper(this). 508 setProperty(null, name, value, true); 509 } 510 511 522 public void setNewProperty(String name, String value) { 523 PropertyHelper.getPropertyHelper(this).setNewProperty(null, name, 524 value); 525 } 526 527 536 public void setUserProperty(String name, String value) { 537 PropertyHelper.getPropertyHelper(this).setUserProperty(null, name, 538 value); 539 } 540 541 553 public void setInheritedProperty(String name, String value) { 554 PropertyHelper ph = PropertyHelper.getPropertyHelper(this); 555 ph.setInheritedProperty(null, name, value); 556 } 557 558 566 private void setPropertyInternal(String name, String value) { 567 PropertyHelper ph = PropertyHelper.getPropertyHelper(this); 568 ph.setProperty(null, name, value, false); 569 } 570 571 580 public String getProperty(String propertyName) { 581 PropertyHelper ph = PropertyHelper.getPropertyHelper(this); 582 return (String ) ph.getProperty(null, propertyName); 583 } 584 585 599 public String replaceProperties(String value) 600 throws BuildException { 601 PropertyHelper ph = PropertyHelper.getPropertyHelper(this); 602 return ph.replaceProperties(null, value, null); 603 } 604 605 614 public String getUserProperty(String propertyName) { 615 PropertyHelper ph = PropertyHelper.getPropertyHelper(this); 616 return (String ) ph.getUserProperty(null, propertyName); 617 } 618 619 624 public Hashtable getProperties() { 625 PropertyHelper ph = PropertyHelper.getPropertyHelper(this); 626 return ph.getProperties(); 627 } 628 629 633 public Hashtable getUserProperties() { 634 PropertyHelper ph = PropertyHelper.getPropertyHelper(this); 635 return ph.getUserProperties(); 636 } 637 638 650 public void copyUserProperties(Project other) { 651 PropertyHelper ph = PropertyHelper.getPropertyHelper(this); 652 ph.copyUserProperties(other); 653 } 654 655 667 public void copyInheritedProperties(Project other) { 668 PropertyHelper ph = PropertyHelper.getPropertyHelper(this); 669 ph.copyInheritedProperties(other); 670 } 671 672 683 public void setDefaultTarget(String defaultTarget) { 684 this.defaultTarget = defaultTarget; 685 } 686 687 692 public String getDefaultTarget() { 693 return defaultTarget; 694 } 695 696 703 public void setDefault(String defaultTarget) { 704 this.defaultTarget = defaultTarget; 705 } 706 707 714 public void setName(String name) { 715 setUserProperty("ant.project.name", name); 716 this.name = name; 717 } 718 719 724 public String getName() { 725 return name; 726 } 727 728 734 public void setDescription(String description) { 735 this.description = description; 736 } 737 738 744 public String getDescription() { 745 if (description == null) { 746 description = Description.getDescription(this); 747 } 748 return description; 749 } 750 751 764 public void addFilter(String token, String value) { 765 if (token == null) { 766 return; 767 } 768 globalFilterSet.addFilter(new FilterSet.Filter(token, value)); 769 } 770 771 783 public Hashtable getFilters() { 784 return globalFilterSet.getFilterHash(); 786 } 787 788 797 public void setBasedir(String baseD) throws BuildException { 798 setBaseDir(new File (baseD)); 799 } 800 801 810 public void setBaseDir(File baseDir) throws BuildException { 811 baseDir = FILE_UTILS.normalize(baseDir.getAbsolutePath()); 812 if (!baseDir.exists()) { 813 throw new BuildException("Basedir " + baseDir.getAbsolutePath() 814 + " does not exist"); 815 } 816 if (!baseDir.isDirectory()) { 817 throw new BuildException("Basedir " + baseDir.getAbsolutePath() 818 + " is not a directory"); 819 } 820 this.baseDir = baseDir; 821 setPropertyInternal(MagicNames.PROJECT_BASEDIR, this.baseDir.getPath()); 822 String msg = "Project base dir set to: " + this.baseDir; 823 log(msg, MSG_VERBOSE); 824 } 825 826 832 public File getBaseDir() { 833 if (baseDir == null) { 834 try { 835 setBasedir("."); 836 } catch (BuildException ex) { 837 ex.printStackTrace(); 838 } 839 } 840 return baseDir; 841 } 842 843 852 public void setKeepGoingMode(boolean keepGoingMode) { 853 this.keepGoingMode = keepGoingMode; 854 } 855 856 863 public boolean isKeepGoingMode() { 864 return this.keepGoingMode; 865 } 866 867 874 public static String getJavaVersion() { 875 return JavaEnvUtils.getJavaVersion(); 876 } 877 878 888 public void setJavaVersionProperty() throws BuildException { 889 String javaVersion = JavaEnvUtils.getJavaVersion(); 890 setPropertyInternal(MagicNames.ANT_JAVA_VERSION, javaVersion); 891 892 if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_0) 894 || JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) { 895 throw new BuildException("Ant cannot work on Java 1.0 / 1.1"); 896 } 897 log("Detected Java version: " + javaVersion + " in: " 898 + System.getProperty("java.home"), MSG_VERBOSE); 899 900 log("Detected OS: " + System.getProperty("os.name"), MSG_VERBOSE); 901 } 902 903 907 public void setSystemProperties() { 908 &nb
|