You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Project.java 81 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190
  1. /*
  2. * Copyright 2000-2005 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.io.EOFException;
  21. import java.io.InputStream;
  22. import java.lang.reflect.Method;
  23. import java.lang.reflect.Modifier;
  24. import java.util.Collections;
  25. import java.util.Enumeration;
  26. import java.util.Hashtable;
  27. import java.util.Iterator;
  28. import java.util.Properties;
  29. import java.util.Stack;
  30. import java.util.Vector;
  31. import java.util.Set;
  32. import java.util.HashSet;
  33. import java.util.Map;
  34. import java.util.WeakHashMap;
  35. import org.apache.tools.ant.input.DefaultInputHandler;
  36. import org.apache.tools.ant.input.InputHandler;
  37. import org.apache.tools.ant.helper.DefaultExecutor;
  38. import org.apache.tools.ant.types.FilterSet;
  39. import org.apache.tools.ant.types.FilterSetCollection;
  40. import org.apache.tools.ant.types.Description;
  41. import org.apache.tools.ant.types.Path;
  42. import org.apache.tools.ant.util.FileUtils;
  43. import org.apache.tools.ant.util.JavaEnvUtils;
  44. import org.apache.tools.ant.util.StringUtils;
  45. /**
  46. * Central representation of an Ant project. This class defines an
  47. * Ant project with all of its targets, tasks and various other
  48. * properties. It also provides the mechanism to kick off a build using
  49. * a particular target name.
  50. * <p>
  51. * This class also encapsulates methods which allow files to be referred
  52. * to using abstract path names which are translated to native system
  53. * file paths at runtime.
  54. *
  55. */
  56. public class Project {
  57. /** Message priority of &quot;error&quot;. */
  58. public static final int MSG_ERR = 0;
  59. /** Message priority of &quot;warning&quot;. */
  60. public static final int MSG_WARN = 1;
  61. /** Message priority of &quot;information&quot;. */
  62. public static final int MSG_INFO = 2;
  63. /** Message priority of &quot;verbose&quot;. */
  64. public static final int MSG_VERBOSE = 3;
  65. /** Message priority of &quot;debug&quot;. */
  66. public static final int MSG_DEBUG = 4;
  67. /**
  68. * Constant for the &quot;visiting&quot; state, used when
  69. * traversing a DFS of target dependencies.
  70. */
  71. private static final String VISITING = "VISITING";
  72. /**
  73. * Constant for the &quot;visited&quot; state, used when
  74. * traversing a DFS of target dependencies.
  75. */
  76. private static final String VISITED = "VISITED";
  77. /**
  78. * Version constant for Java 1.0 .
  79. *
  80. * @deprecated Use {@link JavaEnvUtils#JAVA_1_0} instead.
  81. */
  82. public static final String JAVA_1_0 = JavaEnvUtils.JAVA_1_0;
  83. /**
  84. * Version constant for Java 1.1 .
  85. *
  86. * @deprecated Use {@link JavaEnvUtils#JAVA_1_1} instead.
  87. */
  88. public static final String JAVA_1_1 = JavaEnvUtils.JAVA_1_1;
  89. /**
  90. * Version constant for Java 1.2 .
  91. *
  92. * @deprecated Use {@link JavaEnvUtils#JAVA_1_2} instead.
  93. */
  94. public static final String JAVA_1_2 = JavaEnvUtils.JAVA_1_2;
  95. /**
  96. * Version constant for Java 1.3 .
  97. *
  98. * @deprecated Use {@link JavaEnvUtils#JAVA_1_3} instead.
  99. */
  100. public static final String JAVA_1_3 = JavaEnvUtils.JAVA_1_3;
  101. /**
  102. * Version constant for Java 1.4 .
  103. *
  104. * @deprecated Use {@link JavaEnvUtils#JAVA_1_4} instead.
  105. */
  106. public static final String JAVA_1_4 = JavaEnvUtils.JAVA_1_4;
  107. /** Default filter start token. */
  108. public static final String TOKEN_START = FilterSet.DEFAULT_TOKEN_START;
  109. /** Default filter end token. */
  110. public static final String TOKEN_END = FilterSet.DEFAULT_TOKEN_END;
  111. /** Instance of a utility class to use for file operations. */
  112. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  113. /** Name of this project. */
  114. private String name;
  115. /** Description for this project (if any). */
  116. private String description;
  117. /** Map of references within the project (paths etc) (String to Object). */
  118. private Hashtable references = new AntRefTable();
  119. /** Name of the project's default target. */
  120. private String defaultTarget;
  121. /** Map from target names to targets (String to Target). */
  122. private Hashtable targets = new Hashtable();
  123. /** Set of global filters. */
  124. private FilterSet globalFilterSet = new FilterSet();
  125. {
  126. // Initialize the globalFileSet's project
  127. globalFilterSet.setProject(this);
  128. }
  129. /**
  130. * Wrapper around globalFilterSet. This collection only ever
  131. * contains one FilterSet, but the wrapper is needed in order to
  132. * make it easier to use the FileUtils interface.
  133. */
  134. private FilterSetCollection globalFilters
  135. = new FilterSetCollection(globalFilterSet);
  136. /** Project base directory. */
  137. private File baseDir;
  138. /** List of listeners to notify of build events. */
  139. private Vector listeners = new Vector();
  140. /**
  141. * The Ant core classloader--may be <code>null</code> if using
  142. * parent classloader.
  143. */
  144. private ClassLoader coreLoader = null;
  145. /** Records the latest task to be executed on a thread. */
  146. private Map/*<Thread,Task>*/ threadTasks = Collections.synchronizedMap(new WeakHashMap());
  147. /** Records the latest task to be executed on a thread group. */
  148. private Map/*<ThreadGroup,Task>*/ threadGroupTasks = Collections.synchronizedMap(new WeakHashMap());
  149. /**
  150. * Called to handle any input requests.
  151. */
  152. private InputHandler inputHandler = null;
  153. /**
  154. * The default input stream used to read any input.
  155. */
  156. private InputStream defaultInputStream = null;
  157. /**
  158. * Keep going flag.
  159. */
  160. private boolean keepGoingMode = false;
  161. /**
  162. * Flag which catches Listeners which try to use System.out or System.err .
  163. */
  164. private boolean loggingMessage = false;
  165. public static final String ANT_JAVA_VERSION = "ant.java.version";
  166. /**
  167. * Set the input handler.
  168. *
  169. * @param handler the InputHandler instance to use for gathering input.
  170. */
  171. public void setInputHandler(InputHandler handler) {
  172. inputHandler = handler;
  173. }
  174. /**
  175. * Set the default System input stream. Normally this stream is set to
  176. * System.in. This inputStream is used when no task input redirection is
  177. * being performed.
  178. *
  179. * @param defaultInputStream the default input stream to use when input
  180. * is requested.
  181. * @since Ant 1.6
  182. */
  183. public void setDefaultInputStream(InputStream defaultInputStream) {
  184. this.defaultInputStream = defaultInputStream;
  185. }
  186. /**
  187. * Get this project's input stream.
  188. *
  189. * @return the InputStream instance in use by this Project instance to
  190. * read input.
  191. */
  192. public InputStream getDefaultInputStream() {
  193. return defaultInputStream;
  194. }
  195. /**
  196. * Retrieve the current input handler.
  197. *
  198. * @return the InputHandler instance currently in place for the project
  199. * instance.
  200. */
  201. public InputHandler getInputHandler() {
  202. return inputHandler;
  203. }
  204. /**
  205. * Create a new Ant project.
  206. */
  207. public Project() {
  208. inputHandler = new DefaultInputHandler();
  209. }
  210. /**
  211. * Create and initialize a subproject. By default the subproject will be of
  212. * the same type as its parent. If a no-arg constructor is unavailable, the
  213. * <code>Project</code> class will be used.
  214. * @return a Project instance configured as a subproject of this Project.
  215. * @since Ant 1.7
  216. */
  217. public Project createSubProject() {
  218. Project subProject = null;
  219. try {
  220. subProject = (Project) (getClass().newInstance());
  221. } catch (Exception e) {
  222. subProject = new Project();
  223. }
  224. initSubProject(subProject);
  225. return subProject;
  226. }
  227. /**
  228. * Initialize a subproject.
  229. * @param subProject the subproject to initialize.
  230. */
  231. public void initSubProject(Project subProject) {
  232. ComponentHelper.getComponentHelper(subProject)
  233. .initSubProject(ComponentHelper.getComponentHelper(this));
  234. subProject.setDefaultInputStream(getDefaultInputStream());
  235. subProject.setKeepGoingMode(this.isKeepGoingMode());
  236. subProject.setExecutor(getExecutor().getSubProjectExecutor());
  237. }
  238. /**
  239. * Initialise the project.
  240. *
  241. * This involves setting the default task definitions and loading the
  242. * system properties.
  243. *
  244. * @exception BuildException if the default task list cannot be loaded.
  245. */
  246. public void init() throws BuildException {
  247. setJavaVersionProperty();
  248. ComponentHelper.getComponentHelper(this).initDefaultDefinitions();
  249. setSystemProperties();
  250. }
  251. /**
  252. * Factory method to create a class loader for loading classes from
  253. * a given path.
  254. *
  255. * @param path the path from which classes are to be loaded.
  256. *
  257. * @return an appropriate classloader.
  258. */
  259. public AntClassLoader createClassLoader(Path path) {
  260. AntClassLoader loader = new AntClassLoader();
  261. loader.setProject(this);
  262. loader.setClassPath(path);
  263. return loader;
  264. }
  265. /**
  266. * Set the core classloader for the project. If a <code>null</code>
  267. * classloader is specified, the parent classloader should be used.
  268. *
  269. * @param coreLoader The classloader to use for the project.
  270. * May be <code>null</code>.
  271. */
  272. public void setCoreLoader(ClassLoader coreLoader) {
  273. this.coreLoader = coreLoader;
  274. }
  275. /**
  276. * Return the core classloader to use for this project.
  277. * This may be <code>null</code>, indicating that
  278. * the parent classloader should be used.
  279. *
  280. * @return the core classloader to use for this project.
  281. *
  282. */
  283. public ClassLoader getCoreLoader() {
  284. return coreLoader;
  285. }
  286. /**
  287. * Add a build listener to the list. This listener will
  288. * be notified of build events for this project.
  289. *
  290. * @param listener The listener to add to the list.
  291. * Must not be <code>null</code>.
  292. */
  293. public synchronized void addBuildListener(BuildListener listener) {
  294. // If the listeners already has this listener, do nothing
  295. if (listeners.contains(listener)) {
  296. return;
  297. }
  298. // create a new Vector to avoid ConcurrentModificationExc when
  299. // the listeners get added/removed while we are in fire
  300. Vector newListeners = getBuildListeners();
  301. newListeners.addElement(listener);
  302. listeners = newListeners;
  303. }
  304. /**
  305. * Remove a build listener from the list. This listener
  306. * will no longer be notified of build events for this project.
  307. *
  308. * @param listener The listener to remove from the list.
  309. * Should not be <code>null</code>.
  310. */
  311. public synchronized void removeBuildListener(BuildListener listener) {
  312. // create a new Vector to avoid ConcurrentModificationExc when
  313. // the listeners get added/removed while we are in fire
  314. Vector newListeners = getBuildListeners();
  315. newListeners.removeElement(listener);
  316. listeners = newListeners;
  317. }
  318. /**
  319. * Return a copy of the list of build listeners for the project.
  320. *
  321. * @return a list of build listeners for the project
  322. */
  323. public Vector getBuildListeners() {
  324. return (Vector) listeners.clone();
  325. }
  326. /**
  327. * Write a message to the log with the default log level
  328. * of MSG_INFO .
  329. * @param message The text to log. Should not be <code>null</code>.
  330. */
  331. public void log(String message) {
  332. log(message, MSG_INFO);
  333. }
  334. /**
  335. * Write a project level message to the log with the given log level.
  336. * @param message The text to log. Should not be <code>null</code>.
  337. * @param msgLevel The log priority level to use.
  338. */
  339. public void log(String message, int msgLevel) {
  340. fireMessageLogged(this, message, msgLevel);
  341. }
  342. /**
  343. * Write a task level message to the log with the given log level.
  344. * @param task The task to use in the log. Must not be <code>null</code>.
  345. * @param message The text to log. Should not be <code>null</code>.
  346. * @param msgLevel The log priority level to use.
  347. */
  348. public void log(Task task, String message, int msgLevel) {
  349. fireMessageLogged(task, message, msgLevel);
  350. }
  351. /**
  352. * Write a target level message to the log with the given log level.
  353. * @param target The target to use in the log.
  354. * Must not be <code>null</code>.
  355. * @param message The text to log. Should not be <code>null</code>.
  356. * @param msgLevel The log priority level to use.
  357. */
  358. public void log(Target target, String message, int msgLevel) {
  359. fireMessageLogged(target, message, msgLevel);
  360. }
  361. /**
  362. * Return the set of global filters.
  363. *
  364. * @return the set of global filters.
  365. */
  366. public FilterSet getGlobalFilterSet() {
  367. return globalFilterSet;
  368. }
  369. /**
  370. * Set a property. Any existing property of the same name
  371. * is overwritten, unless it is a user property.
  372. * @param name The name of property to set.
  373. * Must not be <code>null</code>.
  374. * @param value The new value of the property.
  375. * Must not be <code>null</code>.
  376. */
  377. public void setProperty(String name, String value) {
  378. PropertyHelper.getPropertyHelper(this).
  379. setProperty(null, name, value, true);
  380. }
  381. /**
  382. * Set a property if no value currently exists. If the property
  383. * exists already, a message is logged and the method returns with
  384. * no other effect.
  385. *
  386. * @param name The name of property to set.
  387. * Must not be <code>null</code>.
  388. * @param value The new value of the property.
  389. * Must not be <code>null</code>.
  390. * @since 1.5
  391. */
  392. public void setNewProperty(String name, String value) {
  393. PropertyHelper.getPropertyHelper(this).setNewProperty(null, name,
  394. value);
  395. }
  396. /**
  397. * Set a user property, which cannot be overwritten by
  398. * set/unset property calls. Any previous value is overwritten.
  399. * @param name The name of property to set.
  400. * Must not be <code>null</code>.
  401. * @param value The new value of the property.
  402. * Must not be <code>null</code>.
  403. * @see #setProperty(String,String)
  404. */
  405. public void setUserProperty(String name, String value) {
  406. PropertyHelper.getPropertyHelper(this).setUserProperty(null, name,
  407. value);
  408. }
  409. /**
  410. * Set a user property, which cannot be overwritten by set/unset
  411. * property calls. Any previous value is overwritten. Also marks
  412. * these properties as properties that have not come from the
  413. * command line.
  414. *
  415. * @param name The name of property to set.
  416. * Must not be <code>null</code>.
  417. * @param value The new value of the property.
  418. * Must not be <code>null</code>.
  419. * @see #setProperty(String,String)
  420. */
  421. public void setInheritedProperty(String name, String value) {
  422. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  423. ph.setInheritedProperty(null, name, value);
  424. }
  425. /**
  426. * Set a property unless it is already defined as a user property
  427. * (in which case the method returns silently).
  428. *
  429. * @param name The name of the property.
  430. * Must not be <code>null</code>.
  431. * @param value The property value. Must not be <code>null</code>.
  432. */
  433. private void setPropertyInternal(String name, String value) {
  434. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  435. ph.setProperty(null, name, value, false);
  436. }
  437. /**
  438. * Return the value of a property, if it is set.
  439. *
  440. * @param propertyName The name of the property.
  441. * May be <code>null</code>, in which case
  442. * the return value is also <code>null</code>.
  443. * @return the property value, or <code>null</code> for no match
  444. * or if a <code>null</code> name is provided.
  445. */
  446. public String getProperty(String propertyName) {
  447. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  448. return (String) ph.getProperty(null, propertyName);
  449. }
  450. /**
  451. * Replace ${} style constructions in the given value with the
  452. * string value of the corresponding data types.
  453. *
  454. * @param value The string to be scanned for property references.
  455. * May be <code>null</code>.
  456. *
  457. * @return the given string with embedded property names replaced
  458. * by values, or <code>null</code> if the given string is
  459. * <code>null</code>.
  460. *
  461. * @exception BuildException if the given value has an unclosed
  462. * property name, e.g. <code>${xxx</code>.
  463. */
  464. public String replaceProperties(String value)
  465. throws BuildException {
  466. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  467. return ph.replaceProperties(null, value, null);
  468. }
  469. /**
  470. * Return the value of a user property, if it is set.
  471. *
  472. * @param propertyName The name of the property.
  473. * May be <code>null</code>, in which case
  474. * the return value is also <code>null</code>.
  475. * @return the property value, or <code>null</code> for no match
  476. * or if a <code>null</code> name is provided.
  477. */
  478. public String getUserProperty(String propertyName) {
  479. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  480. return (String) ph.getUserProperty(null, propertyName);
  481. }
  482. /**
  483. * Return a copy of the properties table.
  484. * @return a hashtable containing all properties
  485. * (including user properties).
  486. */
  487. public Hashtable getProperties() {
  488. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  489. return ph.getProperties();
  490. }
  491. /**
  492. * Return a copy of the user property hashtable.
  493. * @return a hashtable containing just the user properties.
  494. */
  495. public Hashtable getUserProperties() {
  496. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  497. return ph.getUserProperties();
  498. }
  499. /**
  500. * Copy all user properties that have been set on the command
  501. * line or a GUI tool from this instance to the Project instance
  502. * given as the argument.
  503. *
  504. * <p>To copy all &quot;user&quot; properties, you will also have to call
  505. * {@link #copyInheritedProperties copyInheritedProperties}.</p>
  506. *
  507. * @param other the project to copy the properties to. Must not be null.
  508. *
  509. * @since Ant 1.5
  510. */
  511. public void copyUserProperties(Project other) {
  512. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  513. ph.copyUserProperties(other);
  514. }
  515. /**
  516. * Copy all user properties that have not been set on the
  517. * command line or a GUI tool from this instance to the Project
  518. * instance given as the argument.
  519. *
  520. * <p>To copy all &quot;user&quot; properties, you will also have to call
  521. * {@link #copyUserProperties copyUserProperties}.</p>
  522. *
  523. * @param other the project to copy the properties to. Must not be null.
  524. *
  525. * @since Ant 1.5
  526. */
  527. public void copyInheritedProperties(Project other) {
  528. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  529. ph.copyInheritedProperties(other);
  530. }
  531. /**
  532. * Set the default target of the project.
  533. *
  534. * @param defaultTarget The name of the default target for this project.
  535. * May be <code>null</code>, indicating that there is
  536. * no default target.
  537. *
  538. * @deprecated use setDefault
  539. * @see #setDefault(String)
  540. */
  541. public void setDefaultTarget(String defaultTarget) {
  542. this.defaultTarget = defaultTarget;
  543. }
  544. /**
  545. * Return the name of the default target of the project.
  546. * @return name of the default target or
  547. * <code>null</code> if no default has been set.
  548. */
  549. public String getDefaultTarget() {
  550. return defaultTarget;
  551. }
  552. /**
  553. * Set the default target of the project.
  554. *
  555. * @param defaultTarget The name of the default target for this project.
  556. * May be <code>null</code>, indicating that there is
  557. * no default target.
  558. */
  559. public void setDefault(String defaultTarget) {
  560. this.defaultTarget = defaultTarget;
  561. }
  562. /**
  563. * Set the name of the project, also setting the user
  564. * property <code>ant.project.name</code>.
  565. *
  566. * @param name The name of the project.
  567. * Must not be <code>null</code>.
  568. */
  569. public void setName(String name) {
  570. setUserProperty("ant.project.name", name);
  571. this.name = name;
  572. }
  573. /**
  574. * Return the project name, if one has been set.
  575. *
  576. * @return the project name, or <code>null</code> if it hasn't been set.
  577. */
  578. public String getName() {
  579. return name;
  580. }
  581. /**
  582. * Set the project description.
  583. *
  584. * @param description The description of the project.
  585. * May be <code>null</code>.
  586. */
  587. public void setDescription(String description) {
  588. this.description = description;
  589. }
  590. /**
  591. * Return the project description, if one has been set.
  592. *
  593. * @return the project description, or <code>null</code> if it hasn't
  594. * been set.
  595. */
  596. public String getDescription() {
  597. if (description == null) {
  598. description = Description.getDescription(this);
  599. }
  600. return description;
  601. }
  602. /**
  603. * Add a filter to the set of global filters.
  604. *
  605. * @param token The token to filter.
  606. * Must not be <code>null</code>.
  607. * @param value The replacement value.
  608. * Must not be <code>null</code>.
  609. * @deprecated Use getGlobalFilterSet().addFilter(token,value)
  610. *
  611. * @see #getGlobalFilterSet()
  612. * @see FilterSet#addFilter(String,String)
  613. */
  614. public void addFilter(String token, String value) {
  615. if (token == null) {
  616. return;
  617. }
  618. globalFilterSet.addFilter(new FilterSet.Filter(token, value));
  619. }
  620. /**
  621. * Return a hashtable of global filters, mapping tokens to values.
  622. *
  623. * @return a hashtable of global filters, mapping tokens to values
  624. * (String to String).
  625. *
  626. * @deprecated Use getGlobalFilterSet().getFilterHash()
  627. *
  628. * @see #getGlobalFilterSet()
  629. * @see FilterSet#getFilterHash()
  630. */
  631. public Hashtable getFilters() {
  632. // we need to build the hashtable dynamically
  633. return globalFilterSet.getFilterHash();
  634. }
  635. /**
  636. * Set the base directory for the project, checking that
  637. * the given filename exists and is a directory.
  638. *
  639. * @param baseD The project base directory.
  640. * Must not be <code>null</code>.
  641. *
  642. * @exception BuildException if the directory if invalid.
  643. */
  644. public void setBasedir(String baseD) throws BuildException {
  645. setBaseDir(new File(baseD));
  646. }
  647. /**
  648. * Set the base directory for the project, checking that
  649. * the given file exists and is a directory.
  650. *
  651. * @param baseDir The project base directory.
  652. * Must not be <code>null</code>.
  653. * @exception BuildException if the specified file doesn't exist or
  654. * isn't a directory.
  655. */
  656. public void setBaseDir(File baseDir) throws BuildException {
  657. baseDir = FILE_UTILS.normalize(baseDir.getAbsolutePath());
  658. if (!baseDir.exists()) {
  659. throw new BuildException("Basedir " + baseDir.getAbsolutePath()
  660. + " does not exist");
  661. }
  662. if (!baseDir.isDirectory()) {
  663. throw new BuildException("Basedir " + baseDir.getAbsolutePath()
  664. + " is not a directory");
  665. }
  666. this.baseDir = baseDir;
  667. setPropertyInternal("basedir", this.baseDir.getPath());
  668. String msg = "Project base dir set to: " + this.baseDir;
  669. log(msg, MSG_VERBOSE);
  670. }
  671. /**
  672. * Return the base directory of the project as a file object.
  673. *
  674. * @return the project base directory, or <code>null</code> if the
  675. * base directory has not been successfully set to a valid value.
  676. */
  677. public File getBaseDir() {
  678. if (baseDir == null) {
  679. try {
  680. setBasedir(".");
  681. } catch (BuildException ex) {
  682. ex.printStackTrace();
  683. }
  684. }
  685. return baseDir;
  686. }
  687. /**
  688. * Set &quot;keep-going&quot; mode. In this mode Ant will try to execute
  689. * as many targets as possible. All targets that do not depend
  690. * on failed target(s) will be executed. If the keepGoing settor/getter
  691. * methods are used in conjunction with the <code>ant.executor.class</code>
  692. * property, they will have no effect.
  693. * @param keepGoingMode &quot;keep-going&quot; mode
  694. * @since Ant 1.6
  695. */
  696. public void setKeepGoingMode(boolean keepGoingMode) {
  697. this.keepGoingMode = keepGoingMode;
  698. }
  699. /**
  700. * Return the keep-going mode. If the keepGoing settor/getter
  701. * methods are used in conjunction with the <code>ant.executor.class</code>
  702. * property, they will have no effect.
  703. * @return &quot;keep-going&quot; mode
  704. * @since Ant 1.6
  705. */
  706. public boolean isKeepGoingMode() {
  707. return this.keepGoingMode;
  708. }
  709. /**
  710. * Return the version of Java this class is running under.
  711. * @return the version of Java as a String, e.g. "1.1" .
  712. * @see org.apache.tools.ant.util.JavaEnvUtils#getJavaVersion
  713. * @deprecated use org.apache.tools.ant.util.JavaEnvUtils instead
  714. */
  715. public static String getJavaVersion() {
  716. return JavaEnvUtils.getJavaVersion();
  717. }
  718. /**
  719. * Set the <code>ant.java.version</code> property and tests for
  720. * unsupported JVM versions. If the version is supported,
  721. * verbose log messages are generated to record the Java version
  722. * and operating system name.
  723. *
  724. * @exception BuildException if this Java version is not supported.
  725. *
  726. * @see org.apache.tools.ant.util.JavaEnvUtils#getJavaVersion
  727. */
  728. public void setJavaVersionProperty() throws BuildException {
  729. String javaVersion = JavaEnvUtils.getJavaVersion();
  730. setPropertyInternal(ANT_JAVA_VERSION, javaVersion);
  731. // sanity check
  732. if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_0)) {
  733. throw new BuildException("Ant cannot work on Java 1.0");
  734. }
  735. log("Detected Java version: " + javaVersion + " in: "
  736. + System.getProperty("java.home"), MSG_VERBOSE);
  737. log("Detected OS: " + System.getProperty("os.name"), MSG_VERBOSE);
  738. }
  739. /**
  740. * Add all system properties which aren't already defined as
  741. * user properties to the project properties.
  742. */
  743. public void setSystemProperties() {
  744. Properties systemP = System.getProperties();
  745. Enumeration e = systemP.propertyNames();
  746. while (e.hasMoreElements()) {
  747. String propertyName = (String) e.nextElement();
  748. String value = systemP.getProperty(propertyName);
  749. this.setPropertyInternal(propertyName, value);
  750. }
  751. }
  752. /**
  753. * Add a new task definition to the project.
  754. * Attempting to override an existing definition with an
  755. * equivalent one (i.e. with the same classname) results in
  756. * a verbose log message. Attempting to override an existing definition
  757. * with a different one results in a warning log message and
  758. * invalidates any tasks which have already been created with the
  759. * old definition.
  760. *
  761. * @param taskName The name of the task to add.
  762. * Must not be <code>null</code>.
  763. * @param taskClass The full name of the class implementing the task.
  764. * Must not be <code>null</code>.
  765. *
  766. * @exception BuildException if the class is unsuitable for being an Ant
  767. * task. An error level message is logged before
  768. * this exception is thrown.
  769. *
  770. * @see #checkTaskClass(Class)
  771. */
  772. public void addTaskDefinition(String taskName, Class taskClass)
  773. throws BuildException {
  774. ComponentHelper.getComponentHelper(this).addTaskDefinition(taskName,
  775. taskClass);
  776. }
  777. /**
  778. * Check whether or not a class is suitable for serving as Ant task.
  779. * Ant task implementation classes must be public, concrete, and have
  780. * a no-arg constructor.
  781. *
  782. * @param taskClass The class to be checked.
  783. * Must not be <code>null</code>.
  784. *
  785. * @exception BuildException if the class is unsuitable for being an Ant
  786. * task. An error level message is logged before
  787. * this exception is thrown.
  788. */
  789. public void checkTaskClass(final Class taskClass) throws BuildException {
  790. ComponentHelper.getComponentHelper(this).checkTaskClass(taskClass);
  791. if (!Modifier.isPublic(taskClass.getModifiers())) {
  792. final String message = taskClass + " is not public";
  793. log(message, Project.MSG_ERR);
  794. throw new BuildException(message);
  795. }
  796. if (Modifier.isAbstract(taskClass.getModifiers())) {
  797. final String message = taskClass + " is abstract";
  798. log(message, Project.MSG_ERR);
  799. throw new BuildException(message);
  800. }
  801. try {
  802. taskClass.getConstructor((Class[]) null);
  803. // don't have to check for public, since
  804. // getConstructor finds public constructors only.
  805. } catch (NoSuchMethodException e) {
  806. final String message = "No public no-arg constructor in "
  807. + taskClass;
  808. log(message, Project.MSG_ERR);
  809. throw new BuildException(message);
  810. } catch (LinkageError e) {
  811. String message = "Could not load " + taskClass + ": " + e;
  812. log(message, Project.MSG_ERR);
  813. throw new BuildException(message, e);
  814. }
  815. if (!Task.class.isAssignableFrom(taskClass)) {
  816. TaskAdapter.checkTaskClass(taskClass, this);
  817. }
  818. }
  819. /**
  820. * Return the current task definition hashtable. The returned hashtable is
  821. * &quot;live&quot; and so should not be modified.
  822. *
  823. * @return a map of from task name to implementing class
  824. * (String to Class).
  825. */
  826. public Hashtable getTaskDefinitions() {
  827. return ComponentHelper.getComponentHelper(this).getTaskDefinitions();
  828. }
  829. /**
  830. * Add a new datatype definition.
  831. * Attempting to override an existing definition with an
  832. * equivalent one (i.e. with the same classname) results in
  833. * a verbose log message. Attempting to override an existing definition
  834. * with a different one results in a warning log message, but the
  835. * definition is changed.
  836. *
  837. * @param typeName The name of the datatype.
  838. * Must not be <code>null</code>.
  839. * @param typeClass The full name of the class implementing the datatype.
  840. * Must not be <code>null</code>.
  841. */
  842. public void addDataTypeDefinition(String typeName, Class typeClass) {
  843. ComponentHelper.getComponentHelper(this).addDataTypeDefinition(typeName,
  844. typeClass);
  845. }
  846. /**
  847. * Return the current datatype definition hashtable. The returned
  848. * hashtable is &quot;live&quot; and so should not be modified.
  849. *
  850. * @return a map of from datatype name to implementing class
  851. * (String to Class).
  852. */
  853. public Hashtable getDataTypeDefinitions() {
  854. return ComponentHelper.getComponentHelper(this).getDataTypeDefinitions();
  855. }
  856. /**
  857. * Add a <em>new</em> target to the project.
  858. *
  859. * @param target The target to be added to the project.
  860. * Must not be <code>null</code>.
  861. *
  862. * @exception BuildException if the target already exists in the project
  863. *
  864. * @see Project#addOrReplaceTarget(Target)
  865. */
  866. public void addTarget(Target target) throws BuildException {
  867. addTarget(target.getName(), target);
  868. }
  869. /**
  870. * Add a <em>new</em> target to the project.
  871. *
  872. * @param targetName The name to use for the target.
  873. * Must not be <code>null</code>.
  874. * @param target The target to be added to the project.
  875. * Must not be <code>null</code>.
  876. *
  877. * @exception BuildException if the target already exists in the project.
  878. *
  879. * @see Project#addOrReplaceTarget(String, Target)
  880. */
  881. public void addTarget(String targetName, Target target)
  882. throws BuildException {
  883. if (targets.get(targetName) != null) {
  884. throw new BuildException("Duplicate target: `" + targetName + "'");
  885. }
  886. addOrReplaceTarget(targetName, target);
  887. }
  888. /**
  889. * Add a target to the project, or replaces one with the same
  890. * name.
  891. *
  892. * @param target The target to be added or replaced in the project.
  893. * Must not be <code>null</code>.
  894. */
  895. public void addOrReplaceTarget(Target target) {
  896. addOrReplaceTarget(target.getName(), target);
  897. }
  898. /**
  899. * Add a target to the project, or replaces one with the same
  900. * name.
  901. *
  902. * @param targetName The name to use for the target.
  903. * Must not be <code>null</code>.
  904. * @param target The target to be added or replaced in the project.
  905. * Must not be <code>null</code>.
  906. */
  907. public void addOrReplaceTarget(String targetName, Target target) {
  908. String msg = " +Target: " + targetName;
  909. log(msg, MSG_DEBUG);
  910. target.setProject(this);
  911. targets.put(targetName, target);
  912. }
  913. /**
  914. * Return the hashtable of targets. The returned hashtable
  915. * is &quot;live&quot; and so should not be modified.
  916. * @return a map from name to target (String to Target).
  917. */
  918. public Hashtable getTargets() {
  919. return targets;
  920. }
  921. /**
  922. * Create a new instance of a task, adding it to a list of
  923. * created tasks for later invalidation. This causes all tasks
  924. * to be remembered until the containing project is removed
  925. * @param taskType The name of the task to create an instance of.
  926. * Must not be <code>null</code>.
  927. *
  928. * @return an instance of the specified task, or <code>null</code> if
  929. * the task name is not recognised.
  930. *
  931. * @exception BuildException if the task name is recognised but task
  932. * creation fails.
  933. */
  934. public Task createTask(String taskType) throws BuildException {
  935. return ComponentHelper.getComponentHelper(this).createTask(taskType);
  936. }
  937. /**
  938. * Create a new instance of a data type.
  939. *
  940. * @param typeName The name of the data type to create an instance of.
  941. * Must not be <code>null</code>.
  942. *
  943. * @return an instance of the specified data type, or <code>null</code> if
  944. * the data type name is not recognised.
  945. *
  946. * @exception BuildException if the data type name is recognised but
  947. * instance creation fails.
  948. */
  949. public Object createDataType(String typeName) throws BuildException {
  950. return ComponentHelper.getComponentHelper(this).createDataType(typeName);
  951. }
  952. /**
  953. * Set the Executor instance for this Project.
  954. * @param e the Executor to use.
  955. */
  956. public void setExecutor(Executor e) {
  957. addReference(MagicNames.ANT_EXECUTOR_REFERENCE, e);
  958. }
  959. /**
  960. * Get this Project's Executor (setting it if necessary).
  961. * @return an Executor instance.
  962. */
  963. public Executor getExecutor() {
  964. Object o = getReference(MagicNames.ANT_EXECUTOR_REFERENCE);
  965. if (o == null) {
  966. String classname = getProperty(MagicNames.ANT_EXECUTOR_CLASSNAME);
  967. if (classname == null) {
  968. classname = DefaultExecutor.class.getName();
  969. }
  970. log("Attempting to create object of type " + classname, MSG_DEBUG);
  971. try {
  972. o = Class.forName(classname, true, coreLoader).newInstance();
  973. } catch (ClassNotFoundException seaEnEfEx) {
  974. //try the current classloader
  975. try {
  976. o = Class.forName(classname).newInstance();
  977. } catch (Exception ex) {
  978. log(ex.toString(), MSG_ERR);
  979. }
  980. } catch (Exception ex) {
  981. log(ex.toString(), MSG_ERR);
  982. }
  983. if (o == null) {
  984. throw new BuildException(
  985. "Unable to obtain a Target Executor instance.");
  986. }
  987. setExecutor((Executor) o);
  988. }
  989. return (Executor) o;
  990. }
  991. /**
  992. * Execute the specified sequence of targets, and the targets
  993. * they depend on.
  994. *
  995. * @param names A vector of target name strings to execute.
  996. * Must not be <code>null</code>.
  997. *
  998. * @exception BuildException if the build failed.
  999. */
  1000. public void executeTargets(Vector names) throws BuildException {
  1001. getExecutor().executeTargets(this,
  1002. (String[]) (names.toArray(new String[names.size()])));
  1003. }
  1004. /**
  1005. * Demultiplex output so that each task receives the appropriate
  1006. * messages. If the current thread is not currently executing a task,
  1007. * the message is logged directly.
  1008. *
  1009. * @param output Message to handle. Should not be <code>null</code>.
  1010. * @param isWarning Whether the text represents an warning (<code>true</code>)
  1011. * or information (<code>false</code>).
  1012. */
  1013. public void demuxOutput(String output, boolean isWarning) {
  1014. Task task = getThreadTask(Thread.currentThread());
  1015. if (task == null) {
  1016. log(output, isWarning ? MSG_WARN : MSG_INFO);
  1017. } else {
  1018. if (isWarning) {
  1019. task.handleErrorOutput(output);
  1020. } else {
  1021. task.handleOutput(output);
  1022. }
  1023. }
  1024. }
  1025. /**
  1026. * Read data from the default input stream. If no default has been
  1027. * specified, System.in is used.
  1028. *
  1029. * @param buffer the buffer into which data is to be read.
  1030. * @param offset the offset into the buffer at which data is stored.
  1031. * @param length the amount of data to read.
  1032. *
  1033. * @return the number of bytes read.
  1034. *
  1035. * @exception IOException if the data cannot be read.
  1036. * @since Ant 1.6
  1037. */
  1038. public int defaultInput(byte[] buffer, int offset, int length)
  1039. throws IOException {
  1040. if (defaultInputStream != null) {
  1041. System.out.flush();
  1042. return defaultInputStream.read(buffer, offset, length);
  1043. } else {
  1044. throw new EOFException("No input provided for project");
  1045. }
  1046. }
  1047. /**
  1048. * Demux an input request to the correct task.
  1049. *
  1050. * @param buffer the buffer into which data is to be read.
  1051. * @param offset the offset into the buffer at which data is stored.
  1052. * @param length the amount of data to read.
  1053. *
  1054. * @return the number of bytes read.
  1055. *
  1056. * @exception IOException if the data cannot be read.
  1057. * @since Ant 1.6
  1058. */
  1059. public int demuxInput(byte[] buffer, int offset, int length)
  1060. throws IOException {
  1061. Task task = getThreadTask(Thread.currentThread());
  1062. if (task == null) {
  1063. return defaultInput(buffer, offset, length);
  1064. } else {
  1065. return task.handleInput(buffer, offset, length);
  1066. }
  1067. }
  1068. /**
  1069. * Demultiplex flush operations so that each task receives the appropriate
  1070. * messages. If the current thread is not currently executing a task,
  1071. * the message is logged directly.
  1072. *
  1073. * @since Ant 1.5.2
  1074. *
  1075. * @param output Message to handle. Should not be <code>null</code>.
  1076. * @param isError Whether the text represents an error (<code>true</code>)
  1077. * or information (<code>false</code>).
  1078. */
  1079. public void demuxFlush(String output, boolean isError) {
  1080. Task task = getThreadTask(Thread.currentThread());
  1081. if (task == null) {
  1082. fireMessageLogged(this, output, isError ? MSG_ERR : MSG_INFO);
  1083. } else {
  1084. if (isError) {
  1085. task.handleErrorFlush(output);
  1086. } else {
  1087. task.handleFlush(output);
  1088. }
  1089. }
  1090. }
  1091. /**
  1092. * Execute the specified target and any targets it depends on.
  1093. *
  1094. * @param targetName The name of the target to execute.
  1095. * Must not be <code>null</code>.
  1096. *
  1097. * @exception BuildException if the build failed.
  1098. */
  1099. public void executeTarget(String targetName) throws BuildException {
  1100. // sanity check ourselves, if we've been asked to build nothing
  1101. // then we should complain
  1102. if (targetName == null) {
  1103. String msg = "No target specified";
  1104. throw new BuildException(msg);
  1105. }
  1106. // Sort and run the dependency tree.
  1107. // Sorting checks if all the targets (and dependencies)
  1108. // exist, and if there is any cycle in the dependency
  1109. // graph.
  1110. executeSortedTargets(topoSort(targetName, targets, false));
  1111. }
  1112. /**
  1113. * Execute a <code>Vector</code> of sorted targets.
  1114. * @param sortedTargets the aforementioned <code>Vector</code>.
  1115. * @throws BuildException on error.
  1116. */
  1117. public void executeSortedTargets(Vector sortedTargets)
  1118. throws BuildException {
  1119. Set succeededTargets = new HashSet();
  1120. BuildException buildException = null; // first build exception
  1121. for (Enumeration iter = sortedTargets.elements();
  1122. iter.hasMoreElements();) {
  1123. Target curtarget = (Target) iter.nextElement();
  1124. boolean canExecute = true;
  1125. for (Enumeration depIter = curtarget.getDependencies();
  1126. depIter.hasMoreElements();) {
  1127. String dependencyName = ((String) depIter.nextElement());
  1128. if (!succeededTargets.contains(dependencyName)) {
  1129. canExecute = false;
  1130. log(curtarget,
  1131. "Cannot execute '" + curtarget.getName() + "' - '"
  1132. + dependencyName + "' failed or was not executed.",
  1133. MSG_ERR);
  1134. break;
  1135. }
  1136. }
  1137. if (canExecute) {
  1138. Throwable thrownException = null;
  1139. try {
  1140. curtarget.performTasks();
  1141. succeededTargets.add(curtarget.getName());
  1142. } catch (RuntimeException ex) {
  1143. if (!(keepGoingMode)) {
  1144. throw ex; // throw further
  1145. }
  1146. thrownException = ex;
  1147. } catch (Throwable ex) {
  1148. if (!(keepGoingMode)) {
  1149. throw new BuildException(ex);
  1150. }
  1151. thrownException = ex;
  1152. }
  1153. if (thrownException != null) {
  1154. if (thrownException instanceof BuildException) {
  1155. log(curtarget,
  1156. "Target '" + curtarget.getName()
  1157. + "' failed with message '"
  1158. + thrownException.getMessage() + "'.", MSG_ERR);
  1159. // only the first build exception is reported
  1160. if (buildException == null) {
  1161. buildException = (BuildException) thrownException;
  1162. }
  1163. } else {
  1164. log(curtarget,
  1165. "Target '" + curtarget.getName()
  1166. + "' failed with message '"
  1167. + thrownException.getMessage() + "'.", MSG_ERR);
  1168. thrownException.printStackTrace(System.err);
  1169. if (buildException == null) {
  1170. buildException =
  1171. new BuildException(thrownException);
  1172. }
  1173. }
  1174. }
  1175. }
  1176. }
  1177. if (buildException != null) {
  1178. throw buildException;
  1179. }
  1180. }
  1181. /**
  1182. * Return the canonical form of a filename.
  1183. * <p>
  1184. * If the specified file name is relative it is resolved
  1185. * with respect to the given root directory.
  1186. *
  1187. * @param fileName The name of the file to resolve.
  1188. * Must not be <code>null</code>.
  1189. *
  1190. * @param rootDir The directory respective to which relative file names
  1191. * are resolved. May be <code>null</code>, in which case
  1192. * the current directory is used.
  1193. *
  1194. * @return the resolved File.
  1195. *
  1196. * @deprecated
  1197. */
  1198. public File resolveFile(String fileName, File rootDir) {
  1199. return FILE_UTILS.resolveFile(rootDir, fileName);
  1200. }
  1201. /**
  1202. * Return the canonical form of a filename.
  1203. * <p>
  1204. * If the specified file name is relative it is resolved
  1205. * with respect to the project's base directory.
  1206. *
  1207. * @param fileName The name of the file to resolve.
  1208. * Must not be <code>null</code>.
  1209. *
  1210. * @return the resolved File.
  1211. *
  1212. */
  1213. public File resolveFile(String fileName) {
  1214. return FILE_UTILS.resolveFile(baseDir, fileName);
  1215. }
  1216. /**
  1217. * Translate a path into its native (platform specific) format.
  1218. * <p>
  1219. * This method uses PathTokenizer to separate the input path
  1220. * into its components. This handles DOS style paths in a relatively
  1221. * sensible way. The file separators are then converted to their platform
  1222. * specific versions.
  1223. *
  1224. * @param toProcess The path to be translated.
  1225. * May be <code>null</code>.
  1226. *
  1227. * @return the native version of the specified path or
  1228. * an empty string if the path is <code>null</code> or empty.
  1229. *
  1230. * @see PathTokenizer
  1231. */
  1232. public static String translatePath(String toProcess) {
  1233. if (toProcess == null || toProcess.length() == 0) {
  1234. return "";
  1235. }
  1236. StringBuffer path = new StringBuffer(toProcess.length() + 50);
  1237. PathTokenizer tokenizer = new PathTokenizer(toProcess);
  1238. while (tokenizer.hasMoreTokens()) {
  1239. String pathComponent = tokenizer.nextToken();
  1240. pathComponent = pathComponent.replace('/', File.separatorChar);
  1241. pathComponent = pathComponent.replace('\\', File.separatorChar);
  1242. if (path.length() != 0) {
  1243. path.append(File.pathSeparatorChar);
  1244. }
  1245. path.append(pathComponent);
  1246. }
  1247. return path.toString();
  1248. }
  1249. /**
  1250. * Convenience method to copy a file from a source to a destination.
  1251. * No filtering is performed.
  1252. *
  1253. * @param sourceFile Name of file to copy from.
  1254. * Must not be <code>null</code>.
  1255. * @param destFile Name of file to copy to.
  1256. * Must not be <code>null</code>.
  1257. *
  1258. * @exception IOException if the copying fails.
  1259. *
  1260. * @deprecated
  1261. */
  1262. public void copyFile(String sourceFile, String destFile)
  1263. throws IOException {
  1264. FILE_UTILS.copyFile(sourceFile, destFile);
  1265. }
  1266. /**
  1267. * Convenience method to copy a file from a source to a destination
  1268. * specifying if token filtering should be used.
  1269. *
  1270. * @param sourceFile Name of file to copy from.
  1271. * Must not be <code>null</code>.
  1272. * @param destFile Name of file to copy to.
  1273. * Must not be <code>null</code>.
  1274. * @param filtering Whether or not token filtering should be used during
  1275. * the copy.
  1276. *
  1277. * @exception IOException if the copying fails.
  1278. *
  1279. * @deprecated
  1280. */
  1281. public void copyFile(String sourceFile, String destFile, boolean filtering)
  1282. throws IOException {
  1283. FILE_UTILS.copyFile(sourceFile, destFile,
  1284. filtering ? globalFilters : null);
  1285. }
  1286. /**
  1287. * Convenience method to copy a file from a source to a
  1288. * destination specifying if token filtering should be used and if
  1289. * source files may overwrite newer destination files.
  1290. *
  1291. * @param sourceFile Name of file to copy from.
  1292. * Must not be <code>null</code>.
  1293. * @param destFile Name of file to copy to.
  1294. * Must not be <code>null</code>.
  1295. * @param filtering Whether or not token filtering should be used during
  1296. * the copy.
  1297. * @param overwrite Whether or not the destination file should be
  1298. * overwritten if it already exists.
  1299. *
  1300. * @exception IOException if the copying fails.
  1301. *
  1302. * @deprecated
  1303. */
  1304. public void copyFile(String sourceFile, String destFile, boolean filtering,
  1305. boolean overwrite) throws IOException {
  1306. FILE_UTILS.copyFile(sourceFile, destFile,
  1307. filtering ? globalFilters : null, overwrite);
  1308. }
  1309. /**
  1310. * Convenience method to copy a file from a source to a
  1311. * destination specifying if token filtering should be used, if
  1312. * source files may overwrite newer destination files, and if the
  1313. * last modified time of the resulting file should be set to
  1314. * that of the source file.
  1315. *
  1316. * @param sourceFile Name of file to copy from.
  1317. * Must not be <code>null</code>.
  1318. * @param destFile Name of file to copy to.
  1319. * Must not be <code>null</code>.
  1320. * @param filtering Whether or not token filtering should be used during
  1321. * the copy.
  1322. * @param overwrite Whether or not the destination file should be
  1323. * overwritten if it already exists.
  1324. * @param preserveLastModified Whether or not the last modified time of
  1325. * the resulting file should be set to that
  1326. * of the source file.
  1327. *
  1328. * @exception IOException if the copying fails.
  1329. *
  1330. * @deprecated
  1331. */
  1332. public void copyFile(String sourceFile, String destFile, boolean filtering,
  1333. boolean overwrite, boolean preserveLastModified)
  1334. throws IOException {
  1335. FILE_UTILS.copyFile(sourceFile, destFile,
  1336. filtering ? globalFilters : null, overwrite, preserveLastModified);
  1337. }
  1338. /**
  1339. * Convenience method to copy a file from a source to a destination.
  1340. * No filtering is performed.
  1341. *
  1342. * @param sourceFile File to copy from.
  1343. * Must not be <code>null</code>.
  1344. * @param destFile File to copy to.
  1345. * Must not be <code>null</code>.
  1346. *
  1347. * @exception IOException if the copying fails.
  1348. *
  1349. * @deprecated
  1350. */
  1351. public void copyFile(File sourceFile, File destFile) throws IOException {
  1352. FILE_UTILS.copyFile(sourceFile, destFile);
  1353. }
  1354. /**
  1355. * Convenience method to copy a file from a source to a destination
  1356. * specifying if token filtering should be used.
  1357. *
  1358. * @param sourceFile File to copy from.
  1359. * Must not be <code>null</code>.
  1360. * @param destFile File to copy to.
  1361. * Must not be <code>null</code>.
  1362. * @param filtering Whether or not token filtering should be used during
  1363. * the copy.
  1364. *
  1365. * @exception IOException if the copying fails.
  1366. *
  1367. * @deprecated
  1368. */
  1369. public void copyFile(File sourceFile, File destFile, boolean filtering)
  1370. throws IOException {
  1371. FILE_UTILS.copyFile(sourceFile, destFile,
  1372. filtering ? globalFilters : null);
  1373. }
  1374. /**
  1375. * Convenience method to copy a file from a source to a
  1376. * destination specifying if token filtering should be used and if
  1377. * source files may overwrite newer destination files.
  1378. *
  1379. * @param sourceFile File to copy from.
  1380. * Must not be <code>null</code>.
  1381. * @param destFile File to copy to.
  1382. * Must not be <code>null</code>.
  1383. * @param filtering Whether or not token filtering should be used during
  1384. * the copy.
  1385. * @param overwrite Whether or not the destination file should be
  1386. * overwritten if it already exists.
  1387. *
  1388. * @exception IOException if the file cannot be copied.
  1389. *
  1390. * @deprecated
  1391. */
  1392. public void copyFile(File sourceFile, File destFile, boolean filtering,
  1393. boolean overwrite) throws IOException {
  1394. FILE_UTILS.copyFile(sourceFile, destFile,
  1395. filtering ? globalFilters : null, overwrite);
  1396. }
  1397. /**
  1398. * Convenience method to copy a file from a source to a
  1399. * destination specifying if token filtering should be used, if
  1400. * source files may overwrite newer destination files, and if the
  1401. * last modified time of the resulting file should be set to
  1402. * that of the source file.
  1403. *
  1404. * @param sourceFile File to copy from.
  1405. * Must not be <code>null</code>.
  1406. * @param destFile File to copy to.
  1407. * Must not be <code>null</code>.
  1408. * @param filtering Whether or not token filtering should be used during
  1409. * the copy.
  1410. * @param overwrite Whether or not the destination file should be
  1411. * overwritten if it already exists.
  1412. * @param preserveLastModified Whether or not the last modified time of
  1413. * the resulting file should be set to that
  1414. * of the source file.
  1415. *
  1416. * @exception IOException if the file cannot be copied.
  1417. *
  1418. * @deprecated
  1419. */
  1420. public void copyFile(File sourceFile, File destFile, boolean filtering,
  1421. boolean overwrite, boolean preserveLastModified)
  1422. throws IOException {
  1423. FILE_UTILS.copyFile(sourceFile, destFile,
  1424. filtering ? globalFilters : null, overwrite, preserveLastModified);
  1425. }
  1426. /**
  1427. * Call File.setLastModified(long time) on Java above 1.1, and logs
  1428. * a warning on Java 1.1.
  1429. *
  1430. * @param file The file to set the last modified time on.
  1431. * Must not be <code>null</code>.
  1432. *
  1433. * @param time the required modification time.
  1434. *
  1435. * @deprecated
  1436. *
  1437. * @exception BuildException if the last modified time cannot be set
  1438. * despite running on a platform with a version
  1439. * above 1.1.
  1440. */
  1441. public void setFileLastModified(File file, long time)
  1442. throws BuildException {
  1443. FILE_UTILS.setFileLastModified(file, time);
  1444. log("Setting modification time for " + file, MSG_VERBOSE);
  1445. }
  1446. /**
  1447. * Return the boolean equivalent of a string, which is considered
  1448. * <code>true</code> if either <code>"on"</code>, <code>"true"</code>,
  1449. * or <code>"yes"</code> is found, ignoring case.
  1450. *
  1451. * @param s The string to convert to a boolean value.
  1452. *
  1453. * @return <code>true</code> if the given string is <code>"on"</code>,
  1454. * <code>"true"</code> or <code>"yes"</code>, or
  1455. * <code>false</code> otherwise.
  1456. */
  1457. public static boolean toBoolean(String s) {
  1458. return ("on".equalsIgnoreCase(s)
  1459. || "true".equalsIgnoreCase(s)
  1460. || "yes".equalsIgnoreCase(s));
  1461. }
  1462. /**
  1463. * Topologically sort a set of targets. Equivalent to calling
  1464. * <code>topoSort(new String[] {root}, targets, true)</code>.
  1465. *
  1466. * @param root The name of the root target. The sort is created in such
  1467. * a way that the sequence of Targets up to the root
  1468. * target is the minimum possible such sequence.
  1469. * Must not be <code>null</code>.
  1470. * @param targetTable A Hashtable mapping names to Targets.
  1471. * Must not be <code>null</code>.
  1472. * @return a Vector of ALL Target objects in sorted order.
  1473. * @exception BuildException if there is a cyclic dependency among the
  1474. * targets, or if a named target does not exist.
  1475. */
  1476. public final Vector topoSort(String root, Hashtable targetTable)
  1477. throws BuildException {
  1478. return topoSort(new String[] {root}, targetTable, true);
  1479. }
  1480. /**
  1481. * Topologically sort a set of targets. Equivalent to calling
  1482. * <code>topoSort(new String[] {root}, targets, returnAll)</code>.
  1483. *
  1484. * @param root The name of the root target. The sort is created in such
  1485. * a way that the sequence of Targets up to the root
  1486. * target is the minimum possible such sequence.
  1487. * Must not be <code>null</code>.
  1488. * @param targetTable A Hashtable mapping names to Targets.
  1489. * Must not be <code>null</code>.
  1490. * @param returnAll <code>boolean</code> indicating whether to return all
  1491. * targets, or the execution sequence only.
  1492. * @return a Vector of Target objects in sorted order.
  1493. * @exception BuildException if there is a cyclic dependency among the
  1494. * targets, or if a named target does not exist.
  1495. * @since Ant 1.6.3
  1496. */
  1497. public final Vector topoSort(String root, Hashtable targetTable,
  1498. boolean returnAll) throws BuildException {
  1499. return topoSort(new String[] {root}, targetTable, returnAll);
  1500. }
  1501. /**
  1502. * Topologically sort a set of targets.
  1503. *
  1504. * @param root <code>String[]</code> containing the names of the root targets.
  1505. * The sort is created in such a way that the ordered sequence of
  1506. * Targets is the minimum possible such sequence to the specified
  1507. * root targets.
  1508. * Must not be <code>null</code>.
  1509. * @param targetTable A map of names to targets (String to Target).
  1510. * Must not be <code>null</code>.
  1511. * @param returnAll <code>boolean</code> indicating whether to return all
  1512. * targets, or the execution sequence only.
  1513. * @return a Vector of Target objects in sorted order.
  1514. * @exception BuildException if there is a cyclic dependency among the
  1515. * targets, or if a named target does not exist.
  1516. * @since Ant 1.6.3
  1517. */
  1518. public final Vector topoSort(String[] root, Hashtable targetTable,
  1519. boolean returnAll) throws BuildException {
  1520. Vector ret = new Vector();
  1521. Hashtable state = new Hashtable();
  1522. Stack visiting = new Stack();
  1523. // We first run a DFS based sort using each root as a starting node.
  1524. // This creates the minimum sequence of Targets to the root node(s).
  1525. // We then do a sort on any remaining unVISITED targets.
  1526. // This is unnecessary for doing our build, but it catches
  1527. // circular dependencies or missing Targets on the entire
  1528. // dependency tree, not just on the Targets that depend on the
  1529. // build Target.
  1530. for (int i = 0; i < root.length; i++) {
  1531. String st = (String) (state.get(root[i]));
  1532. if (st == null) {
  1533. tsort(root[i], targetTable, state, visiting, ret);
  1534. } else if (st == VISITING) {
  1535. throw new RuntimeException("Unexpected node in visiting state: "
  1536. + root[i]);
  1537. }
  1538. }
  1539. StringBuffer buf = new StringBuffer("Build sequence for target(s)");
  1540. for (int j = 0; j < root.length; j++) {
  1541. buf.append((j == 0) ? " `" : ", `").append(root[j]).append('\'');
  1542. }
  1543. buf.append(" is " + ret);
  1544. log(buf.toString(), MSG_VERBOSE);
  1545. Vector complete = (returnAll) ? ret : new Vector(ret);
  1546. for (Enumeration en = targetTable.keys(); en.hasMoreElements();) {
  1547. String curTarget = (String) en.nextElement();
  1548. String st = (String) state.get(curTarget);
  1549. if (st == null) {
  1550. tsort(curTarget, targetTable, state, visiting, complete);
  1551. } else if (st == VISITING) {
  1552. throw new RuntimeException("Unexpected node in visiting state: "
  1553. + curTarget);
  1554. }
  1555. }
  1556. log("Complete build sequence is " + complete, MSG_VERBOSE);
  1557. return ret;
  1558. }
  1559. /**
  1560. * Perform a single step in a recursive depth-first-search traversal of
  1561. * the target dependency tree.
  1562. * <p>
  1563. * The current target is first set to the &quot;visiting&quot; state, and
  1564. * pushed onto the &quot;visiting&quot; stack.
  1565. * <p>
  1566. * An exception is then thrown if any child of the current node is in the
  1567. * visiting state, as that implies a circular dependency. The exception
  1568. * contains details of the cycle, using elements of the &quot;visiting&quot;
  1569. * stack.
  1570. * <p>
  1571. * If any child has not already been &quot;visited&quot;, this method is
  1572. * called recursively on it.
  1573. * <p>
  1574. * The current target is then added to the ordered list of targets. Note
  1575. * that this is performed after the children have been visited in order
  1576. * to get the correct order. The current target is set to the
  1577. * &quot;visited&quot; state.
  1578. * <p>
  1579. * By the time this method returns, the ordered list contains the sequence
  1580. * of targets up to and including the current target.
  1581. *
  1582. * @param root The current target to inspect.
  1583. * Must not be <code>null</code>.
  1584. * @param targetTable A mapping from names to targets (String to Target).
  1585. * Must not be <code>null</code>.
  1586. * @param state A mapping from target names to states (String to String).
  1587. * The states in question are &quot;VISITING&quot; and
  1588. * &quot;VISITED&quot;. Must not be <code>null</code>.
  1589. * @param visiting A stack of targets which are currently being visited.
  1590. * Must not be <code>null</code>.
  1591. * @param ret The list to add target names to. This will end up
  1592. * containing the complete list of dependencies in
  1593. * dependency order.
  1594. * Must not be <code>null</code>.
  1595. *
  1596. * @exception BuildException if a non-existent target is specified or if
  1597. * a circular dependency is detected.
  1598. */
  1599. private void tsort(String root, Hashtable targetTable,
  1600. Hashtable state, Stack visiting,
  1601. Vector ret)
  1602. throws BuildException {
  1603. state.put(root, VISITING);
  1604. visiting.push(root);
  1605. Target target = (Target) targetTable.get(root);
  1606. // Make sure we exist
  1607. if (target == null) {
  1608. StringBuffer sb = new StringBuffer("Target `");
  1609. sb.append(root);
  1610. sb.append("' does not exist in this project. ");
  1611. visiting.pop();
  1612. if (!visiting.empty()) {
  1613. String parent = (String) visiting.peek();
  1614. sb.append("It is used from target `");
  1615. sb.append(parent);
  1616. sb.append("'.");
  1617. }
  1618. throw new BuildException(new String(sb));
  1619. }
  1620. for (Enumeration en = target.getDependencies(); en.hasMoreElements();) {
  1621. String cur = (String) en.nextElement();
  1622. String m = (String) state.get(cur);
  1623. if (m == null) {
  1624. // Not been visited
  1625. tsort(cur, targetTable, state, visiting, ret);
  1626. } else if (m == VISITING) {
  1627. // Currently visiting this node, so have a cycle
  1628. throw makeCircularException(cur, visiting);
  1629. }
  1630. }
  1631. String p = (String) visiting.pop();
  1632. if (root != p) {
  1633. throw new RuntimeException("Unexpected internal error: expected to "
  1634. + "pop " + root + " but got " + p);
  1635. }
  1636. state.put(root, VISITED);
  1637. ret.addElement(target);
  1638. }
  1639. /**
  1640. * Build an appropriate exception detailing a specified circular
  1641. * dependency.
  1642. *
  1643. * @param end The dependency to stop at. Must not be <code>null</code>.
  1644. * @param stk A stack of dependencies. Must not be <code>null</code>.
  1645. *
  1646. * @return a BuildException detailing the specified circular dependency.
  1647. */
  1648. private static BuildException makeCircularException(String end, Stack stk) {
  1649. StringBuffer sb = new StringBuffer("Circular dependency: ");
  1650. sb.append(end);
  1651. String c;
  1652. do {
  1653. c = (String) stk.pop();
  1654. sb.append(" <- ");
  1655. sb.append(c);
  1656. } while (!c.equals(end));
  1657. return new BuildException(new String(sb));
  1658. }
  1659. /**
  1660. * Add a reference to the project.
  1661. *
  1662. * @param referenceName The name of the reference. Must not be <code>null</code>.
  1663. * @param value The value of the reference. Must not be <code>null</code>.
  1664. */
  1665. public void addReference(String referenceName, Object value) {
  1666. synchronized (references) {
  1667. Object old = ((AntRefTable) references).getReal(referenceName);
  1668. if (old == value) {
  1669. // no warning, this is not changing anything
  1670. return;
  1671. }
  1672. if (old != null && !(old instanceof UnknownElement)) {
  1673. log("Overriding previous definition of reference to " + referenceName,
  1674. MSG_WARN);
  1675. }
  1676. log("Adding reference: " + referenceName, MSG_DEBUG);
  1677. references.put(referenceName, value);
  1678. }
  1679. }
  1680. /**
  1681. * Return a map of the references in the project (String to Object).
  1682. * The returned hashtable is &quot;live&quot; and so must not be modified.
  1683. *
  1684. * @return a map of the references in the project (String to Object).
  1685. */
  1686. public Hashtable getReferences() {
  1687. return references;
  1688. }
  1689. /**
  1690. * Look up a reference by its key (ID).
  1691. *
  1692. * @param key The key for the desired reference.
  1693. * Must not be <code>null</code>.
  1694. *
  1695. * @return the reference with the specified ID, or <code>null</code> if
  1696. * there is no such reference in the project.
  1697. */
  1698. public Object getReference(String key) {
  1699. return references.get(key);
  1700. }
  1701. /**
  1702. * Return a description of the type of the given element, with
  1703. * special handling for instances of tasks and data types.
  1704. * <p>
  1705. * This is useful for logging purposes.
  1706. *
  1707. * @param element The element to describe.
  1708. * Must not be <code>null</code>.
  1709. *
  1710. * @return a description of the element type.
  1711. *
  1712. * @since 1.95, Ant 1.5
  1713. */
  1714. public String getElementName(Object element) {
  1715. return ComponentHelper.getComponentHelper(this).getElementName(element);
  1716. }
  1717. /**
  1718. * Send a &quot;build started&quot; event
  1719. * to the build listeners for this project.
  1720. */
  1721. public void fireBuildStarted() {
  1722. BuildEvent event = new BuildEvent(this);
  1723. Iterator iter = listeners.iterator();
  1724. while (iter.hasNext()) {
  1725. BuildListener listener = (BuildListener) iter.next();
  1726. listener.buildStarted(event);
  1727. }
  1728. }
  1729. /**
  1730. * Send a &quot;build finished&quot; event to the build listeners
  1731. * for this project.
  1732. * @param exception an exception indicating a reason for a build
  1733. * failure. May be <code>null</code>, indicating
  1734. * a successful build.
  1735. */
  1736. public void fireBuildFinished(Throwable exception) {
  1737. BuildEvent event = new BuildEvent(this);
  1738. event.setException(exception);
  1739. Iterator iter = listeners.iterator();
  1740. while (iter.hasNext()) {
  1741. BuildListener listener = (BuildListener) iter.next();
  1742. listener.buildFinished(event);
  1743. }
  1744. }
  1745. /**
  1746. * Send a &quot;subbuild started&quot; event to the build listeners for
  1747. * this project.
  1748. *
  1749. * @since Ant 1.6.2
  1750. */
  1751. public void fireSubBuildStarted() {
  1752. BuildEvent event = new BuildEvent(this);
  1753. Iterator iter = listeners.iterator();
  1754. while (iter.hasNext()) {
  1755. Object listener = iter.next();
  1756. if (listener instanceof SubBuildListener) {
  1757. ((SubBuildListener) listener).subBuildStarted(event);
  1758. }
  1759. }
  1760. }
  1761. /**
  1762. * Send a &quot;subbuild finished&quot; event to the build listeners for
  1763. * this project.
  1764. * @param exception an exception indicating a reason for a build
  1765. * failure. May be <code>null</code>, indicating
  1766. * a successful build.
  1767. *
  1768. * @since Ant 1.6.2
  1769. */
  1770. public void fireSubBuildFinished(Throwable exception) {
  1771. BuildEvent event = new BuildEvent(this);
  1772. event.setException(exception);
  1773. Iterator iter = listeners.iterator();
  1774. while (iter.hasNext()) {
  1775. Object listener = iter.next();
  1776. if (listener instanceof SubBuildListener) {
  1777. ((SubBuildListener) listener).subBuildFinished(event);
  1778. }
  1779. }
  1780. }
  1781. /**
  1782. * Send a &quot;target started&quot; event to the build listeners
  1783. * for this project.
  1784. *
  1785. * @param target The target which is starting to build.
  1786. * Must not be <code>null</code>.
  1787. */
  1788. protected void fireTargetStarted(Target target) {
  1789. BuildEvent event = new BuildEvent(target);
  1790. Iterator iter = listeners.iterator();
  1791. while (iter.hasNext()) {
  1792. BuildListener listener = (BuildListener) iter.next();
  1793. listener.targetStarted(event);
  1794. }
  1795. }
  1796. /**
  1797. * Send a &quot;target finished&quot; event to the build listeners
  1798. * for this project.
  1799. *
  1800. * @param target The target which has finished building.
  1801. * Must not be <code>null</code>.
  1802. * @param exception an exception indicating a reason for a build
  1803. * failure. May be <code>null</code>, indicating
  1804. * a successful build.
  1805. */
  1806. protected void fireTargetFinished(Target target, Throwable exception) {
  1807. BuildEvent event = new BuildEvent(target);
  1808. event.setException(exception);
  1809. Iterator iter = listeners.iterator();
  1810. while (iter.hasNext()) {
  1811. BuildListener listener = (BuildListener) iter.next();
  1812. listener.targetFinished(event);
  1813. }
  1814. }
  1815. /**
  1816. * Send a &quot;task started&quot; event to the build listeners
  1817. * for this project.
  1818. *
  1819. * @param task The target which is starting to execute.
  1820. * Must not be <code>null</code>.
  1821. */
  1822. protected void fireTaskStarted(Task task) {
  1823. // register this as the current task on the current thread.
  1824. registerThreadTask(Thread.currentThread(), task);
  1825. BuildEvent event = new BuildEvent(task);
  1826. Iterator iter = listeners.iterator();
  1827. while (iter.hasNext()) {
  1828. BuildListener listener = (BuildListener) iter.next();
  1829. listener.taskStarted(event);
  1830. }
  1831. }
  1832. /**
  1833. * Send a &quot;task finished&quot; event to the build listeners for this
  1834. * project.
  1835. *
  1836. * @param task The task which has finished executing.
  1837. * Must not be <code>null</code>.
  1838. * @param exception an exception indicating a reason for a build
  1839. * failure. May be <code>null</code>, indicating
  1840. * a successful build.
  1841. */
  1842. protected void fireTaskFinished(Task task, Throwable exception) {
  1843. registerThreadTask(Thread.currentThread(), null);
  1844. System.out.flush();
  1845. System.err.flush();
  1846. BuildEvent event = new BuildEvent(task);
  1847. event.setException(exception);
  1848. Iterator iter = listeners.iterator();
  1849. while (iter.hasNext()) {
  1850. BuildListener listener = (BuildListener) iter.next();
  1851. listener.taskFinished(event);
  1852. }
  1853. }
  1854. /**
  1855. * Send a &quot;message logged&quot; event to the build listeners
  1856. * for this project.
  1857. *
  1858. * @param event The event to send. This should be built up with the
  1859. * appropriate task/target/project by the caller, so that
  1860. * this method can set the message and priority, then send
  1861. * the event. Must not be <code>null</code>.
  1862. * @param message The message to send. Should not be <code>null</code>.
  1863. * @param priority The priority of the message.
  1864. */
  1865. private void fireMessageLoggedEvent(BuildEvent event, String message,
  1866. int priority) {
  1867. if (message.endsWith(StringUtils.LINE_SEP)) {
  1868. int endIndex = message.length() - StringUtils.LINE_SEP.length();
  1869. event.setMessage(message.substring(0, endIndex), priority);
  1870. } else {
  1871. event.setMessage(message, priority);
  1872. }
  1873. synchronized (this) {
  1874. if (loggingMessage) {
  1875. /*
  1876. * One of the Listeners has attempted to access
  1877. * System.err or System.out.
  1878. *
  1879. * We used to throw an exception in this case, but
  1880. * sometimes Listeners can't prevent it(like our own
  1881. * Log4jListener which invokes getLogger() which in
  1882. * turn wants to write to the console).
  1883. *
  1884. * @see http://marc.theaimsgroup.com/?t=110538624200006&r=1&w=2
  1885. *
  1886. * We now (Ant 1.7 and 1.6.3) simply swallow the message.
  1887. */
  1888. return;
  1889. }
  1890. try {
  1891. loggingMessage = true;
  1892. Iterator iter = listeners.iterator();
  1893. while (iter.hasNext()) {
  1894. BuildListener listener = (BuildListener) iter.next();
  1895. listener.messageLogged(event);
  1896. }
  1897. } finally {
  1898. loggingMessage = false;
  1899. }
  1900. }
  1901. }
  1902. /**
  1903. * Send a &quot;message logged&quot; project level event
  1904. * to the build listeners for this project.
  1905. *
  1906. * @param project The project generating the event.
  1907. * Should not be <code>null</code>.
  1908. * @param message The message to send. Should not be <code>null</code>.
  1909. * @param priority The priority of the message.
  1910. */
  1911. protected void fireMessageLogged(Project project, String message,
  1912. int priority) {
  1913. BuildEvent event = new BuildEvent(project);
  1914. fireMessageLoggedEvent(event, message, priority);
  1915. }
  1916. /**
  1917. * Send a &quot;message logged&quot; target level event
  1918. * to the build listeners for this project.
  1919. *
  1920. * @param target The target generating the event.
  1921. * Must not be <code>null</code>.
  1922. * @param message The message to send. Should not be <code>null</code>.
  1923. * @param priority The priority of the message.
  1924. */
  1925. protected void fireMessageLogged(Target target, String message,
  1926. int priority) {
  1927. BuildEvent event = new BuildEvent(target);
  1928. fireMessageLoggedEvent(event, message, priority);
  1929. }
  1930. /**
  1931. * Send a &quot;message logged&quot; task level event
  1932. * to the build listeners for this project.
  1933. *
  1934. * @param task The task generating the event.
  1935. * Must not be <code>null</code>.
  1936. * @param message The message to send. Should not be <code>null</code>.
  1937. * @param priority The priority of the message.
  1938. */
  1939. protected void fireMessageLogged(Task task, String message, int priority) {
  1940. BuildEvent event = new BuildEvent(task);
  1941. fireMessageLoggedEvent(event, message, priority);
  1942. }
  1943. /**
  1944. * Register a task as the current task for a thread.
  1945. * If the task is null, the thread's entry is removed.
  1946. *
  1947. * @param thread the thread on which the task is registered.
  1948. * @param task the task to be registered.
  1949. * @since Ant 1.5
  1950. */
  1951. public synchronized void registerThreadTask(Thread thread, Task task) {
  1952. if (task != null) {
  1953. threadTasks.put(thread, task);
  1954. threadGroupTasks.put(thread.getThreadGroup(), task);
  1955. } else {
  1956. threadTasks.remove(thread);
  1957. threadGroupTasks.remove(thread.getThreadGroup());
  1958. }
  1959. }
  1960. /**
  1961. * Get the current task associated with a thread, if any.
  1962. *
  1963. * @param thread the thread for which the task is required.
  1964. * @return the task which is currently registered for the given thread or
  1965. * null if no task is registered.
  1966. */
  1967. public Task getThreadTask(Thread thread) {
  1968. Task task = (Task) threadTasks.get(thread);
  1969. if (task == null) {
  1970. ThreadGroup group = thread.getThreadGroup();
  1971. while (task == null && group != null) {
  1972. task = (Task) threadGroupTasks.get(group);
  1973. group = group.getParent();
  1974. }
  1975. }
  1976. return task;
  1977. }
  1978. // Should move to a separate public class - and have API to add
  1979. // listeners, etc.
  1980. private static class AntRefTable extends Hashtable {
  1981. AntRefTable() {
  1982. super();
  1983. }
  1984. /** Returns the unmodified original object.
  1985. * This method should be called internally to
  1986. * get the &quot;real&quot; object.
  1987. * The normal get method will do the replacement
  1988. * of UnknownElement (this is similar with the JDNI
  1989. * refs behavior).
  1990. */
  1991. private Object getReal(Object key) {
  1992. return super.get(key);
  1993. }
  1994. /** Get method for the reference table.
  1995. * It can be used to hook dynamic references and to modify
  1996. * some references on the fly--for example for delayed
  1997. * evaluation.
  1998. *
  1999. * It is important to make sure that the processing that is
  2000. * done inside is not calling get indirectly.
  2001. *
  2002. * @param key lookup key.
  2003. * @return mapped value.
  2004. */
  2005. public Object get(Object key) {
  2006. //System.out.println("AntRefTable.get " + key);
  2007. Object o = getReal(key);
  2008. if (o instanceof UnknownElement) {
  2009. // Make sure that
  2010. UnknownElement ue = (UnknownElement) o;
  2011. ue.maybeConfigure();
  2012. o = ue.getRealThing();
  2013. }
  2014. return o;
  2015. }
  2016. }
  2017. /**
  2018. * Set a reference to this Project on the parameterized object.
  2019. * Need to set the project before other set/add elements
  2020. * are called.
  2021. * @param obj the object to invoke setProject(this) on.
  2022. */
  2023. public final void setProjectReference(final Object obj) {
  2024. if (obj instanceof ProjectComponent) {
  2025. ((ProjectComponent) obj).setProject(this);
  2026. return;
  2027. }
  2028. try {
  2029. Method method =
  2030. obj.getClass().getMethod(
  2031. "setProject", new Class[] {Project.class});
  2032. if (method != null) {
  2033. method.invoke(obj, new Object[] {this});
  2034. }
  2035. } catch (Throwable e) {
  2036. // ignore this if the object does not have
  2037. // a set project method or the method
  2038. // is private/protected.
  2039. }
  2040. }
  2041. }